Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f06d53ad5072bdc40fc2713cb48cfde54d48fd66a8d83af140298a0409f2ab60fef2939b5b62a6378d56e11d73eeb45902526e387a579bc4e5bf89d0863f353f8b4527a436c99b68505c23baa244bead6d592920e136989File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
Member 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; | ||
| } | ||
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_NormalizeExceptionis 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