Skip to content

Commit 9a4b14c

Browse files
pythongh-101277: Add repeat type to module state
1 parent de3669e commit 9a4b14c

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
@@ -25,6 +25,7 @@ typedef struct {
2525
PyTypeObject *pairwise_type;
2626
PyTypeObject *permutations_type;
2727
PyTypeObject *product_type;
28+
PyTypeObject *repeat_type;
2829
PyTypeObject *starmap_type;
2930
PyTypeObject *takewhile_type;
3031
PyTypeObject *ziplongest_type;
@@ -4261,8 +4262,6 @@ typedef struct {
42614262
Py_ssize_t cnt;
42624263
} repeatobject;
42634264

4264-
static PyTypeObject repeat_type;
4265-
42664265
static PyObject *
42674266
repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42684267
{
@@ -4292,14 +4291,17 @@ repeat_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
42924291
static void
42934292
repeat_dealloc(repeatobject *ro)
42944293
{
4294+
PyTypeObject *tp = Py_TYPE(ro);
42954295
PyObject_GC_UnTrack(ro);
42964296
Py_XDECREF(ro->element);
4297-
Py_TYPE(ro)->tp_free(ro);
4297+
tp->tp_free(ro);
4298+
Py_DECREF(tp);
42984299
}
42994300

43004301
static int
43014302
repeat_traverse(repeatobject *ro, visitproc visit, void *arg)
43024303
{
4304+
Py_VISIT(Py_TYPE(ro));
43034305
Py_VISIT(ro->element);
43044306
return 0;
43054307
}
@@ -4361,48 +4363,25 @@ PyDoc_STRVAR(repeat_doc,
43614363
for the specified number of times. If not specified, returns the object\n\
43624364
endlessly.");
43634365

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

44084387

@@ -4707,6 +4686,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47074686
Py_VISIT(state->pairwise_type);
47084687
Py_VISIT(state->permutations_type);
47094688
Py_VISIT(state->product_type);
4689+
Py_VISIT(state->repeat_type);
47104690
Py_VISIT(state->starmap_type);
47114691
Py_VISIT(state->takewhile_type);
47124692
Py_VISIT(state->ziplongest_type);
@@ -4730,6 +4710,7 @@ itertoolsmodule_clear(PyObject *mod)
47304710
Py_CLEAR(state->pairwise_type);
47314711
Py_CLEAR(state->permutations_type);
47324712
Py_CLEAR(state->product_type);
4713+
Py_CLEAR(state->repeat_type);
47334714
Py_CLEAR(state->starmap_type);
47344715
Py_CLEAR(state->takewhile_type);
47354716
Py_CLEAR(state->ziplongest_type);
@@ -4770,6 +4751,7 @@ itertoolsmodule_exec(PyObject *mod)
47704751
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47714752
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
47724753
ADD_TYPE(mod, state->product_type, &product_spec);
4754+
ADD_TYPE(mod, state->repeat_type, &repeat_spec);
47734755
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47744756
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
47754757
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
@@ -4778,7 +4760,6 @@ itertoolsmodule_exec(PyObject *mod)
47784760
&batched_type,
47794761
&islice_type,
47804762
&chain_type,
4781-
&repeat_type,
47824763
&tee_type,
47834764
&teedataobject_type
47844765
};

0 commit comments

Comments
 (0)