diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2023-03-07 21:27:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-07 21:27:46 (GMT) |
commit | a33ca2ad1fcf857817cba505a788e15cf9d6ed0c (patch) | |
tree | 8717635b992632d1f3f95f27631f0085423d5423 /Lib/test/test_capi/test_exceptions.py | |
parent | 54060ae91da2df44b3f6e6c698694d40284687e9 (diff) | |
download | cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.zip cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.tar.gz cpython-a33ca2ad1fcf857817cba505a788e15cf9d6ed0c.tar.bz2 |
gh-102493: fix normalization in PyErr_SetObject (#102502)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test/test_capi/test_exceptions.py')
-rw-r--r-- | Lib/test/test_capi/test_exceptions.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_capi/test_exceptions.py b/Lib/test/test_capi/test_exceptions.py index b543a1a..55f1316 100644 --- a/Lib/test/test_capi/test_exceptions.py +++ b/Lib/test/test_capi/test_exceptions.py @@ -140,6 +140,34 @@ class Test_ErrSetAndRestore(unittest.TestCase): 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() |