Skip to content

Commit 832e413

Browse files
committed
Set generator_return_kind in clear_gen_frame
Generator calls may be nested so it's not sufficient to set the field before the call to PyEval_Frame.
1 parent 4b82b8c commit 832e413

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

Objects/genobject.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,19 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, int exc)
235235
_PyErr_ChainStackItem();
236236
}
237237

238-
// The generator_return_kind field is used to distinguish between a
239-
// yield and a return from within _PyEval_EvalFrame(). Earlier versions
240-
// of CPython (prior to 3.15) used gi_frame_state for this purpose, but
241-
// that requires the GIL for thread-safety.
242-
((_PyThreadStateImpl *)tstate)->generator_return_kind = GENERATOR_RETURN;
243-
244238
EVAL_CALL_STAT_INC(EVAL_CALL_GENERATOR);
245-
PyObject *result = _PyEval_EvalFrame(tstate, frame, exc);
239+
PyObject *result = PyEval_EvalFrame(tstate, frame, exc);
246240
assert(tstate->exc_info == prev_exc_info);
247241
#ifndef Py_GIL_DISABLED
248242
assert(gen->gi_exc_state.previous_item == NULL);
249243
assert(frame->previous == NULL);
250244
assert(gen->gi_frame_state != FRAME_EXECUTING);
251245
#endif
252246

247+
// The generator_return_kind field is used to distinguish between a
248+
// yield and a return from within PyEval_EvalFrame(). Earlier versions
249+
// of CPython (prior to 3.15) used gi_frame_state for this purpose, but
250+
// that requires the GIL for thread-safety.
253251
int return_kind = ((_PyThreadStateImpl *)tstate)->generator_return_kind;
254252

255253
if (return_kind == GENERATOR_YIELD) {

Python/ceval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,7 @@ clear_gen_frame(PyThreadState *tstate, _PyInterpreterFrame * frame)
23052305
assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
23062306
PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame);
23072307
FT_ATOMIC_STORE_INT8_RELEASE(gen->gi_frame_state, FRAME_CLEARED);
2308+
((_PyThreadStateImpl *)tstate)->generator_return_kind = GENERATOR_RETURN;
23082309
assert(tstate->exc_info == &gen->gi_exc_state);
23092310
tstate->exc_info = gen->gi_exc_state.previous_item;
23102311
gen->gi_exc_state.previous_item = NULL;

0 commit comments

Comments
 (0)