-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (in Objects/) #102218
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
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f06d53a
Objects/dictobject.c
iritkatriel d5072bd
Objects/frameobject.c
iritkatriel c40fc27
Objects/genobject.c
iritkatriel 13cb48c
Objects/object.c
iritkatriel fde54d4
Objects/odictobject.c
iritkatriel 8fd66a8
Objects/typeobject.c
iritkatriel d83af14
Objects/weakrefobject.c
iritkatriel 0298a04
Objects/exceptions.c
iritkatriel 09f2ab6
put back the Py_DECREF(exc)
iritkatriel 0fef293
Merge branch 'main' into fetch-restore-objects
iritkatriel 9b5b62a
Merge branch 'main' into fetch-restore-objects
iritkatriel 6378d56
Merge branch 'main' into fetch-restore-objects
iritkatriel e11d73e
Merge branch 'main' into fetch-restore-objects
iritkatriel eb45902
Merge branch 'main' into fetch-restore-objects
iritkatriel 526e387
Merge branch 'main' into fetch-restore-objects
iritkatriel a579bc4
Merge branch 'main' into fetch-restore-objects
iritkatriel e5bf89d
Merge branch 'main' into fetch-restore-objects
iritkatriel 0863f35
Merge branch 'main' into fetch-restore-objects
iritkatriel 3f8b452
Merge branch 'main' into fetch-restore-objects
iritkatriel 7a436c9
Merge branch 'main' into fetch-restore-objects
iritkatriel 9b68505
remove redundant null check and obsolete comments (Mark's review comm…
iritkatriel c23baa2
Merge branch 'main' into fetch-restore-objects
iritkatriel 44bead6
Merge branch 'main' into fetch-restore-objects
iritkatriel d592920
Merge branch 'main' into fetch-restore-objects
iritkatriel e136989
Merge branch 'main' into fetch-restore-objects
iritkatriel 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3781,16 +3781,13 @@ PyObject * | |
_PyErr_TrySetFromCause(const char *format, ...) | ||
{ | ||
PyObject* msg_prefix; | ||
PyObject *exc, *val, *tb; | ||
PyTypeObject *caught_type; | ||
PyObject *instance_args; | ||
Py_ssize_t num_args, caught_type_size, base_exc_size; | ||
PyObject *new_exc, *new_val, *new_tb; | ||
va_list vargs; | ||
int same_basic_size; | ||
|
||
PyErr_Fetch(&exc, &val, &tb); | ||
caught_type = (PyTypeObject *)exc; | ||
PyObject *exc = PyErr_GetRaisedException(); | ||
PyTypeObject *caught_type = Py_TYPE(exc); | ||
/* Ensure type info indicates no extra state is stored at the C level | ||
* and that the type can be reinstantiated using PyErr_Format | ||
*/ | ||
|
@@ -3810,31 +3807,30 @@ _PyErr_TrySetFromCause(const char *format, ...) | |
* more state than just the exception type. Accordingly, we just | ||
* leave it alone. | ||
*/ | ||
PyErr_Restore(exc, val, tb); | ||
PyErr_SetRaisedException(exc); | ||
return NULL; | ||
} | ||
|
||
/* Check the args are empty or contain a single string */ | ||
PyErr_NormalizeException(&exc, &val, &tb); | ||
instance_args = ((PyBaseExceptionObject *)val)->args; | ||
instance_args = ((PyBaseExceptionObject *)exc)->args; | ||
num_args = PyTuple_GET_SIZE(instance_args); | ||
if (num_args > 1 || | ||
(num_args == 1 && | ||
!PyUnicode_CheckExact(PyTuple_GET_ITEM(instance_args, 0)))) { | ||
/* More than 1 arg, or the one arg we do have isn't a string | ||
*/ | ||
PyErr_Restore(exc, val, tb); | ||
PyErr_SetRaisedException(exc); | ||
return NULL; | ||
} | ||
|
||
/* Ensure the instance dict is also empty */ | ||
if (!_PyObject_IsInstanceDictEmpty(val)) { | ||
if (!_PyObject_IsInstanceDictEmpty(exc)) { | ||
/* While we could potentially copy a non-empty instance dictionary | ||
* to the replacement exception, for now we take the more | ||
* conservative path of leaving exceptions with attributes set | ||
* alone. | ||
*/ | ||
PyErr_Restore(exc, val, tb); | ||
PyErr_SetRaisedException(exc); | ||
return NULL; | ||
} | ||
|
||
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 block comment should go as well (if I'm correct about removing the above code) |
||
|
@@ -3847,28 +3843,19 @@ _PyErr_TrySetFromCause(const char *format, ...) | |
* types as well, but that's quite a bit trickier due to the extra | ||
* state potentially stored on OSError instances. | ||
*/ | ||
/* Ensure the traceback is set correctly on the existing exception */ | ||
if (tb != NULL) { | ||
PyException_SetTraceback(val, tb); | ||
Py_DECREF(tb); | ||
} | ||
|
||
va_start(vargs, format); | ||
msg_prefix = PyUnicode_FromFormatV(format, vargs); | ||
va_end(vargs); | ||
if (msg_prefix == NULL) { | ||
Py_DECREF(exc); | ||
Py_DECREF(val); | ||
return NULL; | ||
} | ||
|
||
PyErr_Format(exc, "%U (%s: %S)", | ||
msg_prefix, Py_TYPE(val)->tp_name, val); | ||
Py_DECREF(exc); | ||
PyErr_Format((PyObject*)Py_TYPE(exc), "%U (%s: %S)", | ||
msg_prefix, Py_TYPE(exc)->tp_name, exc); | ||
Py_DECREF(msg_prefix); | ||
PyErr_Fetch(&new_exc, &new_val, &new_tb); | ||
PyErr_NormalizeException(&new_exc, &new_val, &new_tb); | ||
PyException_SetCause(new_val, val); | ||
PyErr_Restore(new_exc, new_val, new_tb); | ||
return new_val; | ||
PyObject *new_exc = PyErr_GetRaisedException(); | ||
PyException_SetCause(new_exc, exc); | ||
PyErr_SetRaisedException(new_exc); | ||
return new_exc; | ||
} |
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
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
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.
Does all this stuff about "wrapping safely" make sense any more?
Since there are no "denomralized" exceptions, this should always work.
I think all this code is making sure that calling
PyErr_NormalizeException
is safe.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.
Good question. Let me try to understand what's going on 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 don't think this is about PyErr_NormalizeException. This function is called from only one place:
This is a classic use case for notes (PEP 678). It is trying to create an exception of the same type with additional info (and does a lot of stuff to check that it can indeed safely create a new exception of the same type).
I would just rewrite this whole thing to attach the additional info as a note (but not in this PR, it's unrelated).
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.
A PR for that is at: #102407