normalize exceptions passed to the __exit__ method #7853

In Python 2.x, exceptions in finally blocks are not normalized.  Since with
statements are implemented using finally blocks, ceval.c had to be tweaked to
distinguish between with finally blocks and normal ones.

A test for the finalization of generators containing with statements was also
added.
This commit is contained in:
Benjamin Peterson 2010-02-05 02:12:14 +00:00
parent 4a7ff1d80a
commit 565d78586b
5 changed files with 26 additions and 6 deletions

View file

@ -1700,6 +1700,17 @@ And finalization:
>>> del g
exiting
>>> class context(object):
... def __enter__(self): pass
... def __exit__(self, *args): print 'exiting'
>>> def f():
... with context():
... yield
>>> g = f()
>>> g.next()
>>> del g
exiting
GeneratorExit is not caught by except Exception:

View file

@ -361,7 +361,6 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
self.assertAfterWithManagerInvariantsWithError(cm)
self.assertAfterWithGeneratorInvariantsWithError(self.resource)
@unittest.expectedFailure
def testExceptionNormalized(self):
cm = mock_contextmanager_generator()
def shouldThrow():