]> granicus.if.org Git - python/commitdiff
prevent generator finalization from invalidating sys.exc_info() #7173
authorBenjamin Peterson <benjamin@python.org>
Sun, 7 Mar 2010 17:10:51 +0000 (17:10 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sun, 7 Mar 2010 17:10:51 +0000 (17:10 +0000)
Lib/test/test_exceptions.py
Misc/NEWS
Python/ceval.c

index 2838d47689653dae5aebd0133aabcbcd18d34a25..5372b2b2773feac66a577b925483570d79cfdc69 100644 (file)
@@ -6,7 +6,8 @@ import unittest
 import pickle
 import weakref
 
-from test.support import TESTFN, unlink, run_unittest, captured_output
+from test.support import (TESTFN, unlink, run_unittest, captured_output,
+                          gc_collect)
 
 # XXX This is not really enough, each *operation* should be tested!
 
@@ -554,6 +555,20 @@ class ExceptionTests(unittest.TestCase):
             del g
             self.assertEquals(sys.exc_info()[0], TypeError)
 
+    def test_generator_finalizing_and_exc_info(self):
+        # See #7173
+        def simple_gen():
+            yield 1
+        def run_gen():
+            gen = simple_gen()
+            try:
+                raise RuntimeError
+            except RuntimeError:
+                return next(gen)
+        run_gen()
+        gc_collect()
+        self.assertEqual(sys.exc_info(), (None, None, None))
+
     def test_3114(self):
         # Bug #3114: in its destructor, MyObject retrieves a pointer to
         # obsolete and/or deallocated objects.
index bb4dcf4834263ca6f7d4af3b75e0cb00f5024a32..de9e4f2250d6700b033d1688883982c049b666b0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #7173: Generator finalization could invalidate sys.exc_info().
+
 - Issue #7544: Preallocate thread memory before creating the thread to avoid
   a fatal error in low memory condition.
 
index 5d1fb285bebfaa853a517a5ad91684648d04a77f..47c53cfa040af3317d6d621c50539cfbcfb44bcd 100644 (file)
@@ -1159,7 +1159,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
        assert(stack_pointer != NULL);
        f->f_stacktop = NULL;   /* remains NULL unless yield suspends frame */
 
-       if (f->f_code->co_flags & CO_GENERATOR) {
+       if (co->co_flags & CO_GENERATOR && !throwflag) {
                if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
                        /* We were in an except handler when we left,
                           restore the exception state which was put aside