-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
gh-95882: fix regression in the traceback of exceptions propagated from inside a contextlib context manager #95883
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
Changes from 7 commits
8110f28
3454e86
5171ed7
7f448d8
25d3ee0
2990340
adf2bf3
0c9ef8b
cf3bcad
d448aaf
7904e4b
b6c1f6e
d8800cd
d4e0f2e
8f785e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,15 +104,41 @@ def f(): | |
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), 2) | ||
self.assertEqual(frames[0].name, 'f') | ||
self.assertEqual(frames[0].line, 'yield') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iritkatriel do you expect to see this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does 3.10 do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the import contextlib
import logging
logger = logging.getLogger("__name__")
@contextlib.contextmanager
def f():
yield
try:
with f():
raise StopIteration
except StopIteration:
logger.exception("stop iteration")
print("================ overwriting StopIteration via globals!==============")
class StopIteration(Exception):
pass
try:
with f():
raise StopIteration
except StopIteration:
logger.exception("stop iteration")
Notice the extra
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed it! |
||
self.assertEqual(frames[1].name, 'test_contextmanager_traceback') | ||
self.assertEqual(frames[1].line, 'raise stop_exc') | ||
|
||
def test_contextmanager_no_reraise(self): | ||
@contextmanager | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a 3.11 regression in :func:`~contextlib.asynccontextmanager`, which caused it to propagate exceptions with incorrect tracebacks. | ||
graingert marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a test for this case
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iritkatriel it doesn't look like this line does anything. If I delete it I get the same behavior, same with L176:
cpython/Lib/contextlib.py
Lines 166 to 177 in adf2bf3