-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
GH-101578: Normalize the current exception #101607
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
markshannon
merged 13 commits into
python:main
from
faster-cpython:normalize-current-exception
Feb 8, 2023
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
87889bc
Make sure that the current exception is always normalized.
markshannon 14f21ff
Remove redundant type and traceback fields for the current exception.
markshannon 8b62323
Replace a some uses of PyErr_Fetch with PyErr_Fetch1.
markshannon 508850f
Refactor assertion.
markshannon 073409a
Document new C-API functions
markshannon d260b13
Add news
markshannon d5480e9
Fix typos
markshannon 0c3dc7b
Update generated code.
markshannon aa70bad
Rename new functions and fix up docs.
markshannon 1ae8eda
Fix up news entry
markshannon fe6c34f
Update stable ABI.
markshannon 566f6e1
Restore behavior of PyErr_Restore, and add tests.
markshannon 99f0880
Fix refleaks
markshannon 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -402,6 +402,8 @@ Querying the error indicator | |
|
||
.. c:function:: void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback) | ||
|
||
As of 3.12, this function is deprecated. Use :c:func:`PyErr_Fetch1` instead. | ||
|
||
Retrieve the error indicator into three variables whose addresses are passed. | ||
If the error indicator is not set, set all three variables to ``NULL``. If it is | ||
set, it will be cleared and you own a reference to each object retrieved. The | ||
|
@@ -422,6 +424,49 @@ Querying the error indicator | |
} | ||
|
||
|
||
.. c 8000 :function:: PyObject *PyErr_Fetch1(void) | ||
|
||
Returns the current error indicator, clearing the error indicator at the same time. | ||
|
||
.. note:: | ||
|
||
This function is normally only used by code that needs to catch exceptions or | ||
by code that needs to save and restore the error indicator temporarily, e.g.:: | ||
|
||
{ | ||
PyObject *exc = PyErr_Fetch1(); | ||
|
||
/* ... code that might produce other errors ... */ | ||
|
||
PyErr_Restore1(exc); | ||
} | ||
|
||
.. versionadded:: 3.12 | ||
|
||
|
||
.. c:function:: void PyErr_Restore1(PyObject *exc) | ||
|
||
As of 3.12, this function is deprecated. Use :c:func:`PyErr_Restore1` instead. | ||
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. ?? this is PyErr_Restore1 |
||
|
||
Set the error indicator to ``exc``. | ||
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. This looks like it's in the wrong place. |
||
If the error indicator is already set, it is cleared first. | ||
|
||
``exc`` should be a valid exception. | ||
(Violating this rules will cause subtle problems later.) | ||
This call consumes a reference to the ``exc`` object: you must own a | ||
reference to that object before the call and after the call you no longer own | ||
that reference. | ||
(If you don't understand this, don't use this function. I warned you.) | ||
|
||
.. note:: | ||
|
||
This function is normally only used by code that needs to save and restore the | ||
error indicator temporarily. Use :c:func:`PyErr_Fetch` to save the current | ||
error indicator. | ||
|
||
.. versionadded:: 3.12 | ||
|
||
|
||
.. c:function:: void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) | ||
|
||
Set the error indicator from the three objects. If the error indicator is | ||
|
@@ -437,12 +482,15 @@ Querying the error indicator | |
.. note:: | ||
|
||
This function is normally only used by code that needs to save and restore the | ||
error indicator temporarily. Use :c:func:`PyErr_Fetch` to save the current | ||
error indicator temporarily. Use :c:func:`PyErr_Fetch1` to save the current | ||
error indicator. | ||
|
||
|
||
.. c:function:: void PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) | ||
|
||
As of 3.12, this function is deprecated. | ||
Use :c:func:`PyErr_Fetch1` instead of :c:func:`PyErr_Fetch`. | ||
|
||
Under certain circumstances, the values returned by :c:func:`PyErr_Fetch` below | ||
can be "unnormalized", meaning that ``*exc`` is a class object but ``*val`` is | ||
not an instance of the same class. This function can be used to instantiate | ||
|
@@ -704,6 +752,18 @@ Exception Objects | |
:attr:`__suppress_context__` is implicitly set to ``True`` by this function. | ||
|
||
|
||
.. c:function:: PyObject* PyException_GetArgs(PyObject *ex) | ||
|
||
Return args of the given exception as a new reference, | ||
as accessible from Python through :attr:`args`. | ||
|
||
|
||
.. c:function:: void PyException_SetArgs(PyObject *ex, PyObject *args) | ||
|
||
Set the args of the given exception, | ||
as accessible from Python through :attr:`args`. | ||
|
||
|
||
.. _unicodeexceptions: | ||
|
||
Unicode Exception Objects | ||
|
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
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
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/C API/2023-02-06-16-14-30.gh-issue-101578.PW5fA9.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,5 @@ | ||
Deprecate the three arguments forms of :cfunc:`PyErr_Fetch` and | ||
:cfunc:`PyErr_Restore` and replace them with one argument forms that take | ||
and return the exception object. Also add :cfunc:`PyException_GetArgs` and | ||
:cfunc:`PyException_SetArgs` as convenience functions to help dealing with | ||
exceptions in the C API. |
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
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.
Uh oh!
There was an error while loading. Please reload this page.