[Python-checkins] cpython (3.2): restore a generator's caller's exception state both on yield and (last) return
benjamin.peterson
python-checkins at python.org
Sun Jul 3 20:44:34 CEST 2011
http://hg.python.org/cpython/rev/cc7ae81cfe91
changeset: 71151:cc7ae81cfe91
branch: 3.2
parent: 71149:60cc7eb9816e
user: Benjamin Peterson <benjamin at python.org>
date: Sun Jul 03 13:44:00 2011 -0500
summary:
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.
files:
Lib/test/test_exceptions.py | 15 +++++++++++++++
Misc/NEWS | 3 +++
Python/ceval.c | 9 +++++----
3 files changed, 23 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -566,6 +566,21 @@
del g
self.assertEqual(sys.exc_info()[0], TypeError)
+ def test_generator_leaking2(self):
+ # See issue 12475.
+ def g():
+ yield
+ try:
+ raise RuntimeError
+ except RuntimeError:
+ it = g()
+ next(it)
+ try:
+ next(it)
+ except StopIteration:
+ pass
+ self.assertEqual(sys.exc_info(), (None, None, None))
+
def test_generator_finalizing_and_exc_info(self):
# See #7173
def simple_gen():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
Core and Builtins
-----------------
+- Issue #12475: Prevent generators from leaking their exception state into the
+ callers frame as they return for the last time.
+
Library
-------
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1881,10 +1881,6 @@
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 @@
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) {
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list