@@ -22,6 +22,7 @@ typedef struct {
22
22
PyTypeObject * filterfalse_type ;
23
23
PyTypeObject * groupby_type ;
24
24
PyTypeObject * _grouper_type ;
25
+ PyTypeObject * islice_type ;
25
26
PyTypeObject * pairwise_type ;
26
27
PyTypeObject * permutations_type ;
27
28
PyTypeObject * product_type ;
@@ -1683,8 +1684,6 @@ typedef struct {
1683
1684
Py_ssize_t cnt ;
1684
1685
} isliceobject ;
1685
1686
1686
- static PyTypeObject islice_type ;
1687
-
1688
1687
static PyObject *
1689
1688
islice_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
1690
1689
{
@@ -1694,9 +1693,11 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1694
1693
Py_ssize_t numargs ;
1695
1694
isliceobject * lz ;
1696
1695
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 )) {
1699
1699
return NULL ;
1700
+ }
1700
1701
1701
1702
if (!PyArg_UnpackTuple (args , "islice" , 2 , 4 , & seq , & a1 , & a2 , & a3 ))
1702
1703
return NULL ;
@@ -1773,14 +1774,17 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1773
1774
static void
1774
1775
islice_dealloc (isliceobject * lz )
1775
1776
{
1777
+ PyTypeObject * tp = Py_TYPE (lz );
1776
1778
PyObject_GC_UnTrack (lz );
1777
1779
Py_XDECREF (lz -> it );
1778
- Py_TYPE (lz )-> tp_free (lz );
1780
+ tp -> tp_free (lz );
1781
+ Py_DECREF (tp );
1779
1782
}
1780
1783
1781
1784
static int
1782
1785
islice_traverse (isliceobject * lz , visitproc visit , void * arg )
1783
1786
{
1787
+ Py_VISIT (Py_TYPE (lz ));
1784
1788
Py_VISIT (lz -> it );
1785
1789
return 0 ;
1786
1790
}
@@ -1886,48 +1890,25 @@ specified as another value, step determines how many values are\n\
1886
1890
skipped between successive calls. Works like a slice() on a list\n\
1887
1891
but returns an iterator." );
1888
1892
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 ,
1931
1912
};
1932
1913
1933
1914
@@ -3693,8 +3674,6 @@ accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored))
3693
3674
3694
3675
if (PyType_Ready (& chain_type ) < 0 )
3695
3676
return NULL ;
3696
- if (PyType_Ready (& islice_type ) < 0 )
3697
- return NULL ;
3698
3677
it = PyObject_CallFunction ((PyObject * )& chain_type , "(O)O" ,
3699
3678
lz -> total , lz -> it );
3700
3679
if (it == NULL )
@@ -3703,7 +3682,10 @@ accumulate_reduce(accumulateobject *lz, PyObject *Py_UNUSED(ignored))
3703
3682
it , lz -> binop ? lz -> binop : Py_None );
3704
3683
if (it == NULL )
3705
3684
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 );
3707
3689
}
3708
3690
return Py_BuildValue ("O(OO)O" , Py_TYPE (lz ),
3709
3691
lz -> it , lz -> binop ?lz -> binop :Py_None ,
@@ -4683,6 +4665,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
4683
4665
Py_VISIT (state -> filterfalse_type );
4684
4666
Py_VISIT (state -> groupby_type );
4685
4667
Py_VISIT (state -> _grouper_type );
4668
+ Py_VISIT (state -> islice_type );
4686
4669
Py_VISIT (state -> pairwise_type );
4687
4670
Py_VISIT (state -> permutations_type );
4688
4671
Py_VISIT (state -> product_type );
@@ -4707,6 +4690,7 @@ itertoolsmodule_clear(PyObject *mod)
4707
4690
Py_CLEAR (state -> filterfalse_type );
4708
4691
Py_CLEAR (state -> groupby_type );
4709
4692
Py_CLEAR (state -> _grouper_type );
4693
+ Py_CLEAR (state -> islice_type );
4710
4694
Py_CLEAR (state -> pairwise_type );
4711
4695
Py_CLEAR (state -> permutations_type );
4712
4696
Py_CLEAR (state -> product_type );
@@ -4748,6 +4732,7 @@ itertoolsmodule_exec(PyObject *mod)
4748
4732
ADD_TYPE (mod , state -> filterfalse_type , & filterfalse_spec );
4749
4733
ADD_TYPE (mod , state -> groupby_type , & groupby_spec );
4750
4734
ADD_TYPE (mod , state -> _grouper_type , & _grouper_spec );
4735
+ ADD_TYPE (mod , state -> islice_type , & islice_spec );
4751
4736
ADD_TYPE (mod , state -> pairwise_type , & pairwise_spec );
4752
4737
ADD_TYPE (mod , state -> permutations_type , & permutations_spec );
4753
4738
ADD_TYPE (mod , state -> product_type , & product_spec );
@@ -4758,7 +4743,6 @@ itertoolsmodule_exec(PyObject *mod)
4758
4743
4759
4744
PyTypeObject * typelist [] = {
4760
4745
& batched_type ,
4761
- & islice_type ,
4762
4746
& chain_type ,
4763
4747
& tee_type ,
4764
4748
& teedataobject_type
0 commit comments