99#include "pycore_genobject.h" // struct _Py_async_gen_state
1010#include "pycore_modsupport.h" // _PyArg_CheckPositional()
1111#include "pycore_object.h" // _PyObject_GC_UNTRACK()
12- #include "pycore_opcode_metadata.h" // _PyOpcode_Caches
1312#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
1413#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
1514#include "pycore_pystate.h" // _PyThreadState_GET()
1615
17- #include "opcode.h" // SEND
18- #include "frameobject.h" // _PyInterpreterFrame_GetLine
1916#include "pystats.h"
2017
2118static PyObject * gen_close (PyGenObject * , PyObject * );
@@ -43,19 +40,12 @@ PyGen_GetCode(PyGenObject *gen) {
4340 return res ;
4441}
4542
46- static inline int
47- exc_state_traverse (_PyErr_StackItem * exc_state , visitproc visit , void * arg )
48- {
49- Py_VISIT (exc_state -> exc_value );
50- return 0 ;
51- }
52-
5343static int
5444gen_traverse (PyGenObject * gen , visitproc visit , void * arg )
5545{
5646 Py_VISIT (gen -> gi_name );
5747 Py_VISIT (gen -> gi_qualname );
58- if (gen -> gi_frame_state < FRAME_CLEARED ) {
48+ if (gen -> gi_frame_state != FRAME_CLEARED ) {
5949 _PyInterpreterFrame * frame = (_PyInterpreterFrame * )(gen -> gi_iframe );
6050 assert (frame -> frame_obj == NULL ||
6151 frame -> frame_obj -> f_frame -> owner == FRAME_OWNED_BY_GENERATOR );
@@ -66,15 +56,16 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
6656 }
6757 /* No need to visit cr_origin, because it's just tuples/str/int, so can't
6858 participate in a reference cycle. */
69- return exc_state_traverse (& gen -> gi_exc_state , visit , arg );
59+ Py_VISIT (gen -> gi_exc_state .exc_value );
60+ return 0 ;
7061}
7162
7263void
7364_PyGen_Finalize (PyObject * self )
7465{
7566 PyGenObject * gen = (PyGenObject * )self ;
7667
77- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
68+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
7869 /* Generator isn't paused, so no need to close */
7970 return ;
8071 }
@@ -147,7 +138,7 @@ gen_dealloc(PyGenObject *gen)
147138 and GC_Del. */
148139 Py_CLEAR (((PyAsyncGenObject * )gen )-> ag_origin_or_finalizer );
149140 }
150- if (gen -> gi_frame_state < FRAME_CLEARED ) {
141+ if (gen -> gi_frame_state != FRAME_CLEARED ) {
151142 _PyInterpreterFrame * frame = (_PyInterpreterFrame * )gen -> gi_iframe ;
152143 gen -> gi_frame_state = FRAME_CLEARED ;
153144 frame -> previous = NULL ;
@@ -171,7 +162,6 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
171162{
172163 PyThreadState * tstate = _PyThreadState_GET ();
173164 _PyInterpreterFrame * frame = (_PyInterpreterFrame * )gen -> gi_iframe ;
174- PyObject * result ;
175165
176166 * presult = NULL ;
177167 if (gen -> gi_frame_state == FRAME_CREATED && arg && arg != Py_None ) {
@@ -198,7 +188,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
198188 PyErr_SetString (PyExc_ValueError , msg );
199189 return PYGEN_ERROR ;
200190 }
201- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
191+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
202192 if (PyCoro_CheckExact (gen ) && !closing ) {
203193 /* `gen` is an exhausted coroutine: raise an error,
204194 except when called from gen_close(), which should
@@ -216,10 +206,12 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
216206 return PYGEN_ERROR ;
217207 }
218208
219- assert (gen -> gi_frame_state < FRAME_EXECUTING );
209+ assert ((gen -> gi_frame_state == FRAME_CREATED ) ||
210+ FRAME_STATE_SUSPENDED (gen -> gi_frame_state ));
211+
220212 /* Push arg onto the frame's value stack */
221- result = arg ? arg : Py_None ;
222- _PyFrame_StackPush (frame , Py_NewRef (result ));
213+ PyObject * arg_obj = arg ? arg : Py_None ;
214+ _PyFrame_StackPush (frame , Py_NewRef (arg_obj ));
223215
224216 _PyErr_StackItem * prev_exc_info = tstate -> exc_info ;
225217 gen -> gi_exc_state .previous_item = prev_exc_info ;
@@ -232,7 +224,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
232224
233225 gen -> gi_frame_state = FRAME_EXECUTING ;
234226 EVAL_CALL_STAT_INC (EVAL_CALL_GENERATOR );
235- result = _PyEval_EvalFrame (tstate , frame , exc );
227+ PyObject * result = _PyEval_EvalFrame (tstate , frame , exc );
236228 assert (tstate -> exc_info == prev_exc_info );
237229 assert (gen -> gi_exc_state .previous_item == NULL );
238230 assert (gen -> gi_frame_state != FRAME_EXECUTING );
@@ -366,7 +358,7 @@ gen_close(PyGenObject *gen, PyObject *args)
366358 gen -> gi_frame_state = FRAME_COMPLETED ;
367359 Py_RETURN_NONE ;
368360 }
369- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
361+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
370362 Py_RETURN_NONE ;
371363 }
372364 PyObject * yf = _PyGen_yf (gen );
@@ -2095,7 +2087,7 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
20952087 return NULL ;
20962088 }
20972089
2098- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
2090+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
20992091 o -> agt_state = AWAITABLE_STATE_CLOSED ;
21002092 PyErr_SetNone (PyExc_StopIteration );
21012093 return NULL ;
0 commit comments