|
| 1 | +import sys |
| 2 | +import unittest |
| 3 | +import sysconfig |
| 4 | + |
| 5 | +from test.support import subTests |
| 6 | +from test.support import import_helper |
| 7 | + |
| 8 | +_testcapi = import_helper.import_module('_testcapi') |
| 9 | + |
| 10 | + |
| 11 | +class Test_ABIInfo_Check(unittest.TestCase): |
| 12 | + @subTests('modname', (None, 'test_mod')) |
| 13 | + def test_zero(self, modname): |
| 14 | + _testcapi.pyabiinfo_check(modname, 0, 0, 0, 0, 0) |
| 15 | + _testcapi.pyabiinfo_check(modname, 1, 0, 0, 0, 0) |
| 16 | + |
| 17 | + def test_large_major_version(self): |
| 18 | + with self.assertRaisesRegex(ImportError, |
| 19 | + '^PyABIInfo version too high$'): |
| 20 | + _testcapi.pyabiinfo_check(None, 2, 0, 0, 0, 0) |
| 21 | + with self.assertRaisesRegex(ImportError, |
| 22 | + '^test_mod: PyABIInfo version too high$'): |
| 23 | + _testcapi.pyabiinfo_check("test_mod", 2, 0, 0, 0, 0) |
| 24 | + |
| 25 | + @subTests('modname', (None, 'test_mod')) |
| 26 | + def test_large_minor_version(self, modname): |
| 27 | + _testcapi.pyabiinfo_check(modname, 1, 2, 0, 0, 0) |
| 28 | + |
| 29 | + @subTests('modname', (None, 'test_mod')) |
| 30 | + @subTests('major', (0, 1)) |
| 31 | + @subTests('minor', (0, 1, 9)) |
| 32 | + @subTests('build', (0, sys.hexversion)) |
| 33 | + def test_positive_regular(self, modname, major, minor, build): |
| 34 | + ver = sys.hexversion |
| 35 | + truncated = ver & 0xffff0000 |
| 36 | + filled = truncated | 0x12b8 |
| 37 | + maxed = truncated | 0xffff |
| 38 | + for abi_version in (0, ver, truncated, filled, maxed): |
| 39 | + with self.subTest(abi_version=abi_version): |
| 40 | + _testcapi.pyabiinfo_check(modname, major, minor, 0, |
| 41 | + build, abi_version) |
| 42 | + |
| 43 | + @subTests('modname', (None, 'test_mod')) |
| 44 | + @subTests('minor', (0, 1, 9)) |
| 45 | + @subTests('build', (0, sys.hexversion)) |
| 46 | + @subTests('offset', (+0x00010000, -0x00010000)) |
| 47 | + def test_negative_regular(self, modname, minor, build, offset): |
| 48 | + ver = sys.hexversion + offset |
| 49 | + truncated = ver & 0xffff0000 |
| 50 | + filled = truncated | 0x12b8 |
| 51 | + maxed = truncated | 0xffff |
| 52 | + for abi_version in (ver, truncated, filled, maxed): |
| 53 | + with self.subTest(abi_version=abi_version): |
| 54 | + with self.assertRaisesRegex( |
| 55 | + ImportError, |
| 56 | + r'incompatible ABI version \(3\.\d+\)$'): |
| 57 | + _testcapi.pyabiinfo_check(modname, 1, minor, 0, |
| 58 | + build, |
| 59 | + abi_version) |
| 60 | + |
| 61 | + @subTests('modname', (None, 'test_mod')) |
| 62 | + @subTests('major', (0, 1)) |
| 63 | + @subTests('minor', (0, 1, 9)) |
| 64 | + @subTests('build', (0, sys.hexversion)) |
| 65 | + @subTests('abi_version', ( |
| 66 | + 0, |
| 67 | + 0x03020000, |
| 68 | + sys.hexversion, |
| 69 | + sys.hexversion & 0xffff0000, |
| 70 | + sys.hexversion - 0x00010000, |
| 71 | + )) |
| 72 | + def test_positive_stable(self, modname, major, minor, build, abi_version): |
| 73 | + _testcapi.pyabiinfo_check(modname, major, minor, |
| 74 | + _testcapi.PyABIInfo_STABLE, |
| 75 | + build, |
| 76 | + abi_version) |
| 77 | + |
| 78 | + @subTests('modname', (None, 'test_mod')) |
| 79 | + @subTests('minor', (0, 1, 9)) |
| 80 | + @subTests('build', (0, sys.hexversion)) |
| 81 | + @subTests('abi_version_and_msg', ( |
| 82 | + (1, 'invalid'), |
| 83 | + (3, 'invalid'), |
| 84 | + (0x0301ffff, 'invalid'), |
| 85 | + ((sys.hexversion & 0xffff0000) + 0x00010000, 'incompatible future'), |
| 86 | + (sys.hexversion + 0x00010000, 'incompatible future'), |
| 87 | + (0x04000000, 'incompatible future'), |
| 88 | + )) |
| 89 | + def test_negative_stable(self, modname, minor, build, abi_version_and_msg): |
| 90 | + abi_version, msg = abi_version_and_msg |
| 91 | + with self.assertRaisesRegex( |
| 92 | + ImportError, |
| 93 | + rf'{msg} stable ABI version \(\d+\.\d+\)$'): |
| 94 | + _testcapi.pyabiinfo_check(modname, 1, minor, |
| 95 | + _testcapi.PyABIInfo_STABLE, |
| 96 | + build, |
| 97 | + abi_version) |
| 98 | + |
| 99 | + @subTests('modname', (None, 'test_mod')) |
| 100 | + @subTests('major', (0, 1)) |
| 101 | + @subTests('minor', (0, 1, 9)) |
| 102 | + @subTests('build', (0, sys.hexversion)) |
| 103 | + @subTests('abi_version', (0, sys.hexversion)) |
| 104 | + def test_positive_internal(self, modname, major, minor, build, abi_version): |
| 105 | + _testcapi.pyabiinfo_check(modname, major, minor, |
| 106 | + _testcapi.PyABIInfo_INTERNAL, |
| 107 | + build, |
| 108 | + abi_version) |
| 109 | + |
| 110 | + @subTests('modname', (None, 'test_mod')) |
| 111 | + @subTests('minor', (0, 1, 9)) |
| 112 | + @subTests('build', (0, sys.hexversion)) |
| 113 | + @subTests('abi_version', ( |
| 114 | + sys.hexversion - 0x00010000, |
| 115 | + sys.hexversion - 1, |
| 116 | + sys.hexversion + 1, |
| 117 | + sys.hexversion + 0x00010000, |
| 118 | + )) |
| 119 | + def test_negative_internal(self, modname, minor, build, abi_version): |
| 120 | + with self.assertRaisesRegex( |
| 121 | + ImportError, |
| 122 | + r'incompatible internal ABI \(0x[\da-f]+ != 0x[\da-f]+\)$'): |
| 123 | + _testcapi.pyabiinfo_check(modname, 1, minor, |
| 124 | + _testcapi.PyABIInfo_INTERNAL, |
| 125 | + build, |
| 126 | + abi_version) |
| 127 | + |
| 128 | + @subTests('modname', (None, 'test_mod')) |
| 129 | + @subTests('minor', (0, 1, 9)) |
| 130 | + @subTests('build', (0, sys.hexversion)) |
| 131 | + @subTests('abi_version', ( |
| 132 | + sys.hexversion - 0x00010000, |
| 133 | + sys.hexversion - 1, |
| 134 | + sys.hexversion + 1, |
| 135 | + sys.hexversion + 0x00010000, |
| 136 | + )) |
| 137 | + def test_negative_internal(self, modname, minor, build, abi_version): |
| 138 | + with self.assertRaisesRegex( |
| 139 | + ImportError, |
| 140 | + r'incompatible internal ABI \(0x[\da-f]+ != 0x[\da-f]+\)$'): |
| 141 | + _testcapi.pyabiinfo_check(modname, 1, minor, |
| 142 | + _testcapi.PyABIInfo_INTERNAL, |
| 143 | + build, |
| 144 | + abi_version) |
| 145 | + |
| 146 | + @subTests('modname', (None, 'test_mod')) |
| 147 | + @subTests('minor', (0, 1, 9)) |
| 148 | + @subTests('build', (0, sys.hexversion)) |
| 149 | + @subTests('ft_flag', ( |
| 150 | + 0, |
| 151 | + (_testcapi.PyABIInfo_FREETHREADED |
| 152 | + if sysconfig.get_config_var("Py_GIL_DISABLED") |
| 153 | + else _testcapi.PyABIInfo_GIL), |
| 154 | + _testcapi.PyABIInfo_FREETHREADING_AGNOSTIC, |
| 155 | + )) |
| 156 | + def test_positive_freethreading(self, modname, minor, build, ft_flag): |
| 157 | + self.assertEqual(ft_flag & _testcapi.PyABIInfo_FREETHREADING_AGNOSTIC, |
| 158 | + ft_flag) |
| 159 | + _testcapi.pyabiinfo_check(modname, 1, minor, ft_flag, build, 0) |
| 160 | + |
| 161 | + @subTests('modname', (None, 'test_mod')) |
| 162 | + @subTests('minor', (0, 1, 9)) |
| 163 | + @subTests('build', (0, sys.hexversion)) |
| 164 | + def test_negative_freethreading(self, modname, minor, build): |
| 165 | + if sysconfig.get_config_var("Py_GIL_DISABLED"): |
| 166 | + ft_flag = _testcapi.PyABIInfo_GIL |
| 167 | + msg = "incompatible with free-threaded CPython" |
| 168 | + else: |
| 169 | + ft_flag = _testcapi.PyABIInfo_FREETHREADED |
| 170 | + msg = "only compatible with free-threaded CPython" |
| 171 | + with self.assertRaisesRegex(ImportError, msg): |
| 172 | + _testcapi.pyabiinfo_check(modname, 1, minor, ft_flag, build, 0) |
0 commit comments