[Python-checkins] bpo-41873: Add vectorcall for float() (GH-22432)

Dennis Sweeney webhook-mailer at python.org
Mon Sep 28 20:56:03 EDT 2020


https://github.com/python/cpython/commit/e8acc355d430b45f1c3ff83312e72272262a854f
commit: e8acc355d430b45f1c3ff83312e72272262a854f
branch: master
author: Dennis Sweeney <36520290+sweeneyde at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-09-29T09:55:52+09:00
summary:

bpo-41873: Add vectorcall for float() (GH-22432)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst
M Lib/test/test_float.py
M Objects/floatobject.c

diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 9651281e24edb..99c81f0b72a5a 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -64,6 +64,9 @@ def test_float(self):
         # See bpo-34087
         self.assertRaises(ValueError, float, '\u3053\u3093\u306b\u3061\u306f')
 
+    def test_noargs(self):
+        self.assertEqual(float(), 0.0)
+
     def test_underscores(self):
         for lit in VALID_UNDERSCORE_LITERALS:
             if not any(ch in lit for ch in 'jJxXoObB'):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst b/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst
new file mode 100644
index 0000000000000..ee2636704c299
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-09-28-08-58-28.bpo-41873.VzEDhA.rst	
@@ -0,0 +1 @@
+Calls to ``float()`` are now faster due to the ``vectorcall`` calling convention. Patch by Dennis Sweeney.
\ No newline at end of file
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 0606f29ff5408..d0af0ea1a9825 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -1649,6 +1649,24 @@ 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] : _PyLong_Zero;
+    return float_new_impl((PyTypeObject *)type, x);
+}
+
+
 /*[clinic input]
 float.__getnewargs__
 [clinic start generated code]*/
@@ -1937,6 +1955,7 @@ PyTypeObject PyFloat_Type = {
     0,                                          /* tp_init */
     0,                                          /* tp_alloc */
     float_new,                                  /* tp_new */
+    .tp_vectorcall = (vectorcallfunc)float_vectorcall,
 };
 
 int



More information about the Python-checkins mailing list