@@ -32,6 +32,7 @@ typedef struct {
3232 PyTypeObject * permutations_type ;
3333 PyTypeObject * product_type ;
3434 PyTypeObject * repeat_type ;
35+ PyTypeObject * serialize_type ;
3536 PyTypeObject * starmap_type ;
3637 PyTypeObject * takewhile_type ;
3738 PyTypeObject * tee_type ;
@@ -85,8 +86,9 @@ class itertools.compress "compressobject *" "clinic_state()->compress_type"
8586class itertools.filterfalse "filterfalseobject *" "clinic_state()->filterfalse_type"
8687class itertools.count "countobject *" "clinic_state()->count_type"
8788class itertools.pairwise "pairwiseobject *" "clinic_state()->pairwise_type"
89+ class itertools.serialize "serializeobject *" "clinic_state()->serialize_type"
8890[clinic start generated code]*/
89- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=aa48fe4de9d4080f ]*/
91+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=1261e430ec3a27e1 ]*/
9092
9193#define clinic_state () (find_state_by_type(type))
9294#define clinic_state_by_cls () (get_module_state_by_cls(base_tp))
@@ -3697,6 +3699,100 @@ static PyType_Spec repeat_spec = {
36973699 .slots = repeat_slots ,
36983700};
36993701
3702+ /* serialize object **************************************************************/
3703+
3704+ typedef struct {
3705+ PyObject_HEAD
3706+ PyObject * it ;
3707+ } serializeobject ;
3708+
3709+ #define serializeobject_CAST (op ) ((serializeobject *)(op))
3710+
3711+ /*[clinic input]
3712+ @classmethod
3713+ itertools.serialize.__new__
3714+ iterable: object
3715+ /
3716+ Make an iterator thread-safe [tbd]
3717+
3718+ [clinic start generated code]*/
3719+
3720+ static PyObject *
3721+ itertools_serialize_impl (PyTypeObject * type , PyObject * iterable )
3722+ /*[clinic end generated code: output=abd19e483759f1b7 input=0099ab7fd57cdc9f]*/
3723+ {
3724+ /* Get iterator. */
3725+ PyObject * it = PyObject_GetIter (iterable );
3726+ if (it == NULL )
3727+ return NULL ;
3728+
3729+ serializeobject * lz = (serializeobject * )type -> tp_alloc (type , 0 );
3730+ lz -> it = it ;
3731+
3732+ return (PyObject * )lz ;
3733+ }
3734+
3735+ static void
3736+ serialize_dealloc (PyObject * op )
3737+ {
3738+ serializeobject * lz = serializeobject_CAST (op );
3739+ PyTypeObject * tp = Py_TYPE (lz );
3740+ PyObject_GC_UnTrack (lz );
3741+ Py_XDECREF (lz -> it );
3742+ tp -> tp_free (lz );
3743+ Py_DECREF (tp );
3744+ }
3745+
3746+ static int
3747+ serialize_traverse (PyObject * op , visitproc visit , void * arg )
3748+ {
3749+ serializeobject * lz = serializeobject_CAST (op );
3750+ Py_VISIT (Py_TYPE (lz ));
3751+ Py_VISIT (lz -> it );
3752+ return 0 ;
3753+ }
3754+
3755+ static PyObject *
3756+ serialize_next (PyObject * op )
3757+ {
3758+ serializeobject * lz = serializeobject_CAST (op );
3759+ PyObject * result = NULL ;
3760+
3761+ Py_BEGIN_CRITICAL_SECTION (op ); // or lock on op->it ?
3762+ PyObject * it = lz -> it ;
3763+ if (it != NULL ) {
3764+ result = PyIter_Next (lz -> it );
3765+ if (result == NULL ) {
3766+ /* Note: StopIteration is already cleared by PyIter_Next() */
3767+ if (PyErr_Occurred ())
3768+ return NULL ;
3769+ Py_CLEAR (lz -> it );
3770+ }
3771+ }
3772+ Py_END_CRITICAL_SECTION ();
3773+ return result ;
3774+ }
3775+
3776+ static PyType_Slot serialize_slots [] = {
3777+ {Py_tp_dealloc , serialize_dealloc },
3778+ {Py_tp_getattro , PyObject_GenericGetAttr },
3779+ {Py_tp_doc , (void * )itertools_serialize__doc__ },
3780+ {Py_tp_traverse , serialize_traverse },
3781+ {Py_tp_iter , PyObject_SelfIter },
3782+ {Py_tp_iternext , serialize_next },
3783+ {Py_tp_new , itertools_serialize },
3784+ {Py_tp_free , PyObject_GC_Del },
3785+ {0 , NULL },
3786+ };
3787+
3788+ static PyType_Spec serialize_spec = {
3789+ .name = "itertools.serialize" ,
3790+ .basicsize = sizeof (serializeobject ),
3791+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE |
3792+ Py_TPFLAGS_IMMUTABLETYPE ),
3793+ .slots = serialize_slots ,
3794+ };
3795+
37003796
37013797/* ziplongest object *********************************************************/
37023798
@@ -3963,6 +4059,7 @@ itertoolsmodule_traverse(PyObject *mod, visitproc visit, void *arg)
39634059 Py_VISIT (state -> permutations_type );
39644060 Py_VISIT (state -> product_type );
39654061 Py_VISIT (state -> repeat_type );
4062+ Py_VISIT (state -> serialize_type );
39664063 Py_VISIT (state -> starmap_type );
39674064 Py_VISIT (state -> takewhile_type );
39684065 Py_VISIT (state -> tee_type );
@@ -3992,6 +4089,7 @@ itertoolsmodule_clear(PyObject *mod)
39924089 Py_CLEAR (state -> permutations_type );
39934090 Py_CLEAR (state -> product_type );
39944091 Py_CLEAR (state -> repeat_type );
4092+ Py_CLEAR (state -> serialize_type );
39954093 Py_CLEAR (state -> starmap_type );
39964094 Py_CLEAR (state -> takewhile_type );
39974095 Py_CLEAR (state -> tee_type );
@@ -4038,6 +4136,7 @@ itertoolsmodule_exec(PyObject *mod)
40384136 ADD_TYPE (mod , state -> permutations_type , & permutations_spec );
40394137 ADD_TYPE (mod , state -> product_type , & product_spec );
40404138 ADD_TYPE (mod , state -> repeat_type , & repeat_spec );
4139+ ADD_TYPE (mod , state -> serialize_type , & serialize_spec );
40414140 ADD_TYPE (mod , state -> starmap_type , & starmap_spec );
40424141 ADD_TYPE (mod , state -> takewhile_type , & takewhile_spec );
40434142 ADD_TYPE (mod , state -> tee_type , & tee_spec );
0 commit comments