|
| 1 | +""" |
| 2 | +Tests on PyConfig API (PEP 587). |
| 3 | +""" |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | +from test import support |
| 8 | +try: |
| 9 | + import _testcapi |
| 10 | +except ImportError: |
| 11 | + _testcapi = None |
| 12 | + |
| 13 | + |
| 14 | +@unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 15 | +class CAPITests(unittest.TestCase): |
| 16 | + def test_config_get(self): |
| 17 | + config_get = _testcapi.config_get |
| 18 | + |
| 19 | + # PyConfig_MEMBER_WSTR type |
| 20 | + self.assertEqual(config_get('platlibdir'), sys.platlibdir) |
| 21 | + |
| 22 | + if 'PYTHONDUMPREFSFILE' not in os.environ: |
| 23 | + # PyConfig_MEMBER_WSTR_OPT type |
| 24 | + self.assertIsNone(config_get('dump_refs_file')) |
| 25 | + |
| 26 | + # PyConfig_MEMBER_WSTR_LIST type |
| 27 | + self.assertEqual(config_get('argv'), sys.argv) |
| 28 | + |
| 29 | + # attributes read from sys |
| 30 | + value = "TEST_MARKER_STR" |
| 31 | + for name in ( |
| 32 | + "base_exec_prefix", |
| 33 | + "base_prefix", |
| 34 | + "exec_prefix", |
| 35 | + "executable", |
| 36 | + "platlibdir", |
| 37 | + "prefix", |
| 38 | + "pycache_prefix", |
| 39 | + ): |
| 40 | + with self.subTest(name=name): |
| 41 | + with support.swap_attr(sys, name, value): |
| 42 | + self.assertEqual(config_get(name), value) |
| 43 | + |
| 44 | + # attributes read from sys with a different name |
| 45 | + # (add underscore prefix) |
| 46 | + for config_name, sys_name in ( |
| 47 | + ("base_executable", "_base_executable"), |
| 48 | + ("stdlib_dir", "_stdlib_dir"), |
| 49 | + ): |
| 50 | + with self.subTest(config_name=config_name, sys_name=sys_name): |
| 51 | + with support.swap_attr(sys, sys_name, value): |
| 52 | + self.assertEqual(config_get(config_name), value) |
| 53 | + |
| 54 | + # attributes read from sys |
| 55 | + value = ["TEST_MARKER_STRLIST"] |
| 56 | + for name in ( |
| 57 | + "argv", |
| 58 | + "orig_argv", |
| 59 | + "warnoptions", |
| 60 | + ): |
| 61 | + with self.subTest(name=name): |
| 62 | + with support.swap_attr(sys, name, value): |
| 63 | + self.assertEqual(config_get(name), value) |
| 64 | + |
| 65 | + with support.swap_attr(sys, "path", value): |
| 66 | + self.assertEqual(config_get("module_search_paths"), value) |
| 67 | + |
| 68 | + # sys.xoptions should be a dictionary, but config_get() returns |
| 69 | + # any sys.xoptions value. |
| 70 | + for value in ( |
| 71 | + {"x": "value", "y": True}, |
| 72 | + "not_a_dict", |
| 73 | + ): |
| 74 | + with self.subTest(value=value): |
| 75 | + with support.swap_attr(sys, "_xoptions", value): |
| 76 | + self.assertEqual(config_get("xoptions"), value) |
| 77 | + |
| 78 | + # write_bytecode is read from sys.dont_write_bytecode as int |
| 79 | + with support.swap_attr(sys, "dont_write_bytecode", 0): |
| 80 | + self.assertEqual(config_get('write_bytecode'), 1) |
| 81 | + with support.swap_attr(sys, "dont_write_bytecode", "yes"): |
| 82 | + self.assertEqual(config_get('write_bytecode'), 0) |
| 83 | + with support.swap_attr(sys, "dont_write_bytecode", []): |
| 84 | + self.assertEqual(config_get('write_bytecode'), 1) |
| 85 | + |
| 86 | + def test_config_getint(self): |
| 87 | + config_getint = _testcapi.config_getint |
| 88 | + # PyConfig_MEMBER_INT type |
| 89 | + self.assertEqual(config_getint('verbose'), sys.flags.verbose) |
| 90 | + |
| 91 | + # PyConfig_MEMBER_UINT type |
| 92 | + self.assertEqual(config_getint('isolated'), sys.flags.isolated) |
| 93 | + |
| 94 | + # PyConfig_MEMBER_ULONG type |
| 95 | + hash_seed = config_getint('hash_seed') |
| 96 | + self.assertIsInstance(hash_seed, int) |
| 97 | + self.assertGreaterEqual(hash_seed, 0) |
| 98 | + |
| 99 | + # platlibdir is a str |
| 100 | + with self.assertRaises(TypeError): |
| 101 | + config_getint('platlibdir') |
| 102 | + |
| 103 | + NONEXISTENT_KEY = 'NONEXISTENT_KEY' |
| 104 | + err_msg = f'unknown config option name: {NONEXISTENT_KEY}' |
| 105 | + with self.assertRaisesRegex(ValueError, err_msg): |
| 106 | + config_getint('NONEXISTENT_KEY') |
| 107 | + |
| 108 | + # write_bytecode is read from sys.dont_write_bytecode |
| 109 | + with support.swap_attr(sys, "dont_write_bytecode", 0): |
| 110 | + self.assertEqual(config_getint('write_bytecode'), 1) |
| 111 | + with support.swap_attr(sys, "dont_write_bytecode", "yes"): |
| 112 | + self.assertEqual(config_getint('write_bytecode'), 0) |
| 113 | + with support.swap_attr(sys, "dont_write_bytecode", []): |
| 114 | + self.assertEqual(config_getint('write_bytecode'), 1) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + unittest.main() |
0 commit comments