Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
iritkatriel merged 13 commits intopython:mainfromiritkatriel:setobject-note
Mar 16, 2023
Merged
Show file tree
Hide file tree
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
iritkatrielMar 13, 2023
31e6415
📜🤖 Added by blurb_it.
blurb-it[bot]Mar 14, 2023
8d6285c
Merge branch 'main' into setobject-note
iritkatrielMar 14, 2023
827a48c
add missing 'static'
iritkatrielMar 14, 2023
4333933
apply offline comments from Guido
iritkatrielMar 14, 2023
b3448fd
Merge branch 'main' into setobject-note
iritkatrielMar 14, 2023
03dbc6b
use Py_XSETREF
iritkatrielMar 14, 2023
8f0f2be
Merge branch 'main' into setobject-note
iritkatrielMar 14, 2023
a9d1068
fix whitespace
iritkatrielMar 15, 2023
e834a8f
Merge branch 'main' into setobject-note
iritkatrielMar 15, 2023
a1a7d78
Merge branch 'main' into setobject-note
AlexWaygoodMar 15, 2023
ed90b66
Merge branch 'main' into setobject-note
iritkatrielMar 15, 2023
140770f
Merge branch 'main' into setobject-note
iritkatrielMar 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletionsInclude/cpython/pyerrors.h
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,6 +112,10 @@ PyAPI_FUNC(PyObject *) _PyErr_FormatFromCause(

/* In exceptions.c */

PyAPI_FUNC(PyObject*) _PyException_AddNote(
PyBaseExceptionObject *exc,
PyObject *note);

/* Helper that attempts to replace the current exception with one of the
* same type but with a prefix added to the exception text. The resulting
* exception description looks like:
Expand Down
20 changes: 20 additions & 0 deletionsLib/test/test_capi/test_exceptions.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'))
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()))
self.assertIsInstance(exc, ValueError)
self.assertEqual(exc.__notes__[0],
'Normalization failed: type=Broken args=<unknown>')


if __name__ == "__main__":
unittest.main()
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -92,6 +92,26 @@ exc_set_object(PyObject *self, PyObject *args)
returnNULL;
}

staticPyObject*
exc_set_object_fetch(PyObject*self,PyObject*args)
{
PyObject*exc;
PyObject*obj;
PyObject*type;
PyObject*value;
PyObject*tb;

if (!PyArg_ParseTuple(args,"OO:exc_set_object",&exc,&obj)) {
returnNULL;
}

PyErr_SetObject(exc,obj);
PyErr_Fetch(&type,&value,&tb);
Py_XDECREF(type);
Py_XDECREF(tb);
returnvalue;
}

staticPyObject*
raise_exception(PyObject*self,PyObject*args)
{
Expand DownExpand Up@@ -262,6 +282,7 @@ static PyMethodDef test_methods[] = {
{"make_exception_with_doc",_PyCFunction_CAST(make_exception_with_doc),
METH_VARARGS |METH_KEYWORDS},
{"exc_set_object",exc_set_object,METH_VARARGS},
{"exc_set_object_fetch",exc_set_object_fetch,METH_VARARGS},
{"raise_exception",raise_exception,METH_VARARGS},
{"raise_memoryerror",raise_memoryerror,METH_NOARGS},
{"set_exc_info",test_set_exc_info,METH_VARARGS},
Expand Down
6 changes: 6 additions & 0 deletionsObjects/exceptions.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3749,6 +3749,12 @@ _PyExc_Fini(PyInterpreterState *interp)
_PyExc_FiniTypes(interp);
}

PyObject *
_PyException_AddNote(PyBaseExceptionObject *exc, PyObject *note)
{
return BaseException_add_note((PyObject *)exc, note);
}

/* Helper to do the equivalent of "raise X from Y" in C, but always using
* the current exception rather than passing one in.
*
Expand Down
32 changes: 32 additions & 0 deletionsPython/errors.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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)
{
Expand DownExpand Up@@ -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);
return;
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp