Skip to content

Commit a3c78ad

Browse files
pythongh-101277: Add repeat type to module state
1 parent 1c3828d commit a3c78ad

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

Modules/itertoolsmodule.c

+27-46
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct {
2424
PyTypeObject *pairwise_type;
2525
PyTypeObject *permutations_type;
2626
PyTypeObject *product_type;
27+
PyTypeObject *repeat_type;
2728
PyTypeObject *starmap_type;
2829
PyTypeObject *takewhile_type;
2930
PyTypeObject *ziplongest_type;
@@ -4247,8 +4248,6 @@ typedef struct {
42474248
Py_ssize_t cnt;
42484249
} repeatobject;
42494250

4250-
static PyTypeObject repeat_type;
4251-
42524251
static PyObject *
42534252
repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42544253
{
@@ -4278,14 +4277,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42784277
static void
42794278
repeat_dealloc(repeatobject *ro)
42804279
{
4280+
PyTypeObject *tp = Py_TYPE(ro);
42814281
PyObject_GC_UnTrack(ro);
42824282
Py_XDECREF(ro->element);
4283-
Py_TYPE(ro)->tp_free(ro);
4283+
tp->tp_free(ro);
4284+
Py_DECREF(tp);
42844285
}
42854286

42864287
static int
42874288
repeat_traverse(repeatobject *ro, visitproc visit, void *arg)
42884289
{
4290+
Py_VISIT(Py_TYPE(ro));
42894291
Py_VISIT(ro->element);
42904292
return 0;
42914293
}
@@ -4347,48 +4349,25 @@ PyDoc_STRVAR(repeat_doc,
43474349
for the specified number of times. If not specified, returns the object\n\
43484350
endlessly.");
43494351

4350-
static PyTypeObject repeat_type = {
4351-
PyVarObject_HEAD_INIT(NULL, 0)
4352-
"itertools.repeat", /* tp_name */
4353-
sizeof(repeatobject), /* tp_basicsize */
4354-
0, /* tp_itemsize */
4355-
/* methods */
4356-
(destructor)repeat_dealloc, /* tp_dealloc */
4357-
0, /* tp_vectorcall_offset */
4358-
0, /* tp_getattr */
4359-
0, /* tp_setattr */
4360-
0, /* tp_as_async */
4361-
(reprfunc)repeat_repr, /* tp_repr */
4362-
0, /* tp_as_number */
4363-
0, /* tp_as_sequence */
4364-
0, /* tp_as_mapping */
4365-
0, /* tp_hash */
4366-
0, /* tp_call */
4367-
0, /* tp_str */
4368-
PyObject_GenericGetAttr, /* tp_getattro */
4369-
0, /* tp_setattro */
4370-
0, /* tp_as_buffer */
4371-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4372-
Py_TPFLAGS_BASETYPE, /* tp_flags */
4373-
repeat_doc, /* tp_doc */
4374-
(traverseproc)repeat_traverse, /* tp_traverse */
4375-
0, /* tp_clear */
4376-
0, /* tp_richcompare */
4377-
0, /* tp_weaklistoffset */
4378-
PyObject_SelfIter, /* tp_iter */
4379-
(iternextfunc)repeat_next, /* tp_iternext */
4380-
repeat_methods, /* tp_methods */
4381-
0, /* tp_members */
4382-
0, /* tp_getset */
4383-
0, /* tp_base */
4384-
0, /* tp_dict */
4385-
0, /* tp_descr_get */
4386-
0, /* tp_descr_set */
4387-
0, /* tp_dictoffset */
4388-
0, /* tp_init */
4389-
0, /* tp_alloc */
4390-
repeat_new, /* tp_new */
4391-
PyObject_GC_Del, /* tp_free */
4352+
static PyType_Slot repeat_slots[] = {
4353+
{Py_tp_dealloc, repeat_dealloc},
4354+
{Py_tp_repr, repeat_repr},
4355+
{Py_tp_getattro, PyObject_GenericGetAttr},
4356+
{Py_tp_doc, (void *)repeat_doc},
4357+
{Py_tp_traverse, repeat_traverse},
4358+
{Py_tp_iter, PyObject_SelfIter},
4359+
{Py_tp_iternext, repeat_next},
4360+
{Py_tp_methods, repeat_methods},
4361+
{Py_tp_new, repeat_new},
4362+
{Py_tp_free, PyObject_GC_Del},
4363+
{0, NULL},
4364+
};
4365+
4366+
static PyType_Spec repeat_spec = {
4367+
.name = "itertools.repeat",
4368+
.basicsize = sizeof(repeatobject),
4369+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE,
4370+
.slots = repeat_slots,
43924371
};
43934372

43944373

@@ -4692,6 +4671,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
46924671
Py_VISIT(state->_grouper_type);
46934672
Py_VISIT(state->permutations_type);
46944673
Py_VISIT(state->product_type);
4674+
Py_VISIT(state->repeat_type);
46954675
Py_VISIT(state->starmap_type);
46964676
Py_VISIT(state->takewhile_type);
46974677
Py_VISIT(state->ziplongest_type);
@@ -4714,6 +4694,7 @@ itertoolsmodule_clear(PyObject *mod)
47144694
Py_CLEAR(state->_grouper_type);
47154695
Py_CLEAR(state->permutations_type);
47164696
Py_CLEAR(state->product_type);
4697+
Py_CLEAR(state->repeat_type);
47174698
Py_CLEAR(state->starmap_type);
47184699
Py_CLEAR(state->takewhile_type);
47194700
Py_CLEAR(state->ziplongest_type);
@@ -4754,6 +4735,7 @@ itertoolsmodule_exec(PyObject *mod)
47544735
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47554736
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
47564737
ADD_TYPE(mod, state->product_type, &product_spec);
4738+
ADD_TYPE(mod, state->repeat_type, &repeat_spec);
47574739
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47584740
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
47594741
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
@@ -4762,7 +4744,6 @@ itertoolsmodule_exec(PyObject *mod)
47624744
&batched_type,
47634745
&islice_type,
47644746
&chain_type,
4765-
&repeat_type,
47664747
&tee_type,
47674748
&teedataobject_type
47684749
};

0 commit comments

Comments
 (0)