9
9
#include "pycore_genobject.h" // struct _Py_async_gen_state
10
10
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
11
11
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
12
- #include "pycore_opcode_metadata.h" // _PyOpcode_Caches
13
12
#include "pycore_opcode_utils.h" // RESUME_AFTER_YIELD_FROM
14
13
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
15
14
#include "pycore_pystate.h" // _PyThreadState_GET()
16
15
17
- #include "opcode.h" // SEND
18
- #include "frameobject.h" // _PyInterpreterFrame_GetLine
19
16
#include "pystats.h"
20
17
21
18
static PyObject * gen_close (PyGenObject * , PyObject * );
@@ -43,19 +40,12 @@ PyGen_GetCode(PyGenObject *gen) {
43
40
return res ;
44
41
}
45
42
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
-
53
43
static int
54
44
gen_traverse (PyGenObject * gen , visitproc visit , void * arg )
55
45
{
56
46
Py_VISIT (gen -> gi_name );
57
47
Py_VISIT (gen -> gi_qualname );
58
- if (gen -> gi_frame_state < FRAME_CLEARED ) {
48
+ if (gen -> gi_frame_state != FRAME_CLEARED ) {
59
49
_PyInterpreterFrame * frame = (_PyInterpreterFrame * )(gen -> gi_iframe );
60
50
assert (frame -> frame_obj == NULL ||
61
51
frame -> frame_obj -> f_frame -> owner == FRAME_OWNED_BY_GENERATOR );
@@ -66,15 +56,16 @@ gen_traverse(PyGenObject *gen, visitproc visit, void *arg)
66
56
}
67
57
/* No need to visit cr_origin, because it's just tuples/str/int, so can't
68
58
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 ;
70
61
}
71
62
72
63
void
73
64
_PyGen_Finalize (PyObject * self )
74
65
{
75
66
PyGenObject * gen = (PyGenObject * )self ;
76
67
77
- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
68
+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
78
69
/* Generator isn't paused, so no need to close */
79
70
return ;
80
71
}
@@ -147,7 +138,7 @@ gen_dealloc(PyGenObject *gen)
147
138
and GC_Del. */
148
139
Py_CLEAR (((PyAsyncGenObject * )gen )-> ag_origin_or_finalizer );
149
140
}
150
- if (gen -> gi_frame_state < FRAME_CLEARED ) {
141
+ if (gen -> gi_frame_state != FRAME_CLEARED ) {
151
142
_PyInterpreterFrame * frame = (_PyInterpreterFrame * )gen -> gi_iframe ;
152
143
gen -> gi_frame_state = FRAME_CLEARED ;
153
144
frame -> previous = NULL ;
@@ -171,7 +162,6 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
171
162
{
172
163
PyThreadState * tstate = _PyThreadState_GET ();
173
164
_PyInterpreterFrame * frame = (_PyInterpreterFrame * )gen -> gi_iframe ;
174
- PyObject * result ;
175
165
176
166
* presult = NULL ;
177
167
if (gen -> gi_frame_state == FRAME_CREATED && arg && arg != Py_None ) {
@@ -198,7 +188,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
198
188
PyErr_SetString (PyExc_ValueError , msg );
199
189
return PYGEN_ERROR ;
200
190
}
201
- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
191
+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
202
192
if (PyCoro_CheckExact (gen ) && !closing ) {
203
193
/* `gen` is an exhausted coroutine: raise an error,
204
194
except when called from gen_close(), which should
@@ -216,10 +206,12 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
216
206
return PYGEN_ERROR ;
217
207
}
218
208
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
+
220
212
/* 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 ));
223
215
224
216
_PyErr_StackItem * prev_exc_info = tstate -> exc_info ;
225
217
gen -> gi_exc_state .previous_item = prev_exc_info ;
@@ -232,7 +224,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
232
224
233
225
gen -> gi_frame_state = FRAME_EXECUTING ;
234
226
EVAL_CALL_STAT_INC (EVAL_CALL_GENERATOR );
235
- result = _PyEval_EvalFrame (tstate , frame , exc );
227
+ PyObject * result = _PyEval_EvalFrame (tstate , frame , exc );
236
228
assert (tstate -> exc_info == prev_exc_info );
237
229
assert (gen -> gi_exc_state .previous_item == NULL );
238
230
assert (gen -> gi_frame_state != FRAME_EXECUTING );
@@ -366,7 +358,7 @@ gen_close(PyGenObject *gen, PyObject *args)
366
358
gen -> gi_frame_state = FRAME_COMPLETED ;
367
359
Py_RETURN_NONE ;
368
360
}
369
- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
361
+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
370
362
Py_RETURN_NONE ;
371
363
}
372
364
PyObject * yf = _PyGen_yf (gen );
@@ -2095,7 +2087,7 @@ async_gen_athrow_send(PyAsyncGenAThrow *o, PyObject *arg)
2095
2087
return NULL ;
2096
2088
}
2097
2089
2098
- if (gen -> gi_frame_state >= FRAME_COMPLETED ) {
2090
+ if (FRAME_STATE_FINISHED ( gen -> gi_frame_state ) ) {
2099
2091
o -> agt_state = AWAITABLE_STATE_CLOSED ;
2100
2092
PyErr_SetNone (PyExc_StopIteration );
2101
2093
return NULL ;
0 commit comments