Skip to content

Commit 1e0c23d

Browse files
gh-101277: Add count type to module state
1 parent b29e885 commit 1e0c23d

File tree

1 file changed

+30
-47
lines changed

1 file changed

+30
-47
lines changed

Modules/itertoolsmodule.c

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct {
1414
PyTypeObject *accumulate_type;
1515
PyTypeObject *combinations_type;
1616
PyTypeObject *compress_type;
17+
PyTypeObject *count_type;
1718
PyTypeObject *cwr_type;
1819
PyTypeObject *cycle_type;
1920
PyTypeObject *dropwhile_type;
@@ -61,15 +62,14 @@ class itertools.permutations "permutationsobject *" "clinic_state()->permutation
6162
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
6263
class itertools.compress "compressobject *" "clinic_state()->compress_type"
6364
class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
64-
class itertools.count "countobject *" "&count_type"
65+
class itertools.count "countobject *" "clinic_state()->count_type"
6566
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
6667
[clinic start generated code]*/
67-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=041cf92c608e0a3b]*/
68+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=338b4d26465f3eb1]*/
6869

6970
static PyTypeObject teedataobject_type;
7071
static PyTypeObject tee_type;
7172
static PyTypeObject batched_type;
72-
static PyTypeObject count_type;
7373
static PyTypeObject pairwise_type;
7474

7575
#include "clinic/itertoolsmodule.c.h"
@@ -4172,15 +4172,18 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
41724172
static void
41734173
count_dealloc(countobject *lz)
41744174
{
4175+
PyTypeObject *tp = Py_TYPE(lz);
41754176
PyObject_GC_UnTrack(lz);
41764177
Py_XDECREF(lz->long_cnt);
41774178
Py_XDECREF(lz->long_step);
4178-
Py_TYPE(lz)->tp_free(lz);
4179+
tp->tp_free(lz);
4180+
Py_DECREF(tp);
41794181
}
41804182

41814183
static int
41824184
count_traverse(countobject *lz, visitproc visit, void *arg)
41834185
{
4186+
Py_VISIT(Py_TYPE(lz));
41844187
Py_VISIT(lz->long_cnt);
41854188
Py_VISIT(lz->long_step);
41864189
return 0;
@@ -4254,48 +4257,26 @@ static PyMethodDef count_methods[] = {
42544257
{NULL, NULL} /* sentinel */
42554258
};
42564259

4257-
static PyTypeObject count_type = {
4258-
PyVarObject_HEAD_INIT(NULL, 0)
4259-
"itertools.count", /* tp_name */
4260-
sizeof(countobject), /* tp_basicsize */
4261-
0, /* tp_itemsize */
4262-
/* methods */
4263-
(destructor)count_dealloc, /* tp_dealloc */
4264-
0, /* tp_vectorcall_offset */
4265-
0, /* tp_getattr */
4266-
0, /* tp_setattr */
4267-
0, /* tp_as_async */
4268-
(reprfunc)count_repr, /* tp_repr */
4269-
0, /* tp_as_number */
4270-
0, /* tp_as_sequence */
4271-
0, /* tp_as_mapping */
4272-
0, /* tp_hash */
4273-
0, /* tp_call */
4274-
0, /* tp_str */
4275-
PyObject_GenericGetAttr, /* tp_getattro */
4276-
0, /* tp_setattro */
4277-
0, /* tp_as_buffer */
4278-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4279-
Py_TPFLAGS_BASETYPE, /* tp_flags */
4280-
itertools_count__doc__, /* tp_doc */
4281-
(traverseproc)count_traverse, /* tp_traverse */
4282-
0, /* tp_clear */
4283-
0, /* tp_richcompare */
4284-
0, /* tp_weaklistoffset */
4285-
PyObject_SelfIter, /* tp_iter */
4286-
(iternextfunc)count_next, /* tp_iternext */
4287-
count_methods, /* tp_methods */
4288-
0, /* tp_members */
4289-
0, /* tp_getset */
4290-
0, /* tp_base */
4291-
0, /* tp_dict */
4292-
0, /* tp_descr_get */
4293-
0, /* tp_descr_set */
4294-
0, /* tp_dictoffset */
4295-
0, /* tp_init */
4296-
0, /* tp_alloc */
4297-
itertools_count, /* tp_new */
4298-
PyObject_GC_Del, /* tp_free */
4260+
static PyType_Slot count_slots[] = {
4261+
{Py_tp_dealloc, count_dealloc},
4262+
{Py_tp_repr, count_repr},
4263+
{Py_tp_getattro, PyObject_GenericGetAttr},
4264+
{Py_tp_doc, (void *)itertools_count__doc__},
4265+
{Py_tp_traverse, count_traverse},
4266+
{Py_tp_iter, PyObject_SelfIter},
4267+
{Py_tp_iternext, count_next},
4268+
{Py_tp_methods, count_methods},
4269+
{Py_tp_new, itertools_count},
4270+
{Py_tp_free, PyObject_GC_Del},
4271+
{0, NULL},
4272+
};
4273+
4274+
static PyType_Spec count_spec = {
4275+
.name = "itertools.count",
4276+
.basicsize = sizeof(countobject),
4277+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
4278+
Py_TPFLAGS_IMMUTABLETYPE),
4279+
.slots = count_slots,
42994280
};
43004281

43014282

@@ -4765,6 +4746,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47654746
Py_VISIT(state->accumulate_type);
47664747
Py_VISIT(state->combinations_type);
47674748
Py_VISIT(state->compress_type);
4749+
Py_VISIT(state->count_type);
47684750
Py_VISIT(state->cwr_type);
47694751
Py_VISIT(state->cycle_type);
47704752
Py_VISIT(state->dropwhile_type);
@@ -4784,6 +4766,7 @@ itertoolsmodule_clear(PyObject *mod)
47844766
Py_CLEAR(state->accumulate_type);
47854767
Py_CLEAR(state->combinations_type);
47864768
Py_CLEAR(state->compress_type);
4769+
Py_CLEAR(state->count_type);
47874770
Py_CLEAR(state->cwr_type);
47884771
Py_CLEAR(state->cycle_type);
47894772
Py_CLEAR(state->dropwhile_type);
@@ -4820,6 +4803,7 @@ itertoolsmodule_exec(PyObject *mod)
48204803
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48214804
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
48224805
ADD_TYPE(mod, state->compress_type, &compress_spec);
4806+
ADD_TYPE(mod, state->count_type, &count_spec);
48234807
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48244808
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48254809
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4834,7 +4818,6 @@ itertoolsmodule_exec(PyObject *mod)
48344818
&batched_type,
48354819
&islice_type,
48364820
&chain_type,
4837-
&count_type,
48384821
&ziplongest_type,
48394822
&pairwise_type,
48404823
&product_type,

0 commit comments

Comments
 (0)