Skip to content

Commit

Permalink
pythongh-101277: Add takewhile 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 54c43a4 commit 2ede7ad
Show file tree
Hide file tree
Showing 2 changed files with 31 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.

75 changes: 28 additions & 47 deletions Modules/itertoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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 @@ -44,7 +45,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 @@ -56,12 +57,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 @@ -1583,10 +1583,12 @@ 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
Expand Down Expand Up @@ -1651,48 +1653,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 @@ -4926,6 +4905,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 @@ -4937,6 +4917,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 @@ -4965,13 +4946,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

0 comments on commit 2ede7ad

Please sign in to comment.