10000 [3.10] gh-92118: Add test for traceback when exception is modified by (Async)ExitStack.__exit__ (GH-92339) by miss-islington · Pull Request #92343 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.10] gh-92118: Add test for traceback when exception is modified by (Async)ExitStack.__exit__ (GH-92339) #92343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Lib/test/test_co 8000 ntextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import tempfile
import threading
import traceback
import unittest
from contextlib import * # Tests __all__
from test import support
Expand Down Expand Up @@ -701,6 +702,38 @@ def test_exit_suppress(self):
stack.push(lambda *exc: True)
1/0

def test_exit_exception_traceback(self):
# This test captures the current behavior of ExitStack so that we know
# if we ever unintendedly change it. It is not a statement of what the
# desired behavior is (for instance, we may want to remove some of the
# internal contextlib frames).

def raise_exc(exc):
raise exc

try:
with self.exit_stack() as stack:
stack.callback(raise_exc, ValueError)
1/0
except ValueError as e:
exc = e

self.assertIsInstance(exc, ValueError)
ve_frames = traceback.extract_tb(exc.__traceback__)
expected = \
[('test_exit_exception_traceback', 'with self.exit_stack() as stack:')] + \
self.callback_error_internal_frames + \
[('_exit_wrapper', 'callback(*args, **kwds)'),
('raise_exc', 'raise exc')]

self.assertEqual(
[(f.name, f.line) for f in ve_frames], expected)

self.assertIsInstance(exc.__context__, ZeroDivisionError)
zde_frames = traceback.extract_tb(exc.__context__.__traceback__)
self.assertEqual([(f.name, f.line) for f in zde_frames],
[('test_exit_exception_traceback', '1/0')])

def test_exit_exception_chaining_reference(self):
# Sanity check to make sure that ExitStack chaining matches
# actual nested with statements
Expand Down Expand Up @@ -968,6 +1001,10 @@ def first():

class TestExitStack(TestBaseExitStack, unittest.TestCase):
exit_stack = ExitStack
callback_error_internal_frames = [
('__exit__', 'raise exc_details[1]'),
('__exit__', 'if cb(*exc_details):'),
]


class TestRedirectStream:
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_contextlib_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ def __exit__(self, *exc_details):
return self.run_coroutine(self.__aexit__(*exc_details))

exit_stack = SyncAsyncExitStack
callback_error_internal_frames = [
('__exit__', 'return self.run_coroutine(self.__aexit__(*exc_details))'),
('run_coroutine', 'raise exc'),
('run_coroutine', 'raise exc'),
('__aexit__', 'raise exc_details[1]'),
('__aexit__', 'cb_suppress = cb(*exc_details)'),
]

def setUp(self):
self.loop = asyncio.new_event_loop()
Expand Down
0