Skip to content

Commit a66b888

Browse files
pythongh-101277: Add islice type to module state
1 parent 9a4b14c commit a66b888

File tree

1 file changed

+35
-51
lines changed

1 file changed

+35
-51
lines changed

Modules/itertoolsmodule.c

+35-51
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef struct {
2222
PyTypeObject *filterfalse_type;
2323
PyTypeObject *groupby_type;
2424
PyTypeObject *_grouper_type;
25+
PyTypeObject *islice_type;
2526
PyTypeObject *pairwise_type;
2627
PyTypeObject *permutations_type;
2728
PyTypeObject *product_type;
@@ -1683,8 +1684,6 @@ typedef struct {
16831684
Py_ssize_t cnt;
16841685
} isliceobject;
16851686

1686-
static PyTypeObject islice_type;
1687-
16881687
static PyObject *
16891688
islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16901689
{
@@ -1694,9 +1693,11 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16941693
Py_ssize_t numargs;
16951694
isliceobject *lz;
16961695

1697-
if ((type == &islice_type || type->tp_init == islice_type.tp_init) &&
1698-
!_PyArg_NoKeywords("islice", kwds))
1696+
itertools_state *st = find_state_by_type(type);
1697+
if ((type == st->islice_type || type->tp_init == st->islice_type->tp_init)
1698+
&& !_PyArg_NoKeywords("islice", kwds)) {
16991699
return NULL;
1700+
}
17001701

17011702
if (!PyArg_UnpackTuple(args, "islice", 2, 4, &seq, &a1, &a2, &a3))
17021703
return NULL;
@@ -1773,14 +1774,17 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17731774
static void
17741775
islice_dealloc(isliceobject *lz)
17751776
{
1777+
PyTypeObject *tp = Py_TYPE(lz);
17761778
PyObject_GC_UnTrack(lz);
17771779
Py_XDECREF(lz->it);
1778-
Py_TYPE(lz)->tp_free(lz);
1780+
tp->tp_free(lz);
1781+
Py_DECREF(tp);
17791782
}
17801783

17811784
static int
17821785
islice_traverse(isliceobject *lz, visitproc visit, void *arg)
17831786
{
1787+
Py_VISIT(Py_TYPE(lz));
17841788
Py_VISIT(lz->it);
17851789
return 0;
17861790
}
@@ -1886,48 +1890,25 @@ specified as another value, step determines how many values are\n\
18861890
skipped between successive calls. Works like a slice() on a list\n\
18871891
but returns an iterator.");
18881892

1889-
static PyTypeObject islice_type = {
1890-
PyVarObject_HEAD_INIT(NULL, 0)
1891-
"itertools.islice", /* tp_name */
1892-
sizeof(isliceobject), /* tp_basicsize */
1893-
0, /* tp_itemsize */
1894-
/* methods */
1895-
(destructor)islice_dealloc, /* tp_dealloc */
1896-
0, /* tp_vectorcall_offset */
1897-
0, /* tp_getattr */
1898-
0, /* tp_setattr */
1899-
0, /* tp_as_async */
1900-
0, /* tp_repr */
1901-
0, /* tp_as_number */
1902-
0, /* tp_as_sequence */
1903-
0, /* tp_as_mapping */
1904-
0, /* tp_hash */
1905-
0, /* tp_call */
1906-
0, /* tp_str */
1907-
PyObject_GenericGetAttr, /* tp_getattro */
1908-
0, /* tp_setattro */
1909-
0, /* tp_as_buffer */
1910-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1911-
Py_TPFLAGS_BASETYPE, /* tp_flags */
1912-
islice_doc, /* tp_doc */
1913-
(traverseproc)islice_traverse, /* tp_traverse */
1914-
0, /* tp_clear */
1915-
0, /* tp_richcompare */
1916-
0, /* tp_weaklistoffset */
1917-
PyObject_SelfIter, /* tp_iter */
1918-
(iternextfunc)islice_next, /* tp_iternext */
1919-
islice_methods, /* tp_methods */
1920-
0, /* tp_members */
1921-
0, /* tp_getset */
1922-
0, /* tp_base */
1923-
0, /* tp_dict */
1924-
0, /* tp_descr_get */
1925-
0, /* tp_descr_set */
1926-
0, /* tp_dictoffset */
1927-
0, /* tp_init */
1928-
0, /* tp_alloc */
1929-
islice_new, /* tp_new */
1930-
PyObject_GC_Del, /* tp_free */
1893+
static PyType_Slot islice_slots[] = {
1894+
{Py_tp_dealloc, islice_dealloc},
1895+
{Py_tp_getattro, PyObject_GenericGetAttr},
1896+
{Py_tp_doc, (void *)islice_doc},
1897+
{Py_tp_traverse, islice_traverse},
1898+
{Py_tp_iter, PyObject_SelfIter},
1899+
{Py_tp_iternext, islice_next},
1900+
{Py_tp_methods, islice_methods},
1901+
{Py_tp_new, islice_new},
1902+
{Py_tp_free, PyObject_GC_Del},
1903+
{0, NULL},
1904+
};
1905+
1906+
static PyType_Spec islice_spec = {
1907+
.name = "itertools.islice",
1908+
.basicsize = sizeof(isliceobject),
1909+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
1910+
Py_TPFLAGS_IMMUTABLETYPE),
1911+
.slots = islice_slots,
19311912
};
19321913

19331914

@@ -3693,8 +3674,6 @@ accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored))
36933674

