isinstance(value, StopIteration)
and exc.__cause__ is value
):
- exc.__traceback__ = traceback
+ value.__traceback__ = traceback
return False
raise
except BaseException as exc:
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
isinstance(value, (StopIteration, StopAsyncIteration))
and exc.__cause__ is value
):
+ value.__traceback__ = traceback
return False
raise
except BaseException as exc:
# and the __exit__() protocol.
if exc is not value:
raise
+ exc.__traceback__ = traceback
return False
raise RuntimeError("generator didn't stop after athrow()")
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
import functools
from test import support
import unittest
+import traceback
from test.test_contextlib import TestBaseExitStack
raise ZeroDivisionError()
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
Tim Graham
Kim Gräsman
Alex Grönholm
+Thomas Grainger
Nathaniel Gray
Eddy De Greef
Duane Griffin
--- /dev/null
+Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks and fix a 3.11 regression in :func:`~contextlib.contextmanager`, which caused it to propagate exceptions with incorrect tracebacks for :exc:`StopIteration`.