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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from4 commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
ff03c61 PyErr_SetObject adds note to exception raised on normalization error
iritkatriel31e6415 📜🤖 Added by blurb_it.
blurb-it[bot]8d6285c Merge branch 'main' into setobject-note
iritkatriel827a48c add missing 'static'
iritkatriel4333933 apply offline comments from Guido
iritkatrielb3448fd Merge branch 'main' into setobject-note
iritkatriel03dbc6b use Py_XSETREF
iritkatriel8f0f2be Merge branch 'main' into setobject-note
iritkatriela9d1068 fix whitespace
iritkatriele834a8f Merge branch 'main' into setobject-note
iritkatriela1a7d78 Merge branch 'main' into setobject-note
AlexWaygooded90b66 Merge branch 'main' into setobject-note
iritkatriel140770f Merge branch 'main' into setobject-note
iritkatrielFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionsInclude/cpython/pyerrors.h
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
20 changes: 20 additions & 0 deletionsLib/test/test_capi/test_exceptions.py
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 |
|---|---|---|
| @@ -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() | ||
1 change: 1 addition & 0 deletionsMisc/NEWS.d/next/Core and Builtins/2023-03-14-00-11-46.gh-issue-102594.BjU-m2.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 @@ | ||
| Add note to exception raised in ``PyErr_SetObject`` when normalization fails. |
21 changes: 21 additions & 0 deletionsModules/_testcapi/exceptions.c
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
32 changes: 32 additions & 0 deletionsPython/errors.c
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 |
|---|---|---|
| @@ -135,6 +135,28 @@ _PyErr_GetTopmostException(PyThreadState *tstate) | ||
| return exc_info; | ||
| } | ||
| static 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; | ||
| } | ||
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.