1010static int
1111gen_traverse (PyGenObject * gen , visitproc visit , void * arg )
1212{
13- return visit ((PyObject * )gen -> gi_frame , arg );
13+ Py_VISIT (gen -> gi_frame );
14+ return 0 ;
1415}
1516
1617static void
@@ -26,7 +27,7 @@ gen_dealloc(PyGenObject *gen)
2627
2728 _PyObject_GC_TRACK (self );
2829
29- if (gen -> gi_frame -> f_stacktop != NULL ) {
30+ if (gen -> gi_frame != NULL && gen -> gi_frame -> f_stacktop != NULL ) {
3031 /* Generator is paused, so we need to close */
3132 gen -> ob_type -> tp_del (self );
3233 if (self -> ob_refcnt > 0 )
@@ -51,7 +52,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
5152 "generator already executing" );
5253 return NULL ;
5354 }
54- if (( PyObject * ) f == Py_None || f -> f_stacktop == NULL ) {
55+ if (f == NULL || f -> f_stacktop == NULL ) {
5556 /* Only set exception if called from send() */
5657 if (arg && !exc ) PyErr_SetNone (PyExc_StopIteration );
5758 return NULL ;
@@ -98,8 +99,7 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
9899 if (!result || f -> f_stacktop == NULL ) {
99100 /* generator can't be rerun, so release the frame */
100101 Py_DECREF (f );
101- gen -> gi_frame = (PyFrameObject * )Py_None ;
102- Py_INCREF (Py_None );
102+ gen -> gi_frame = NULL ;
103103 }
104104
105105 return result ;
@@ -147,7 +147,7 @@ gen_del(PyObject *self)
147147 PyObject * error_type , * error_value , * error_traceback ;
148148 PyGenObject * gen = (PyGenObject * )self ;
149149
150- if (( PyObject * ) gen -> gi_frame == Py_None || gen -> gi_frame -> f_stacktop == NULL )
150+ if (! gen -> gi_frame || gen -> gi_frame -> f_stacktop == NULL )
151151 /* Generator isn't paused, so no need to close */
152152 return ;
153153
@@ -366,7 +366,7 @@ PyGen_NeedsFinalizing(PyGenObject *gen)
366366 int i ;
367367 PyFrameObject * f = gen -> gi_frame ;
368368
369- if (( PyObject * ) f == Py_None || f -> f_stacktop == NULL || f -> f_iblock <=0 )
369+ if (f == NULL || f -> f_stacktop == NULL || f -> f_iblock <=0 )
370370 return 0 ; /* no frame or no blockstack == no finalization */
371371
372372 for (i = f -> f_iblock ; i >=0 ; i -- ) {
0 commit comments