Skip to content

Commit 7c3c956

Browse files
gh-101277: Add product type to module state
1 parent 5e9cda3 commit 7c3c956

File tree

1 file changed

+27
-46
lines changed

1 file changed

+27
-46
lines changed

Modules/itertoolsmodule.c

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct {
2323
PyTypeObject *_grouper_type;
2424
PyTypeObject *pairwise_type;
2525
PyTypeObject *permutations_type;
26+
PyTypeObject *product_type;
2627
PyTypeObject *starmap_type;
2728
PyTypeObject *takewhile_type;
2829
PyTypeObject *ziplongest_type;
@@ -2268,8 +2269,6 @@ typedef struct {
22682269
int stopped; /* set to 1 when the iterator is exhausted */
22692270
} productobject;
22702271

2271-
static PyTypeObject product_type;
2272-
22732272
static PyObject *
22742273
product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22752274
{
@@ -2356,12 +2355,14 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23562355
static void
23572356
product_dealloc(productobject *lz)
23582357
{
2358+
PyTypeObject *tp = Py_TYPE(lz);
23592359
PyObject_GC_UnTrack(lz);
23602360
Py_XDECREF(lz->pools);
23612361
Py_XDECREF(lz->result);
23622362
if (lz->indices != NULL)
23632363
PyMem_Free(lz->indices);
2364-
Py_TYPE(lz)->tp_free(lz);
2364+
tp->tp_free(lz);
2365+
Py_DECREF(tp);
23652366
}
23662367

23672368
static PyObject *
@@ -2377,6 +2378,7 @@ PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes.");
23772378
static int
23782379
product_traverse(productobject *lz, visitproc visit, void *arg)
23792380
{
2381+
Py_VISIT(Py_TYPE(lz));
23802382
Py_VISIT(lz->pools);
23812383
Py_VISIT(lz->result);
23822384
return 0;
@@ -2568,48 +2570,25 @@ product(A, repeat=4) means the same as product(A, A, A, A).\n\n\
25682570
product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\
25692571
product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...");
25702572

2571-
static PyTypeObject product_type = {
2572-
PyVarObject_HEAD_INIT(NULL, 0)
2573-
"itertools.product", /* tp_name */
2574-
sizeof(productobject), /* tp_basicsize */
2575-
0, /* tp_itemsize */
2576-
/* methods */
2577-
(destructor)product_dealloc, /* tp_dealloc */
2578-
0, /* tp_vectorcall_offset */
2579-
0, /* tp_getattr */
2580-
0, /* tp_setattr */
2581-
0, /* tp_as_async */
2582-
0, /* tp_repr */
2583-
0, /* tp_as_number */
2584-
0, /* tp_as_sequence */
2585-
0, /* tp_as_mapping */
2586-
0, /* tp_hash */
2587-
0, /* tp_call */
2588-
0, /* tp_str */
2589-
PyObject_GenericGetAttr, /* tp_getattro */
2590-
0, /* tp_setattro */
2591-
0, /* tp_as_buffer */
2592-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2593-
Py_TPFLAGS_BASETYPE, /* tp_flags */
2594-
product_doc, /* tp_doc */
2595-
(traverseproc)product_traverse, /* tp_traverse */
2596-
0, /* tp_clear */
2597-
0, /* tp_richcompare */
2598-
0, /* tp_weaklistoffset */
2599-
PyObject_SelfIter, /* tp_iter */
2600-
(iternextfunc)product_next, /* tp_iternext */
2601-
product_methods, /* tp_methods */
2602-
0, /* tp_members */
2603-
0, /* tp_getset */
2604-
0, /* tp_base */
2605-
0, /* tp_dict */
2606-
0, /* tp_descr_get */
2607-
0, /* tp_descr_set */
2608-
0, /* tp_dictoffset */
2609-
0, /* tp_init */
2610-
0, /* tp_alloc */
2611-
product_new, /* tp_new */
2612-
PyObject_GC_Del, /* tp_free */
2573+
static PyType_Slot product_slots[] = {
2574+
{Py_tp_dealloc, product_dealloc},
2575+
{Py_tp_getattro, PyObject_GenericGetAttr},
2576+
{Py_tp_doc, (void *)product_doc},
2577+
{Py_tp_traverse, product_traverse},
2578+
{Py_tp_iter, PyObject_SelfIter},
2579+
{Py_tp_iternext, product_next},
2580+
{Py_tp_methods, product_methods},
2581+
{Py_tp_new, product_new},
2582+
{Py_tp_free, PyObject_GC_Del},
2583+
{0, NULL},
2584+
};
2585+
2586+
static PyType_Spec product_spec = {
2587+
.name = "itertools.product",
2588+
.basicsize = sizeof(productobject),
2589+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
2590+
Py_TPFLAGS_IMMUTABLETYPE),
2591+
.slots = product_slots,
26132592
};
26142593

26152594

@@ -4714,6 +4693,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47144693
Py_VISIT(state->_grouper_type);
47154694
Py_VISIT(state->pairwise_type);
47164695
Py_VISIT(state->permutations_type);
4696+
Py_VISIT(state->product_type);
47174697
Py_VISIT(state->starmap_type);
47184698
Py_VISIT(state->takewhile_type);
47194699
Py_VISIT(state->ziplongest_type);
@@ -4736,6 +4716,7 @@ itertoolsmodule_clear(PyObject *mod)
47364716
Py_CLEAR(state->_grouper_type);
47374717
Py_CLEAR(state->pairwise_type);
47384718
Py_CLEAR(state->permutations_type);
4719+
Py_CLEAR(state->product_type);
47394720
Py_CLEAR(state->starmap_type);
47404721
Py_CLEAR(state->takewhile_type);
47414722
Py_CLEAR(state->ziplongest_type);
@@ -4775,6 +4756,7 @@ itertoolsmodule_exec(PyObject *mod)
47754756
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
47764757
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47774758
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
4759+
ADD_TYPE(mod, state->product_type, &product_spec);
47784760
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47794761
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
47804762
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
@@ -4783,7 +4765,6 @@ itertoolsmodule_exec(PyObject *mod)
47834765
&batched_type,
47844766
&islice_type,
47854767
&chain_type,
4786-
&product_type,
47874768
&repeat_type,
47884769
&tee_type,
47894770
&teedataobject_type

0 commit comments

Comments
 (0)