diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index c63dc0661bd69e..a777d51cb35be9 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -48,8 +48,9 @@ find_module_state_by_def(PyTypeObject *type) module _collections class _tuplegetter "_tuplegetterobject *" "clinic_state()->tuplegetter_type" class _collections.deque "dequeobject *" "clinic_state()->deque_type" +class _collections.defaultdict "PyObject *" "type" [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a033cc2a8476b3f1]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3488c614fa7c6bd5]*/ typedef struct dequeobject dequeobject; @@ -2455,32 +2456,34 @@ defdict_tp_clear(PyObject *op) return PyDict_Type.tp_clear(op); } +/*[clinic input] +@vectorcall +_collections.defaultdict.__init__ as defdict_init + default_factory: object = None + / + *args: tuple + **kwargs: dict +[clinic start generated code]*/ + static int -defdict_init(PyObject *self, PyObject *args, PyObject *kwds) +defdict_init_impl(PyObject *self, PyObject *default_factory, PyObject *args, + PyObject *kwargs) +/*[clinic end generated code: output=af68ec37fc3b209b input=3809f9457f1187f1]*/ { defdictobject *dd = defdictobject_CAST(self); PyObject *olddefault = dd->default_factory; - PyObject *newdefault = NULL; PyObject *newargs; int result; - if (args == NULL || !PyTuple_Check(args)) - newargs = PyTuple_New(0); - else { - Py_ssize_t n = PyTuple_GET_SIZE(args); - if (n > 0) { - newdefault = PyTuple_GET_ITEM(args, 0); - if (!PyCallable_Check(newdefault) && newdefault != Py_None) { - PyErr_SetString(PyExc_TypeError, - "first argument must be callable or None"); - return -1; - } - } - newargs = PySequence_GetSlice(args, 1, n); - } - if (newargs == NULL) + + if (!PyCallable_Check(default_factory) && default_factory != Py_None) { + PyErr_SetString(PyExc_TypeError, + "first argument must be callable or None"); return -1; - dd->default_factory = Py_XNewRef(newdefault); - result = PyDict_Type.tp_init(self, newargs, kwds); + } + newargs = Py_NewRef(args); + + dd->default_factory = Py_NewRef(default_factory); + result = PyDict_Type.tp_init(self, newargs, kwargs); Py_DECREF(newargs); Py_XDECREF(olddefault); return result; @@ -2511,6 +2514,7 @@ static PyType_Slot defdict_slots[] = { {Py_tp_methods, defdict_methods}, {Py_tp_members, defdict_members}, {Py_tp_init, defdict_init}, + {Py_tp_vectorcall, defdict_vectorcall}, {Py_tp_alloc, PyType_GenericAlloc}, {Py_tp_free, PyObject_GC_Del}, {0, NULL}, diff --git a/Modules/clinic/_collectionsmodule.c.h b/Modules/clinic/_collectionsmodule.c.h index 2966d68aa3813b..179f21c47f6b18 100644 --- a/Modules/clinic/_collectionsmodule.c.h +++ b/Modules/clinic/_collectionsmodule.c.h @@ -4,11 +4,11 @@ preserve #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) # include "pycore_gc.h" // PyGC_Head -# include "pycore_runtime.h" // _Py_ID() #endif #include "pycore_abstract.h" // _PyNumber_Index() #include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION() #include "pycore_modsupport.h" // _PyArg_CheckPositional() +#include "pycore_runtime.h" // _Py_SINGLETON() PyDoc_STRVAR(deque_pop__doc__, "pop($self, /)\n" @@ -640,6 +640,93 @@ deque___reversed__(PyObject *deque, PyObject *Py_UNUSED(ignored)) return deque___reversed___impl((dequeobject *)deque); } +static int +defdict_init_impl(PyObject *self, PyObject *default_factory, PyObject *args, + PyObject *kwargs); + +static int +defdict_init(PyObject *self, PyObject *args, PyObject *kwargs) +{ + int return_value = -1; + PyObject *default_factory = Py_None; + PyObject *__clinic_args = NULL; + PyObject *__clinic_kwargs = NULL; + + if (PyTuple_GET_SIZE(args) < 1) { + goto skip_optional; + } + default_factory = PyTuple_GET_ITEM(args, 0); +skip_optional: + __clinic_args = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); + if (!__clinic_args) { + goto exit; + } + __clinic_kwargs = Py_XNewRef(kwargs); + return_value = defdict_init_impl(self, default_factory, __clinic_args, __clinic_kwargs); + +exit: + /* Cleanup for args */ + Py_XDECREF(__clinic_args); + /* Cleanup for kwargs */ + Py_XDECREF(__clinic_kwargs); + + return return_value; +} + +static PyObject * +defdict_vectorcall(PyObject *type, PyObject *const *args, + size_t nargsf, PyObject *kwnames) +{ + PyObject *return_value = NULL; + Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); + PyObject *self; + int _result; + PyObject *default_factory = Py_None; + PyObject *__clinic_args = NULL; + PyObject *__clinic_kwargs = NULL; + + assert(_PyType_CAST(type)->tp_init == defdict_init); + /* 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 (nargs < 1) { + goto skip_optional; + } + default_factory = args[0]; +skip_optional: + __clinic_args = nargs > 1 + ? PyTuple_FromArray(args + 1, nargs - 1) + : PyTuple_New(0); + if (__clinic_args == NULL) { + goto exit; + } + if (kwnames != NULL) { + __clinic_kwargs = _PyStack_AsDict(args + nargs, kwnames); + if (__clinic_kwargs == NULL) { + goto exit; + } + } + self = _PyType_CAST(type)->tp_new(_PyType_CAST(type), + (PyObject *)&_Py_SINGLETON(tuple_empty), NULL); + if (self == NULL) { + goto exit; + } + _result = defdict_init_impl((PyObject *)self, default_factory, __clinic_args, __clinic_kwargs); + if (_result != 0) { + Py_DECREF(self); + goto exit; + } + return_value = self; + +exit: + /* Cleanup for args */ + Py_XDECREF(__clinic_args); + /* Cleanup for kwargs */ + Py_XDECREF(__clinic_kwargs); + + return return_value; +} + PyDoc_STRVAR(_collections__count_elements__doc__, "_count_elements($module, mapping, iterable, /)\n" "--\n" @@ -707,4 +794,4 @@ tuplegetter_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) exit: return return_value; } -/*[clinic end generated code: output=de400069cf6f11a4 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=9aff82dec0b38221 input=a9049054013a1b77]*/