Skip to content

Commit 98636d2

Browse files
pythongh-101277: Add cycle type to module state
1 parent 50410bf commit 98636d2

File tree

2 files changed

+32
-50
lines changed

2 files changed

+32
-50
lines changed

Modules/clinic/itertoolsmodule.c.h

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

Modules/itertoolsmodule.c

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
typedef struct {
14+
PyTypeObject *cycle_type;
1415
PyTypeObject *groupby_type;
1516
PyTypeObject *_grouper_type;
1617
} itertools_state;
@@ -40,7 +41,7 @@ class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
4041
class itertools.teedataobject "teedataobject *" "&teedataobject_type"
4142
class itertools._tee "teeobject *" "&tee_type"
4243
class itertools.batched "batchedobject *" "&batched_type"
43-
class itertools.cycle "cycleobject *" "&cycle_type"
44+
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
4445
class itertools.dropwhile "dropwhileobject *" "&dropwhile_type"
4546
class itertools.takewhile "takewhileobject *" "&takewhile_type"
4647
class itertools.starmap "starmapobject *" "&starmap_type"
@@ -54,12 +55,11 @@ class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
5455
class itertools.count "countobject *" "&count_type"
5556
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
5657
[clinic start generated code]*/
57-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=424108522584b55b]*/
58+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b73cdca8e1fddfb5]*/
5859

5960
static PyTypeObject teedataobject_type;
6061
static PyTypeObject tee_type;
6162
static PyTypeObject batched_type;
62-
static PyTypeObject cycle_type;
6363
static PyTypeObject dropwhile_type;
6464
static PyTypeObject takewhile_type;
6565
static PyTypeObject starmap_type;
@@ -1273,15 +1273,18 @@ itertools_cycle_impl(PyTypeObject *type, PyObject *iterable)
12731273
static void
12741274
cycle_dealloc(cycleobject *lz)
12751275
{
1276+
PyTypeObject *tp = Py_TYPE(lz);
12761277
PyObject_GC_UnTrack(lz);
12771278
Py_XDECREF(lz->it);
12781279
Py_XDECREF(lz->saved);
1279-
Py_TYPE(lz)->tp_free(lz);
1280+
tp->tp_free(lz);
1281+
Py_DECREF(tp);
12801282
}
12811283

12821284
static int
12831285
cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
12841286
{
1287+
Py_VISIT(Py_TYPE(lz));
12851288
Py_VISIT(lz->it);
12861289
Py_VISIT(lz->saved);
12871290
return 0;
@@ -1368,48 +1371,25 @@ static PyMethodDef cycle_methods[] = {
13681371
{NULL, NULL} /* sentinel */
13691372
};
13701373

1371-
static PyTypeObject cycle_type = {
1372-
PyVarObject_HEAD_INIT(NULL, 0)
1373-
"itertools.cycle", /* tp_name */
1374-
sizeof(cycleobject), /* tp_basicsize */
1375-
0, /* tp_itemsize */
1376-
/* methods */
1377-
(destructor)cycle_dealloc, /* tp_dealloc */
1378-
0, /* tp_vectorcall_offset */
1379-
0, /* tp_getattr */
1380-
0, /* tp_setattr */
1381-
0, /* tp_as_async */
1382-
0, /* tp_repr */
1383-
0, /* tp_as_number */
1384-
0, /* tp_as_sequence */
1385-
0, /* tp_as_mapping */
1386-
0, /* tp_hash */
1387-
0, /* tp_call */
1388-
0, /* tp_str */
1389-
PyObject_GenericGetAttr, /* tp_getattro */
1390-
0, /* tp_setattro */
1391-
0, /* tp_as_buffer */
1392-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1393-
Py_TPFLAGS_BASETYPE, /* tp_flags */
1394-
itertools_cycle__doc__, /* tp_doc */
1395-
(traverseproc)cycle_traverse, /* tp_traverse */
1396-
0, /* tp_clear */
1397-
0, /* tp_richcompare */
1398-
0, /* tp_weaklistoffset */
1399-
PyObject_SelfIter, /* tp_iter */
1400-
(iternextfunc)cycle_next, /* tp_iternext */
1401-
cycle_methods, /* tp_methods */
1402-
0, /* tp_members */
1403-
0, /* tp_getset */
1404-
0, /* tp_base */
1405-
0, /* tp_dict */
1406-
0, /* tp_descr_get */
1407-
0, /* tp_descr_set */
1408-
0, /* tp_dictoffset */
1409-
0, /* tp_init */
1410-
0, /* tp_alloc */
1411-
itertools_cycle, /* tp_new */
1412-
PyObject_GC_Del, /* tp_free */
1374+
static PyType_Slot cycle_slots[] = {
1375+
{Py_tp_dealloc, cycle_dealloc},
1376+
{Py_tp_getattro, PyObject_GenericGetAttr},
1377+
{Py_tp_doc, (void *)itertools_cycle__doc__},
1378+
{Py_tp_traverse, cycle_traverse},
1379+
{Py_tp_iter, PyObject_SelfIter},
1380+
{Py_tp_iternext, cycle_next},
1381+
{Py_tp_methods, cycle_methods},
1382+
{Py_tp_new, itertools_cycle},
1383+
{Py_tp_free, PyObject_GC_Del},
1384+
{0, NULL},
1385+
};
1386+
1387+
static PyType_Spec cycle_spec = {
1388+
.name = "itertools.cycle",
1389+
.basicsize = sizeof(cycleobject),
1390+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
1391+
Py_TPFLAGS_IMMUTABLETYPE),
1392+
.slots = cycle_slots,
14131393
};
14141394

14151395

@@ -4962,6 +4942,7 @@ static int
49624942
itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
49634943
{
49644944
itertools_state *state = get_module_state(mod);
4945+
Py_VISIT(state->cycle_type);
49654946
Py_VISIT(state->groupby_type);
49664947
Py_VISIT(state->_grouper_type);
49674948
return 0;
@@ -4971,6 +4952,7 @@ static int
49714952
itertoolsmodule_clear(PyObject *mod)
49724953
{
49734954
itertools_state *state = get_module_state(mod);
4955+
Py_CLEAR(state->cycle_type);
49744956
Py_CLEAR(state->groupby_type);
49754957
Py_CLEAR(state->_grouper_type);
49764958
return 0;
@@ -4997,6 +4979,7 @@ static int
49974979
itertoolsmodule_exec(PyObject *mod)
49984980
{
49994981
itertools_state *state = get_module_state(mod);
4982+
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
50004983
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
50014984
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
50024985

@@ -5005,7 +4988,6 @@ itertoolsmodule_exec(PyObject *mod)
50054988
&batched_type,
50064989
&combinations_type,
50074990
&cwr_type,
5008-
&cycle_type,
50094991
&dropwhile_type,
50104992
&takewhile_type,
50114993
&islice_type,

0 commit comments

Comments
 (0)