From 15040c9c419b7f1c4d598daf153b58e2649ec70e Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 05:38:24 +0800 Subject: [PATCH 1/4] CellType AC vectorcall --- Objects/cellobject.c | 52 ++++++++++++------------ Objects/clinic/cellobject.c.h | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 Objects/clinic/cellobject.c.h diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 823f3c91044c78..187f4ff2e3ccdc 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -5,8 +5,15 @@ #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 +28,27 @@ 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 +cell.__new__ as cell_new + contents: object(c_default="NULL") = None + / -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=98ef2b18af28df93]*/ +{ + 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..093cf70feac8f2 --- /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=None, /)\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=20733d23a75f61ba input=a9049054013a1b77]*/ From c2d3ea66f047baa2d39bca4d6d5cc0577644112f Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 16:15:37 +0800 Subject: [PATCH 2/4] fix test: remove 'CellType' from 'unsupported_signature' as it now has AC-generated signature 'cell(contents=None, /)' which is supported by 'inspect.signature' --- Lib/test/test_inspect/test_inspect.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index ef9d2061611f33..8c1c824c50f660 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -6334,7 +6334,6 @@ def test_builtins_have_signatures(self): methods_no_signature, methods_unsupported_signature) def test_types_module_has_signatures(self): - unsupported_signature = {'CellType'} methods_no_signature = { 'AsyncGeneratorType': {'athrow'}, 'CoroutineType': {'throw'}, @@ -6342,7 +6341,6 @@ def test_types_module_has_signatures(self): 'FrameLocalsProxyType': {'setdefault', 'pop', 'get'}, } self._test_module_has_signatures(types, - unsupported_signature=unsupported_signature, methods_no_signature=methods_no_signature) def test_sys_module_has_signatures(self): From a0e92e73887efd7dfa6b7b2b60fa4fdec57f9783 Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 16:55:18 +0800 Subject: [PATCH 3/4] use @text_signature to keep old 'cell([contents])' --- Objects/cellobject.c | 3 ++- Objects/clinic/cellobject.c.h | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 187f4ff2e3ccdc..b229fc7d057a7b 100644 --- a/Objects/cellobject.c +++ b/Objects/cellobject.c @@ -31,6 +31,7 @@ PyCell_New(PyObject *obj) /*[clinic input] @vectorcall @classmethod +@text_signature "([contents])" cell.__new__ as cell_new contents: object(c_default="NULL") = None @@ -46,7 +47,7 @@ Create a new cell object. static PyObject * cell_new_impl(PyTypeObject *type, PyObject *contents) -/*[clinic end generated code: output=0b7db264f0621bb4 input=98ef2b18af28df93]*/ +/*[clinic end generated code: output=0b7db264f0621bb4 input=2772e49193d8e5d9]*/ { return PyCell_New(contents); } diff --git a/Objects/clinic/cellobject.c.h b/Objects/clinic/cellobject.c.h index 093cf70feac8f2..bf2b21d5dc4437 100644 --- a/Objects/clinic/cellobject.c.h +++ b/Objects/clinic/cellobject.c.h @@ -5,7 +5,7 @@ preserve #include "pycore_modsupport.h" // _PyArg_CheckPositional() PyDoc_STRVAR(cell_new__doc__, -"cell(contents=None, /)\n" +"cell([contents])\n" "--\n" "\n" "Create a new cell object.\n" @@ -71,4 +71,4 @@ cell_vectorcall(PyObject *type, PyObject *const *args, exit: return return_value; } -/*[clinic end generated code: output=20733d23a75f61ba input=a9049054013a1b77]*/ +/*[clinic end generated code: output=687470647f1a75f8 input=a9049054013a1b77]*/ From d5bbf13b3e0dc810e7fec9c93caf01129dde698d Mon Sep 17 00:00:00 2001 From: Tan Long Date: Sun, 22 Nov 2026 16:55:25 +0800 Subject: [PATCH 4/4] 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. --- Lib/test/test_inspect/test_inspect.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 8c1c824c50f660..ef9d2061611f33 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -6334,6 +6334,7 @@ def test_builtins_have_signatures(self): methods_no_signature, methods_unsupported_signature) def test_types_module_has_signatures(self): + unsupported_signature = {'CellType'} methods_no_signature = { 'AsyncGeneratorType': {'athrow'}, 'CoroutineType': {'throw'}, @@ -6341,6 +6342,7 @@ def test_types_module_has_signatures(self): 'FrameLocalsProxyType': {'setdefault', 'pop', 'get'}, } self._test_module_has_signatures(types, + unsupported_signature=unsupported_signature, methods_no_signature=methods_no_signature) def test_sys_module_has_signatures(self):