summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib_async.py
diff options
context:
space:
mode:
authorThomas Grainger <tagrain@gmail.com>2023-01-03 15:47:13 (GMT)
committerGitHub <noreply@github.com>2023-01-03 15:47:13 (GMT)
commitb3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19 (patch)
tree942d61e3869c65751b8fff5f8ab6960506f50c21 /Lib/test/test_contextlib_async.py
parent85869498331f7020e18bb243c89cd694f674b911 (diff)
downloadcpython-b3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19.zip
cpython-b3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19.tar.gz
cpython-b3722ca058f6a6d6505cf2ea9ffabaf7fb6b6e19.tar.bz2
gh-95882: fix regression in the traceback of exceptions propagated from inside a contextlib context manager (#95883)
Diffstat (limited to 'Lib/test/test_contextlib_async.py')
-rw-r--r--Lib/test/test_contextlib_async.py57
1 files changed, 57 insertions, 0 deletions
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():