https://github.com/python/cpython/commit/f80a0fabe604cfe031f620919cf906d2a08c5f13
commit: f80a0fabe604cfe031f620919cf906d2a08c5f13
branch: main
author: Tan Long <[email protected]>
committer: asvetlov <[email protected]>
date: 2026-11-22T17:04:27+01:00
summary:
gh-162350: Use AC vectorcall for cell.__new__ to speed up types.CellType
(#162422)
* CellType AC vectorcall
* fix test: remove 'CellType' from 'unsupported_signature' as it now has
AC-generated signature 'cell(contents=None, /)' which is supported by
'inspect.signature'
* use @text_signature to keep old 'cell([contents])'
* Revert "fix test: remove 'CellType' from 'unsupported_signature' as it now
has AC-generated signature 'cell(contents=None, /)' which is supported by
'inspect.signature'"
This reverts commit c2d3ea66f047baa2d39bca4d6d5cc0577644112f.
* remove unused include
* make it clear that cell(None) differs from cell()
files:
A Objects/clinic/cellobject.c.h
M Objects/cellobject.c
diff --git a/Objects/cellobject.c b/Objects/cellobject.c
index 823f3c91044c78..a1c7dd42912bcc 100644
--- a/Objects/cellobject.c
+++ b/Objects/cellobject.c
@@ -2,11 +2,17 @@
#include "Python.h"
#include "pycore_cell.h" // PyCell_GetRef()
-#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_object.h"
+#include "clinic/cellobject.c.h"
+
#define _PyCell_CAST(op) _Py_CAST(PyCellObject*, (op))
+/*[clinic input]
+class cell "PyCellObject *" "&PyCell_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3d6baba8cf810af0]*/
+
PyObject *
PyCell_New(PyObject *obj)
{
@@ -21,35 +27,28 @@ PyCell_New(PyObject *obj)
return (PyObject *)op;
}
-PyDoc_STRVAR(cell_new_doc,
-"cell([contents])\n"
-"--\n"
-"\n"
-"Create a new cell object.\n"
-"\n"
-" contents\n"
-" the contents of the cell. If not specified, the cell will be empty,\n"
-" and further attempts to access its cell_contents attribute will\n"
-" raise a ValueError.");
+/*[clinic input]
+@vectorcall
+@classmethod
+@text_signature "([contents])"
+cell.__new__ as cell_new
+ contents: object = NULL
+ /
-static PyObject *
-cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
- PyObject *return_value = NULL;
- PyObject *obj = NULL;
+Create a new cell object.
- if (!_PyArg_NoKeywords("cell", kwargs)) {
- goto exit;
- }
- /* min = 0: we allow the cell to be empty */
- if (!PyArg_UnpackTuple(args, "cell", 0, 1, &obj)) {
- goto exit;
- }
- return_value = PyCell_New(obj);
+ contents
+ the contents of the cell. If not specified, the cell will be empty,
+ and further attempts to access its cell_contents attribute will
+ raise a ValueError.
+[clinic start generated code]*/
-exit:
- return return_value;
+static PyObject *
+cell_new_impl(PyTypeObject *type, PyObject *contents)
+/*[clinic end generated code: output=0b7db264f0621bb4 input=4460339a82243798]*/
+{
+ return PyCell_New(contents);
}
PyObject *
@@ -190,7 +189,7 @@ PyTypeObject PyCell_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
- cell_new_doc, /* tp_doc */
+ cell_new__doc__, /* tp_doc */
cell_traverse, /* tp_traverse */
cell_clear, /* tp_clear */
cell_richcompare, /* tp_richcompare */
@@ -209,4 +208,5 @@ PyTypeObject PyCell_Type = {
0, /* tp_alloc */
cell_new, /* tp_new */
0, /* tp_free */
+ .tp_vectorcall = cell_vectorcall,
};
diff --git a/Objects/clinic/cellobject.c.h b/Objects/clinic/cellobject.c.h
new file mode 100644
index 00000000000000..bf2b21d5dc4437
--- /dev/null
+++ b/Objects/clinic/cellobject.c.h
@@ -0,0 +1,74 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+#include "pycore_modsupport.h" // _PyArg_CheckPositional()
+
+PyDoc_STRVAR(cell_new__doc__,
+"cell([contents])\n"
+"--\n"
+"\n"
+"Create a new cell object.\n"
+"\n"
+" contents\n"
+" the contents of the cell. If not specified, the cell will be empty,\n"
+" and further attempts to access its cell_contents attribute will\n"
+" raise a ValueError.");
+
+static PyObject *
+cell_new_impl(PyTypeObject *type, PyObject *contents);
+
+static PyObject *
+cell_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
+{
+ PyObject *return_value = NULL;
+ PyTypeObject *base_tp = &PyCell_Type;
+ PyObject *contents = NULL;
+
+ if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
+ !_PyArg_NoKeywords("cell", kwargs)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("cell", PyTuple_GET_SIZE(args), 0, 1)) {
+ goto exit;
+ }
+ if (PyTuple_GET_SIZE(args) < 1) {
+ goto skip_optional;
+ }
+ contents = PyTuple_GET_ITEM(args, 0);
+skip_optional:
+ return_value = cell_new_impl(type, contents);
+
+exit:
+ return return_value;
+}
+
+static PyObject *
+cell_vectorcall(PyObject *type, PyObject *const *args,
+ size_t nargsf, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
+ PyObject *contents = NULL;
+
+ assert(_PyType_CAST(type)->tp_new == cell_new);
+ /* 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("cell", kwnames)) {
+ goto exit;
+ }
+ if (!_PyArg_CheckPositional("cell", nargs, 0, 1)) {
+ goto exit;
+ }
+ if (nargs < 1) {
+ goto skip_optional;
+ }
+ contents = args[0];
+skip_optional:
+ return_value = cell_new_impl(_PyType_CAST(type), contents);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=687470647f1a75f8 input=a9049054013a1b77]*/
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]