Skip to content

Commit 97eef29

Browse files
Add batched type to module state
1 parent 3559c28 commit 97eef29

File tree

1 file changed

+29
-60
lines changed

1 file changed

+29
-60
lines changed

Modules/itertoolsmodule.c

+29-60
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
typedef struct {
1515
PyTypeObject *accumulate_type;
16+
PyTypeObject *batched_type;
1617
PyTypeObject *chain_type;
1718
PyTypeObject *combinations_type;
1819
PyTypeObject *compress_type;
@@ -59,7 +60,7 @@ class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type"
5960
class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
6061
class itertools.teedataobject "teedataobject *" "clinic_state()->teedataobject_type"
6162
class itertools._tee "teeobject *" "clinic_state()->tee_type"
62-
class itertools.batched "batchedobject *" "&batched_type"
63+
class itertools.batched "batchedobject *" "clinic_state()->batched_type"
6364
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
6465
class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type"
6566
class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type"
@@ -76,8 +77,6 @@ class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
7677
[clinic start generated code]*/
7778
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=419423f2d82cf388]*/
7879

79-
static PyTypeObject batched_type;
80-
8180
#include "clinic/itertoolsmodule.c.h"
8281
#undef clinic_state
8382

@@ -154,17 +153,18 @@ batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n)
154153
static void
155154
batched_dealloc(batchedobject *bo)
156155
{
156+
PyTypeObject *tp = Py_TYPE(bo);
157157
PyObject_GC_UnTrack(bo);
158158
Py_XDECREF(bo->it);
159-
Py_TYPE(bo)->tp_free(bo);
159+
tp->tp_free(bo);
160+
Py_DECREF(tp);
160161
}
161162

162163
static int
163164
batched_traverse(batchedobject *bo, visitproc visit, void *arg)
164165
{
165-
if (bo->it != NULL) {
166-
Py_VISIT(bo->it);
167-
}
166+
Py_VISIT(Py_TYPE(bo));
167+
Py_VISIT(bo->it);
168168
return 0;
169169
}
170170

@@ -214,48 +214,25 @@ batched_next(batchedobject *bo)
214214
return result;
215215
}
216216

217-
static PyTypeObject batched_type = {
218-
PyVarObject_HEAD_INIT(&PyType_Type, 0)
219-
"itertools.batched", /* tp_name */
220-
sizeof(batchedobject), /* tp_basicsize */
221-
0, /* tp_itemsize */
222-
/* methods */
223-
(destructor)batched_dealloc, /* tp_dealloc */
224-
0, /* tp_vectorcall_offset */
225-
0, /* tp_getattr */
226-
0, /* tp_setattr */
227-
0, /* tp_as_async */
228-
0, /* tp_repr */
229-
0, /* tp_as_number */
230-
0, /* tp_as_sequence */
231-
0, /* tp_as_mapping */
232-
0, /* tp_hash */
233-
0, /* tp_call */
234-
0, /* tp_str */
235-
PyObject_GenericGetAttr, /* tp_getattro */
236-
0, /* tp_setattro */
237-
0, /* tp_as_buffer */
238-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
239-
Py_TPFLAGS_BASETYPE, /* tp_flags */
240-
batched_new__doc__, /* tp_doc */
241-
(traverseproc)batched_traverse, /* tp_traverse */
242-
0, /* tp_clear */
243-
0, /* tp_richcompare */
244-
0, /* tp_weaklistoffset */
245-
PyObject_SelfIter, /* tp_iter */
246-
(iternextfunc)batched_next, /* tp_iternext */
247-
0, /* tp_methods */
248-
0, /* tp_members */
249-
0, /* tp_getset */
250-
0, /* tp_base */
251-
0, /* tp_dict */
252-
0, /* tp_descr_get */
253-
0, /* tp_descr_set */
254-
0, /* tp_dictoffset */
255-
0, /* tp_init */
256-
PyType_GenericAlloc, /* tp_alloc */
257-
batched_new, /* tp_new */
258-
PyObject_GC_Del, /* tp_free */
217+
static PyType_Slot batched_slots[] = {
218+
{Py_tp_dealloc, batched_dealloc},
219+
{Py_tp_getattro, PyObject_GenericGetAttr},
220+
{Py_tp_doc, (void *)batched_new__doc__},
221+
{Py_tp_traverse, batched_traverse},
222+
{Py_tp_iter, PyObject_SelfIter},
223+
{Py_tp_iternext, batched_next},
224+
{Py_tp_alloc, PyType_GenericAlloc},
225+
{Py_tp_new, batched_new},
226+
{Py_tp_free, PyObject_GC_Del},
227+
{0, NULL},
228+
};
229+
230+
static PyType_Spec batched_spec = {
231+
.name = "itertools.batched",
232+
.basicsize = sizeof(batchedobject),
233+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
234+
Py_TPFLAGS_IMMUTABLETYPE),
235+
.slots = batched_slots,
259236
};
260237

261238

@@ -4599,6 +4576,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
45994576
{
46004577
itertools_state *state = get_module_state(mod);
46014578
Py_VISIT(state->accumulate_type);
4579+
Py_VISIT(state->batched_type);
46024580
Py_VISIT(state->chain_type);
46034581
Py_VISIT(state->combinations_type);
46044582
Py_VISIT(state->compress_type);
@@ -4626,6 +4604,7 @@ itertoolsmodule_clear(PyObject *mod)
46264604
{
46274605
itertools_state *state = get_module_state(mod);
46284606
Py_CLEAR(state->accumulate_type);
4607+
Py_CLEAR(state->batched_type);
46294608
Py_CLEAR(state->chain_type);
46304609
Py_CLEAR(state->combinations_type);
46314610
Py_CLEAR(state->compress_type);
@@ -4670,6 +4649,7 @@ itertoolsmodule_exec(PyObject *mod)
46704649
{
46714650
itertools_state *state = get_module_state(mod);
46724651
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
4652+
ADD_TYPE(mod, state->batched_type, &batched_spec);
46734653
ADD_TYPE(mod, state->chain_type, &chain_spec);
46744654
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
46754655
ADD_TYPE(mod, state->compress_type, &compress_spec);
@@ -4691,18 +4671,7 @@ itertoolsmodule_exec(PyObject *mod)
46914671
ADD_TYPE(mod, state->teedataobject_type, &teedataobject_spec);
46924672
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
46934673

4694-
PyTypeObject *typelist[] = {
4695-
&batched_type,
4696-
};
4697-
46984674
Py_SET_TYPE(state->teedataobject_type, &PyType_Type);
4699-
4700-
for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
4701-
if (PyModule_AddType(mod, typelist[i]) < 0) {
4702-
return -1;
4703-
}
4704-
}
4705-
47064675
return 0;
47074676
}
47084677

0 commit comments

Comments
 (0)