Skip to content

Commit 89b8e34

Browse files
pythongh-101277: Add accumulate type to module state
1 parent 2373926 commit 89b8e34

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

Modules/itertoolsmodule.c

+29-47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
typedef struct {
14+
PyTypeObject *accumulate_type;
1415
PyTypeObject *combinations_type;
1516
PyTypeObject *cwr_type;
1617
PyTypeObject *cycle_type;
@@ -55,18 +56,17 @@ class itertools.chain "chainobject *" "&chain_type"
5556
class itertools.combinations "combinationsobject *" "clinic_state()->combinations_type"
5657
class itertools.combinations_with_replacement "cwr_object *" "clinic_state()->cwr_type"
5758
class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type"
58-
class itertools.accumulate "accumulateobject *" "&accumulate_type"
59+
class itertools.accumulate "accumulateobject *" "clinic_state()->accumulate_type"
5960
class itertools.compress "compressobject *" "&compress_type"
6061
class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
6162
class itertools.count "countobject *" "&count_type"
6263
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
6364
[clinic start generated code]*/
64-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1790ac655869a651]*/
65+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0155dd6d01d40dd]*/
6566

6667
static PyTypeObject teedataobject_type;
6768
static PyTypeObject tee_type;
6869
static PyTypeObject batched_type;
69-
static PyTypeObject accumulate_type;
7070
static PyTypeObject compress_type;
7171
static PyTypeObject filterfalse_type;
7272
static PyTypeObject count_type;
@@ -3645,17 +3645,20 @@ itertools_accumulate_impl(PyTypeObject *type, PyObject *iterable,
36453645
static void
36463646
accumulate_dealloc(accumulateobject *lz)
36473647
{
3648+
PyTypeObject *tp = Py_TYPE(lz);
36483649
PyObject_GC_UnTrack(lz);
36493650
Py_XDECREF(lz->binop);
36503651
Py_XDECREF(lz->total);
36513652
Py_XDECREF(lz->it);
36523653
Py_XDECREF(lz->initial);
3653-
Py_TYPE(lz)->tp_free(lz);
3654+
tp->tp_free(lz);
3655+
Py_DECREF(tp);
36543656
}
36553657

36563658
static int
36573659
accumulate_traverse(accumulateobject *lz, visitproc visit, void *arg)
36583660
{
3661+
Py_VISIT(Py_TYPE(lz));
36593662
Py_VISIT(lz->binop);
36603663
Py_VISIT(lz->it);
36613664
Py_VISIT(lz->total);
@@ -3749,48 +3752,25 @@ static PyMethodDef accumulate_methods[] = {
37493752
{NULL, NULL} /* sentinel */
37503753
};
37513754

3752-
static PyTypeObject accumulate_type = {
3753-
PyVarObject_HEAD_INIT(NULL, 0)
3754-
"itertools.accumulate", /* tp_name */
3755-
sizeof(accumulateobject), /* tp_basicsize */
3756-
0, /* tp_itemsize */
3757-
/* methods */
3758-
(destructor)accumulate_dealloc, /* tp_dealloc */
3759-
0, /* tp_vectorcall_offset */
3760-
0, /* tp_getattr */
3761-
0, /* tp_setattr */
3762-
0, /* tp_as_async */
3763-
0, /* tp_repr */
3764-
0, /* tp_as_number */
3765-
0, /* tp_as_sequence */
3766-
0, /* tp_as_mapping */
3767-
0, /* tp_hash */
3768-
0, /* tp_call */
3769-
0, /* tp_str */
3770-
PyObject_GenericGetAttr, /* tp_getattro */
3771-
0, /* tp_setattro */
3772-
0, /* tp_as_buffer */
3773-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3774-
Py_TPFLAGS_BASETYPE, /* tp_flags */
3775-
itertools_accumulate__doc__, /* tp_doc */
3776-
(traverseproc)accumulate_traverse, /* tp_traverse */
3777-
0, /* tp_clear */
3778-
0, /* tp_richcompare */
3779-
0, /* tp_weaklistoffset */
3780-
PyObject_SelfIter, /* tp_iter */
3781-
(iternextfunc)accumulate_next, /* tp_iternext */
3782-
accumulate_methods, /* tp_methods */
3783-
0, /* tp_members */
3784-
0, /* tp_getset */
3785-
0, /* tp_base */
3786-
0, /* tp_dict */
3787-
0, /* tp_descr_get */
3788-
0, /* tp_descr_set */
3789-
0, /* tp_dictoffset */
3790-
0, /* tp_init */
3791-
0, /* tp_alloc */
3792-
itertools_accumulate, /* tp_new */
3793-
PyObject_GC_Del, /* tp_free */
3755+
static PyType_Slot accumulate_slots[] = {
3756+
{Py_tp_dealloc, accumulate_dealloc},
3757+
{Py_tp_getattro, PyObject_GenericGetAttr},
3758+
{Py_tp_doc, (void *)itertools_accumulate__doc__},
3759+
{Py_tp_traverse, accumulate_traverse},
3760+
{Py_tp_iter, PyObject_SelfIter},
3761+
{Py_tp_iternext, accumulate_next},
3762+
{Py_tp_methods, accumulate_methods},
3763+
{Py_tp_new, itertools_accumulate},
3764+
{Py_tp_free, PyObject_GC_Del},
3765+
{0, NULL},
3766+
};
3767+
3768+
static PyType_Spec accumulate_spec = {
3769+
.name = "itertools.accumulate",
3770+
.basicsize = sizeof(accumulateobject),
3771+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
3772+
Py_TPFLAGS_IMMUTABLETYPE),
3773+
.slots = accumulate_slots,
37943774
};
37953775

37963776

@@ -4822,6 +4802,7 @@ static int
48224802
itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
48234803
{
48244804
itertools_state *state = get_module_state(mod);
4805+
Py_VISIT(state->accumulate_type);
48254806
Py_VISIT(state->combinations_type);
48264807
Py_VISIT(state->cwr_type);
48274808
Py_VISIT(state->cycle_type);
@@ -4838,6 +4819,7 @@ static int
48384819
itertoolsmodule_clear(PyObject *mod)
48394820
{
48404821
itertools_state *state = get_module_state(mod);
4822+
Py_CLEAR(state->accumulate_type);
48414823
Py_CLEAR(state->combinations_type);
48424824
Py_CLEAR(state->cwr_type);
48434825
Py_CLEAR(state->cycle_type);
@@ -4871,6 +4853,7 @@ static int
48714853
itertoolsmodule_exec(PyObject *mod)
48724854
{
48734855
itertools_state *state = get_module_state(mod);
4856+
ADD_TYPE(mod, state->accumulate_type, &accumulate_spec);
48744857
ADD_TYPE(mod, state->combinations_type, &combinations_spec);
48754858
ADD_TYPE(mod, state->cwr_type, &cwr_spec);
48764859
ADD_TYPE(mod, state->cycle_type, &cycle_spec);
@@ -4882,7 +4865,6 @@ itertoolsmodule_exec(PyObject *mod)
48824865
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
48834866

48844867
PyTypeObject *typelist[] = {
4885-
&accumulate_type,
48864868
&batched_type,
48874869
&islice_type,
48884870
&chain_type,

0 commit comments

Comments
 (0)