Skip to content
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

gh-102493: fix normalization in PyErr_SetObject #102502

Merged
merged 8 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
19 changes: 19 additions & 0 deletions Lib/test/test_capi/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ def test_err_restore(self):
self.assertEqual(1, v.args[0])
self.assertIs(tb, v.__traceback__.tb_next)

def test_set_object(self):

# new exception as obj is not an exception
with self.assertRaises(ValueError) as e:
_testcapi.exc_set_object(ValueError, 42)
self.assertEqual(e.exception.args, (42,))

# wraps the exception because unrelated types
with self.assertRaises(ValueError) as e:
_testcapi.exc_set_object(ValueError, TypeError(1,2,3))
wrapped = e.exception.args[0]
self.assertIsInstance(wrapped, TypeError)
self.assertEqual(wrapped.args, (1, 2, 3))

# is superclass, so does not wrap
with self.assertRaises(PermissionError) as e:
_testcapi.exc_set_object(OSError, PermissionError(24))
self.assertEqual(e.exception.args, (24,))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression in semantics of normalisation in ``PyErr_SetObject``.
15 changes: 15 additions & 0 deletions Modules/_testcapi/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ make_exception_with_doc(PyObject *self, PyObject *args, PyObject *kwargs)
return PyErr_NewExceptionWithDoc(name, doc, base, dict);
}

static PyObject *
exc_set_object(PyObject *self, PyObject *args)
{
PyObject *exc;
PyObject *obj;

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

PyErr_SetObject(exc, obj);
return NULL;
}

static PyObject *
raise_exception(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -247,6 +261,7 @@ static PyMethodDef test_methods[] = {
PyDoc_STR("fatal_error(message, release_gil=False): call Py_FatalError(message)")},
{"make_exception_with_doc", _PyCFunction_CAST(make_exception_with_doc),
METH_VARARGS | METH_KEYWORDS},
{"exc_set_object", exc_set_object, 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
11 changes: 9 additions & 2 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
}
Py_XINCREF(value);
/* Normalize the exception */
if (value == NULL || (PyObject *)Py_TYPE(value) != exception) {
int is_subclass = 0;
if (value != NULL) {
is_subclass = PyObject_IsSubclass((PyObject*)Py_TYPE(value), exception);
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved
if (is_subclass < 0) {
return;
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (value == NULL || !is_subclass) {
/* We must normalize the value right now */
PyObject *fixed_value;

Expand Down Expand Up @@ -208,7 +215,7 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
}
if (value != NULL && PyExceptionInstance_Check(value))
tb = PyException_GetTraceback(value);
_PyErr_Restore(tstate, Py_XNewRef(exception), value, tb);
_PyErr_Restore(tstate, Py_NewRef(Py_TYPE(value)), value, tb);
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
}

void
Expand Down