36943675
if (PyType_Ready(&chain_type) < 0)
36953676
return NULL;
3696-
if (PyType_Ready(&islice_type) < 0)
3697-
return NULL;
36983677
it = PyObject_CallFunction((PyObject *)&chain_type, "(O)O",
36993678
lz->total, lz->it);
37003679
if (it == NULL)
@@ -3703,7 +3682,10 @@ accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored))
37033682
it, lz->binop ? lz->binop : Py_None);
37043683
if (it == NULL)
37053684
return NULL;
3706-
return Py_BuildValue("O(NiO)", &islice_type, it, 1, Py_None);
3685+
3686+
PyTypeObject *tp = Py_TYPE(lz);
3687+
itertools_state *state = find_state_by_type(tp);
3688+
return Py_BuildValue("O(NiO)", state->islice_type, it, 1, Py_None);
37073689
}
37083690
return Py_BuildValue("O(OO)O", Py_TYPE(lz),
37093691
lz->it, lz->binop?lz->binop:Py_None,
@@ -4683,6 +4665,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
46834665
Py_VISIT(state->filterfalse_type);
46844666
Py_VISIT(state->groupby_type);
46854667
Py_VISIT(state->_grouper_type);
4668+
Py_VISIT(state->islice_type);
46864669
Py_VISIT(state->pairwise_type);
46874670
Py_VISIT(state->permutations_type);
46884671
Py_VISIT(state->product_type);
@@ -4707,6 +4690,7 @@ itertoolsmodule_clear(PyObject *mod)
47074690
Py_CLEAR(state->filterfalse_type);
47084691
Py_CLEAR(state->groupby_type);
47094692
Py_CLEAR(state->_grouper_type);
4693+
Py_CLEAR(state->islice_type);
47104694
Py_CLEAR(state->pairwise_type);
47114695
Py_CLEAR(state->permutations_type);
47124696
Py_CLEAR(state->product_type);
@@ -4748,6 +4732,7 @@ itertoolsmodule_exec(PyObject *mod)
47484732
ADD_TYPE(mod, state->filterfalse_type, &filterfalse_spec);
47494733
ADD_TYPE(mod, state->groupby_type, &groupby_spec);
47504734
ADD_TYPE(mod, state->_grouper_type, &_grouper_spec);
4735+
ADD_TYPE(mod, state->islice_type, &islice_spec);
47514736
ADD_TYPE(mod, state->pairwise_type, &pairwise_spec);
47524737
ADD_TYPE(mod, state->permutations_type, &permutations_spec);
47534738
ADD_TYPE(mod, state->product_type, &product_spec);
@@ -4758,7 +4743,6 @@ itertoolsmodule_exec(PyObject *mod)
47584743

47594744
PyTypeObject *typelist[] = {
47604745
&batched_type,
4761-
&islice_type,
47624746
&chain_type,
47634747
&tee_type,
47644748
&teedataobject_type

0 commit comments

Comments
 (0)