-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
721ce85
0a6540c
8cc13ea
3183587
a879a9f
887eb54
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add sys._set_exception() function that can set/clear the current exception | ||
context. Patch by Carey Metcalfe. | ||
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. We have 3 news files here. This and the next one should be deleted. 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. Will do. Should the 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. Probably not needed if it's private. But you can wait with the news file till we know what's going in. |
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
TBH, I'm not that comfortable adding this function to the
sys
module, even with an_
. It is an API that shouldn't really exist, we're adding it just to enable a hack in contextmanager, which needs to change the semantics of code.Is there a way that we can roll this functionality into the
gen.throw()
function instead? Such as -- add a parameter that indicates that the exception stack needs to be fudged in this way?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.
Yeah, that makes sense. I don't really have strong opinions on how it's implemented, I only went with
sys._set_exception
because it seemed like it would work and there is precedent for a function that directly manipulates the current exception state.With that being said, I don't know if doing it entirely within
throw()
is possible, at least in this case. Because the exception that needs to be set is the one being handled in the__enter__
, I don't think there's enough context whenthrow()
is called to correctly manipulate the 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.
I was thinking you pass the exc_context as a new kwwarg to throw which defaults to None.
(I wasn't aware of exc_clear actually. It was added in 2.3, so quite old. Doesn't seem to be used anywhere in the stdlib at the moment.)
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.
Ah, that makes sense. Now that I've got
sys._set_exception()
working and am therefore (reasonably) sure that theNone
/NULL
exception stuff works properly, I'll take a look at adding a kwarg tothrow()
that essentially just makes it callPyErr_SetHandledException
behind the scenes.