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()
--- /dev/null
+Fix regression in semantics of normalisation in ``PyErr_SetObject``.
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)
{
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},
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;
+ }
+ }
+ Py_XINCREF(value);
+ if (!is_subclass) {
/* We must normalize the value right now */
PyObject *fixed_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);
}
void