Skip to content

Commit 4264f8b

Browse files
Add batched type to module state
1 parent e937984 commit 4264f8b

File tree

1 file changed

+30
-61
lines changed

1 file changed

+30
-61
lines changed

Modules/itertoolsmodule.c

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
typedef struct {
1616
PyTypeObject *accumulate_type;
17+
PyTypeObject *batched_type;
1718
PyTypeObject *chain_type;
1819
PyTypeObject *combinations_type;
1920
PyTypeObject *compress_type;
@@ -68,7 +69,7 @@ class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type"
6869
class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
6970
class itertools.teedataobject "teedataobject *" "clinic_state()->teedataobject_type"
7071
class itertools._tee "teeobject *" "clinic_state()->tee_type"
71-
class itertools.batched "batchedobject *" "&batched_type"
72+
class itertools.batched "batchedobject *" "clinic_state()->batched_type"
7273
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
7374
class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type"
7475
class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type"
@@ -83,9 +84,7 @@ class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_t
8384
class itertools.count "countobject *" "clinic_state()->count_type"
8485
class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
8586
[clinic start generated code]*/
86-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=419423f2d82cf388]*/
87-
88-
static PyTypeObject batched_type;
87+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=aa48fe4de9d4080f]*/
8988

9089
#define clinic_state() (find_state_by_type(type))
9190
#define clinic_state_by_cls() (get_module_state_by_cls(base_tp))
@@ -166,17 +165,18 @@ batched_new_impl(PyTypeObject *type, PyObject *iterable, Py_ssize_t n)
166165
static void
167166
batched_dealloc(batchedobject *bo)
168167
{
168+
PyTypeObject *tp = Py_TYPE(bo);
169169
PyObject_GC_UnTrack(bo);
170170
Py_XDECREF(bo->it);
171-
Py_TYPE(bo)->tp_free(bo);
171+
tp->tp_free(bo);
172+
Py_DECREF(tp);
172173
}
173174

174175
static int
175176
batched_traverse(batchedobject *bo, visitproc visit, void *arg)
176177
{
177-
if (bo->it != NULL) {
178-
Py_VISIT(bo->it);
179-
}
178+
Py_VISIT(Py_TYPE(bo));
179+
Py_VISIT(bo->it);
180180
return 0;
181181
}
182182

@@ -226,48 +226,25 @@ batched_next(batchedobject *bo)
226226
return result;
227227
}
228228

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

273250

@@ -4612,6 +4589,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
46124589
{
46134590
itertools_state *state = get_module_state(mod);
46144591
Py_VISIT(state->accumulate_type);
4592+
Py_VISIT(state->batched_type);
46154593
Py_VISIT(state->chain_type);
46164594
Py_VISIT(state->combinations_type);
46174595
Py_VISIT(state->compress_type);
@@ -4639,6 +4617,7 @@ itertoolsmodule_clear(PyObject *mod)
46394617
{
46404618
itertools_state *state = get_module_state(mod);
46414619
Py_CLEAR(state->accumulate_type);
4620+
Py_CLEAR(state->batched_type);
46424621
Py_CLEAR(state->chain_type);
46434622
Py_CLEAR(state->combinations_type);
46444623
Py_CLEAR(state->compress_type);
@@ -4683,6 +4662,7 @@ itertoolsmodule_exec(PyObject *mod)
46834662
{
46844663
itertools_state *state = get_module_state(mod);
46854664
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
4665+
ADD_TYPE(mod, state->batched_type, &batched_spec);
46864666
ADD_TYPE(mod, state->chain_type, &chain_spec);
46874667
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
46884668
ADD_TYPE(mod, state->compress_type, &compress_spec);
@@ -4704,18 +4684,7 @@ itertoolsmodule_exec(PyObject *mod)
47044684
ADD_TYPE(mod, state->teedataobject_type, &teedataobject_spec);
47054685
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
47064686

4707-
PyTypeObject *typelist[] = {
4708-
&batched_type,
4709-
};
4710-
47114687
Py_SET_TYPE(state->teedataobject_type, &PyType_Type);
4712-
4713-
for (size_t i = 0; i < Py_ARRAY_LENGTH(typelist); i++) {
4714-
if (PyModule_AddType(mod, typelist[i]) < 0) {
4715-
return -1;
4716-
}
4717-
}
4718-
47194688
return 0;
47204689
}
47214690

0 commit comments

Comments
 (0)