| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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 | ||
| */ | ||
| Expand All | @@ -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; | ||
| } | ||
|
|
||
|
Comment thread
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis block comment should go as well (if I'm correct about removing the above code)
Sorry, something went wrong.
All reactions
|
||
| Expand All | @@ -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; | ||
| } | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityDoes 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityGood question. Let me try to understand what's going on here.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI don't think this is about PyErr_NormalizeException. This function is called from only one place:
/* Helper that tries to ensure the reported exception chain indicates the * codec that was invoked to trigger the failure without changing the type * of the exception raised. */ static void wrap_codec_error(const char *operation, const char *encoding) { /* TrySetFromCause will replace the active exception with a suitably * updated clone if it can, otherwise it will leave the original * exception alone. */ _PyErr_TrySetFromCause("%s with '%s' codec failed", operation, encoding); }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).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityA PR for that is at: #102407
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.