Skip to content

Commit 1d2d93c

Browse files
pythongh-101277: Add count type to module state
1 parent ff0e0f6 commit 1d2d93c

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"
@@ -4171,15 +4171,18 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
41714171
static void
41724172
count_dealloc(countobject *lz)
41734173
{
4174+
PyTypeObject *tp = Py_TYPE(lz);
41744175
PyObject_GC_UnTrack(lz);
41754176
Py_XDECREF(lz->long_cnt);
41764177
Py_XDECREF(lz->long_step);
4177-
Py_TYPE(lz)->tp_free(lz);
4178+
tp->tp_free(lz);
4179+
Py_DECREF(tp);
41784180
}
41794181

41804182
static int
41814183
count_traverse(countobject *lz, visitproc visit, void *arg)
41824184
{
4185+
Py_VISIT(Py_TYPE(lz));
41834186
Py_VISIT(lz->long_cnt);
41844187
Py_VISIT(lz->long_step);
41854188
return 0;
@@ -4253,48 +4256,26 @@ static PyMethodDef count_methods[] = {
42534256
{NULL, NULL} /* sentinel */
42544257
};
42554258

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

43004281

@@ -4764,6 +4745,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47644745
Py_VISIT(state->accumulate_type);
47654746
Py_VISIT(state->combinations_type);
47664747
Py_VISIT(state->compress_type);
4748+
Py_VISIT(state->count_type);
47674749
Py_VISIT(state->cwr_type);
47684750
Py_VISIT(state->cycle_type);
47694751
Py_VISIT(state->dropwhile_type);
@@ -4783,6 +4765,7 @@ itertoolsmodule_clear(PyObject *mod)
47834765
Py_CLEAR(state->accumulate_type);
47844766
Py_CLEAR(state->combinations_type);
47854767
Py_CLEAR(state->compress_type);
4768+
Py_CLEAR(state->count_type);
47864769
Py_CLEAR(state->cwr_type);
47874770
Py_CLEAR(state->cycle_type);
47884771
Py_CLEAR(state->dropwhile_type);
@@ -4819,6 +4802,7 @@ itertoolsmodule_exec(PyObject *mod)
48194802
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48204803
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
48214804
ADD_TYPE(mod, state->compress_type, &compress_spec);
4805+
ADD_TYPE(mod, state->count_type, &count_spec);
48224806
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48234807
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48244808
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4833,7 +4817,6 @@ itertoolsmodule_exec(PyObject *mod)
48334817
&batched_type,
48344818
&islice_type,
48354819
&chain_type,
4836-
&count_type,
48374820
&ziplongest_type,
48384821
&pairwise_type,
48394822
&product_type,

0 commit comments

Comments
 (0)