Skip to content
Merged
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions Lib/test/test_contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,39 @@ 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__)
self.assertEqual(len(ve_frames), 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is redundant, And if it fail, the report will be less informative than failing the test which compares lists.

self.assertEqual(
[(f.name, f.line) for f in ve_frames],
[('test_exit_exception_traceback', 'with self.exit_stack() as stack:'),
('__exit__', 'raise exc_details[1]'),
('__exit__', 'if cb(*exc_details):'),
('_exit_wrapper', 'callback(*args, **kwds)'),
('raise_exc', 'raise exc')])

self.assertIsInstance(exc.__context__, ZeroDivisionError)
zde_frames = traceback.extract_tb(exc.__context__.__traceback__)
self.assertEqual(len(zde_frames), 1)
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