Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ae5f2f4

Browse files
committed
prevent generator finalization from invalidating sys.exc_info() #7173
1 parent 0e4c22c commit ae5f2f4

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/test/test_exceptions.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import pickle
77
import weakref
88

9-
from test.support import TESTFN, unlink, run_unittest, captured_output
9+
from test.support import (TESTFN, unlink, run_unittest, captured_output,
10+
gc_collect)
1011

1112
# XXX This is not really enough, each *operation* should be tested!
1213

@@ -554,6 +555,20 @@ def yield_raise():
554555
del g
555556
self.assertEquals(sys.exc_info()[0], TypeError)
556557

558+
def test_generator_finalizing_and_exc_info(self):
559+
# See #7173
560+
def simple_gen():
561+
yield 1
562+
def run_gen():
563+
gen = simple_gen()
564+
try:
565+
raise RuntimeError
566+
except RuntimeError:
567+
return next(gen)
568+
run_gen()
569+
gc_collect()
570+
self.assertEqual(sys.exc_info(), (None, None, None))
571+
557572
def test_3114(self):
558573
# Bug #3114: in its destructor, MyObject retrieves a pointer to
559574
# obsolete and/or deallocated objects.

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7173: Generator finalization could invalidate sys.exc_info().
16+
1517
- Issue #7544: Preallocate thread memory before creating the thread to avoid
1618
a fatal error in low memory condition.
1719

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
11591159
assert(stack_pointer != NULL);
11601160
f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
11611161

1162-
if (f->f_code->co_flags & CO_GENERATOR) {
1162+
if (co->co_flags & CO_GENERATOR && !throwflag) {
11631163
if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
11641164
/* We were in an except handler when we left,
11651165
restore the exception state which was put aside

0 commit comments

Comments
 (0)