8000 bpo-41873: Add vectorcall for float() (GH-22432) · python/cpython@e8acc35 · GitHub
[go: up one dir, main page]

Skip to content

Commit e8acc35

Browse files
authored
bpo-41873: Add vectorcall for float() (GH-22432)
1 parent cb6db8b commit e8acc35

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Lib/test/test_float.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def test_float(self):
6464
# See bpo-34087
6565
self.assertRaises(ValueError, float, '\u3053\u3093\u306b\u3061\u306f')
6666

67+
def test_noargs(self):
68+
self.assertEqual(float(), 0.0)
69+
6770
def test_underscores(self):
6871
for lit in VALID_UNDERSCORE_LITERALS:
6972
if not any(ch in lit for ch in 'jJxXoObB'):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Calls to ``float()`` are now faster due to the ``vectorcall`` calling convention. Patch by Dennis Sweeney.

Objects/floatobject.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,24 @@ float_subtype_new(PyTypeObject *type, PyObject *x)
16491649
return newobj;
16501650
}
16511651

1652+
static PyObject *
1653+
float_vectorcall(PyObject *type, PyObject * const*args,
1654+
size_t nargsf, PyObject *kwnames)
1655+
{
1656+
if (!_PyArg_NoKwnames("float", kwnames)) {
1657 8000 +
return NULL;
1658+
}
1659+
1660+
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
1661+
if (!_PyArg_CheckPositional("float", nargs, 0, 1)) {
1662+
return NULL;
1663+
}
1664+
1665+
PyObject *x = nargs >= 1 ? args[0] : _PyLong_Zero;
1666+
return float_new_impl((PyTypeObject *)type, x);
1667+
}
1668+
1669+
16521670
/*[clinic input]
16531671
float.__getnewargs__
16541672
[clinic start generated code]*/
@@ -1937,6 +1955,7 @@ PyTypeObject PyFloat_Type = {
19371955
0, /* tp_init */
19381956
0, /* tp_alloc */
19391957
float_new, /* tp_new */
1958+
.tp_vectorcall = (vectorcallfunc)float_vectorcall,
19401959
};
19411960

19421961
int

0 commit comments

Comments
 (0)
0