summaryrefslogtreecommitdiffstats
path: root/Python/ceval.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-07-03 21:25:11 (GMT)
committerBenjamin Peterson <benjamin@python.org>2011-07-03 21:25:11 (GMT)
commitac91341333d27bf39dd8b8c1c3164b5bdc19f03b (patch)
tree39cbb66e8f46780439a8ce370a46a92deb1a7d99 /Python/ceval.c
parent9cf960c94fcd2ec044e1525bb8c51f85c9048f35 (diff)
downloadcpython-ac91341333d27bf39dd8b8c1c3164b5bdc19f03b.zip
cpython-ac91341333d27bf39dd8b8c1c3164b5bdc19f03b.tar.gz
cpython-ac91341333d27bf39dd8b8c1c3164b5bdc19f03b.tar.bz2
never retain a generator's caller's exception state on the generator after a yield/return
This requires some trickery to properly save the exception state if the generator creates its own exception state.
Diffstat (limited to 'Python/ceval.c')
-rw-r--r--Python/ceval.c40
1 files changed, 36 insertions, 4 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index c0f2874..5c3bb83 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1145,6 +1145,23 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
f->f_exc_traceback = tmp; \
}
+#define RESTORE_AND_CLEAR_EXC_STATE() \
+ { \
+ PyObject *type, *value, *tb; \
+ type = tstate->exc_type; \
+ value = tstate->exc_value; \
+ tb = tstate->exc_traceback; \
+ tstate->exc_type = f->f_exc_type; \
+ tstate->exc_value = f->f_exc_value; \
+ tstate->exc_traceback = f->f_exc_traceback; \
+ f->f_exc_type = NULL; \
+ f->f_exc_value = NULL; \
+ f->f_exc_traceback = NULL; \
+ Py_XDECREF(type); \
+ Py_XDECREF(value); \
+ Py_XDECREF(tb); \
+ }
+
/* Start of code */
if (f == NULL)
@@ -3017,10 +3034,25 @@ 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 (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
+ /* The purpose of this block is to put aside the generator's exception
+ state and restore that of the calling frame. If the current
+ exception state is from the caller, we clear the exception values
+ on the generator frame, so they are not swapped back in latter. The
+ origin of the current exception state is determined by checking for
+ except handler blocks, which we must be in iff a new exception
+ state came into existence in this frame. (An uncaught exception
+ would have why == WHY_EXCEPTION, and we wouldn't be here). */
+ int i;
+ for (i = 0; i < f->f_iblock; i++)
+ if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
+ break;
+ if (i == f->f_iblock)
+ /* We did not create this exception. */
+ RESTORE_AND_CLEAR_EXC_STATE()
+ else
+ SWAP_EXC_STATE()
+ }
if (tstate->use_tracing) {
if (tstate->c_tracefunc) {