diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-01-03 16:18:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-03 16:18:45 (GMT) |
commit | 861cdefc1bccd419bd268a9f60b34bffbaff9ea2 (patch) | |
tree | 3449e145139fb7ba6f2a78806097919a42cbb542 /Lib | |
parent | b99ac1dbc081e4f2d2e68906e9c7c535e628611a (diff) | |
download | cpython-861cdefc1bccd419bd268a9f60b34bffbaff9ea2.zip cpython-861cdefc1bccd419bd268a9f60b34bffbaff9ea2.tar.gz cpython-861cdefc1bccd419bd268a9f60b34bffbaff9ea2.tar.bz2 |
gh-95882: fix regression in the traceback of exceptions propagated from inside a contextlib context manager (GH-95883)
(cherry picked from commit b3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19)
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/contextlib.py | 5 | ||||
-rw-r--r-- | Lib/test/test_contextlib.py | 30 | ||||
-rw-r--r-- | Lib/test/test_contextlib_async.py | 57 |
3 files changed, 88 insertions, 4 deletions
diff --git a/Lib/contextlib.py b/Lib/contextlib.py index 625bb33..58e9a49 100644 --- a/Lib/contextlib.py +++ b/Lib/contextlib.py @@ -173,7 +173,7 @@ class _GeneratorContextManager( isinstance(value, StopIteration) and exc.__cause__ is value ): - exc.__traceback__ = traceback + value.__traceback__ = traceback return False raise except BaseException as exc: @@ -228,6 +228,7 @@ class _AsyncGeneratorContextManager( except RuntimeError as exc: # Don't re-raise the passed in exception. (issue27122) if exc is value: + exc.__traceback__ = traceback return False # Avoid suppressing if a Stop(Async)Iteration exception # was passed to athrow() and later wrapped into a RuntimeError @@ -239,6 +240,7 @@ class _AsyncGeneratorContextManager( isinstance(value, (StopIteration, StopAsyncIteration)) and exc.__cause__ is value ): + value.__traceback__ = traceback return False raise except BaseException as exc: @@ -250,6 +252,7 @@ class _AsyncGeneratorContextManager( # and the __exit__() protocol. if exc is not value: raise + exc.__traceback__ = traceback return False raise RuntimeError("generator didn't stop after athrow()") diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 31f5c74..ec06785 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -104,15 +104,39 @@ class ContextManagerTestCase(unittest.TestCase): self.assertEqual(frames[0].line, '1/0') # Repeat with RuntimeError (which goes through a different code path) + class RuntimeErrorSubclass(RuntimeError): + pass + try: with f(): - raise NotImplementedError(42) - except NotImplementedError as e: + raise RuntimeErrorSubclass(42) + except RuntimeErrorSubclass as e: frames = traceback.extract_tb(e.__traceback__) self.assertEqual(len(frames), 1) self.assertEqual(frames[0].name, 'test_contextmanager_traceback') - self.assertEqual(frames[0].line, 'raise NotImplementedError(42)') + self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)') + + class StopIterationSubclass(StopIteration): + pass + + for stop_exc in ( + StopIteration('spam'), + StopIterationSubclass('spam'), + ): + with self.subTest(type=type(stop_exc)): + try: + with f(): + raise stop_exc + except type(stop_exc) as e: + self.assertIs(e, stop_exc) + frames = traceback.extract_tb(e.__traceback__) + else: + self.fail(f'{stop_exc} was suppressed') + + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, 'raise stop_exc') def test_contextmanager_no_reraise(self): @contextmanager diff --git a/Lib/test/test_contextlib_async.py b/Lib/test/test_contextlib_async.py index b64673d..3d43ed0 100644 --- a/Lib/test/test_contextlib_async.py +++ b/Lib/test/test_contextlib_async.py @@ -5,6 +5,7 @@ from contextlib import ( import functools from test import support import unittest +import traceback from test.test_contextlib import TestBaseExitStack @@ -126,6 +127,62 @@ class AsyncContextManagerTestCase(unittest.TestCase): self.assertEqual(state, [1, 42, 999]) @_async_test + async def test_contextmanager_traceback(self): + @asynccontextmanager + async def f(): + yield + + try: + async with f(): + 1/0 + except ZeroDivisionError as e: + frames = traceback.extract_tb(e.__traceback__) + + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, '1/0') + + # Repeat with RuntimeError (which goes through a different code path) + class RuntimeErrorSubclass(RuntimeError): + pass + + try: + async with f(): + raise RuntimeErrorSubclass(42) + except RuntimeErrorSubclass as e: + frames = traceback.extract_tb(e.__traceback__) + + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, 'raise RuntimeErrorSubclass(42)') + + class StopIterationSubclass(StopIteration): + pass + + class StopAsyncIterationSubclass(StopAsyncIteration): + pass + + for stop_exc in ( + StopIteration('spam'), + StopAsyncIteration('ham'), + StopIterationSubclass('spam'), + StopAsyncIterationSubclass('spam') + ): + with self.subTest(type=type(stop_exc)): + try: + async with f(): + raise stop_exc + except type(stop_exc) as e: + self.assertIs(e, stop_exc) + frames = traceback.extract_tb(e.__traceback__) + else: + self.fail(f'{stop_exc} was suppressed') + + self.assertEqual(len(frames), 1) + self.assertEqual(frames[0].name, 'test_contextmanager_traceback') + self.assertEqual(frames[0].line, 'raise stop_exc') + + @_async_test async def test_contextmanager_no_reraise(self): @asynccontextmanager async def whee(): |