Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix refleak when is_subclass fails
  • Loading branch information
iritkatriel committed Mar 7, 2023
commit fd0181213a05ff99dc8c6e61d052a461860e7555
9 changes: 9 additions & 0 deletions Lib/test/test_capi/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ def test_set_object(self):
_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()
2 changes: 1 addition & 1 deletion Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
exception);
return;
}
Py_XINCREF(value);
/* Normalize the exception */
int is_subclass = 0;
if (value != NULL && PyExceptionInstance_Check(value)) {
Expand All @@ -158,6 +157,7 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
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