Skip to content

Commit 1c3828d

Browse files
gh-101277: Add product type to module state
1 parent 19ca7b1 commit 1c3828d

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
@@ -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;
@@ -2267,8 +2268,6 @@ typedef struct {
22672268
int stopped; /* set to 1 when the iterator is exhausted */
22682269
} productobject;
22692270

2270-
static PyTypeObject product_type;
2271-
22722271
static PyObject *
22732272
product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22742273
{
@@ -2355,12 +2354,14 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23552354
static void
23562355
product_dealloc(productobject *lz)
23572356
{
2357+
PyTypeObject *tp = Py_TYPE(lz);
23582358
PyObject_GC_UnTrack(lz);
23592359
Py_XDECREF(lz->pools);
23602360
Py_XDECREF(lz->result);
23612361
if (lz->indices != NULL)
23622362
PyMem_Free(lz->indices);
2363-
Py_TYPE(lz)->tp_free(lz);
2363+
tp->tp_free(lz);
2364+
Py_DECREF(tp);
23642365
}
23652366

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

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

26142593

@@ -4712,6 +4691,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47124691
Py_VISIT(state->groupby_type);
47134692
Py_VISIT(state->_grouper_type);
47144693
Py_VISIT(state->permutations_type);
4694+
Py_VISIT(state->product_type);
47154695
Py_VISIT(state->starmap_type);
47164696
Py_VISIT(state->takewhile_type);
47174697
Py_VISIT(state->ziplongest_type);
@@ -4733,6 +4713,7 @@ itertoolsmodule_clear(PyObject *mod)
47334713
Py_CLEAR(state->groupby_type);
47344714
Py_CLEAR(state->_grouper_type);
47354715
Py_CLEAR(state->permutations_type);
4716+
Py_CLEAR(state->product_type);
47364717
Py_CLEAR(state->starmap_type);
47374718
Py_CLEAR(state->takewhile_type);
47384719
Py_CLEAR(state->ziplongest_type);
@@ -4772,6 +4753,7 @@ itertoolsmodule_exec(PyObject *mod)
47724753
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
47734754
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47744755
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
4756+
ADD_TYPE(mod, state->product_type, &product_spec);
47754757
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47764758
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
47774759
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
@@ -4780,7 +4762,6 @@ itertoolsmodule_exec(PyObject *mod)
47804762
&batched_type,
47814763
&islice_type,
47824764
&chain_type,
4783-
&product_type,
47844765
&repeat_type,
47854766
&tee_type,
47864767
&teedataobject_type

0 commit comments

Comments
 (0)