diff options
author | Benjamin Peterson <benjamin@python.org> | 2011-07-03 13:44:00 -0500 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2011-07-03 13:44:00 -0500 |
commit | e960b4e7aaebd483ad420e895eded15c7857597f (patch) | |
tree | db173ccc06696c50739248f087465b03b2224c8d /Python/ceval.c | |
parent | 8af7edff702d983b9df7cee1f7da04d840812f55 (diff) | |
download | cpython-e960b4e7aaebd483ad420e895eded15c7857597f.tar.gz |
restore a generator's caller's exception state both on yield and (last) return
This prevents generator exception state from leaking into the caller.
Closes #12475.
Diffstat (limited to 'Python/ceval.c')
-rw-r--r-- | Python/ceval.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 705ed415a9..c0f2874a8f 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1881,10 +1881,6 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) retval = POP(); f->f_stacktop = stack_pointer; why = WHY_YIELD; - /* Put aside the current exception state and restore - that of the calling frame. This only serves when - "yield" is used inside an except handler. */ - SWAP_EXC_STATE(); goto fast_yield; TARGET(POP_EXCEPT) @@ -3021,6 +3017,11 @@ fast_block_end: retval = NULL; fast_yield: + if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) + /* Put aside the current exception state and restore that of the + calling frame. */ + SWAP_EXC_STATE(); + if (tstate->use_tracing) { if (tstate->c_tracefunc) { if (why == WHY_RETURN || why == WHY_YIELD) { |