Skip to content

Commit

Permalink
pythongh-101277: Add cycle type to module state
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Jan 24, 2023
1 parent 50410bf commit 98636d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
6 changes: 3 additions & 3 deletions Modules/clinic/itertoolsmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 29 additions & 47 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

typedef struct {
PyTypeObject *cycle_type;
PyTypeObject *groupby_type;
PyTypeObject *_grouper_type;
} itertools_state;
Expand Down Expand Up @@ -40,7 +41,7 @@ class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
class itertools.teedataobject "teedataobject *" "&teedataobject_type"
class itertools._tee "teeobject *" "&tee_type"
class itertools.batched "batchedobject *" "&batched_type"
class itertools.cycle "cycleobject *" "&cycle_type"
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
class itertools.dropwhile "dropwhileobject *" "&dropwhile_type"
class itertools.takewhile "takewhileobject *" "&takewhile_type"
class itertools.starmap "starmapobject *" "&starmap_type"
Expand All @@ -54,12 +55,11 @@ class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
class itertools.count "countobject *" "&count_type"
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=424108522584b55b]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b73cdca8e1fddfb5]*/

static PyTypeObject teedataobject_type;
static PyTypeObject tee_type;
static PyTypeObject batched_type;
static PyTypeObject cycle_type;
static PyTypeObject dropwhile_type;
static PyTypeObject takewhile_type;
static PyTypeObject starmap_type;
Expand Down Expand Up @@ -1273,15 +1273,18 @@ itertools_cycle_impl(PyTypeObject *type, PyObject *iterable)
static void
cycle_dealloc(cycleobject *lz)
{
PyTypeObject *tp = Py_TYPE(lz);
PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->it);
Py_XDECREF(lz->saved);
Py_TYPE(lz)->tp_free(lz);
tp->tp_free(lz);
Py_DECREF(tp);
}

static int
cycle_traverse(cycleobject *lz, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(lz));
Py_VISIT(lz->it);
Py_VISIT(lz->saved);
return 0;
Expand Down Expand Up @@ -1368,48 +1371,25 @@ static PyMethodDef cycle_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyTypeObject cycle_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.cycle", /* tp_name */
sizeof(cycleobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)cycle_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_BASETYPE, /* tp_flags */
itertools_cycle__doc__, /* tp_doc */
(traverseproc)cycle_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)cycle_next, /* tp_iternext */
cycle_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
itertools_cycle, /* tp_new */
PyObject_GC_Del, /* tp_free */
static PyType_Slot cycle_slots[] = {
{Py_tp_dealloc, cycle_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)itertools_cycle__doc__},
{Py_tp_traverse, cycle_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, cycle_next},
{Py_tp_methods, cycle_methods},
{Py_tp_new, itertools_cycle},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};

static PyType_Spec cycle_spec = {
.name = "itertools.cycle",
.basicsize = sizeof(cycleobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = cycle_slots,
};


Expand Down Expand Up @@ -4962,6 +4942,7 @@ static int
itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
{
itertools_state *state = get_module_state(mod);
Py_VISIT(state->cycle_type);
Py_VISIT(state->groupby_type);
Py_VISIT(state->_grouper_type);
return 0;
Expand All @@ -4971,6 +4952,7 @@ static int
itertoolsmodule_clear(PyObject *mod)
{
itertools_state *state = get_module_state(mod);
Py_CLEAR(state->cycle_type);
Py_CLEAR(state->groupby_type);
Py_CLEAR(state->_grouper_type);
return 0;
Expand All @@ -4997,6 +4979,7 @@ static int
itertoolsmodule_exec(PyObject *mod)
{
itertools_state *state = get_module_state(mod);
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);

Expand All @@ -5005,7 +4988,6 @@ itertoolsmodule_exec(PyObject *mod)
&batched_type,
&combinations_type,
&cwr_type,
&cycle_type,
&dropwhile_type,
&takewhile_type,
&islice_type,
Expand Down

0 comments on commit 98636d2

Please sign in to comment.