-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-111375: Fix handling of exceptions within @contextmanager
-decorated functions
#111676
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
Open
pR0Ps
wants to merge
11
commits into
python:main
Choose a base branch
from
pR0Ps:bugfix/with-statement-exception-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+398
−29
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0138ff1
Add sys._set_exception() to expose the `PyErr_SetHandledExecption` fu…
pR0Ps b285e9a
Fix exception context in @contextmanagers
pR0Ps ea784d7
Reference GH issue in comment
pR0Ps bf4ef1e
Add more checks for exception context
pR0Ps bbc212b
Fix sys._set_exception() docstring wording
pR0Ps 59ef993
Fix test case and add explanation in comment
pR0Ps c0e7400
Add exc_context parameter to {gen/coro}.throw()
pR0Ps 5e8323a
Move exc_context setting into _gen_throw and restore previous exception
pR0Ps a7e2dc9
Default .throw()'s exc_context to the current exception
pR0Ps 90fdbe0
Remove overactive assert
pR0Ps 5da75d0
Rename test case
pR0Ps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,131 @@ def f(): | |
self.assertIsInstance(e, ValueError) | ||
self.assertIs(exc, e) | ||
|
||
class SetExceptionTests(unittest.TestCase): | ||
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 suggestion is to move this from sys to contextlib. Right? |
||
|
||
def tearDown(self): | ||
# make sure we don't leave the global exception set | ||
sys._set_exception(None); | ||
|
||
def test_set_exc_invalid_values(self): | ||
for x in (0, "1", b"2"): | ||
with self.assertRaises(TypeError): | ||
sys._set_exception(x); | ||
|
||
def test_clear_exc(self): | ||
try: | ||
raise ValueError() | ||
except ValueError: | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
sys._set_exception(None) | ||
self.assertIsNone(sys.exception()) | ||
|
||
def test_set_exc(self): | ||
exc = ValueError() | ||
self.assertIsNone(sys.exception()) | ||
sys._set_exception(exc) | ||
self.assertIs(sys.exception(), exc) | ||
|
||
def test_set_exc_replaced_by_new_exception_and_restored(self): | ||
exc = ValueError() | ||
sys._set_exception(exc) | ||
self.assertIs(sys.exception(), exc) | ||
try: | ||
raise TypeError() | ||
except TypeError: | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIs(sys.exception().__context__, exc) | ||
|
||
self.assertIs(sys.exception(), exc) | ||
|
||
def test_set_exc_popped_on_exit_except(self): | ||
exc = ValueError() | ||
try: | ||
raise TypeError() | ||
except TypeError: | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
sys._set_exception(exc) | ||
self.assertIs(sys.exception(), exc) | ||
self.assertIsNone(sys.exception()) | ||
|
||
def test_cleared_exc_overridden_and_restored(self): | ||
try: | ||
raise ValueError() | ||
except ValueError: | ||
try: | ||
raise TypeError() | ||
except TypeError: | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
sys._set_exception(None) | ||
self.assertIsNone(sys.exception()) | ||
try: | ||
raise IndexError() | ||
except IndexError: | ||
self.assertIsInstance(sys.exception(), IndexError) | ||
self.assertIsNone(sys.exception().__context__) | ||
self.assertIsNone(sys.exception()) | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
self.assertIsNone(sys.exception()) | ||
|
||
def test_clear_exc_in_generator(self): | ||
def inner(): | ||
self.assertIsNone(sys.exception()) | ||
yield | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
sys._set_exception(None) | ||
self.assertIsNone(sys.exception()) | ||
yield | ||
self.assertIsNone(sys.exception()) | ||
|
||
# with a single exception in exc_info stack | ||
g = inner() | ||
next(g) | ||
try: | ||
raise ValueError() | ||
except: | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
next(g) | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
self.assertIsNone(sys.exception()) | ||
with self.assertRaises(StopIteration): | ||
next(g) | ||
self.assertIsNone(sys.exception()) | ||
|
||
# with multiple exceptions in exc_info stack by chaining generators | ||
def outer(): | ||
g = inner() | ||
self.assertIsNone(sys.exception()) | ||
yield next(g) | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
try: | ||
raise ValueError() | ||
except: | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
self.assertIsInstance(sys.exception().__context__, TypeError) | ||
yield next(g) | ||
# at this point the TypeError from the caller has been handled | ||
# by the caller's except block. Even still, it should still be | ||
# referenced as the __context__ of the current exception. | ||
self.assertIsInstance(sys.exception(), ValueError) | ||
self.assertIsInstance(sys.exception().__context__, TypeError) | ||
# not handling an exception, caller isn't handling one either | ||
self.assertIsNone(sys.exception()) | ||
with self.assertRaises(StopIteration): | ||
next(g) | ||
self.assertIsNone(sys.exception()) | ||
|
||
g = outer() | ||
next(g) | ||
try: | ||
raise TypeError() | ||
except: | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
next(g) | ||
self.assertIsInstance(sys.exception(), TypeError) | ||
self.assertIsNone(sys.exception()) | ||
with self.assertRaises(StopIteration): | ||
next(g) | ||
self.assertIsNone(sys.exception()) | ||
|
||
class ExceptHookTest(unittest.TestCase): | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2023-12-19-17-27-08.gh-issue-111375.XeHOl_.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add sys._set_exception() function that can set/clear the current exception | ||
context. Patch by Carey Metcalfe. |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2023-11-03-01-44-51.gh-issue-111375.qDBcCI.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix handling of ``sys.exception()`` within ``@contextlib.contextmanager`` | ||
functions. Patch by Carey Metcalfe. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Now that I forgot everything I'm finding this comment less clear. What is "the actual current exception"?
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.
Also, when you write "If X then Y", it's not immediately clear whether you are talking about what happens, what should happen, or what would happen if we didn't do what we're doing here.
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.
I definitely agree that it's not very clear, it's a bit of a complex issue to explain. How about this: