diff --git a/Objects/cellobject.c b/Objects/cellobject.c index 823f3c91044c780..b229fc7d057a7bb 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,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(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=2772e49193d8e5d9]*/ +{ + return PyCell_New(contents); } PyObject * @@ -190,7 +190,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 +209,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 000000000000000..bf2b21d5dc44372 --- /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]*/