|
| 1 | +import re |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | +from test import support |
| 6 | +from test.support import import_helper |
| 7 | +from test.support.script_helper import assert_python_failure |
| 8 | + |
| 9 | +from .test_misc import decode_stderr |
| 10 | + |
| 11 | +# Skip this test if the _testcapi module isn't available. |
| 12 | +_testcapi = import_helper.import_module('_testcapi') |
| 13 | + |
| 14 | +class Test_Exceptions(unittest.TestCase): |
| 15 | + |
| 16 | + def test_exception(self): |
| 17 | + raised_exception = ValueError("5") |
| 18 | + new_exc = TypeError("TEST") |
| 19 | + try: |
| 20 | + raise raised_exception |
| 21 | + except ValueError as e: |
| 22 | + orig_sys_exception = sys.exception() |
| 23 | + orig_exception = _testcapi.set_exception(new_exc) |
| 24 | + new_sys_exception = sys.exception() |
| 25 | + new_exception = _testcapi.set_exception(orig_exception) |
| 26 | + reset_sys_exception = sys.exception() |
| 27 | + |
| 28 | + self.assertEqual(orig_exception, e) |
| 29 | + |
| 30 | + self.assertEqual(orig_exception, raised_exception) |
| 31 | + self.assertEqual(orig_sys_exception, orig_exception) |
| 32 | + self.assertEqual(reset_sys_exception, orig_exception) |
| 33 | + self.assertEqual(new_exception, new_exc) |
| 34 | + self.assertEqual(new_sys_exception, new_exception) |
| 35 | + else: |
| 36 | + self.fail("Exception not raised") |
| 37 | + |
| 38 | + def test_exc_info(self): |
| 39 | + raised_exception = ValueError("5") |
| 40 | + new_exc = TypeError("TEST") |
| 41 | + try: |
| 42 | + raise raised_exception |
| 43 | + except ValueError as e: |
| 44 | + tb = e.__traceback__ |
| 45 | + orig_sys_exc_info = sys.exc_info() |
| 46 | + orig_exc_info = _testcapi.set_exc_info(new_exc.__class__, new_exc, None) |
| 47 | + new_sys_exc_info = sys.exc_info() |
| 48 | + new_exc_info = _testcapi.set_exc_info(*orig_exc_info) |
| 49 | + reset_sys_exc_info = sys.exc_info() |
| 50 | + |
| 51 | + self.assertEqual(orig_exc_info[1], e) |
| 52 | + |
| 53 | + self.assertSequenceEqual(orig_exc_info, (raised_exception.__class__, raised_exception, tb)) |
| 54 | + self.assertSequenceEqual(orig_sys_exc_info, orig_exc_info) |
| 55 | + self.assertSequenceEqual(reset_sys_exc_info, orig_exc_info) |
| 56 | + self.assertSequenceEqual(new_exc_info, (new_exc.__class__, new_exc, None)) |
| 57 | + self.assertSequenceEqual(new_sys_exc_info, new_exc_info) |
| 58 | + else: |
| 59 | + self.assertTrue(False) |
| 60 | + |
| 61 | + |
| 62 | +class Test_FatalError(unittest.TestCase): |
| 63 | + |
| 64 | + def check_fatal_error(self, code, expected, not_expected=()): |
| 65 | + with support.SuppressCrashReport(): |
| 66 | + rc, out, err = assert_python_failure('-sSI', '-c', code) |
| 67 | + |
| 68 | + err = decode_stderr(err) |
| 69 | + self.assertIn('Fatal Python error: test_fatal_error: MESSAGE\n', |
| 70 | + err) |
| 71 | + |
| 72 | + match = re.search(r'^Extension modules:(.*) \(total: ([0-9]+)\)$', |
| 73 | + err, re.MULTILINE) |
| 74 | + if not match: |
| 75 | + self.fail(f"Cannot find 'Extension modules:' in {err!r}") |
| 76 | + modules = set(match.group(1).strip().split(', ')) |
| 77 | + total = int(match.group(2)) |
| 78 | + |
| 79 | + for name in expected: |
| 80 | + self.assertIn(name, modules) |
| 81 | + for name in not_expected: |
| 82 | + self.assertNotIn(name, modules) |
| 83 | + self.assertEqual(len(modules), total) |
| 84 | + |
| 85 | + @support.requires_subprocess() |
| 86 | + def test_fatal_error(self): |
| 87 | + # By default, stdlib extension modules are ignored, |
| 88 | + # but not test modules. |
| 89 | + expected = ('_testcapi',) |
| 90 | + not_expected = ('sys',) |
| 91 | + code = 'import _testcapi, sys; _testcapi.fatal_error(b"MESSAGE")' |
| 92 | + self.check_fatal_error(code, expected, not_expected) |
| 93 | + |
| 94 | + # Mark _testcapi as stdlib module, but not sys |
| 95 | + expected = ('sys',) |
| 96 | + not_expected = ('_testcapi',) |
| 97 | + code = """if True: |
| 98 | + import _testcapi, sys |
| 99 | + sys.stdlib_module_names = frozenset({"_testcapi"}) |
| 100 | + _testcapi.fatal_error(b"MESSAGE") |
| 101 | + """ |
| 102 | + self.check_fatal_error(code, expected) |
| 103 | + |
| 104 | + |
| 105 | +class Test_ErrSetAndRestore(unittest.TestCase): |
| 106 | + |
| 107 | + def test_err_set_raised(self): |
| 108 | + with self.assertRaises(ValueError): |
| 109 | + _testcapi.err_set_raised(ValueError()) |
| 110 | + v = ValueError() |
| 111 | + try: |
| 112 | + _testcapi.err_set_raised(v) |
| 113 | + except ValueError as ex: |
| 114 | + self.assertIs(v, ex) |
| 115 | + |
| 116 | + def test_err_restore(self): |
| 117 | + with self.assertRaises(ValueError): |
| 118 | + _testcapi.err_restore(ValueError) |
| 119 | + with self.assertRaises(ValueError): |
| 120 | + _testcapi.err_restore(ValueError, 1) |
| 121 | + with self.assertRaises(ValueError): |
| 122 | + _testcapi.err_restore(ValueError, 1, None) |
| 123 | + with self.assertRaises(ValueError): |
| 124 | + _testcapi.err_restore(ValueError, ValueError()) |
| 125 | + try: |
| 126 | + _testcapi.err_restore(KeyError, "hi") |
| 127 | + except KeyError as k: |
| 128 | + self.assertEqual("hi", k.args[0]) |
| 129 | + try: |
| 130 | + 1/0 |
| 131 | + except Exception as e: |
| 132 | + tb = e.__traceback__ |
| 133 | + with self.assertRaises(ValueError): |
| 134 | + _testcapi.err_restore(ValueError, 1, tb) |
| 135 | + with self.assertRaises(TypeError): |
| 136 | + _testcapi.err_restore(ValueError, 1, 0) |
| 137 | + try: |
| 138 | + _testcapi.err_restore(ValueError, 1, tb) |
| 139 | + except ValueError as v: |
| 140 | + self.assertEqual(1, v.args[0]) |
| 141 | + self.assertIs(tb, v.__traceback__.tb_next) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + unittest.main() |
0 commit comments