diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index f5334f70768dc45..acfaec419c3b5f6 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -5597,7 +5597,8 @@ def through_init(cls): return cls, partial(cls.__init__, cls(1)) entry_points = [ - through_new(enumerate), # the only non-test @vectorcall function + through_new(enumerate), + through_new(float), through_new(ac_tester.VcNew), through_new(ac_tester.VcNewBase), through_new(ac_tester.VcKwOnly), diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h index 8768555c909257a..7fc6d10858bbfa1 100644 --- a/Objects/clinic/floatobject.c.h +++ b/Objects/clinic/floatobject.c.h @@ -240,6 +240,35 @@ float_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) return return_value; } +static PyObject * +float_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *x = NULL; + + assert(Py_Is(_PyType_CAST(type), &PyFloat_Type)); + /* Make sure the type object is immutable: the generated + * vectorcall doesn't deal e.g. with users reassigning __init__. */ + assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE)); + if (!_PyArg_NoKwnames("float", kwnames)) { + goto exit; + } + if (!_PyArg_CheckPositional("float", nargs, 0, 1)) { + goto exit; + } + if (nargs < 1) { + goto skip_optional; + } + x = args[0]; +skip_optional: + return_value = float_new_impl(_PyType_CAST(type), x); + +exit: + return return_value; +} + PyDoc_STRVAR(float_from_number__doc__, "from_number($type, number, /)\n" "--\n" @@ -353,4 +382,4 @@ float___format__(PyObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=5d7b0bf9e47ff997 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=3e2ac34f890dbbcb input=a9049054013a1b77]*/ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 1bb5311ac678a5d..e3e490f812ede9b 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1555,6 +1555,7 @@ static PyObject * float_subtype_new(PyTypeObject *type, PyObject *x); /*[clinic input] +@vectorcall @classmethod float.__new__ as float_new x: object(c_default="NULL") = 0 @@ -1565,7 +1566,7 @@ Convert a string or number to a floating-point number, if possible. static PyObject * float_new_impl(PyTypeObject *type, PyObject *x) -/*[clinic end generated code: output=ccf1e8dc460ba6ba input=55909f888aa0c8a6]*/ +/*[clinic end generated code: output=ccf1e8dc460ba6ba input=39b40d663c372a91]*/ { if (type != &PyFloat_Type) { if (x == NULL) { @@ -1609,23 +1610,6 @@ float_subtype_new(PyTypeObject *type, PyObject *x) return newobj; } -static PyObject * -float_vectorcall(PyObject *type, PyObject *const *args, - size_t nargsf, PyObject *kwnames) -{ - if (!_PyArg_NoKwnames("float", kwnames)) { - return NULL; - } - - Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); - if (!_PyArg_CheckPositional("float", nargs, 0, 1)) { - return NULL; - } - - PyObject *x = nargs >= 1 ? args[0] : NULL; - return float_new_impl(_PyType_CAST(type), x); -} - /*[clinic input] @classmethod