diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2022-01-13 12:35:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-13 12:35:58 (GMT) |
commit | c590b581bba517f81ced2e6f531ccc9e2e22eab5 (patch) | |
tree | f3c49f2fa4a5eb43d403be8d8a811ebfc11f33fe /Lib/test/test_sys.py | |
parent | 9c2ebb906d1c68c3d571b100c92ceb08805b94cd (diff) | |
download | cpython-c590b581bba517f81ced2e6f531ccc9e2e22eab5.zip cpython-c590b581bba517f81ced2e6f531ccc9e2e22eab5.tar.gz cpython-c590b581bba517f81ced2e6f531ccc9e2e22eab5.tar.bz2 |
bpo-46328: Add sys.exception() (GH-30514)
Diffstat (limited to 'Lib/test/test_sys.py')
-rw-r--r-- | Lib/test/test_sys.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 38771d4..f05cd75 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -71,6 +71,69 @@ class DisplayHookTest(unittest.TestCase): code = compile("42", "<string>", "single") self.assertRaises(ValueError, eval, code) +class ActiveExceptionTests(unittest.TestCase): + def test_exc_info_no_exception(self): + self.assertEqual(sys.exc_info(), (None, None, None)) + + def test_sys_exception_no_exception(self): + self.assertEqual(sys.exception(), None) + + def test_exc_info_with_exception_instance(self): + def f(): + raise ValueError(42) + + try: + f() + except Exception as e_: + e = e_ + exc_info = sys.exc_info() + + self.assertIsInstance(e, ValueError) + self.assertIs(exc_info[0], ValueError) + self.assertIs(exc_info[1], e) + self.assertIs(exc_info[2], e.__traceback__) + + def test_exc_info_with_exception_type(self): + def f(): + raise ValueError + + try: + f() + except Exception as e_: + e = e_ + exc_info = sys.exc_info() + + self.assertIsInstance(e, ValueError) + self.assertIs(exc_info[0], ValueError) + self.assertIs(exc_info[1], e) + self.assertIs(exc_info[2], e.__traceback__) + + def test_sys_exception_with_exception_instance(self): + def f(): + raise ValueError(42) + + try: + f() + except Exception as e_: + e = e_ + exc = sys.exception() + + self.assertIsInstance(e, ValueError) + self.assertIs(exc, e) + + def test_sys_exception_with_exception_type(self): + def f(): + raise ValueError + + try: + f() + except Exception as e_: + e = e_ + exc = sys.exception() + + self.assertIsInstance(e, ValueError) + self.assertIs(exc, e) + class ExceptHookTest(unittest.TestCase): |