@@ -31,6 +31,7 @@ typedef struct {
31
31
PyTypeObject * starmap_type ;
32
32
PyTypeObject * takewhile_type ;
33
33
PyTypeObject * tee_type ;
34
+ PyTypeObject * teedataobject_type ;
34
35
PyTypeObject * ziplongest_type ;
35
36
} itertools_state ;
36
37
@@ -56,7 +57,7 @@ find_state_by_type(PyTypeObject *tp)
56
57
module itertools
57
58
class itertools.groupby "groupbyobject *" "clinic_state()->groupby_type"
58
59
class itertools._grouper "_grouperobject *" "clinic_state()->_grouper_type"
59
- class itertools.teedataobject "teedataobject *" "& teedataobject_type"
60
+ class itertools.teedataobject "teedataobject *" "clinic_state()-> teedataobject_type"
60
61
class itertools._tee "teeobject *" "clinic_state()->tee_type"
61
62
class itertools.batched "batchedobject *" "&batched_type"
62
63
class itertools.cycle "cycleobject *" "clinic_state()->cycle_type"
@@ -73,9 +74,8 @@ class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_t
73
74
class itertools.count "countobject *" "clinic_state()->count_type"
74
75
class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
75
76
[clinic start generated code]*/
76
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=2de6179a0523a80d ]*/
77
+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=419423f2d82cf388 ]*/
77
78
78
- static PyTypeObject teedataobject_type ;
79
79
static PyTypeObject batched_type ;
80
80
81
81
#include "clinic/itertoolsmodule.c.h"
@@ -731,13 +731,13 @@ typedef struct {
731
731
} teeobject ;
732
732
733
733
static PyObject *
734
- teedataobject_newinternal (PyObject * it )
734
+ teedataobject_newinternal (itertools_state * state , PyObject * it )
735
735
{
736
- teedataobject * tdo ;
737
-
738
- tdo = PyObject_GC_New (teedataobject , & teedataobject_type );
739
- if (tdo == NULL )
736
+ teedataobject * tdo = PyObject_GC_New (teedataobject ,
737
+ state -> teedataobject_type );
738
+ if (tdo == NULL ) {
740
739
return NULL ;
740
+ }
741
741
742
742
tdo -> running = 0 ;
743
743
tdo -> numread = 0 ;
@@ -750,8 +750,11 @@ teedataobject_newinternal(PyObject *it)
750
750
static PyObject *
751
751
teedataobject_jumplink (teedataobject * tdo )
752
752
{
753
- if (tdo -> nextlink == NULL )
754
- tdo -> nextlink = teedataobject_newinternal (tdo -> it );
753
+ if (tdo -> nextlink == NULL ) {
754
+ PyTypeObject * tp = Py_TYPE (tdo );
755
+ itertools_state * state = find_state_by_type (tp );
756
+ tdo -> nextlink = teedataobject_newinternal (state , tdo -> it );
757
+ }
755
758
return Py_XNewRef (tdo -> nextlink );
756
759
}
757
760
@@ -787,6 +790,7 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
787
790
{
788
791
int i ;
789
792
793
+ Py_VISIT (Py_TYPE (tdo ));
790
794
Py_VISIT (tdo -> it );
791
795
for (i = 0 ; i < tdo -> numread ; i ++ )
792
796
Py_VISIT (tdo -> values [i ]);
@@ -795,9 +799,9 @@ teedataobject_traverse(teedataobject *tdo, visitproc visit, void * arg)
795
799
}
796
800
797
801
static void
798
- teedataobject_safe_decref (PyObject * obj )
802
+ teedataobject_safe_decref (PyObject * obj , PyTypeObject * tdo_type )
799
803
{
800
- while (obj && Py_IS_TYPE (obj , & teedataobject_type ) &&
804
+ while (obj && Py_IS_TYPE (obj , tdo_type ) &&
801
805
Py_REFCNT (obj ) == 1 ) {
802
806
PyObject * nextlink = ((teedataobject * )obj )-> nextlink ;
803
807
((teedataobject * )obj )-> nextlink = NULL ;
@@ -817,16 +821,19 @@ teedataobject_clear(teedataobject *tdo)
817
821
Py_CLEAR (tdo -> values [i ]);
818
822
tmp = tdo -> nextlink ;
819
823
tdo -> nextlink = NULL ;
820
- teedataobject_safe_decref (tmp );
824
+ itertools_state * state = find_state_by_type (Py_TYPE (tdo ));
825
+ teedataobject_safe_decref (tmp , state -> teedataobject_type );
821
826
return 0 ;
822
827
}
823
828
824
829
static void
825
830
teedataobject_dealloc (teedataobject * tdo )
826
831
{
832
+ PyTypeObject * tp = Py_TYPE (tdo );
827
833
PyObject_GC_UnTrack (tdo );
828
834
teedataobject_clear (tdo );
829
835
PyObject_GC_Del (tdo );
836
+ Py_DECREF (tp );
830
837
}
831
838
832
839
static PyObject *
@@ -865,9 +872,10 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
865
872
teedataobject * tdo ;
866
873
Py_ssize_t i , len ;
867
874
868
- assert (type == & teedataobject_type );
875
+ itertools_state * state = find_state_by_type (type );
876
+ assert (type == state -> teedataobject_type );
869
877
870
- tdo = (teedataobject * )teedataobject_newinternal (it );
878
+ tdo = (teedataobject * )teedataobject_newinternal (state , it );
871
879
if (!tdo )
872
880
return NULL ;
873
881
@@ -883,7 +891,7 @@ itertools_teedataobject_impl(PyTypeObject *type, PyObject *it,
883
891
884
892
if (len == LINKCELLS ) {
885
893
if (next != Py_None ) {
886
- if (!Py_IS_TYPE (next , & teedataobject_type ))
894
+ if (!Py_IS_TYPE (next , state -> teedataobject_type ))
887
895
goto err ;
888
896
assert (tdo -> nextlink == NULL );
889
897
tdo -> nextlink = Py_NewRef (next );
@@ -906,47 +914,24 @@ static PyMethodDef teedataobject_methods[] = {
906
914
{NULL , NULL } /* sentinel */
907
915
};
908
916
909
- static PyTypeObject teedataobject_type = {
910
- PyVarObject_HEAD_INIT (0 , 0 ) /* Must fill in type value later */
911
- "itertools._tee_dataobject" , /* tp_name */
912
- sizeof (teedataobject ), /* tp_basicsize */
913
- 0 , /* tp_itemsize */
914
- /* methods */
915
- (destructor )teedataobject_dealloc , /* tp_dealloc */
916
- 0 , /* tp_vectorcall_offset */
917
- 0 , /* tp_getattr */
918
- 0 , /* tp_setattr */
919
- 0 , /* tp_as_async */
920
- 0 , /* tp_repr */
921
- 0 , /* tp_as_number */
922
- 0 , /* tp_as_sequence */
923
- 0 , /* tp_as_mapping */
924
- 0 , /* tp_hash */
925
- 0 , /* tp_call */
926
- 0 , /* tp_str */
927
- PyObject_GenericGetAttr , /* tp_getattro */
928
- 0 , /* tp_setattro */
929
- 0 , /* tp_as_buffer */
930
- Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC , /* tp_flags */
931
- itertools_teedataobject__doc__ , /* tp_doc */
932
- (traverseproc )teedataobject_traverse , /* tp_traverse */
933
- (inquiry )teedataobject_clear , /* tp_clear */
934
- 0 , /* tp_richcompare */
935
- 0 , /* tp_weaklistoffset */
936
- 0 , /* tp_iter */
937
- 0 , /* tp_iternext */
938
- teedataobject_methods , /* tp_methods */
939
- 0 , /* tp_members */
940
- 0 , /* tp_getset */
941
- 0 , /* tp_base */
942
- 0 , /* tp_dict */
943
- 0 , /* tp_descr_get */
944
- 0 , /* tp_descr_set */
945
- 0 , /* tp_dictoffset */
946
- 0 , /* tp_init */
947
- 0 , /* tp_alloc */
948
- itertools_teedataobject , /* tp_new */
949
- PyObject_GC_Del , /* tp_free */
917
+ static PyType_Slot teedataobject_slots [] = {
918
+ {Py_tp_dealloc , teedataobject_dealloc },
919
+ {Py_tp_getattro , PyObject_GenericGetAttr },
920
+ {Py_tp_doc , (void * )itertools_teedataobject__doc__ },
921
+ {Py_tp_traverse , teedataobject_traverse },
922
+ {Py_tp_clear , teedataobject_clear },
923
+ {Py_tp_methods , teedataobject_methods },
924
+ {Py_tp_new , itertools_teedataobject },
925
+ {Py_tp_free , PyObject_GC_Del },
926
+ {0 , NULL },
927
+ };
928
+
929
+ static PyType_Spec teedataobject_spec = {
930
+ .name = "itertools._tee_dataobject" ,
931
+ .basicsize = sizeof (teedataobject ),
932
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
933
+ Py_TPFLAGS_IMMUTABLETYPE ),
934
+ .slots = teedataobject_slots ,
950
935
};
951
936
952
937
@@ -1007,7 +992,7 @@ tee_fromiterable(itertools_state *state, PyObject *iterable)
1007
992
goto done ;
1008
993
}
1009
994
1010
- PyObject * dataobj = teedataobject_newinternal (it );
995
+ PyObject * dataobj = teedataobject_newinternal (state , it );
1011
996
if (!dataobj ) {
1012
997
to = NULL ;
1013
998
goto done ;
@@ -1076,7 +1061,9 @@ tee_setstate(teeobject *to, PyObject *state)
1076
1061
PyErr_SetString (PyExc_TypeError , "state is not a tuple" );
1077
1062
return NULL ;
1078
1063
}
1079
- if (!PyArg_ParseTuple (state , "O!i" , & teedataobject_type , & tdo , & index )) {
1064
+ itertools_state * m_state = find_state_by_type (Py_TYPE (to ));
1065
+ PyTypeObject * tdo_type = m_state -> teedataobject_type ;
1066
+ if (!PyArg_ParseTuple (state , "O!i" , tdo_type , & tdo , & index )) {
1080
1067
return NULL ;
1081
1068
}
1082
1069
if (index < 0 || index > LINKCELLS ) {
@@ -4696,14 +4683,14 @@ itertoolsmodule_exec(PyObject *mod)
4696
4683
ADD_TYPE (mod , state -> starmap_type , & starmap_spec );
4697
4684
ADD_TYPE (mod , state -> takewhile_type , & takewhile_spec );
4698
4685
ADD_TYPE (mod , state -> tee_type , & tee_spec );
4686
+ ADD_TYPE (mod , state -> teedataobject_type , & teedataobject_spec );
4699
4687
ADD_TYPE (mod , state -> ziplongest_type , & ziplongest_spec );
4700
4688
4701
4689
PyTypeObject * typelist [] = {
4702
4690
& batched_type ,
4703
- & teedataobject_type
4704
4691
};
4705
4692
4706
- Py_SET_TYPE (& teedataobject_type , & PyType_Type );
4693
+ Py_SET_TYPE (state -> teedataobject_type , & PyType_Type );
4707
4694
4708
4695
for (size_t i = 0 ; i < Py_ARRAY_LENGTH (typelist ); i ++ ) {
4709
4696
if (PyModule_AddType (mod , typelist [i ]) < 0 ) {
0 commit comments