Skip to content

Commit 2ede7ad

Browse files
gh-101277: Add takewhile type to module state
1 parent 54c43a4 commit 2ede7ad

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

Modules/clinic/itertoolsmodule.c.h

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/itertoolsmodule.c

+28-47
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct {
1515
PyTypeObject *dropwhile_type;
1616
PyTypeObject *groupby_type;
1717
PyTypeObject *_grouper_type;
18+
PyTypeObject *takewhile_type;
1819
} itertools_state;
1920

2021
static inline itertools_state *
@@ -44,7 +45,7 @@ class itertools._tee "teeobject *" "&tee_type"
4445
class itertools.batched "batchedobject *" "&batched_type"
4546
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
4647
class itertools.dropwhile "dropwhileobject *" "clinic_state()->dropwhile_type"
47-
class itertools.takewhile "takewhileobject *" "&takewhile_type"
48+
class itertools.takewhile "takewhileobject *" "clinic_state()->takewhile_type"
4849
class itertools.starmap "starmapobject *" "&starmap_type"
4950
class itertools.chain "chainobject *" "&chain_type"
5051
class itertools.combinations "combinationsobject *" "&combinations_type"
@@ -56,12 +57,11 @@ class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
5657
class itertools.count "countobject *" "&count_type"
5758
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
5859
[clinic start generated code]*/
59-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ef6f5c44c6837d9e]*/
60+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3015dff7a88cfc00]*/
6061

6162
static PyTypeObject teedataobject_type;
6263
static PyTypeObject tee_type;
6364
static PyTypeObject batched_type;
64-
static PyTypeObject takewhile_type;
6565
static PyTypeObject starmap_type;
6666
static PyTypeObject combinations_type;
6767
static PyTypeObject cwr_type;
@@ -1583,10 +1583,12 @@ itertools_takewhile_impl(PyTypeObject *type, PyObject *func, PyObject *seq)
15831583
static void
15841584
takewhile_dealloc(takewhileobject *lz)
15851585
{
1586+
PyTypeObject *tp = Py_TYPE(lz);
15861587
PyObject_GC_UnTrack(lz);
15871588
Py_XDECREF(lz->func);
15881589
Py_XDECREF(lz->it);
1589-
Py_TYPE(lz)->tp_free(lz);
1590+
tp->tp_free(lz);
1591+
Py_DECREF(tp);
15901592
}
15911593

15921594
static int
@@ -1651,48 +1653,25 @@ static PyMethodDef takewhile_reduce_methods[] = {
16511653
{NULL, NULL} /* sentinel */
16521654
};
16531655

1654-
static PyTypeObject takewhile_type = {
1655-
PyVarObject_HEAD_INIT(NULL, 0)
1656-
"itertools.takewhile", /* tp_name */
1657-
sizeof(takewhileobject), /* tp_basicsize */
1658-
0, /* tp_itemsize */
1659-
/* methods */
1660-
(destructor)takewhile_dealloc, /* tp_dealloc */
1661-
0, /* tp_vectorcall_offset */
1662-
0, /* tp_getattr */
1663-
0, /* tp_setattr */
1664-
0, /* tp_as_async */
1665-
0, /* tp_repr */
1666-
0, /* tp_as_number */
1667-
0, /* tp_as_sequence */
1668-
0, /* tp_as_mapping */
1669-
0, /* tp_hash */
1670-
0, /* tp_call */
1671-
0, /* tp_str */
1672-
PyObject_GenericGetAttr, /* tp_getattro */
1673-
0, /* tp_setattro */
1674-
0, /* tp_as_buffer */
1675-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1676-
Py_TPFLAGS_BASETYPE, /* tp_flags */
1677-
itertools_takewhile__doc__, /* tp_doc */
1678-
(traverseproc)takewhile_traverse, /* tp_traverse */
1679-
0, /* tp_clear */
1680-
0, /* tp_richcompare */
1681-
0, /* tp_weaklistoffset */
1682-
PyObject_SelfIter, /* tp_iter */
1683-
(iternextfunc)takewhile_next, /* tp_iternext */
1684-
takewhile_reduce_methods, /* tp_methods */
1685-
0, /* tp_members */
1686-
0, /* tp_getset */
1687-
0, /* tp_base */
1688-
0, /* tp_dict */
1689-
0, /* tp_descr_get */
1690-
0, /* tp_descr_set */
1691-
0, /* tp_dictoffset */
1692-
0, /* tp_init */
1693-
0, /* tp_alloc */
1694-
itertools_takewhile, /* tp_new */
1695-
PyObject_GC_Del, /* tp_free */
1656+
static PyType_Slot takewhile_slots[] = {
1657+
{Py_tp_dealloc, takewhile_dealloc},
1658+
{Py_tp_getattro, PyObject_GenericGetAttr},
1659+
{Py_tp_doc, (void *)itertools_takewhile__doc__},
1660+
{Py_tp_traverse, takewhile_traverse},
1661+
{Py_tp_iter, PyObject_SelfIter},
1662+
{Py_tp_iternext, takewhile_next},
1663+
{Py_tp_methods, takewhile_reduce_methods},
1664+
{Py_tp_new, itertools_takewhile},
1665+
{Py_tp_free, PyObject_GC_Del},
1666+
{0, NULL},
1667+
};
1668+
1669+
static PyType_Spec takewhile_spec = {
1670+
.name = "itertools.takewhile",
1671+
.basicsize = sizeof(takewhileobject),
1672+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
1673+
Py_TPFLAGS_IMMUTABLETYPE),
1674+
.slots = takewhile_slots,
16961675
};
16971676

16981677

@@ -4926,6 +4905,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
49264905
Py_VISIT(state->dropwhile_type);
49274906
Py_VISIT(state->groupby_type);
49284907
Py_VISIT(state->_grouper_type);
4908+
Py_VISIT(state->takewhile_type);
49294909
return 0;
49304910
}
49314911

@@ -4937,6 +4917,7 @@ itertoolsmodule_clear(PyObject *mod)
49374917
Py_CLEAR(state->dropwhile_type);
49384918
Py_CLEAR(state->groupby_type);
49394919
Py_CLEAR(state->_grouper_type);
4920+
Py_CLEAR(state->takewhile_type);
49404921
return 0;
49414922
}
49424923

@@ -4965,13 +4946,13 @@ itertoolsmodule_exec(PyObject *mod)
49654946
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
49664947
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
49674948
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
4949+
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
49684950

49694951
PyTypeObject *typelist[] = {
49704952
&accumulate_type,
49714953
&batched_type,
49724954
&combinations_type,
49734955
&cwr_type,
4974-
&takewhile_type,
49754956
&islice_type,
49764957
&starmap_type,
49774958
&chain_type,

0 commit comments

Comments
 (0)