Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
gh-101277: Add takewhile type to module state
  • Loading branch information
erlend-aasland committed Feb 1, 2023
commit 0f486ef65dd32f2f38ac4c5696b025cfdefb8f42
9 changes: 9 additions & 0 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 @@ -16,6 +16,7 @@ typedef struct {
PyTypeObject *dropwhile_type;
PyTypeObject *groupby_type;
PyTypeObject *_grouper_type;
PyTypeObject *takewhile_type;
} itertools_state;

static inline itertools_state *
Expand Down Expand Up @@ -54,7 +55,7 @@ class itertools._tee "teeobject *" "&tee_type"
class itertools.batched "batchedobject *" "&batched_type"
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type"
class itertools.takewhile "takewhileobject *" "&takewhile_type"
class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type"
class itertools.starmap "starmapobject *" "&starmap_type"
class itertools.chain "chainobject *" "&chain_type"
class itertools.combinations "combinationsobject *" "&combinations_type"
Expand All @@ -66,12 +67,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=ef6f5c44c6837d9e]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3015dff7a88cfc00]*/

static PyTypeObject teedataobject_type;
static PyTypeObject tee_type;
static PyTypeObject batched_type;
static PyTypeObject takewhile_type;
static PyTypeObject starmap_type;
static PyTypeObject combinations_type;
static PyTypeObject cwr_type;
Expand Down Expand Up @@ -1596,15 +1596,18 @@ itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
static void
takewhile_dealloc(takewhileobject *lz)
{
PyTypeObject *tp = Py_TYPE(lz);
PyObject_GC_UnTrack(lz);
Py_XDECREF(lz->func);
Py_XDECREF(lz->it);
Py_TYPE(lz)->tp_free(lz);
tp->tp_free(lz);
Py_DECREF(tp);
}

static int
takewhile_traverse(takewhileobject *lz, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(lz));
Py_VISIT(lz->it);
Py_VISIT(lz->func);
return 0;
Expand Down Expand Up @@ -1664,48 +1667,25 @@ static PyMethodDef takewhile_reduce_methods[] = {
{NULL, NULL} /* sentinel */
};

static PyTypeObject takewhile_type = {
PyVarObject_HEAD_INIT(NULL, 0)
"itertools.takewhile", /* tp_name */
sizeof(takewhileobject), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)takewhile_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_takewhile__doc__, /* tp_doc */
(traverseproc)takewhile_traverse, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)takewhile_next, /* tp_iternext */
takewhile_reduce_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_takewhile, /* tp_new */
PyObject_GC_Del, /* tp_free */
static PyType_Slot takewhile_slots[] = {
{Py_tp_dealloc, takewhile_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_doc, (void *)itertools_takewhile__doc__},
{Py_tp_traverse, takewhile_traverse},
{Py_tp_iter, PyObject_SelfIter},
{Py_tp_iternext, takewhile_next},
{Py_tp_methods, takewhile_reduce_methods},
{Py_tp_new, itertools_takewhile},
{Py_tp_free, PyObject_GC_Del},
{0, NULL},
};

static PyType_Spec takewhile_spec = {
.name = "itertools.takewhile",
.basicsize = sizeof(takewhileobject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = takewhile_slots,
};


Expand Down Expand Up @@ -4939,6 +4919,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
Py_VISIT(state->dropwhile_type);
Py_VISIT(state->groupby_type);
Py_VISIT(state->_grouper_type);
Py_VISIT(state->takewhile_type);
return 0;
}

Expand All @@ -4950,6 +4931,7 @@ itertoolsmodule_clear(PyObject *mod)
Py_CLEAR(state->dropwhile_type);
Py_CLEAR(state->groupby_type);
Py_CLEAR(state->_grouper_type);
Py_CLEAR(state->takewhile_type);
return 0;
}

Expand Down Expand Up @@ -4978,13 +4960,13 @@ itertoolsmodule_exec(PyObject *mod)
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);

PyTypeObject *typelist[] = {
&accumulate_type,
&batched_type,
&combinations_type,
&cwr_type,
&takewhile_type,
&islice_type,
&starmap_type,
&chain_type,
Expand Down