Debian Codegraph @

Lib/test/test_capi/test_config.py

raw
1 """
2 Tests PyConfig_Get() and PyConfig_Set() C API (PEP 741).
3 """
4 import os
5 import sys
6 import sysconfig
7 import types
8 import unittest
9 from test import support
10 from test.support import import_helper
11
12 _testcapi = import_helper.import_module('_testcapi')
13
14
15 # Is the Py_STATS macro defined?
16 Py_STATS = hasattr(sys, '_stats_on')
17
18
19 class CAPITests(unittest.TestCase):
20 def test_config_get(self):
21 # Test PyConfig_Get()
22 config_get = _testcapi.config_get
23 config_names = _testcapi.config_names
24
25 TEST_VALUE = {
26 str: "TEST_MARKER_STR",
27 str | None: "TEST_MARKER_OPT_STR",
28 list[str]: ("TEST_MARKER_STR_TUPLE",),
29 dict[str, str | bool]: {"x": "value", "y": True},
30 }
31
32 # read config options and check their type
33 options = [
34 ("allocator", int, None),
35 ("argv", list[str], "argv"),
36 ("base_exec_prefix", str | None, "base_exec_prefix"),
37 ("base_executable", str | None, "_base_executable"),
38 ("base_prefix", str | None, "base_prefix"),
39 ("buffered_stdio", bool, None),
40 ("bytes_warning", int, None),
41 ("check_hash_pycs_mode", str, None),
42 ("code_debug_ranges", bool, None),
43 ("configure_c_stdio", bool, None),
44 ("coerce_c_locale", bool, None),
45 ("coerce_c_locale_warn", bool, None),
46 ("configure_locale", bool, None),
47 ("cpu_count", int, None),
48 ("dev_mode", bool, None),
49 ("dump_refs", bool, None),
50 ("dump_refs_file", str | None, None),
51 ("exec_prefix", str | None, "exec_prefix"),
52 ("executable", str | None, "executable"),
53 ("faulthandler", bool, None),
54 ("filesystem_encoding", str, None),
55 ("filesystem_errors", str, None),
56 ("hash_seed", int, None),
57 ("home", str | None, None),
58 ("thread_inherit_context", int, None),
59 ("context_aware_warnings", int, None),
60 ("import_time", int, None),
61 ("inspect", bool, None),
62 ("install_signal_handlers", bool, None),
63 ("int_max_str_digits", int, None),
64 ("interactive", bool, None),
65 ("isolated", bool, None),
66 ("malloc_stats", bool, None),
67 ("module_search_paths", list[str], "path"),
68 ("optimization_level", int, None),
69 ("orig_argv", list[str], "orig_argv"),
70 ("parser_debug", bool, None),
71 ("parse_argv", bool, None),
72 ("pathconfig_warnings", bool, None),
73 ("perf_profiling", int, None),
74 ("platlibdir", str, "platlibdir"),
75 ("prefix", str | None, "prefix"),
76 ("program_name", str, None),
77 ("pycache_prefix", str | None, "pycache_prefix"),
78 ("quiet", bool, None),
79 ("remote_debug", int, None),
80 ("run_command", str | None, None),
81 ("run_filename", str | None, None),
82 ("run_module", str | None, None),
83 ("safe_path", bool, None),
84 ("show_ref_count", bool, None),
85 ("site_import", bool, None),
86 ("skip_source_first_line", bool, None),
87 ("stdio_encoding", str, None),
88 ("stdio_errors", str, None),
89 ("stdlib_dir", str | None, "_stdlib_dir"),
90 ("tracemalloc", int, None),
91 ("use_environment", bool, None),
92 ("use_frozen_modules", bool, None),
93 ("use_hash_seed", bool, None),
94 ("user_site_directory", bool, None),
95 ("utf8_mode", bool, None),
96 ("verbose", int, None),
97 ("warn_default_encoding", bool, None),
98 ("warnoptions", list[str], "warnoptions"),
99 ("write_bytecode", bool, None),
100 ("xoptions", dict[str, str | bool], "_xoptions"),
101 ]
102 if support.Py_DEBUG:
103 options.append(("run_presite", str | None, None))
104 if support.Py_GIL_DISABLED:
105 options.append(("enable_gil", int, None))
106 options.append(("tlbc_enabled", int, None))
107 if support.MS_WINDOWS:
108 options.extend((
109 ("legacy_windows_stdio", bool, None),
110 ("legacy_windows_fs_encoding", bool, None),
111 ))
112 if Py_STATS:
113 options.extend((
114 ("_pystats", bool, None),
115 ))
116 if support.is_apple:
117 options.extend((
118 ("use_system_logger", bool, None),
119 ))
120
121 for name, option_type, sys_attr in options:
122 with self.subTest(name=name, option_type=option_type,
123 sys_attr=sys_attr):
124 value = config_get(name)
125 if isinstance(option_type, types.GenericAlias):
126 self.assertIsInstance(value, option_type.__origin__)
127 if option_type.__origin__ == dict:
128 key_type = option_type.__args__[0]
129 value_type = option_type.__args__[1]
130 for item in value.items():
131 self.assertIsInstance(item[0], key_type)
132 self.assertIsInstance(item[1], value_type)
133 else:
134 item_type = option_type.__args__[0]
135 for item in value:
136 self.assertIsInstance(item, item_type)
137 else:
138 self.assertIsInstance(value, option_type)
139
140 if sys_attr is not None:
141 expected = getattr(sys, sys_attr)
142 self.assertEqual(expected, value)
143
144 override = TEST_VALUE[option_type]
145 with support.swap_attr(sys, sys_attr, override):
146 self.assertEqual(config_get(name), override)
147
148 # check that the test checks all options
149 self.assertEqual(sorted(name for name, option_type, sys_attr in options),
150 sorted(config_names()))
151
152 def test_config_get_sys_flags(self):
153 # Test PyConfig_Get()
154 config_get = _testcapi.config_get
155
156 # compare config options with sys.flags
157 for flag, name, negate in (
158 ("debug", "parser_debug", False),
159 ("inspect", "inspect", False),
160 ("interactive", "interactive", False),
161 ("optimize", "optimization_level", False),
162 ("dont_write_bytecode", "write_bytecode", True),
163 ("no_user_site", "user_site_directory", True),
164 ("no_site", "site_import", True),
165 ("ignore_environment", "use_environment", True),
166 ("verbose", "verbose", False),
167 ("bytes_warning", "bytes_warning", False),
168 ("quiet", "quiet", False),
169 # "hash_randomization" is tested below
170 ("isolated", "isolated", False),
171 ("dev_mode", "dev_mode", False),
172 ("utf8_mode", "utf8_mode", False),
173 ("warn_default_encoding", "warn_default_encoding", False),
174 ("safe_path", "safe_path", False),
175 ("int_max_str_digits", "int_max_str_digits", False),
176 # "gil", "thread_inherit_context" and "context_aware_warnings" are tested below
177 ):
178 with self.subTest(flag=flag, name=name, negate=negate):
179 value = config_get(name)
180 if negate:
181 value = not value
182 self.assertEqual(getattr(sys.flags, flag), value)
183
184 self.assertEqual(sys.flags.hash_randomization,
185 config_get('use_hash_seed') == 0
186 or config_get('hash_seed') != 0)
187
188 if support.Py_GIL_DISABLED:
189 value = config_get('enable_gil')
190 expected = (value if value != -1 else None)
191 self.assertEqual(sys.flags.gil, expected)
192
193 expected_inherit_context = 1 if support.Py_GIL_DISABLED else 0
194 self.assertEqual(sys.flags.thread_inherit_context, expected_inherit_context)
195
196 expected_safe_warnings = 1 if support.Py_GIL_DISABLED else 0
197 self.assertEqual(sys.flags.context_aware_warnings, expected_safe_warnings)
198
199 def test_config_get_non_existent(self):
200 # Test PyConfig_Get() on non-existent option name
201 config_get = _testcapi.config_get
202 nonexistent_key = 'NONEXISTENT_KEY'
203 err_msg = f'unknown config option name: {nonexistent_key}'
204 with self.assertRaisesRegex(ValueError, err_msg):
205 config_get(nonexistent_key)
206
207 def test_config_get_write_bytecode(self):
208 # PyConfig_Get("write_bytecode") gets sys.dont_write_bytecode
209 # as an integer
210 config_get = _testcapi.config_get
211 with support.swap_attr(sys, "dont_write_bytecode", 0):
212 self.assertEqual(config_get('write_bytecode'), 1)
213 with support.swap_attr(sys, "dont_write_bytecode", "yes"):
214 self.assertEqual(config_get('write_bytecode'), 0)
215 with support.swap_attr(sys, "dont_write_bytecode", []):
216 self.assertEqual(config_get('write_bytecode'), 1)
217
218 def test_config_getint(self):
219 # Test PyConfig_GetInt()
220 config_getint = _testcapi.config_getint
221
222 # PyConfig_MEMBER_INT type
223 self.assertEqual(config_getint('verbose'), sys.flags.verbose)
224
225 # PyConfig_MEMBER_UINT type
226 self.assertEqual(config_getint('isolated'), sys.flags.isolated)
227
228 # PyConfig_MEMBER_ULONG type
229 self.assertIsInstance(config_getint('hash_seed'), int)
230
231 # PyPreConfig member
232 self.assertIsInstance(config_getint('allocator'), int)
233
234 # platlibdir type is str
235 with self.assertRaises(TypeError):
236 config_getint('platlibdir')
237
238 def test_get_config_names(self):
239 names = _testcapi.config_names()
240 self.assertIsInstance(names, frozenset)
241 for name in names:
242 self.assertIsInstance(name, str)
243
244 def test_config_set_sys_attr(self):
245 # Test PyConfig_Set() with sys attributes
246 config_get = _testcapi.config_get
247 config_set = _testcapi.config_set
248
249 # mutable configuration option mapped to sys attributes
250 for name, sys_attr, option_type in (
251 ('argv', 'argv', list[str]),
252 ('base_exec_prefix', 'base_exec_prefix', str | None),
253 ('base_executable', '_base_executable', str | None),
254 ('base_prefix', 'base_prefix', str | None),
255 ('exec_prefix', 'exec_prefix', str | None),
256 ('executable', 'executable', str | None),
257 ('module_search_paths', 'path', list[str]),
258 ('platlibdir', 'platlibdir', str),
259 ('prefix', 'prefix', str | None),
260 ('pycache_prefix', 'pycache_prefix', str | None),
261 ('stdlib_dir', '_stdlib_dir', str | None),
262 ('warnoptions', 'warnoptions', list[str]),
263 ('xoptions', '_xoptions', dict[str, str | bool]),
264 ):
265 with self.subTest(name=name):
266 if option_type == str:
267 test_values = ('TEST_REPLACE',)
268 invalid_types = (1, None)
269 elif option_type == str | None:
270 test_values = ('TEST_REPLACE', None)
271 invalid_types = (123,)
272 elif option_type == list[str]:
273 test_values = (['TEST_REPLACE'], [])
274 invalid_types = ('text', 123, [123])
275 else: # option_type == dict[str, str | bool]:
276 test_values = ({"x": "value", "y": True},)
277 invalid_types = ('text', 123, ['option'],
278 {123: 'value'},
279 {'key': b'bytes'})
280
281 old_opt_value = config_get(name)
282 old_sys_value = getattr(sys, sys_attr)
283 try:
284 for value in test_values:
285 config_set(name, value)
286 self.assertEqual(config_get(name), value)
287 self.assertEqual(getattr(sys, sys_attr), value)
288
289 for value in invalid_types:
290 with self.assertRaises(TypeError):
291 config_set(name, value)
292 finally:
293 setattr(sys, sys_attr, old_sys_value)
294 config_set(name, old_opt_value)
295
296 def test_config_set_sys_flag(self):
297 # Test PyConfig_Set() with sys.flags
298 config_get = _testcapi.config_get
299 config_set = _testcapi.config_set
300
301 # mutable configuration option mapped to sys.flags
302 class unsigned_int(int):
303 pass
304
305 def expect_int(value):
306 value = int(value)
307 return (value, value)
308
309 def expect_bool(value):
310 value = int(bool(value))
311 return (value, value)
312
313 def expect_bool_not(value):
314 value = bool(value)
315 return (int(value), int(not value))
316
317 for name, sys_flag, option_type, expect_func in (
318 # (some flags cannot be set, see comments below.)
319 ('parser_debug', 'debug', bool, expect_bool),
320 ('inspect', 'inspect', bool, expect_bool),
321 ('interactive', 'interactive', bool, expect_bool),
322 ('optimization_level', 'optimize', unsigned_int, expect_int),
323 ('write_bytecode', 'dont_write_bytecode', bool, expect_bool_not),
324 # user_site_directory
325 # site_import
326 ('use_environment', 'ignore_environment', bool, expect_bool_not),
327 ('verbose', 'verbose', unsigned_int, expect_int),
328 ('bytes_warning', 'bytes_warning', unsigned_int, expect_int),
329 ('quiet', 'quiet', bool, expect_bool),
330 # hash_randomization
331 # isolated
332 # dev_mode
333 # utf8_mode
334 # warn_default_encoding
335 # safe_path
336 ('int_max_str_digits', 'int_max_str_digits', unsigned_int, expect_int),
337 # gil
338 ):
339 if name == "int_max_str_digits":
340 new_values = (0, 5_000, 999_999)
341 invalid_values = (-1, 40) # value must 0 or >= 4300
342 invalid_types = (1.0, "abc")
343 elif option_type == int:
344 new_values = (False, True, 0, 1, 5, -5)
345 invalid_values = ()
346 invalid_types = (1.0, "abc")
347 else:
348 new_values = (False, True, 0, 1, 5)
349 invalid_values = (-5,)
350 invalid_types = (1.0, "abc")
351
352 with self.subTest(name=name):
353 old_value = config_get(name)
354 try:
355 for value in new_values:
356 expected, expect_flag = expect_func(value)
357
358 config_set(name, value)
359 self.assertEqual(config_get(name), expected)
360 self.assertEqual(getattr(sys.flags, sys_flag), expect_flag)
361 if name == "write_bytecode":
362 self.assertEqual(getattr(sys, "dont_write_bytecode"),
363 expect_flag)
364 if name == "int_max_str_digits":
365 self.assertEqual(sys.get_int_max_str_digits(),
366 expect_flag)
367
368 for value in invalid_values:
369 with self.assertRaises(ValueError):
370 config_set(name, value)
371
372 for value in invalid_types:
373 with self.assertRaises(TypeError):
374 config_set(name, value)
375 finally:
376 config_set(name, old_value)
377
378 def test_config_set_cpu_count(self):
379 config_get = _testcapi.config_get
380 config_set = _testcapi.config_set
381
382 old_value = config_get('cpu_count')
383 try:
384 config_set('cpu_count', 123)
385 self.assertEqual(os.cpu_count(), 123)
386 finally:
387 config_set('cpu_count', old_value)
388
389 def test_config_set_read_only(self):
390 # Test PyConfig_Set() on read-only options
391 config_set = _testcapi.config_set
392 for name, value in (
393 ("allocator", 0), # PyPreConfig member
394 ("perf_profiling", 8),
395 ("dev_mode", True),
396 ("filesystem_encoding", "utf-8"),
397 ):
398 with self.subTest(name=name, value=value):
399 with self.assertRaisesRegex(ValueError, r"read-only"):
400 config_set(name, value)
401
402
403 if __name__ == "__main__":
404 unittest.main()