Skip to content

Commit 19ca7b1

Browse files
pythongh-101277: Add ziplongest type to module state
1 parent 38bf6e5 commit 19ca7b1

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

Modules/itertoolsmodule.c

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ typedef struct {
2525
PyTypeObject *permutations_type;
2626
PyTypeObject *starmap_type;
2727
PyTypeObject *takewhile_type;
28+
PyTypeObject *ziplongest_type;
2829
} itertools_state;
2930

3031
static inline itertools_state *
@@ -4423,8 +4424,6 @@ typedef struct {
44234424
PyObject *fillvalue;
44244425
} ziplongestobject;
44254426

4426-
static PyTypeObject ziplongest_type;
4427-
44284427
static PyObject *
44294428
zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44304429
{
@@ -4496,16 +4495,19 @@ zip_longest_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44964495
static void
44974496
zip_longest_dealloc(ziplongestobject *lz)
44984497
{
4498+
PyTypeObject *tp = Py_TYPE(lz);
44994499
PyObject_GC_UnTrack(lz);
45004500
Py_XDECREF(lz->ittuple);
45014501
Py_XDECREF(lz->result);
45024502
Py_XDECREF(lz->fillvalue);
4503-
Py_TYPE(lz)->tp_free(lz);
4503+
tp->tp_free(lz);
4504+
Py_DECREF(tp);
45044505
}
45054506

45064507
static int
45074508
zip_longest_traverse(ziplongestobject *lz, visitproc visit, void *arg)
45084509
{
4510+
Py_VISIT(Py_TYPE(lz));
45094511
Py_VISIT(lz->ittuple);
45104512
Py_VISIT(lz->result);
45114513
Py_VISIT(lz->fillvalue);
@@ -4639,48 +4641,25 @@ are exhausted, the fillvalue is substituted in their place. The fillvalue\n\
46394641
defaults to None or can be specified by a keyword argument.\n\
46404642
");
46414643

4642-
static PyTypeObject ziplongest_type = {
4643-
PyVarObject_HEAD_INIT(NULL, 0)
4644-
"itertools.zip_longest", /* tp_name */
4645-
sizeof(ziplongestobject), /* tp_basicsize */
4646-
0, /* tp_itemsize */
4647-
/* methods */
4648-
(destructor)zip_longest_dealloc, /* tp_dealloc */
4649-
0, /* tp_vectorcall_offset */
4650-
0, /* tp_getattr */
4651-
0, /* tp_setattr */
4652-
0, /* tp_as_async */
4653-
0, /* tp_repr */
4654-
0, /* tp_as_number */
4655-
0, /* tp_as_sequence */
4656-
0, /* tp_as_mapping */
4657-
0, /* tp_hash */
4658-
0, /* tp_call */
4659-
0, /* tp_str */
4660-
PyObject_GenericGetAttr, /* tp_getattro */
4661-
0, /* tp_setattro */
4662-
0, /* tp_as_buffer */
4663-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4664-
Py_TPFLAGS_BASETYPE, /* tp_flags */
4665-
zip_longest_doc, /* tp_doc */
4666-
(traverseproc)zip_longest_traverse, /* tp_traverse */
4667-
0, /* tp_clear */
4668-
0, /* tp_richcompare */
4669-
0, /* tp_weaklistoffset */
4670-
PyObject_SelfIter, /* tp_iter */
4671-
(iternextfunc)zip_longest_next, /* tp_iternext */
4672-
zip_longest_methods, /* tp_methods */
4673-
0, /* tp_members */
4674-
0, /* tp_getset */
4675-
0, /* tp_base */
4676-
0, /* tp_dict */
4677-
0, /* tp_descr_get */
4678-
0, /* tp_descr_set */
4679-
0, /* tp_dictoffset */
4680-
0, /* tp_init */
4681-
0, /* tp_alloc */
4682-
zip_longest_new, /* tp_new */
4683-
PyObject_GC_Del, /* tp_free */
4644+
static PyType_Slot ziplongest_slots[] = {
4645+
{Py_tp_dealloc, zip_longest_dealloc},
4646+
{Py_tp_getattro, PyObject_GenericGetAttr},
4647+
{Py_tp_doc, (void *)zip_longest_doc},
4648+
{Py_tp_traverse, zip_longest_traverse},
4649+
{Py_tp_iter, PyObject_SelfIter},
4650+
{Py_tp_iternext, zip_longest_next},
4651+
{Py_tp_methods, zip_longest_methods},
4652+
{Py_tp_new, zip_longest_new},
4653+
{Py_tp_free, PyObject_GC_Del},
4654+
{0, NULL},
4655+
};
4656+
4657+
static PyType_Spec ziplongest_spec = {
4658+
.name = "itertools.zip_longest",
4659+
.basicsize = sizeof(ziplongestobject),
4660+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
4661+
Py_TPFLAGS_IMMUTABLETYPE),
4662+
.slots = ziplongest_slots,
46844663
};
46854664

46864665

@@ -4735,6 +4714,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47354714
Py_VISIT(state->permutations_type);
47364715
Py_VISIT(state->starmap_type);
47374716
Py_VISIT(state->takewhile_type);
4717+
Py_VISIT(state->ziplongest_type);
47384718
return 0;
47394719
}
47404720

@@ -4755,6 +4735,7 @@ itertoolsmodule_clear(PyObject *mod)
47554735
Py_CLEAR(state->permutations_type);
47564736
Py_CLEAR(state->starmap_type);
47574737
Py_CLEAR(state->takewhile_type);
4738+
Py_CLEAR(state->ziplongest_type);
47584739
return 0;
47594740
}
47604741

@@ -4793,12 +4774,12 @@ itertoolsmodule_exec(PyObject *mod)
47934774
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
47944775
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47954776
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
4777+
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
47964778

47974779
PyTypeObject *typelist[] = {
47984780
&batched_type,
47994781
&islice_type,
48004782
&chain_type,
4801-
&ziplongest_type,
48024783
&product_type,
48034784
&repeat_type,
48044785
&tee_type,

0 commit comments

Comments
 (0)