Skip to content

Commit 2373926

Browse files
gh-101277: Add permutations type to module state
1 parent 3da8035 commit 2373926

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

Modules/itertoolsmodule.c

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ typedef struct {
1717
PyTypeObject *dropwhile_type;
1818
PyTypeObject *groupby_type;
1919
PyTypeObject *_grouper_type;
20+
PyTypeObject *permutations_type;
2021
PyTypeObject *starmap_type;
2122
PyTypeObject *takewhile_type;
2223
} itertools_state;
@@ -53,19 +54,18 @@ class itertools.starmap "starmapobject *" "clinic_state()->starmap_type"
5354
class itertools.chain "chainobject *" "&chain_type"
5455
class itertools.combinations "combinationsobject *" "clinic_state()->combinations_type"
5556
class itertools.combinations_with_replacement "cwr_object *" "clinic_state()->cwr_type"
56-
class itertools.permutations "permutationsobject *" "&permutations_type"
57+
class itertools.permutations "permutationsobject *" "clinic_state()->permutations_type"
5758
class itertools.accumulate "accumulateobject *" "&accumulate_type"
5859
class itertools.compress "compressobject *" "&compress_type"
5960
class itertools.filterfalse "filterfalseobject *" "&filterfalse_type"
6061
class itertools.count "countobject *" "&count_type"
6162
class itertools.pairwise "pairwiseobject *" "&pairwise_type"
6263
[clinic start generated code]*/
63-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d44fee9ebae461fb]*/
64+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=1790ac655869a651]*/
6465

6566
static PyTypeObject teedataobject_type;
6667
static PyTypeObject tee_type;
6768
static PyTypeObject batched_type;
68-
static PyTypeObject permutations_type;
6969
static PyTypeObject accumulate_type;
7070
static PyTypeObject compress_type;
7171
static PyTypeObject filterfalse_type;
@@ -3343,12 +3343,14 @@ itertools_permutations_impl(PyTypeObject *type, PyObject *iterable,
33433343
static void
33443344
permutations_dealloc(permutationsobject *po)
33453345
{
3346+
PyTypeObject *tp = Py_TYPE(po);
33463347
PyObject_GC_UnTrack(po);
33473348
Py_XDECREF(po->pool);
33483349
Py_XDECREF(po->result);
33493350
PyMem_Free(po->indices);
33503351
PyMem_Free(po->cycles);
3351-
Py_TYPE(po)->tp_free(po);
3352+
tp->tp_free(po);
3353+
Py_DECREF(tp);
33523354
}
33533355

33543356
static PyObject *
@@ -3363,6 +3365,7 @@ permutations_sizeof(permutationsobject *po, void *unused)
33633365
static int
33643366
permutations_traverse(permutationsobject *po, visitproc visit, void *arg)
33653367
{
3368+
Py_VISIT(Py_TYPE(po));
33663369
Py_VISIT(po->pool);
33673370
Py_VISIT(po->result);
33683371
return 0;
@@ -3568,48 +3571,25 @@ static PyMethodDef permuations_methods[] = {
35683571
{NULL, NULL} /* sentinel */
35693572
};
35703573

3571-
static PyTypeObject permutations_type = {
3572-
PyVarObject_HEAD_INIT(NULL, 0)
3573-
"itertools.permutations", /* tp_name */
3574-
sizeof(permutationsobject), /* tp_basicsize */
3575-
0, /* tp_itemsize */
3576-
/* methods */
3577-
(destructor)permutations_dealloc, /* tp_dealloc */
3578-
0, /* tp_vectorcall_offset */
3579-
0, /* tp_getattr */
3580-
0, /* tp_setattr */
3581-
0, /* tp_as_async */
3582-
0, /* tp_repr */
3583-
0, /* tp_as_number */
3584-
0, /* tp_as_sequence */
3585-
0, /* tp_as_mapping */
3586-
0, /* tp_hash */
3587-
0, /* tp_call */
3588-
0, /* tp_str */
3589-
PyObject_GenericGetAttr, /* tp_getattro */
3590-
0, /* tp_setattro */
3591-
0, /* tp_as_buffer */
3592-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3593-
Py_TPFLAGS_BASETYPE, /* tp_flags */
3594-
itertools_permutations__doc__, /* tp_doc */
3595-
(traverseproc)permutations_traverse,/* tp_traverse */
3596-
0, /* tp_clear */
3597-
0, /* tp_richcompare */
3598-
0, /* tp_weaklistoffset */
3599-
PyObject_SelfIter, /* tp_iter */
3600-
(iternextfunc)permutations_next, /* tp_iternext */
3601-
permuations_methods, /* tp_methods */
3602-
0, /* tp_members */
3603-
0, /* tp_getset */
3604-
0, /* tp_base */
3605-
0, /* tp_dict */
3606-
0, /* tp_descr_get */
3607-
0, /* tp_descr_set */
3608-
0, /* tp_dictoffset */
3609-
0, /* tp_init */
3610-
0, /* tp_alloc */
3611-
itertools_permutations, /* tp_new */
3612-
PyObject_GC_Del, /* tp_free */
3574+
static PyType_Slot permutations_slots[] = {
3575+
{Py_tp_dealloc, permutations_dealloc},
3576+
{Py_tp_getattro, PyObject_GenericGetAttr},
3577+
{Py_tp_doc, (void *)itertools_permutations__doc__},
3578+
{Py_tp_traverse, permutations_traverse},
3579+
{Py_tp_iter, PyObject_SelfIter},
3580+
{Py_tp_iternext, permutations_next},
3581+
{Py_tp_methods, permuations_methods},
3582+
{Py_tp_new, itertools_permutations},
3583+
{Py_tp_free, PyObject_GC_Del},
3584+
{0, NULL},
3585+
};
3586+
3587+
static PyType_Spec permutations_spec = {
3588+
.name = "itertools.permutations",
3589+
.basicsize = sizeof(permutationsobject),
3590+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
3591+
Py_TPFLAGS_IMMUTABLETYPE),
3592+
.slots = permutations_slots,
36133593
};
36143594

36153595

@@ -4848,6 +4828,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
48484828
Py_VISIT(state->dropwhile_type);
48494829
Py_VISIT(state->groupby_type);
48504830
Py_VISIT(state->_grouper_type);
4831+
Py_VISIT(state->permutations_type);
48514832
Py_VISIT(state->starmap_type);
48524833
Py_VISIT(state->takewhile_type);
48534834
return 0;
@@ -4863,6 +4844,7 @@ itertoolsmodule_clear(PyObject *mod)
48634844
Py_CLEAR(state->dropwhile_type);
48644845
Py_CLEAR(state->groupby_type);
48654846
Py_CLEAR(state->_grouper_type);
4847+
Py_CLEAR(state->permutations_type);
48664848
Py_CLEAR(state->starmap_type);
48674849
Py_CLEAR(state->takewhile_type);
48684850
return 0;
@@ -4895,6 +4877,7 @@ itertoolsmodule_exec(PyObject *mod)
48954877
ADD_TYPE(mod, state->dropwhile_type, &dropwhile_spec);
48964878
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
48974879
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
4880+
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
48984881
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
48994882
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
49004883

@@ -4908,7 +4891,6 @@ itertoolsmodule_exec(PyObject *mod)
49084891
&count_type,
49094892
&ziplongest_type,
49104893
&pairwise_type,
4911-
&permutations_type,
49124894
&product_type,
49134895
&repeat_type,
49144896
&tee_type,

0 commit comments

Comments
 (0)