Skip to content

Commit 3c2c97e

Browse files
committed
defaultdict AC vectorcall
1 parent 49a344e commit 3c2c97e

2 files changed

Lines changed: 113 additions & 22 deletions

File tree

‎Modules/_collectionsmodule.c‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ find_module_state_by_def(PyTypeObject *type)
4848
module _collections
4949
class _tuplegetter "_tuplegetterobject *" "clinic_state()->tuplegetter_type"
5050
class _collections.deque "dequeobject *" "clinic_state()->deque_type"
51+
class _collections.defaultdict "PyObject *" "type"
5152
[clinic start generated code]*/
52-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a033cc2a8476b3f1]*/
53+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3488c614fa7c6bd5]*/
5354

5455
typedef struct dequeobject dequeobject;
5556

@@ -2455,32 +2456,34 @@ defdict_tp_clear(PyObject *op)
24552456
return PyDict_Type.tp_clear(op);
24562457
}
24572458

2459+
/*[clinic input]
2460+
@vectorcall
2461+
_collections.defaultdict.__init__ as defdict_init
2462+
default_factory: object = None
2463+
/
2464+
*args: tuple
2465+
**kwargs: dict
2466+
[clinic start generated code]*/
2467+
24582468
static int
2459-
defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
2469+
defdict_init_impl(PyObject *self, PyObject *default_factory, PyObject *args,
2470+
PyObject *kwargs)
2471+
/*[clinic end generated code: output=af68ec37fc3b209b input=3809f9457f1187f1]*/
24602472
{
24612473
defdictobject *dd = defdictobject_CAST(self);
24622474
PyObject *olddefault = dd->default_factory;
2463-
PyObject *newdefault = NULL;
24642475
PyObject *newargs;
24652476
int result;
2466-
if (args == NULL || !PyTuple_Check(args))
2467-
newargs = PyTuple_New(0);
2468-
else {
2469-
Py_ssize_t n = PyTuple_GET_SIZE(args);
2470-
if (n > 0) {
2471-
newdefault = PyTuple_GET_ITEM(args, 0);
2472-
if (!PyCallable_Check(newdefault) && newdefault != Py_None) {
2473-
PyErr_SetString(PyExc_TypeError,
2474-
"first argument must be callable or None");
2475-
return -1;
2476-
}
2477-
}
2478-
newargs = PySequence_GetSlice(args, 1, n);
2479-
}
2480-
if (newargs == NULL)
2477+
2478+
if (!PyCallable_Check(default_factory) && default_factory != Py_None) {
2479+
PyErr_SetString(PyExc_TypeError,
2480+
"first argument must be callable or None");
24812481
return -1;
2482-
dd->default_factory = Py_XNewRef(newdefault);
2483-
result = PyDict_Type.tp_init(self, newargs, kwds);
2482+
}
2483+
newargs = Py_NewRef(args);
2484+
2485+
dd->default_factory = Py_NewRef(default_factory);
2486+
result = PyDict_Type.tp_init(self, newargs, kwargs);
24842487
Py_DECREF(newargs);
24852488
Py_XDECREF(olddefault);
24862489
return result;
@@ -2511,6 +2514,7 @@ static PyType_Slot defdict_slots[] = {
25112514
{Py_tp_methods, defdict_methods},
25122515
{Py_tp_members, defdict_members},
25132516
{Py_tp_init, defdict_init},
2517+
{Py_tp_vectorcall, defdict_vectorcall},
25142518
{Py_tp_alloc, PyType_GenericAlloc},
25152519
{Py_tp_free, PyObject_GC_Del},
25162520
{0, NULL},

‎Modules/clinic/_collectionsmodule.c.h‎

Lines changed: 89 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)