Skip to content

Commit 8708e94

Browse files
pythongh-101277: Add count type to module state
1 parent 378072e commit 8708e94

File tree

1 file changed

+30
-47
lines changed

1 file changed

+30
-47
lines changed

Modules/itertoolsmodule.c

+30-47
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef struct {
1515
PyTypeObject *accumulate_type;
1616
PyTypeObject *combinations_type;
1717
PyTypeObject *compress_type;
18+
PyTypeObject *count_type;
1819
PyTypeObject *cwr_type;
1920
PyTypeObject *cycle_type;
2021
PyTypeObject *dropwhile_type;
@@ -71,15 +72,14 @@ class itertools.permutations "permutationsobject *" "clinic_state()->permutation
7172
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
7273
class itertools.compress "compressobject *" "clinic_state()->compress_type"
7374
class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
74-
class itertools.count "countobject *" "&count_type"
75+
class itertools.count "countobject *" "clinic_state()->count_type"
7576
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
7677
[clinic start generated code]*/
77-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=041cf92c608e0a3b]*/
78+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=338b4d26465f3eb1]*/
7879

7980
static PyTypeObject teedataobject_type;
8081
static PyTypeObject tee_type;
8182
static PyTypeObject batched_type;
82-
static PyTypeObject count_type;
8383
static PyTypeObject pairwise_type;
8484

8585
#define clinic_state_by_cls() (get_module_state_by_cls(base_tp))
@@ -4185,15 +4185,18 @@ itertools_count_impl(PyTypeObject *type, PyObject *long_cnt,
41854185
static void
41864186
count_dealloc(countobject *lz)
41874187
{
4188+
PyTypeObject *tp = Py_TYPE(lz);
41884189
PyObject_GC_UnTrack(lz);
41894190
Py_XDECREF(lz->long_cnt);
41904191
Py_XDECREF(lz->long_step);
4191-
Py_TYPE(lz)->tp_free(lz);
4192+
tp->tp_free(lz);
4193+
Py_DECREF(tp);
41924194
}
41934195

41944196
static int
41954197
count_traverse(countobject *lz, visitproc visit, void *arg)
41964198
{
4199+
Py_VISIT(Py_TYPE(lz));
41974200
Py_VISIT(lz->long_cnt);
41984201
Py_VISIT(lz->long_step);
41994202
return 0;
@@ -4267,48 +4270,26 @@ static PyMethodDef count_methods[] = {
42674270
{NULL, NULL} /* sentinel */
42684271
};
42694272

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

43144295

@@ -4778,6 +4759,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47784759
Py_VISIT(state->accumulate_type);
47794760
Py_VISIT(state->combinations_type);
47804761
Py_VISIT(state->compress_type);
4762+
Py_VISIT(state->count_type);
47814763
Py_VISIT(state->cwr_type);
47824764
Py_VISIT(state->cycle_type);
47834765
Py_VISIT(state->dropwhile_type);
@@ -4797,6 +4779,7 @@ itertoolsmodule_clear(PyObject *mod)
47974779
Py_CLEAR(state->accumulate_type);
47984780
Py_CLEAR(state->combinations_type);
47994781
Py_CLEAR(state->compress_type);
4782+
Py_CLEAR(state->count_type);
48004783
Py_CLEAR(state->cwr_type);
48014784
Py_CLEAR(state->cycle_type);
48024785
Py_CLEAR(state->dropwhile_type);
@@ -4833,6 +4816,7 @@ itertoolsmodule_exec(PyObject *mod)
48334816
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48344817
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
48354818
ADD_TYPE(mod, state->compress_type, &compress_spec);
4819+
ADD_TYPE(mod, state->count_type, &count_spec);
48364820
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48374821
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
48384822
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
@@ -4847,7 +4831,6 @@ itertoolsmodule_exec(PyObject *mod)
48474831
&batched_type,
48484832
&islice_type,
48494833
&chain_type,
4850-
&count_type,
48514834
&ziplongest_type,
48524835
&pairwise_type,
48534836
&product_type,

0 commit comments

Comments
 (0)