Skip to content

Commit cfa334f

Browse files
gh-101277: Add product type to module state
1 parent 336ca9b commit cfa334f

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
@@ -24,6 +24,7 @@ typedef struct {
2424
PyTypeObject *_grouper_type;
2525
PyTypeObject *pairwise_type;
2626
PyTypeObject *permutations_type;
27+
PyTypeObject *product_type;
2728
PyTypeObject *starmap_type;
2829
PyTypeObject *takewhile_type;
2930
PyTypeObject *ziplongest_type;
@@ -2281,8 +2282,6 @@ typedef struct {
22812282
int stopped; /* set to 1 when the iterator is exhausted */
22822283
} productobject;
22832284

2284-
static PyTypeObject product_type;
2285-
22862285
static PyObject *
22872286
product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
22882287
{
@@ -2369,12 +2368,14 @@ product_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
23692368
static void
23702369
product_dealloc(productobject *lz)
23712370
{
2371+
PyTypeObject *tp = Py_TYPE(lz);
23722372
PyObject_GC_UnTrack(lz);
23732373
Py_XDECREF(lz->pools);
23742374
Py_XDECREF(lz->result);
23752375
if (lz->indices != NULL)
23762376
PyMem_Free(lz->indices);
2377-
Py_TYPE(lz)->tp_free(lz);
2377+
tp->tp_free(lz);
2378+
Py_DECREF(tp);
23782379
}
23792380

23802381
static PyObject *
@@ -2390,6 +2391,7 @@ PyDoc_STRVAR(sizeof_doc, "Returns size in memory, in bytes.");
23902391
static int
23912392
product_traverse(productobject *lz, visitproc visit, void *arg)
23922393
{
2394+
Py_VISIT(Py_TYPE(lz));
23932395
Py_VISIT(lz->pools);
23942396
Py_VISIT(lz->result);
23952397
return 0;
@@ -2581,48 +2583,25 @@ product(A, repeat=4) means the same as product(A, A, A, A).\n\n\
25812583
product('ab', range(3)) --> ('a',0) ('a',1) ('a',2) ('b',0) ('b',1) ('b',2)\n\
25822584
product((0,1), (0,1), (0,1)) --> (0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) ...");
25832585

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

26282607

@@ -4727,6 +4706,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
47274706
Py_VISIT(state->_grouper_type);
47284707
Py_VISIT(state->pairwise_type);
47294708
Py_VISIT(state->permutations_type);
4709+
Py_VISIT(state->product_type);
47304710
Py_VISIT(state->starmap_type);
47314711
Py_VISIT(state->takewhile_type);
47324712
Py_VISIT(state->ziplongest_type);
@@ -4749,6 +4729,7 @@ itertoolsmodule_clear(PyObject *mod)
47494729
Py_CLEAR(state->_grouper_type);
47504730
Py_CLEAR(state->pairwise_type);
47514731
Py_CLEAR(state->permutations_type);
4732+
Py_CLEAR(state->product_type);
47524733
Py_CLEAR(state->starmap_type);
47534734
Py_CLEAR(state->takewhile_type);
47544735
Py_CLEAR(state->ziplongest_type);
@@ -4788,6 +4769,7 @@ itertoolsmodule_exec(PyObject *mod)
47884769
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
47894770
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47904771
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
4772+
ADD_TYPE(mod, state->product_type, &product_spec);
47914773
ADD_TYPE(mod, state->starmap_type, &starmap_spec);
47924774
ADD_TYPE(mod, state->takewhile_type, &takewhile_spec);
47934775
ADD_TYPE(mod, state->ziplongest_type, &ziplongest_spec);
@@ -4796,7 +4778,6 @@ itertoolsmodule_exec(PyObject *mod)
47964778
&batched_type,
47974779
&islice_type,
47984780
&chain_type,
4799-
&product_type,
48004781
&repeat_type,
48014782
&tee_type,
48024783
&teedataobject_type

0 commit comments

Comments
 (0)