FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-102493: fix normalization in PyErr_SetObject by iritkatriel · Pull Request #102502 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 28 additions & 0 deletions Lib/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
Expand Up @@ -140,6 +140,34 @@ 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,))

class Meta(type):
def __subclasscheck__(cls, sub):
1/0

class Broken(Exception, metaclass=Meta):
pass

with self.assertRaises(ZeroDivisionError) as e:
_testcapi.exc_set_object(Broken, Broken())

if __name__ == "__main__":
unittest.main()
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 @@
Fix regression in semantics of normalisation in ``PyErr_SetObject``.
15 changes: 15 additions & 0 deletions Modules/_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
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
16 changes: 12 additions & 4 deletions Python/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
Expand Up @@ -149,9 +149,16 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
exception);
return;
}
Py_XINCREF(value);
/* Normalize the exception */
if (value == NULL || (PyObject *)Py_TYPE(value) != exception) {
int is_subclass = 0;
if (value != NULL && PyExceptionInstance_Check(value)) {
is_subclass = PyObject_IsSubclass((PyObject *)Py_TYPE(value), exception);
if (is_subclass < 0) {
return;
Comment thread
iritkatriel marked this conversation as resolved.
}
}
Py_XINCREF(value);
if (!is_subclass) {
/* We must normalize the value right now */
PyObject *fixed_value;

Expand Down Expand Up @@ -206,9 +213,10 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
Py_DECREF(exc_value);
}
}
if (value != NULL && PyExceptionInstance_Check(value))
assert(value != NULL);
if (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);
Comment thread
iritkatriel marked this conversation as resolved.
}

void
Expand Down

Back | FazBrowse Home | New Git URL