Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-102594: PyErr_SetObject adds note to exception raised on normalization error#102675
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 ourterms of service andprivacy 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 from1 commit
ff03c6131e64158d6285c827a48c4333933b3448fd03dbc6b8f0f2bea9d1068e834a8fa1a7d78ed90b66140770fFile 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
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -169,5 +169,25 @@ class Broken(Exception, metaclass=Meta): | ||
| with self.assertRaises(ZeroDivisionError) as e: | ||
| _testcapi.exc_set_object(Broken, Broken()) | ||
| def test_set_object_and_fetch(self): | ||
| class Broken(Exception): | ||
| def __init__(self, *arg): | ||
| raise ValueError("Broken __init__") | ||
| exc = _testcapi.exc_set_object_fetch(Broken, ('abcd')) | ||
iritkatriel marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertIsInstance(exc, ValueError) | ||
| self.assertEqual(exc.__notes__[0], | ||
| "Normalization failed: type=Broken args='abcd'") | ||
| class BadArg: | ||
| def __repr__(self): | ||
| raise TypeError('Broken arg type') | ||
| exc = _testcapi.exc_set_object_fetch(Broken, (BadArg())) | ||
iritkatriel marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| self.assertIsInstance(exc, ValueError) | ||
| self.assertEqual(exc.__notes__[0], | ||
| 'Normalization failed: type=Broken args=<unknown>') | ||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -135,6 +135,28 @@ _PyErr_GetTopmostException(PyThreadState *tstate) | ||
| return exc_info; | ||
| } | ||
| PyObject * | ||
| get_normalization_failure_note(PyThreadState *tstate, PyObject *exception, PyObject *value) | ||
| { | ||
| PyObject *args = PyObject_Repr(value); | ||
| if (args == NULL) { | ||
| _PyErr_Clear(tstate); | ||
| args = PyUnicode_FromFormat("<unknown>"); | ||
| } | ||
| PyObject *note; | ||
| const char *tpname = ((PyTypeObject*)exception)->tp_name; | ||
| if (args == NULL) { | ||
| _PyErr_Clear(tstate); | ||
| note = PyUnicode_FromFormat("Normalization failed: type=%s", tpname); | ||
| } | ||
| else { | ||
| note = PyUnicode_FromFormat("Normalization failed: type=%s args=%S", | ||
| tpname, args); | ||
| Py_DECREF(args); | ||
| } | ||
| return note; | ||
| } | ||
| void | ||
| _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) | ||
| { | ||
| @@ -169,6 +191,16 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) | ||
| fixed_value = _PyErr_CreateException(exception, value); | ||
| Py_XDECREF(value); | ||
| if (fixed_value == NULL) { | ||
| PyObject *exc = _PyErr_GetRaisedException(tstate); | ||
| assert(PyExceptionInstance_Check(exc)); | ||
| PyObject *note = get_normalization_failure_note(tstate, exception, value); | ||
| if (note != NULL) { | ||
| PyObject *res = _PyException_AddNote((PyBaseExceptionObject*)exc, note); | ||
| Py_DECREF(note); | ||
| Py_XDECREF(res); | ||
| } | ||
| _PyErr_SetRaisedException(tstate, exc); | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return; | ||
| } | ||