Debian Codegraph @

Lib/test/test_embed.py

raw
1 # Run the tests in Programs/_testembed.c (tests for the CPython embedding APIs)
2 from test import support
3 from test.support import import_helper, os_helper, threading_helper, MS_WINDOWS
4 import unittest
5
6 from collections import namedtuple
7 import contextlib
8 import json
9 import os
10 import os.path
11 import re
12 import shutil
13 import subprocess
14 import sys
15 import sysconfig
16 import tempfile
17 import textwrap
18
19 if not support.has_subprocess_support:
20 raise unittest.SkipTest("test module requires subprocess")
21
22
23 try:
24 import _testinternalcapi
25 except ImportError:
26 _testinternalcapi = None
27
28
29 MACOS = (sys.platform == 'darwin')
30 PYMEM_ALLOCATOR_NOT_SET = 0
31 PYMEM_ALLOCATOR_DEBUG = 2
32 PYMEM_ALLOCATOR_MALLOC = 3
33 PYMEM_ALLOCATOR_MIMALLOC = 7
34 if support.Py_GIL_DISABLED:
35 ALLOCATOR_FOR_CONFIG = PYMEM_ALLOCATOR_MIMALLOC
36 else:
37 ALLOCATOR_FOR_CONFIG = PYMEM_ALLOCATOR_MALLOC
38
39 Py_STATS = hasattr(sys, '_stats_on')
40
41 # _PyCoreConfig_InitCompatConfig()
42 API_COMPAT = 1
43 # _PyCoreConfig_InitPythonConfig()
44 API_PYTHON = 2
45 # _PyCoreConfig_InitIsolatedConfig()
46 API_ISOLATED = 3
47
48 INIT_LOOPS = 4
49 MAX_HASH_SEED = 4294967295
50
51 ABI_THREAD = 't' if support.Py_GIL_DISABLED else ''
52 # PLATSTDLIB_LANDMARK copied from Modules/getpath.py
53 if os.name == 'nt':
54 PLATSTDLIB_LANDMARK = f'{sys.platlibdir}'
55 else:
56 VERSION_MAJOR = sys.version_info.major
57 VERSION_MINOR = sys.version_info.minor
58 PLATSTDLIB_LANDMARK = (f'{sys.platlibdir}/python{VERSION_MAJOR}.'
59 f'{VERSION_MINOR}{ABI_THREAD}/lib-dynload')
60
61 DEFAULT_THREAD_INHERIT_CONTEXT = 1 if support.Py_GIL_DISABLED else 0
62 DEFAULT_CONTEXT_AWARE_WARNINGS = 1 if support.Py_GIL_DISABLED else 0
63
64 # If we are running from a build dir, but the stdlib has been installed,
65 # some tests need to expect different results.
66 STDLIB_INSTALL = os.path.join(sys.prefix, sys.platlibdir,
67 f'python{sys.version_info.major}.{sys.version_info.minor}')
68 if not os.path.isfile(os.path.join(STDLIB_INSTALL, 'os.py')):
69 STDLIB_INSTALL = None
70
71 def debug_build(program):
72 program = os.path.basename(program)
73 name = os.path.splitext(program)[0]
74 return name.casefold().endswith("_d".casefold())
75
76
77 def remove_python_envvars():
78 env = dict(os.environ)
79 # Remove PYTHON* environment variables to get deterministic environment
80 for key in list(env):
81 if key.startswith('PYTHON'):
82 del env[key]
83 return env
84
85
86 class EmbeddingTestsMixin:
87 def setUp(self):
88 exename = "_testembed"
89 builddir = os.path.dirname(sys.executable)
90 if MS_WINDOWS:
91 ext = ("_d" if debug_build(sys.executable) else "") + ".exe"
92 exename += ext
93 exepath = builddir
94 else:
95 exepath = os.path.join(builddir, 'Programs')
96 self.test_exe = exe = os.path.join(exepath, exename)
97 if not os.path.exists(exe):
98 self.skipTest("%r doesn't exist" % exe)
99 # This is needed otherwise we get a fatal error:
100 # "Py_Initialize: Unable to get the locale encoding
101 # LookupError: no codec search functions registered: can't find encoding"
102 self.oldcwd = os.getcwd()
103 os.chdir(builddir)
104
105 def tearDown(self):
106 os.chdir(self.oldcwd)
107
108 def run_embedded_interpreter(self, *args, env=None,
109 timeout=None, returncode=0, input=None,
110 cwd=None):
111 """Runs a test in the embedded interpreter"""
112 cmd = [self.test_exe]
113 cmd.extend(args)
114 if env is not None and MS_WINDOWS:
115 # Windows requires at least the SYSTEMROOT environment variable to
116 # start Python.
117 env = env.copy()
118 env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
119
120 p = subprocess.Popen(cmd,
121 stdout=subprocess.PIPE,
122 stderr=subprocess.PIPE,
123 universal_newlines=True,
124 env=env,
125 cwd=cwd)
126 try:
127 (out, err) = p.communicate(input=input, timeout=timeout)
128 except:
129 p.terminate()
130 p.wait()
131 raise
132 if p.returncode != returncode and support.verbose:
133 print(f"--- {cmd} failed ---")
134 print(f"stdout:\n{out}")
135 print(f"stderr:\n{err}")
136 print("------")
137
138 self.assertEqual(p.returncode, returncode,
139 "bad returncode %d, stderr is %r" %
140 (p.returncode, err))
141 return out, err
142
143 def run_repeated_init_and_subinterpreters(self):
144 out, err = self.run_embedded_interpreter("test_repeated_init_and_subinterpreters")
145 self.assertEqual(err, "")
146
147 # The output from _testembed looks like this:
148 # --- Pass 1 ---
149 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
150 # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
151 # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
152 # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
153 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
154 # --- Pass 2 ---
155 # ...
156
157 interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
158 r"thread state <(0x[\dA-F]+)>: "
159 r"id\(modules\) = ([\d]+)$")
160 Interp = namedtuple("Interp", "id interp tstate modules")
161
162 numloops = 1
163 current_run = []
164 for line in out.splitlines():
165 if line == "--- Pass {} ---".format(numloops):
166 self.assertEqual(len(current_run), 0)
167 if support.verbose > 1:
168 print(line)
169 numloops += 1
170 continue
171
172 self.assertLess(len(current_run), 5)
173 match = re.match(interp_pat, line)
174 if match is None:
175 self.assertRegex(line, interp_pat)
176
177 # Parse the line from the loop. The first line is the main
178 # interpreter and the 3 afterward are subinterpreters.
179 interp = Interp(*match.groups())
180 if support.verbose > 2:
181 # 5 lines per pass is super-spammy, so limit that to -vvv
182 print(interp)
183 self.assertTrue(interp.interp)
184 self.assertTrue(interp.tstate)
185 self.assertTrue(interp.modules)
186 current_run.append(interp)
187
188 # The last line in the loop should be the same as the first.
189 if len(current_run) == 5:
190 main = current_run[0]
191 self.assertEqual(interp, main)
192 yield current_run
193 current_run = []
194
195
196 class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
197 maxDiff = 100 * 50
198
199 def test_subinterps_main(self):
200 for run in self.run_repeated_init_and_subinterpreters():
201 main = run[0]
202
203 self.assertEqual(main.id, '0')
204
205 def test_subinterps_different_ids(self):
206 for run in self.run_repeated_init_and_subinterpreters():
207 main, *subs, _ = run
208
209 mainid = int(main.id)
210 for i, sub in enumerate(subs):
211 self.assertEqual(sub.id, str(mainid + i + 1))
212
213 def test_subinterps_distinct_state(self):
214 for run in self.run_repeated_init_and_subinterpreters():
215 main, *subs, _ = run
216
217 if '0x0' in main:
218 # XXX Fix on Windows (and other platforms): something
219 # is going on with the pointers in Programs/_testembed.c.
220 # interp.interp is 0x0 and interp.modules is the same
221 # between interpreters.
222 raise unittest.SkipTest('platform prints pointers as 0x0')
223
224 for sub in subs:
225 # A new subinterpreter may have the same
226 # PyInterpreterState pointer as a previous one if
227 # the earlier one has already been destroyed. So
228 # we compare with the main interpreter. The same
229 # applies to tstate.
230 self.assertNotEqual(sub.interp, main.interp)
231 self.assertNotEqual(sub.tstate, main.tstate)
232 self.assertNotEqual(sub.modules, main.modules)
233
234 def test_repeated_init_and_inittab(self):
235 out, err = self.run_embedded_interpreter("test_repeated_init_and_inittab")
236 self.assertEqual(err, "")
237
238 lines = [f"--- Pass {i} ---" for i in range(1, INIT_LOOPS+1)]
239 lines = "\n".join(lines) + "\n"
240 self.assertEqual(out, lines)
241
242 def test_inittab_submodule_multiphase(self):
243 out, err = self.run_embedded_interpreter("test_inittab_submodule_multiphase")
244 self.assertEqual(err, "")
245 self.assertEqual(out,
246 "<module 'mp_pkg.mp_submod' (built-in)>\n"
247 "<module 'mp_pkg.mp_submod' (built-in)>\n"
248 "Hello from sub-module\n"
249 "mp_pkg.mp_submod.mp_submod_exec_slot_ran='yes'\n"
250 "mp_pkg.mp_pkg_exec_slot_ran='yes'\n"
251 )
252
253 def test_inittab_submodule_singlephase(self):
254 out, err = self.run_embedded_interpreter("test_inittab_submodule_singlephase")
255 self.assertEqual(self._nogil_filtered_err(err, "sp_pkg"), "")
256 self.assertEqual(out,
257 "<module 'sp_pkg.sp_submod' (built-in)>\n"
258 "<module 'sp_pkg.sp_submod' (built-in)>\n"
259 "Hello from sub-module\n"
260 )
261
262 def test_forced_io_encoding(self):
263 # Checks forced configuration of embedded interpreter IO streams
264 env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
265 out, err = self.run_embedded_interpreter("test_forced_io_encoding", env=env)
266 if support.verbose > 1:
267 print()
268 print(out)
269 print(err)
270 expected_stream_encoding = "utf-8"
271 expected_errors = "surrogateescape"
272 expected_output = '\n'.join([
273 "--- Use defaults ---",
274 "Expected encoding: default",
275 "Expected errors: default",
276 "stdin: {in_encoding}:{errors}",
277 "stdout: {out_encoding}:{errors}",
278 "stderr: {out_encoding}:backslashreplace",
279 "--- Set errors only ---",
280 "Expected encoding: default",
281 "Expected errors: ignore",
282 "stdin: {in_encoding}:ignore",
283 "stdout: {out_encoding}:ignore",
284 "stderr: {out_encoding}:backslashreplace",
285 "--- Set encoding only ---",
286 "Expected encoding: iso8859-1",
287 "Expected errors: default",
288 "stdin: iso8859-1:{errors}",
289 "stdout: iso8859-1:{errors}",
290 "stderr: iso8859-1:backslashreplace",
291 "--- Set encoding and errors ---",
292 "Expected encoding: iso8859-1",
293 "Expected errors: replace",
294 "stdin: iso8859-1:replace",
295 "stdout: iso8859-1:replace",
296 "stderr: iso8859-1:backslashreplace"])
297 expected_output = expected_output.format(
298 in_encoding=expected_stream_encoding,
299 out_encoding=expected_stream_encoding,
300 errors=expected_errors)
301 # This is useful if we ever trip over odd platform behaviour
302 self.maxDiff = None
303 self.assertEqual(out.strip(), expected_output)
304
305 def test_pre_initialization_api(self):
306 """
307 Checks some key parts of the C-API that need to work before the runtime
308 is initialized (via Py_Initialize()).
309 """
310 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
311 out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env)
312 if support.verbose > 1:
313 print()
314 print(out)
315 print(err)
316 if MS_WINDOWS:
317 expected_path = self.test_exe
318 else:
319 expected_path = os.path.join(os.getcwd(), "spam")
320 expected_output = f"sys.executable: {expected_path}\n"
321 self.assertIn(expected_output, out)
322 self.assertEqual(err, '')
323
324 def test_pre_initialization_sys_options(self):
325 """
326 Checks that sys.warnoptions and sys._xoptions can be set before the
327 runtime is initialized (otherwise they won't be effective).
328 """
329 env = remove_python_envvars()
330 env['PYTHONPATH'] = os.pathsep.join(sys.path)
331 out, err = self.run_embedded_interpreter(
332 "test_pre_initialization_sys_options", env=env)
333 if support.verbose > 1:
334 print()
335 print(out)
336 print(err)
337 expected_output = (
338 "sys.warnoptions: ['once', 'module', 'default']\n"
339 "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
340 "warnings.filters[:3]: ['default', 'module', 'once']\n"
341 )
342 self.assertIn(expected_output, out)
343 self.assertEqual(err, '')
344
345 def test_bpo20891(self):
346 """
347 bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
348 crash.
349 """
350 out, err = self.run_embedded_interpreter("test_bpo20891")
351 self.assertEqual(out, '')
352 self.assertEqual(err, '')
353
354 def test_initialize_twice(self):
355 """
356 bpo-33932: Calling Py_Initialize() twice should do nothing (and not
357 crash!).
358 """
359 out, err = self.run_embedded_interpreter("test_initialize_twice")
360 self.assertEqual(out, '')
361 self.assertEqual(err, '')
362
363 def test_initialize_pymain(self):
364 """
365 bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
366 """
367 out, err = self.run_embedded_interpreter("test_initialize_pymain")
368 self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
369 self.assertEqual(err, '')
370
371 def test_run_main(self):
372 out, err = self.run_embedded_interpreter("test_run_main")
373 self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
374 self.assertEqual(err, '')
375
376 def test_run_main_loop(self):
377 # bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple
378 # times must not crash.
379 nloop = 5
380 out, err = self.run_embedded_interpreter("test_run_main_loop")
381 self.assertEqual(out, "Py_RunMain(): sys.argv=['-c', 'arg2']\n" * nloop)
382 self.assertEqual(err, '')
383
384 def test_finalize_structseq(self):
385 # bpo-46417: Py_Finalize() clears structseq static types. Check that
386 # sys attributes using struct types still work when
387 # Py_Finalize()/Py_Initialize() is called multiple times.
388 # print() calls type->tp_repr(instance) and so checks that the types
389 # are still working properly.
390 script = support.findfile('_test_embed_structseq.py')
391 with open(script, encoding="utf-8") as fp:
392 code = fp.read()
393 out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
394 self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)
395
396 def test_simple_initialization_api(self):
397 # _testembed now uses Py_InitializeFromConfig by default
398 # This case specifically checks Py_Initialize(Ex) still works
399 out, err = self.run_embedded_interpreter("test_repeated_simple_init")
400 self.assertEqual(out, 'Finalized\n' * INIT_LOOPS)
401
402 @support.requires_specialization
403 @unittest.skipUnless(support.TEST_MODULES_ENABLED, "requires test modules")
404 def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self):
405 # https://github.com/python/cpython/issues/92031
406
407 _testinternalcapi = import_helper.import_module("_testinternalcapi")
408
409 code = textwrap.dedent(f"""\
410 import dis
411 import importlib._bootstrap
412 import opcode
413 import test.test_dis
414 import test.support
415
416 def is_specialized(f):
417 for instruction in dis.get_instructions(f, adaptive=True):
418 opname = instruction.opname
419 if (
420 opname in opcode._specialized_opmap
421 # Exclude superinstructions:
422 and "__" not in opname
423 # LOAD_CONST_IMMORTAL is "specialized", but is
424 # inserted during quickening.
425 and opname != "LOAD_CONST_IMMORTAL"
426 ):
427 return True
428 return False
429
430 func = importlib._bootstrap._handle_fromlist
431
432 # "copy" the code to un-specialize it:
433 test.support.reset_code(func)
434
435 assert not is_specialized(func), "specialized instructions found"
436
437 for _ in range({_testinternalcapi.SPECIALIZATION_THRESHOLD}):
438 func(importlib._bootstrap, ["x"], lambda *args: None)
439
440 assert is_specialized(func), "no specialized instructions found"
441
442 print("Tests passed")
443 """)
444 run = self.run_embedded_interpreter
445 out, err = run("test_repeated_init_exec", code)
446 self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)
447
448 def test_ucnhash_capi_reset(self):
449 # bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown.
450 code = "print('\\N{digit nine}')"
451 out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
452 self.assertEqual(out, '9\n' * INIT_LOOPS)
453
454 def test_datetime_reset_strptime(self):
455 code = (
456 "import datetime;"
457 "d = datetime.datetime.strptime('2000-01-01', '%Y-%m-%d');"
458 "print(d.strftime('%Y%m%d'))"
459 )
460 out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
461 self.assertEqual(out, '20000101\n' * INIT_LOOPS)
462
463 def test_static_types_inherited_slots(self):
464 script = textwrap.dedent("""
465 import test.support
466 results = []
467 for cls in test.support.iter_builtin_types():
468 for attr, _ in test.support.iter_slot_wrappers(cls):
469 wrapper = getattr(cls, attr)
470 res = (cls, attr, wrapper)
471 results.append(res)
472 results = ((repr(c), a, repr(w)) for c, a, w in results)
473 """)
474 def collate_results(raw):
475 results = {}
476 for cls, attr, wrapper in raw:
477 key = cls, attr
478 assert key not in results, (results, key, wrapper)
479 results[key] = wrapper
480 return results
481
482 ns = {}
483 exec(script, ns, ns)
484 main_results = collate_results(ns['results'])
485 del ns
486
487 script += textwrap.dedent("""
488 import json
489 import sys
490 text = json.dumps(list(results))
491 print(text, file=sys.stderr)
492 """)
493 out, err = self.run_embedded_interpreter(
494 "test_repeated_init_exec", script, script)
495 _results = err.split('--- Loop #')[1:]
496 (_embedded, _reinit,
497 ) = [json.loads(res.rpartition(' ---\n')[-1]) for res in _results]
498 embedded_results = collate_results(_embedded)
499 reinit_results = collate_results(_reinit)
500
501 for key, expected in main_results.items():
502 cls, attr = key
503 for src, results in [
504 ('embedded', embedded_results),
505 ('reinit', reinit_results),
506 ]:
507 with self.subTest(src, cls=cls, slotattr=attr):
508 actual = results.pop(key)
509 self.assertEqual(actual, expected)
510 self.maxDiff = None
511 self.assertEqual(embedded_results, {})
512 self.assertEqual(reinit_results, {})
513
514 self.assertEqual(out, '')
515
516 def test_getargs_reset_static_parser(self):
517 # Test _PyArg_Parser initializations via _PyArg_UnpackKeywords()
518 # https://github.com/python/cpython/issues/122334
519 code = textwrap.dedent("""
520 try:
521 import _ssl
522 except ModuleNotFoundError:
523 _ssl = None
524 if _ssl is not None:
525 _ssl.txt2obj(txt='1.3')
526 print('1')
527
528 import _queue
529 _queue.SimpleQueue().put_nowait(item=None)
530 print('2')
531
532 import _zoneinfo
533 _zoneinfo.ZoneInfo.clear_cache(only_keys=['Foo/Bar'])
534 print('3')
535 """)
536 out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
537 self.assertEqual(out, '1\n2\n3\n' * INIT_LOOPS)
538
539 @staticmethod
540 def _nogil_filtered_err(err: str, mod_name: str) -> str:
541 if not support.Py_GIL_DISABLED:
542 return err
543
544 # the test imports a singlephase init extension, so it emits a warning
545 # under the free-threaded build
546 expected_runtime_warning = (
547 "RuntimeWarning: The global interpreter lock (GIL)"
548 f" has been enabled to load module '{mod_name}'"
549 )
550 filtered_err_lines = [
551 line
552 for line in err.strip().splitlines()
553 if expected_runtime_warning not in line
554 ]
555 return "\n".join(filtered_err_lines)
556
557
558 def config_dev_mode(preconfig, config):
559 preconfig['allocator'] = PYMEM_ALLOCATOR_DEBUG
560 preconfig['dev_mode'] = 1
561 config['dev_mode'] = 1
562 config['warnoptions'] = ['default']
563 config['faulthandler'] = 1
564
565
566 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
567 class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
568 maxDiff = 4096
569 UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
570
571 # Marker to read the default configuration: get_default_config()
572 GET_DEFAULT_CONFIG = object()
573
574 # Marker to ignore a configuration parameter
575 IGNORE_CONFIG = object()
576
577 PRE_CONFIG_COMPAT = {
578 '_config_init': API_COMPAT,
579 'allocator': PYMEM_ALLOCATOR_NOT_SET,
580 'parse_argv': False,
581 'configure_locale': True,
582 'coerce_c_locale': False,
583 'coerce_c_locale_warn': False,
584 'utf8_mode': False,
585 }
586 if MS_WINDOWS:
587 PRE_CONFIG_COMPAT.update({
588 'legacy_windows_fs_encoding': False,
589 })
590 PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT,
591 _config_init=API_PYTHON,
592 parse_argv=True,
593 coerce_c_locale=GET_DEFAULT_CONFIG,
594 utf8_mode=GET_DEFAULT_CONFIG,
595 )
596 PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT,
597 _config_init=API_ISOLATED,
598 configure_locale=False,
599 isolated=True,
600 use_environment=False,
601 utf8_mode=False,
602 dev_mode=False,
603 coerce_c_locale=False,
604 )
605
606 COPY_PRE_CONFIG = [
607 'dev_mode',
608 'isolated',
609 'use_environment',
610 ]
611
612 CONFIG_COMPAT = {
613 '_config_init': API_COMPAT,
614 'isolated': False,
615 'use_environment': True,
616 'dev_mode': False,
617
618 'install_signal_handlers': True,
619 'use_hash_seed': False,
620 'hash_seed': 0,
621 'int_max_str_digits': sys.int_info.default_max_str_digits,
622 'cpu_count': -1,
623 'faulthandler': False,
624 'tracemalloc': 0,
625 'perf_profiling': 0,
626 'import_time': 0,
627 'thread_inherit_context': DEFAULT_THREAD_INHERIT_CONTEXT,
628 'context_aware_warnings': DEFAULT_CONTEXT_AWARE_WARNINGS,
629 'code_debug_ranges': True,
630 'show_ref_count': False,
631 'dump_refs': False,
632 'dump_refs_file': None,
633 'malloc_stats': False,
634
635 'filesystem_encoding': GET_DEFAULT_CONFIG,
636 'filesystem_errors': GET_DEFAULT_CONFIG,
637
638 'pycache_prefix': None,
639 'program_name': GET_DEFAULT_CONFIG,
640 'parse_argv': False,
641 'argv': [""],
642 'orig_argv': [],
643
644 'xoptions': {},
645 'warnoptions': [],
646
647 'pythonpath_env': None,
648 'home': None,
649 'executable': GET_DEFAULT_CONFIG,
650 'base_executable': GET_DEFAULT_CONFIG,
651
652 'prefix': GET_DEFAULT_CONFIG,
653 'base_prefix': GET_DEFAULT_CONFIG,
654 'exec_prefix': GET_DEFAULT_CONFIG,
655 'base_exec_prefix': GET_DEFAULT_CONFIG,
656 'module_search_paths': GET_DEFAULT_CONFIG,
657 'module_search_paths_set': True,
658 'platlibdir': sys.platlibdir,
659 'stdlib_dir': GET_DEFAULT_CONFIG,
660
661 'site_import': True,
662 'bytes_warning': 0,
663 'warn_default_encoding': False,
664 'inspect': False,
665 'interactive': False,
666 'optimization_level': 0,
667 'parser_debug': False,
668 'write_bytecode': True,
669 'verbose': 0,
670 'quiet': False,
671 'remote_debug': True,
672 'user_site_directory': True,
673 'configure_c_stdio': False,
674 'buffered_stdio': True,
675
676 'stdio_encoding': GET_DEFAULT_CONFIG,
677 'stdio_errors': GET_DEFAULT_CONFIG,
678
679 'skip_source_first_line': False,
680 'run_command': None,
681 'run_module': None,
682 'run_filename': None,
683 'sys_path_0': None,
684
685 '_install_importlib': True,
686 'check_hash_pycs_mode': 'default',
687 'pathconfig_warnings': True,
688 '_init_main': True,
689 'use_frozen_modules': not support.Py_DEBUG,
690 'safe_path': False,
691 '_is_python_build': IGNORE_CONFIG,
692 }
693 if Py_STATS:
694 CONFIG_COMPAT['_pystats'] = False
695 if support.Py_DEBUG:
696 CONFIG_COMPAT['run_presite'] = None
697 if support.Py_GIL_DISABLED:
698 CONFIG_COMPAT['enable_gil'] = -1
699 CONFIG_COMPAT['tlbc_enabled'] = GET_DEFAULT_CONFIG
700 if MS_WINDOWS:
701 CONFIG_COMPAT.update({
702 'legacy_windows_stdio': False,
703 })
704 if support.is_apple:
705 CONFIG_COMPAT['use_system_logger'] = False
706
707 CONFIG_PYTHON = dict(CONFIG_COMPAT,
708 _config_init=API_PYTHON,
709 configure_c_stdio=True,
710 parse_argv=True,
711 )
712 CONFIG_ISOLATED = dict(CONFIG_COMPAT,
713 _config_init=API_ISOLATED,
714 isolated=True,
715 use_environment=False,
716 user_site_directory=False,
717 safe_path=True,
718 dev_mode=False,
719 install_signal_handlers=False,
720 use_hash_seed=False,
721 faulthandler=False,
722 tracemalloc=False,
723 perf_profiling=0,
724 pathconfig_warnings=False,
725 )
726 if MS_WINDOWS:
727 CONFIG_ISOLATED['legacy_windows_stdio'] = False
728
729 # global config
730 DEFAULT_GLOBAL_CONFIG = {
731 'Py_HasFileSystemDefaultEncoding': 0,
732 'Py_HashRandomizationFlag': 1,
733 '_Py_HasFileSystemDefaultEncodeErrors': 0,
734 }
735 COPY_GLOBAL_PRE_CONFIG = [
736 ('Py_UTF8Mode', 'utf8_mode'),
737 ]
738 COPY_GLOBAL_CONFIG = [
739 # Copy core config to global config for expected values
740 # True means that the core config value is inverted (0 => 1 and 1 => 0)
741 ('Py_BytesWarningFlag', 'bytes_warning'),
742 ('Py_DebugFlag', 'parser_debug'),
743 ('Py_DontWriteBytecodeFlag', 'write_bytecode', True),
744 ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),
745 ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),
746 ('Py_FrozenFlag', 'pathconfig_warnings', True),
747 ('Py_IgnoreEnvironmentFlag', 'use_environment', True),
748 ('Py_InspectFlag', 'inspect'),
749 ('Py_InteractiveFlag', 'interactive'),
750 ('Py_IsolatedFlag', 'isolated'),
751 ('Py_NoSiteFlag', 'site_import', True),
752 ('Py_NoUserSiteDirectory', 'user_site_directory', True),
753 ('Py_OptimizeFlag', 'optimization_level'),
754 ('Py_QuietFlag', 'quiet'),
755 ('Py_UnbufferedStdioFlag', 'buffered_stdio', True),
756 ('Py_VerboseFlag', 'verbose'),
757 ]
758 if MS_WINDOWS:
759 COPY_GLOBAL_PRE_CONFIG.extend((
760 ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'),
761 ))
762 COPY_GLOBAL_CONFIG.extend((
763 ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'),
764 ))
765
766 EXPECTED_CONFIG = None
767
768 @classmethod
769 def tearDownClass(cls):
770 # clear cache
771 cls.EXPECTED_CONFIG = None
772
773 def main_xoptions(self, xoptions_list):
774 xoptions = {}
775 for opt in xoptions_list:
776 if '=' in opt:
777 key, value = opt.split('=', 1)
778 xoptions[key] = value
779 else:
780 xoptions[opt] = True
781 return xoptions
782
783 def _get_expected_config_impl(self):
784 env = remove_python_envvars()
785 code = textwrap.dedent('''
786 import json
787 import sys
788 import _testinternalcapi
789
790 configs = _testinternalcapi.get_configs()
791
792 data = json.dumps(configs)
793 data = data.encode('utf-8')
794 sys.stdout.buffer.write(data)
795 sys.stdout.buffer.flush()
796 ''')
797
798 # Use -S to not import the site module: get the proper configuration
799 # when test_embed is run from a venv (bpo-35313)
800 args = [sys.executable, '-S', '-c', code]
801 proc = subprocess.run(args, env=env,
802 stdout=subprocess.PIPE,
803 stderr=subprocess.PIPE)
804 if proc.returncode:
805 raise Exception(f"failed to get the default config: "
806 f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
807 stdout = proc.stdout.decode('utf-8')
808 # ignore stderr
809 try:
810 return json.loads(stdout)
811 except json.JSONDecodeError:
812 self.fail(f"fail to decode stdout: {stdout!r}")
813
814 def _get_expected_config(self):
815 cls = InitConfigTests
816 if cls.EXPECTED_CONFIG is None:
817 cls.EXPECTED_CONFIG = self._get_expected_config_impl()
818
819 # get a copy
820 configs = {}
821 for config_key, config_value in cls.EXPECTED_CONFIG.items():
822 config = {}
823 for key, value in config_value.items():
824 if isinstance(value, list):
825 value = value.copy()
826 config[key] = value
827 configs[config_key] = config
828 return configs
829
830 def get_expected_config(self, expected_preconfig, expected,
831 env, api, modify_path_cb=None):
832 configs = self._get_expected_config()
833
834 pre_config = configs['pre_config']
835 for key, value in expected_preconfig.items():
836 if value is self.GET_DEFAULT_CONFIG:
837 expected_preconfig[key] = pre_config[key]
838
839 if not expected_preconfig['configure_locale'] or api == API_COMPAT:
840 # there is no easy way to get the locale encoding before
841 # setlocale(LC_CTYPE, "") is called: don't test encodings
842 for key in ('filesystem_encoding', 'filesystem_errors',
843 'stdio_encoding', 'stdio_errors'):
844 expected[key] = self.IGNORE_CONFIG
845
846 if not expected_preconfig['configure_locale']:
847 # UTF-8 Mode depends on the locale. There is no easy way
848 # to guess if UTF-8 Mode will be enabled or not if the locale
849 # is not configured.
850 expected_preconfig['utf8_mode'] = self.IGNORE_CONFIG
851
852 if expected_preconfig['utf8_mode'] == 1:
853 if expected['filesystem_encoding'] is self.GET_DEFAULT_CONFIG:
854 expected['filesystem_encoding'] = 'utf-8'
855 if expected['filesystem_errors'] is self.GET_DEFAULT_CONFIG:
856 expected['filesystem_errors'] = self.UTF8_MODE_ERRORS
857 if expected['stdio_encoding'] is self.GET_DEFAULT_CONFIG:
858 expected['stdio_encoding'] = 'utf-8'
859 if expected['stdio_errors'] is self.GET_DEFAULT_CONFIG:
860 expected['stdio_errors'] = 'surrogateescape'
861
862 if MS_WINDOWS:
863 default_executable = self.test_exe
864 elif expected['program_name'] is not self.GET_DEFAULT_CONFIG:
865 default_executable = os.path.abspath(expected['program_name'])
866 else:
867 default_executable = os.path.join(os.getcwd(), '_testembed')
868 if expected['executable'] is self.GET_DEFAULT_CONFIG:
869 expected['executable'] = default_executable
870 if expected['base_executable'] is self.GET_DEFAULT_CONFIG:
871 expected['base_executable'] = default_executable
872 if expected['program_name'] is self.GET_DEFAULT_CONFIG:
873 expected['program_name'] = './_testembed'
874
875 config = configs['config']
876 for key, value in expected.items():
877 if value is self.GET_DEFAULT_CONFIG:
878 expected[key] = config[key]
879
880 if expected['module_search_paths'] is not self.IGNORE_CONFIG:
881 pythonpath_env = expected['pythonpath_env']
882 if pythonpath_env is not None:
883 paths = pythonpath_env.split(os.path.pathsep)
884 expected['module_search_paths'] = [*paths, *expected['module_search_paths']]
885 if modify_path_cb is not None:
886 expected['module_search_paths'] = expected['module_search_paths'].copy()
887 modify_path_cb(expected['module_search_paths'])
888
889 for key in self.COPY_PRE_CONFIG:
890 if key not in expected_preconfig:
891 expected_preconfig[key] = expected[key]
892
893 def check_pre_config(self, configs, expected):
894 pre_config = dict(configs['pre_config'])
895 for key, value in list(expected.items()):
896 if value is self.IGNORE_CONFIG:
897 pre_config.pop(key, None)
898 del expected[key]
899 self.assertEqual(pre_config, expected)
900
901 def check_config(self, configs, expected):
902 config = dict(configs['config'])
903 if MS_WINDOWS:
904 value = config.get(key := 'program_name')
905 if value and isinstance(value, str):
906 value = value[:len(value.lower().removesuffix('.exe'))]
907 if debug_build(sys.executable):
908 value = value[:len(value.lower().removesuffix('_d'))]
909 config[key] = value
910 for key, value in list(expected.items()):
911 if value is self.IGNORE_CONFIG:
912 config.pop(key, None)
913 del expected[key]
914 # Resolve bool/int mismatches to reduce noise in diffs
915 if isinstance(value, (bool, int)) and isinstance(config.get(key), (bool, int)):
916 expected[key] = type(config[key])(expected[key])
917 self.assertEqual(config, expected)
918
919 def check_global_config(self, configs):
920 pre_config = configs['pre_config']
921 config = configs['config']
922
923 expected = dict(self.DEFAULT_GLOBAL_CONFIG)
924 for item in self.COPY_GLOBAL_CONFIG:
925 if len(item) == 3:
926 global_key, core_key, opposite = item
927 expected[global_key] = 0 if config[core_key] else 1
928 else:
929 global_key, core_key = item
930 expected[global_key] = config[core_key]
931 for item in self.COPY_GLOBAL_PRE_CONFIG:
932 if len(item) == 3:
933 global_key, core_key, opposite = item
934 expected[global_key] = 0 if pre_config[core_key] else 1
935 else:
936 global_key, core_key = item
937 expected[global_key] = pre_config[core_key]
938
939 self.assertEqual(configs['global_config'], expected)
940
941 def check_all_configs(self, testname, expected_config=None,
942 expected_preconfig=None,
943 modify_path_cb=None,
944 stderr=None, *, api, preconfig_api=None,
945 env=None, ignore_stderr=False, cwd=None):
946 new_env = remove_python_envvars()
947 if env is not None:
948 new_env.update(env)
949 env = new_env
950
951 if preconfig_api is None:
952 preconfig_api = api
953 if preconfig_api == API_ISOLATED:
954 default_preconfig = self.PRE_CONFIG_ISOLATED
955 elif preconfig_api == API_PYTHON:
956 default_preconfig = self.PRE_CONFIG_PYTHON
957 else:
958 default_preconfig = self.PRE_CONFIG_COMPAT
959 if expected_preconfig is None:
960 expected_preconfig = {}
961 expected_preconfig = dict(default_preconfig, **expected_preconfig)
962
963 if expected_config is None:
964 expected_config = {}
965
966 if api == API_PYTHON:
967 default_config = self.CONFIG_PYTHON
968 elif api == API_ISOLATED:
969 default_config = self.CONFIG_ISOLATED
970 else:
971 default_config = self.CONFIG_COMPAT
972 expected_config = dict(default_config, **expected_config)
973
974 self.get_expected_config(expected_preconfig,
975 expected_config,
976 env,
977 api, modify_path_cb)
978
979 out, err = self.run_embedded_interpreter(testname,
980 env=env, cwd=cwd)
981 if stderr is None and not expected_config['verbose']:
982 stderr = ""
983 if stderr is not None and not ignore_stderr:
984 self.assertEqual(err.rstrip(), stderr)
985 try:
986 configs = json.loads(out)
987 except json.JSONDecodeError:
988 self.fail(f"fail to decode stdout: {out!r}")
989
990 self.check_pre_config(configs, expected_preconfig)
991 self.check_config(configs, expected_config)
992 self.check_global_config(configs)
993 return configs
994
995 @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries")
996 def test_init_default_config(self):
997 self.check_all_configs("test_init_initialize_config", api=API_COMPAT)
998
999 def test_preinit_compat_config(self):
1000 self.check_all_configs("test_preinit_compat_config", api=API_COMPAT)
1001
1002 def test_init_compat_config(self):
1003 self.check_all_configs("test_init_compat_config", api=API_COMPAT)
1004
1005 def test_init_global_config(self):
1006 preconfig = {
1007 'utf8_mode': True,
1008 }
1009 config = {
1010 'program_name': './globalvar',
1011 'site_import': False,
1012 'bytes_warning': True,
1013 'warnoptions': ['default::BytesWarning'],
1014 'inspect': True,
1015 'interactive': True,
1016 'optimization_level': 2,
1017 'write_bytecode': False,
1018 'verbose': True,
1019 'quiet': True,
1020 'buffered_stdio': False,
1021 'remote_debug': True,
1022 'user_site_directory': False,
1023 'pathconfig_warnings': False,
1024 }
1025 self.check_all_configs("test_init_global_config", config, preconfig,
1026 api=API_COMPAT)
1027
1028 def test_init_from_config(self):
1029 preconfig = {
1030 'allocator': ALLOCATOR_FOR_CONFIG,
1031 'utf8_mode': True,
1032 }
1033 config = {
1034 'install_signal_handlers': False,
1035 'use_hash_seed': True,
1036 'hash_seed': 123,
1037 'tracemalloc': 2,
1038 'perf_profiling': 0,
1039 'import_time': 2,
1040 'code_debug_ranges': False,
1041 'show_ref_count': True,
1042 'malloc_stats': True,
1043
1044 'stdio_encoding': 'iso8859-1',
1045 'stdio_errors': 'replace',
1046
1047 'pycache_prefix': 'conf_pycache_prefix',
1048 'program_name': './conf_program_name',
1049 'argv': ['-c', 'arg2'],
1050 'orig_argv': ['python3',
1051 '-W', 'cmdline_warnoption',
1052 '-X', 'cmdline_xoption',
1053 '-c', 'pass',
1054 'arg2'],
1055 'parse_argv': True,
1056 'xoptions': {
1057 'config_xoption1': '3',
1058 'config_xoption2': '',
1059 'config_xoption3': True,
1060 'cmdline_xoption': True,
1061 },
1062 'warnoptions': [
1063 'cmdline_warnoption',
1064 'default::BytesWarning',
1065 'config_warnoption',
1066 ],
1067 'run_command': 'pass\n',
1068
1069 'site_import': False,
1070 'bytes_warning': 1,
1071 'inspect': True,
1072 'interactive': True,
1073 'optimization_level': 2,
1074 'write_bytecode': False,
1075 'verbose': 1,
1076 'quiet': True,
1077 'remote_debug': True,
1078 'configure_c_stdio': True,
1079 'buffered_stdio': False,
1080 'user_site_directory': False,
1081 'faulthandler': True,
1082 'platlibdir': 'my_platlibdir',
1083 'module_search_paths': self.IGNORE_CONFIG,
1084 'safe_path': True,
1085 'int_max_str_digits': 31337,
1086 'cpu_count': 4321,
1087
1088 'check_hash_pycs_mode': 'always',
1089 'pathconfig_warnings': False,
1090 }
1091 if Py_STATS:
1092 config['_pystats'] = 1
1093 self.check_all_configs("test_init_from_config", config, preconfig,
1094 api=API_COMPAT)
1095
1096 @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries")
1097 def test_init_compat_env(self):
1098 preconfig = {
1099 'allocator': ALLOCATOR_FOR_CONFIG,
1100 }
1101 config = {
1102 'use_hash_seed': True,
1103 'hash_seed': 42,
1104 'tracemalloc': 2,
1105 'import_time': 1,
1106 'code_debug_ranges': False,
1107 'malloc_stats': True,
1108 'inspect': True,
1109 'optimization_level': 2,
1110 'pythonpath_env': '/my/path',
1111 'pycache_prefix': 'env_pycache_prefix',
1112 'write_bytecode': False,
1113 'verbose': 1,
1114 'buffered_stdio': False,
1115 'stdio_encoding': 'iso8859-1',
1116 'stdio_errors': 'replace',
1117 'user_site_directory': False,
1118 'faulthandler': True,
1119 'warnoptions': ['EnvVar'],
1120 'platlibdir': 'env_platlibdir',
1121 'module_search_paths': self.IGNORE_CONFIG,
1122 'safe_path': True,
1123 'int_max_str_digits': 4567,
1124 'perf_profiling': 1,
1125 }
1126 if Py_STATS:
1127 config['_pystats'] = 1
1128 self.check_all_configs("test_init_compat_env", config, preconfig,
1129 api=API_COMPAT)
1130
1131 @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries")
1132 def test_init_python_env(self):
1133 preconfig = {
1134 'allocator': ALLOCATOR_FOR_CONFIG,
1135 'utf8_mode': 1,
1136 }
1137 config = {
1138 'use_hash_seed': True,
1139 'hash_seed': 42,
1140 'tracemalloc': 2,
1141 'import_time': 1,
1142 'code_debug_ranges': False,
1143 'malloc_stats': True,
1144 'inspect': True,
1145 'optimization_level': 2,
1146 'pythonpath_env': '/my/path',
1147 'pycache_prefix': 'env_pycache_prefix',
1148 'write_bytecode': False,
1149 'verbose': 1,
1150 'buffered_stdio': False,
1151 'stdio_encoding': 'iso8859-1',
1152 'stdio_errors': 'replace',
1153 'user_site_directory': False,
1154 'faulthandler': True,
1155 'warnoptions': ['EnvVar'],
1156 'platlibdir': 'env_platlibdir',
1157 'module_search_paths': self.IGNORE_CONFIG,
1158 'safe_path': True,
1159 'int_max_str_digits': 4567,
1160 'perf_profiling': 1,
1161 }
1162 if Py_STATS:
1163 config['_pystats'] = True
1164 self.check_all_configs("test_init_python_env", config, preconfig,
1165 api=API_PYTHON)
1166
1167 def test_init_env_dev_mode(self):
1168 preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG)
1169 config = dict(dev_mode=1,
1170 faulthandler=1,
1171 warnoptions=['default'])
1172 self.check_all_configs("test_init_env_dev_mode", config, preconfig,
1173 api=API_COMPAT)
1174
1175 def test_init_env_dev_mode_alloc(self):
1176 preconfig = dict(allocator=ALLOCATOR_FOR_CONFIG)
1177 config = dict(dev_mode=1,
1178 faulthandler=1,
1179 warnoptions=['default'])
1180 self.check_all_configs("test_init_env_dev_mode_alloc", config, preconfig,
1181 api=API_COMPAT)
1182
1183 def test_init_dev_mode(self):
1184 preconfig = {}
1185 config = {
1186 'faulthandler': True,
1187 'dev_mode': True,
1188 'warnoptions': ['default'],
1189 }
1190 config_dev_mode(preconfig, config)
1191 self.check_all_configs("test_init_dev_mode", config, preconfig,
1192 api=API_PYTHON)
1193
1194 def test_preinit_parse_argv(self):
1195 # Pre-initialize implicitly using argv: make sure that -X dev
1196 # is used to configure the allocation in preinitialization
1197 preconfig = {}
1198 config = {
1199 'argv': ['script.py'],
1200 'orig_argv': ['python3', '-X', 'dev', '-P', 'script.py'],
1201 'run_filename': os.path.abspath('script.py'),
1202 'dev_mode': True,
1203 'faulthandler': True,
1204 'warnoptions': ['default'],
1205 'xoptions': {'dev': True},
1206 'safe_path': True,
1207 }
1208 config_dev_mode(preconfig, config)
1209 self.check_all_configs("test_preinit_parse_argv", config, preconfig,
1210 api=API_PYTHON)
1211
1212 def test_preinit_dont_parse_argv(self):
1213 # -X dev must be ignored by isolated preconfiguration
1214 preconfig = {
1215 'isolated': False,
1216 }
1217 argv = ["python3",
1218 "-E", "-I", "-P",
1219 "-X", "dev",
1220 "-X", "utf8",
1221 "script.py"]
1222 config = {
1223 'argv': argv,
1224 'orig_argv': argv,
1225 'isolated': False,
1226 }
1227 self.check_all_configs("test_preinit_dont_parse_argv", config, preconfig,
1228 api=API_ISOLATED)
1229
1230 def test_init_isolated_flag(self):
1231 config = {
1232 'isolated': True,
1233 'safe_path': True,
1234 'use_environment': False,
1235 'user_site_directory': False,
1236 }
1237 self.check_all_configs("test_init_isolated_flag", config, api=API_PYTHON)
1238
1239 def test_preinit_isolated1(self):
1240 # _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
1241 config = {
1242 'isolated': True,
1243 'safe_path': True,
1244 'use_environment': False,
1245 'user_site_directory': False,
1246 }
1247 self.check_all_configs("test_preinit_isolated1", config, api=API_COMPAT)
1248
1249 def test_preinit_isolated2(self):
1250 # _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
1251 config = {
1252 'isolated': True,
1253 'safe_path': True,
1254 'use_environment': False,
1255 'user_site_directory': False,
1256 }
1257 self.check_all_configs("test_preinit_isolated2", config, api=API_COMPAT)
1258
1259 def test_preinit_isolated_config(self):
1260 self.check_all_configs("test_preinit_isolated_config", api=API_ISOLATED)
1261
1262 def test_init_isolated_config(self):
1263 self.check_all_configs("test_init_isolated_config", api=API_ISOLATED)
1264
1265 def test_preinit_python_config(self):
1266 self.check_all_configs("test_preinit_python_config", api=API_PYTHON)
1267
1268 def test_init_python_config(self):
1269 self.check_all_configs("test_init_python_config", api=API_PYTHON)
1270
1271 def test_init_dont_configure_locale(self):
1272 # _PyPreConfig.configure_locale=0
1273 preconfig = {
1274 'configure_locale': 0,
1275 'coerce_c_locale': 0,
1276 }
1277 self.check_all_configs("test_init_dont_configure_locale", {}, preconfig,
1278 api=API_PYTHON)
1279
1280 @unittest.skip('as of 3.11 this test no longer works because '
1281 'path calculations do not occur on read')
1282 def test_init_read_set(self):
1283 config = {
1284 'program_name': './init_read_set',
1285 'executable': 'my_executable',
1286 'base_executable': 'my_executable',
1287 }
1288 def modify_path(path):
1289 path.insert(1, "test_path_insert1")
1290 path.append("test_path_append")
1291 self.check_all_configs("test_init_read_set", config,
1292 api=API_PYTHON,
1293 modify_path_cb=modify_path)
1294
1295 def test_init_sys_add(self):
1296 config = {
1297 'faulthandler': 1,
1298 'xoptions': {
1299 'config_xoption': True,
1300 'cmdline_xoption': True,
1301 'sysadd_xoption': True,
1302 'faulthandler': True,
1303 },
1304 'warnoptions': [
1305 'ignore:::cmdline_warnoption',
1306 'ignore:::sysadd_warnoption',
1307 'ignore:::config_warnoption',
1308 ],
1309 'orig_argv': ['python3',
1310 '-W', 'ignore:::cmdline_warnoption',
1311 '-X', 'cmdline_xoption'],
1312 }
1313 self.check_all_configs("test_init_sys_add", config, api=API_PYTHON)
1314
1315 def test_init_run_main(self):
1316 code = ('import _testinternalcapi, json; '
1317 'print(json.dumps(_testinternalcapi.get_configs()))')
1318 config = {
1319 'argv': ['-c', 'arg2'],
1320 'orig_argv': ['python3', '-c', code, 'arg2'],
1321 'program_name': './python3',
1322 'run_command': code + '\n',
1323 'parse_argv': True,
1324 'sys_path_0': '',
1325 }
1326 self.check_all_configs("test_init_run_main", config, api=API_PYTHON)
1327
1328 def test_init_parse_argv(self):
1329 config = {
1330 'parse_argv': True,
1331 'argv': ['-c', 'arg1', '-v', 'arg3'],
1332 'orig_argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
1333 'program_name': './argv0',
1334 'run_command': 'pass\n',
1335 'use_environment': False,
1336 }
1337 self.check_all_configs("test_init_parse_argv", config, api=API_PYTHON)
1338
1339 def test_init_dont_parse_argv(self):
1340 pre_config = {
1341 'parse_argv': 0,
1342 }
1343 config = {
1344 'parse_argv': False,
1345 'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
1346 'orig_argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
1347 'program_name': './argv0',
1348 }
1349 self.check_all_configs("test_init_dont_parse_argv", config, pre_config,
1350 api=API_PYTHON)
1351
1352 def default_program_name(self, config):
1353 if MS_WINDOWS:
1354 program_name = 'python'
1355 executable = self.test_exe
1356 else:
1357 program_name = 'python3'
1358 if MACOS:
1359 executable = self.test_exe
1360 else:
1361 executable = shutil.which(program_name) or ''
1362 config.update({
1363 'program_name': program_name,
1364 'base_executable': executable,
1365 'executable': executable,
1366 })
1367
1368 def test_init_setpath(self):
1369 # Test Py_SetPath()
1370 config = self._get_expected_config()
1371 paths = config['config']['module_search_paths']
1372
1373 config = {
1374 'module_search_paths': paths,
1375 'prefix': '',
1376 'base_prefix': '',
1377 'exec_prefix': '',
1378 'base_exec_prefix': '',
1379 # The current getpath.c doesn't determine the stdlib dir
1380 # in this case.
1381 'stdlib_dir': '',
1382 }
1383 self.default_program_name(config)
1384 env = {'TESTPATH': os.path.pathsep.join(paths)}
1385
1386 self.check_all_configs("test_init_setpath", config,
1387 api=API_COMPAT, env=env,
1388 ignore_stderr=True)
1389
1390 def test_init_setpath_config(self):
1391 # Test Py_SetPath() with PyConfig
1392 config = self._get_expected_config()
1393 paths = config['config']['module_search_paths']
1394
1395 config = {
1396 # set by Py_SetPath()
1397 'module_search_paths': paths,
1398 'prefix': '',
1399 'base_prefix': '',
1400 'exec_prefix': '',
1401 'base_exec_prefix': '',
1402 # The current getpath.c doesn't determine the stdlib dir
1403 # in this case.
1404 'stdlib_dir': '',
1405 'use_frozen_modules': not support.Py_DEBUG,
1406 # overridden by PyConfig
1407 'program_name': 'conf_program_name',
1408 'base_executable': 'conf_executable',
1409 'executable': 'conf_executable',
1410 }
1411 env = {'TESTPATH': os.path.pathsep.join(paths)}
1412 self.check_all_configs("test_init_setpath_config", config,
1413 api=API_PYTHON, env=env, ignore_stderr=True)
1414
1415 def module_search_paths(self, prefix=None, exec_prefix=None):
1416 config = self._get_expected_config()
1417 if prefix is None:
1418 prefix = config['config']['prefix']
1419 if exec_prefix is None:
1420 exec_prefix = config['config']['prefix']
1421 if MS_WINDOWS:
1422 return config['config']['module_search_paths']
1423 else:
1424 ver = sys.version_info
1425 return [
1426 os.path.join(prefix, sys.platlibdir,
1427 f'python{ver.major}{ver.minor}{ABI_THREAD}.zip'),
1428 os.path.join(prefix, sys.platlibdir,
1429 f'python{ver.major}.{ver.minor}{ABI_THREAD}'),
1430 os.path.join(exec_prefix, sys.platlibdir,
1431 f'python{ver.major}.{ver.minor}{ABI_THREAD}', 'lib-dynload'),
1432 ]
1433
1434 @contextlib.contextmanager
1435 def tmpdir_with_python(self, subdir=None):
1436 # Temporary directory with a copy of the Python program
1437 with tempfile.TemporaryDirectory() as tmpdir:
1438 # bpo-38234: On macOS and FreeBSD, the temporary directory
1439 # can be symbolic link. For example, /tmp can be a symbolic link
1440 # to /var/tmp. Call realpath() to resolve all symbolic links.
1441 tmpdir = os.path.realpath(tmpdir)
1442 if subdir:
1443 tmpdir = os.path.normpath(os.path.join(tmpdir, subdir))
1444 os.makedirs(tmpdir)
1445
1446 if MS_WINDOWS:
1447 # Copy pythonXY.dll (or pythonXY_d.dll)
1448 import fnmatch
1449 exedir = os.path.dirname(self.test_exe)
1450 for f in os.listdir(exedir):
1451 if fnmatch.fnmatch(f, '*.dll'):
1452 shutil.copyfile(os.path.join(exedir, f), os.path.join(tmpdir, f))
1453
1454 # Copy Python program
1455 exec_copy = os.path.join(tmpdir, os.path.basename(self.test_exe))
1456 shutil.copyfile(self.test_exe, exec_copy)
1457 shutil.copystat(self.test_exe, exec_copy)
1458 self.test_exe = exec_copy
1459
1460 yield tmpdir
1461
1462 def test_init_setpythonhome(self):
1463 # Test Py_SetPythonHome(home) with PYTHONPATH env var
1464 config = self._get_expected_config()
1465 paths = config['config']['module_search_paths']
1466 paths_str = os.path.pathsep.join(paths)
1467
1468 for path in paths:
1469 if not os.path.isdir(path):
1470 continue
1471 if os.path.exists(os.path.join(path, 'os.py')):
1472 home = os.path.dirname(path)
1473 break
1474 else:
1475 self.fail(f"Unable to find home in {paths!r}")
1476
1477 prefix = exec_prefix = home
1478 if MS_WINDOWS:
1479 stdlib = os.path.join(home, "Lib")
1480 # Because we are specifying 'home', module search paths
1481 # are fairly static
1482 expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib]
1483 else:
1484 version = f'{sys.version_info.major}.{sys.version_info.minor}'
1485 stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}')
1486 expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)
1487
1488 config = {
1489 'home': home,
1490 'module_search_paths': expected_paths,
1491 'prefix': prefix,
1492 'base_prefix': prefix,
1493 'exec_prefix': exec_prefix,
1494 'base_exec_prefix': exec_prefix,
1495 'pythonpath_env': paths_str,
1496 'stdlib_dir': stdlib,
1497 }
1498 self.default_program_name(config)
1499 env = {'TESTHOME': home, 'PYTHONPATH': paths_str}
1500 self.check_all_configs("test_init_setpythonhome", config,
1501 api=API_COMPAT, env=env)
1502
1503 def test_init_is_python_build_with_home(self):
1504 # Test _Py_path_config._is_python_build configuration (gh-91985)
1505 config = self._get_expected_config()
1506 paths = config['config']['module_search_paths']
1507 paths_str = os.path.pathsep.join(paths)
1508
1509 for path in paths:
1510 if not os.path.isdir(path):
1511 continue
1512 if os.path.exists(os.path.join(path, 'os.py')):
1513 home = os.path.dirname(path)
1514 break
1515 else:
1516 self.fail(f"Unable to find home in {paths!r}")
1517
1518 prefix = exec_prefix = home
1519 if MS_WINDOWS:
1520 stdlib = os.path.join(home, "Lib")
1521 # Because we are specifying 'home', module search paths
1522 # are fairly static
1523 expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib]
1524 else:
1525 version = f'{sys.version_info.major}.{sys.version_info.minor}'
1526 stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}')
1527 expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)
1528
1529 config = {
1530 'home': home,
1531 'module_search_paths': expected_paths,
1532 'prefix': prefix,
1533 'base_prefix': prefix,
1534 'exec_prefix': exec_prefix,
1535 'base_exec_prefix': exec_prefix,
1536 'pythonpath_env': paths_str,
1537 'stdlib_dir': stdlib,
1538 }
1539 # The code above is taken from test_init_setpythonhome()
1540 env = {'TESTHOME': home, 'PYTHONPATH': paths_str}
1541
1542 env['NEGATIVE_ISPYTHONBUILD'] = '1'
1543 config['_is_python_build'] = 0
1544 self.check_all_configs("test_init_is_python_build", config,
1545 api=API_COMPAT, env=env)
1546
1547 env['NEGATIVE_ISPYTHONBUILD'] = '0'
1548 config['_is_python_build'] = 1
1549 exedir = os.path.dirname(sys.executable)
1550 with open(os.path.join(exedir, 'pybuilddir.txt'), encoding='utf8') as f:
1551 expected_paths[1 if MS_WINDOWS else 2] = os.path.normpath(
1552 os.path.join(exedir, f'{f.read()}\n$'.splitlines()[0]))
1553 if not MS_WINDOWS:
1554 # PREFIX (default) is set when running in build directory
1555 prefix = exec_prefix = sys.prefix
1556 # stdlib calculation (/Lib) is not yet supported
1557 expected_paths[0] = self.module_search_paths(prefix=prefix)[0]
1558 config.update(prefix=prefix, base_prefix=prefix,
1559 exec_prefix=exec_prefix, base_exec_prefix=exec_prefix)
1560 self.check_all_configs("test_init_is_python_build", config,
1561 api=API_COMPAT, env=env)
1562
1563 def copy_paths_by_env(self, config):
1564 all_configs = self._get_expected_config()
1565 paths = all_configs['config']['module_search_paths']
1566 paths_str = os.path.pathsep.join(paths)
1567 config['pythonpath_env'] = paths_str
1568 env = {'PYTHONPATH': paths_str}
1569 return env
1570
1571 @unittest.skipIf(MS_WINDOWS, 'See test_init_pybuilddir_win32')
1572 def test_init_pybuilddir(self):
1573 # Test path configuration with pybuilddir.txt configuration file
1574
1575 with self.tmpdir_with_python() as tmpdir:
1576 # pybuilddir.txt is a sub-directory relative to the current
1577 # directory (tmpdir)
1578 vpath = sysconfig.get_config_var("VPATH") or ''
1579 subdir = 'libdir'
1580 libdir = os.path.join(tmpdir, subdir)
1581 # The stdlib dir is dirname(executable) + VPATH + 'Lib'
1582 stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib'))
1583 os.mkdir(libdir)
1584
1585 filename = os.path.join(tmpdir, 'pybuilddir.txt')
1586 with open(filename, "w", encoding="utf8") as fp:
1587 fp.write(subdir)
1588
1589 module_search_paths = self.module_search_paths()
1590 module_search_paths[-2] = stdlibdir
1591 module_search_paths[-1] = libdir
1592
1593 executable = self.test_exe
1594 config = {
1595 'base_exec_prefix': sysconfig.get_config_var("exec_prefix"),
1596 'base_prefix': sysconfig.get_config_var("prefix"),
1597 'base_executable': executable,
1598 'executable': executable,
1599 'module_search_paths': module_search_paths,
1600 'stdlib_dir': stdlibdir,
1601 }
1602 env = self.copy_paths_by_env(config)
1603 self.check_all_configs("test_init_compat_config", config,
1604 api=API_COMPAT, env=env,
1605 ignore_stderr=True, cwd=tmpdir)
1606
1607 @unittest.skipUnless(MS_WINDOWS, 'See test_init_pybuilddir')
1608 def test_init_pybuilddir_win32(self):
1609 # Test path configuration with pybuilddir.txt configuration file
1610
1611 vpath = sysconfig.get_config_var("VPATH")
1612 subdir = r'PCbuild\arch'
1613 if os.path.normpath(vpath).count(os.sep) == 2:
1614 subdir = os.path.join(subdir, 'instrumented')
1615
1616 with self.tmpdir_with_python(subdir) as tmpdir:
1617 # The prefix is dirname(executable) + VPATH
1618 prefix = os.path.normpath(os.path.join(tmpdir, vpath))
1619 # The stdlib dir is dirname(executable) + VPATH + 'Lib'
1620 stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib'))
1621
1622 filename = os.path.join(tmpdir, 'pybuilddir.txt')
1623 with open(filename, "w", encoding="utf8") as fp:
1624 fp.write(tmpdir)
1625
1626 module_search_paths = self.module_search_paths()
1627 module_search_paths[-3] = os.path.join(tmpdir, os.path.basename(module_search_paths[-3]))
1628 module_search_paths[-2] = tmpdir
1629 module_search_paths[-1] = stdlibdir
1630
1631 executable = self.test_exe
1632 config = {
1633 'base_exec_prefix': prefix,
1634 'base_prefix': prefix,
1635 'base_executable': executable,
1636 'executable': executable,
1637 'prefix': prefix,
1638 'exec_prefix': prefix,
1639 'module_search_paths': module_search_paths,
1640 'stdlib_dir': stdlibdir,
1641 }
1642 env = self.copy_paths_by_env(config)
1643 self.check_all_configs("test_init_compat_config", config,
1644 api=API_COMPAT, env=env,
1645 ignore_stderr=False, cwd=tmpdir)
1646
1647 def test_init_pyvenv_cfg(self):
1648 # Test path configuration with pyvenv.cfg configuration file
1649
1650 with self.tmpdir_with_python() as tmpdir, \
1651 tempfile.TemporaryDirectory() as pyvenv_home:
1652
1653 ver = sys.version_info
1654 base_prefix = sysconfig.get_config_var("prefix")
1655
1656 # gh-128690: base_exec_prefix depends if PLATSTDLIB_LANDMARK exists
1657 platstdlib = os.path.join(base_prefix, PLATSTDLIB_LANDMARK)
1658 change_exec_prefix = not os.path.isdir(platstdlib)
1659
1660 if not MS_WINDOWS:
1661 lib_dynload = os.path.join(pyvenv_home,
1662 sys.platlibdir,
1663 f'python{ver.major}.{ver.minor}{ABI_THREAD}',
1664 'lib-dynload')
1665 os.makedirs(lib_dynload)
1666 else:
1667 lib_folder = os.path.join(pyvenv_home, 'Lib')
1668 os.makedirs(lib_folder)
1669 # getpath.py uses Lib\os.py as the LANDMARK
1670 shutil.copyfile(
1671 os.path.join(support.STDLIB_DIR, 'os.py'),
1672 os.path.join(lib_folder, 'os.py'),
1673 )
1674
1675 filename = os.path.join(tmpdir, 'pyvenv.cfg')
1676 with open(filename, "w", encoding="utf8") as fp:
1677 print("home = %s" % pyvenv_home, file=fp)
1678 print("include-system-site-packages = false", file=fp)
1679
1680 paths = self.module_search_paths()
1681 if not MS_WINDOWS:
1682 if change_exec_prefix:
1683 paths[-1] = lib_dynload
1684 else:
1685 paths = [
1686 os.path.join(tmpdir, os.path.basename(paths[0])),
1687 pyvenv_home,
1688 os.path.join(pyvenv_home, "Lib"),
1689 ]
1690
1691 executable = self.test_exe
1692 base_executable = os.path.join(pyvenv_home, os.path.basename(executable))
1693 config = {
1694 'base_prefix': base_prefix,
1695 'exec_prefix': tmpdir,
1696 'prefix': tmpdir,
1697 'base_executable': base_executable,
1698 'executable': executable,
1699 'module_search_paths': paths,
1700 }
1701 if change_exec_prefix:
1702 config['base_exec_prefix'] = pyvenv_home
1703 if MS_WINDOWS:
1704 config['base_prefix'] = pyvenv_home
1705 config['stdlib_dir'] = os.path.join(pyvenv_home, 'Lib')
1706 config['use_frozen_modules'] = bool(not support.Py_DEBUG)
1707 else:
1708 # cannot reliably assume stdlib_dir here because it
1709 # depends too much on our build. But it ought to be found
1710 config['stdlib_dir'] = self.IGNORE_CONFIG
1711 config['use_frozen_modules'] = bool(not support.Py_DEBUG)
1712
1713 env = self.copy_paths_by_env(config)
1714 self.check_all_configs("test_init_compat_config", config,
1715 api=API_COMPAT, env=env,
1716 ignore_stderr=True, cwd=tmpdir)
1717
1718 @unittest.skipUnless(MS_WINDOWS, 'specific to Windows')
1719 def test_getpath_abspath_win32(self):
1720 # Check _Py_abspath() is passed a backslashed path not to fall back to
1721 # GetFullPathNameW() on startup, which (re-)normalizes the path overly.
1722 # Currently, _Py_normpath() doesn't trim trailing dots and spaces.
1723 CASES = [
1724 ("C:/a. . .", "C:\\a. . ."),
1725 ("C:\\a. . .", "C:\\a. . ."),
1726 ("\\\\?\\C:////a////b. . .", "\\\\?\\C:\\a\\b. . ."),
1727 ("//a/b/c. . .", "\\\\a\\b\\c. . ."),
1728 ("\\\\a\\b\\c. . .", "\\\\a\\b\\c. . ."),
1729 ("a. . .", f"{os.getcwd()}\\a"), # relpath gets fully normalized
1730 ]
1731 out, err = self.run_embedded_interpreter(
1732 "test_init_initialize_config",
1733 env={**remove_python_envvars(),
1734 "PYTHONPATH": os.path.pathsep.join(c[0] for c in CASES)}
1735 )
1736 self.assertEqual(err, "")
1737 try:
1738 out = json.loads(out)
1739 except json.JSONDecodeError:
1740 self.fail(f"fail to decode stdout: {out!r}")
1741
1742 results = out['config']["module_search_paths"]
1743 for (_, expected), result in zip(CASES, results):
1744 self.assertEqual(result, expected)
1745
1746 def test_global_pathconfig(self):
1747 # Test C API functions getting the path configuration:
1748 #
1749 # - Py_GetExecPrefix()
1750 # - Py_GetPath()
1751 # - Py_GetPrefix()
1752 # - Py_GetProgramFullPath()
1753 # - Py_GetProgramName()
1754 # - Py_GetPythonHome()
1755 #
1756 # The global path configuration (_Py_path_config) must be a copy
1757 # of the path configuration of PyInterpreter.config (PyConfig).
1758 ctypes = import_helper.import_module('ctypes')
1759
1760 def get_func(name):
1761 func = getattr(ctypes.pythonapi, name)
1762 func.argtypes = ()
1763 func.restype = ctypes.c_wchar_p
1764 return func
1765
1766 Py_GetPath = get_func('Py_GetPath')
1767 Py_GetPrefix = get_func('Py_GetPrefix')
1768 Py_GetExecPrefix = get_func('Py_GetExecPrefix')
1769 Py_GetProgramName = get_func('Py_GetProgramName')
1770 Py_GetProgramFullPath = get_func('Py_GetProgramFullPath')
1771 Py_GetPythonHome = get_func('Py_GetPythonHome')
1772
1773 config = _testinternalcapi.get_configs()['config']
1774
1775 self.assertEqual(tuple(Py_GetPath().split(os.path.pathsep)),
1776 config['module_search_paths'])
1777 self.assertEqual(Py_GetPrefix(), config['prefix'])
1778 self.assertEqual(Py_GetExecPrefix(), config['exec_prefix'])
1779 self.assertEqual(Py_GetProgramName(), config['program_name'])
1780 self.assertEqual(Py_GetProgramFullPath(), config['executable'])
1781 self.assertEqual(Py_GetPythonHome(), config['home'])
1782
1783 def test_init_warnoptions(self):
1784 # lowest to highest priority
1785 warnoptions = [
1786 'ignore:::PyConfig_Insert0', # PyWideStringList_Insert(0)
1787 'default', # PyConfig.dev_mode=1
1788 'ignore:::env1', # PYTHONWARNINGS env var
1789 'ignore:::env2', # PYTHONWARNINGS env var
1790 'ignore:::cmdline1', # -W opt command line option
1791 'ignore:::cmdline2', # -W opt command line option
1792 'default::BytesWarning', # PyConfig.bytes_warnings=1
1793 'ignore:::PySys_AddWarnOption1', # PySys_AddWarnOption()
1794 'ignore:::PySys_AddWarnOption2', # PySys_AddWarnOption()
1795 'ignore:::PyConfig_BeforeRead', # PyConfig.warnoptions
1796 'ignore:::PyConfig_AfterRead'] # PyWideStringList_Append()
1797 preconfig = {}
1798 config = {
1799 'bytes_warning': 1,
1800 'orig_argv': ['python3',
1801 '-Wignore:::cmdline1',
1802 '-Wignore:::cmdline2'],
1803 }
1804 config_dev_mode(preconfig, config)
1805 config['warnoptions'] = warnoptions
1806 self.check_all_configs("test_init_warnoptions", config, preconfig,
1807 api=API_PYTHON)
1808
1809 @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries")
1810 def test_initconfig_api(self):
1811 preconfig = {
1812 'configure_locale': True,
1813 }
1814 config = {
1815 'pycache_prefix': 'conf_pycache_prefix',
1816 'xoptions': {'faulthandler': True},
1817 'hash_seed': 10,
1818 'use_hash_seed': True,
1819 'perf_profiling': 2,
1820 }
1821 config_dev_mode(preconfig, config)
1822 # Temporarily enable ignore_stderr=True to ignore warnings on JIT builds
1823 # See gh-126255 for more information
1824 self.check_all_configs("test_initconfig_api", config, preconfig,
1825 api=API_ISOLATED, ignore_stderr=True)
1826
1827 def test_initconfig_get_api(self):
1828 self.run_embedded_interpreter("test_initconfig_get_api")
1829
1830 def test_initconfig_exit(self):
1831 self.run_embedded_interpreter("test_initconfig_exit")
1832
1833 def test_initconfig_module(self):
1834 self.run_embedded_interpreter("test_initconfig_module")
1835
1836 def test_get_argc_argv(self):
1837 self.run_embedded_interpreter("test_get_argc_argv")
1838 # ignore output
1839
1840 def test_init_use_frozen_modules(self):
1841 tests = {
1842 ('=on', True),
1843 ('=off', False),
1844 ('=', True),
1845 ('', True),
1846 }
1847 for raw, expected in tests:
1848 optval = f'frozen_modules{raw}'
1849 if raw.startswith('='):
1850 xoption_value = raw[1:]
1851 else:
1852 xoption_value = True
1853 config = {
1854 'parse_argv': True,
1855 'argv': ['-c'],
1856 'orig_argv': ['./argv0', '-X', optval, '-c', 'pass'],
1857 'program_name': './argv0',
1858 'run_command': 'pass\n',
1859 'use_environment': True,
1860 'xoptions': {'frozen_modules': xoption_value},
1861 'use_frozen_modules': expected,
1862 }
1863 env = {'TESTFROZEN': raw[1:]} if raw else None
1864 with self.subTest(repr(raw)):
1865 self.check_all_configs("test_init_use_frozen_modules", config,
1866 api=API_PYTHON, env=env)
1867
1868 def test_init_main_interpreter_settings(self):
1869 OBMALLOC = 1<<5
1870 EXTENSIONS = 1<<8
1871 THREADS = 1<<10
1872 DAEMON_THREADS = 1<<11
1873 FORK = 1<<15
1874 EXEC = 1<<16
1875 expected = {
1876 # All optional features should be enabled.
1877 'feature_flags':
1878 OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS,
1879 'own_gil': True,
1880 }
1881 out, err = self.run_embedded_interpreter(
1882 'test_init_main_interpreter_settings',
1883 )
1884 self.assertEqual(err, '')
1885 try:
1886 out = json.loads(out)
1887 except json.JSONDecodeError:
1888 self.fail(f'fail to decode stdout: {out!r}')
1889
1890 self.assertEqual(out, expected)
1891
1892 @threading_helper.requires_working_threading()
1893 def test_init_in_background_thread(self):
1894 # gh-123022: Check that running Py_Initialize() in a background
1895 # thread doesn't crash.
1896 out, err = self.run_embedded_interpreter("test_init_in_background_thread")
1897 self.assertEqual(err, "")
1898
1899
1900 class AuditingTests(EmbeddingTestsMixin, unittest.TestCase):
1901 def test_open_code_hook(self):
1902 self.run_embedded_interpreter("test_open_code_hook")
1903
1904 def test_audit(self):
1905 self.run_embedded_interpreter("test_audit")
1906
1907 def test_audit_tuple(self):
1908 self.run_embedded_interpreter("test_audit_tuple")
1909
1910 def test_audit_subinterpreter(self):
1911 self.run_embedded_interpreter("test_audit_subinterpreter")
1912
1913 def test_audit_run_command(self):
1914 self.run_embedded_interpreter("test_audit_run_command",
1915 timeout=support.SHORT_TIMEOUT,
1916 returncode=1)
1917
1918 def test_audit_run_file(self):
1919 self.run_embedded_interpreter("test_audit_run_file",
1920 timeout=support.SHORT_TIMEOUT,
1921 returncode=1)
1922
1923 def test_audit_run_interactivehook(self):
1924 startup = os.path.join(self.oldcwd, os_helper.TESTFN) + ".py"
1925 with open(startup, "w", encoding="utf-8") as f:
1926 print("import sys", file=f)
1927 print("sys.__interactivehook__ = lambda: None", file=f)
1928 try:
1929 env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
1930 self.run_embedded_interpreter("test_audit_run_interactivehook",
1931 timeout=support.SHORT_TIMEOUT,
1932 returncode=10, env=env)
1933 finally:
1934 os.unlink(startup)
1935
1936 def test_audit_run_startup(self):
1937 startup = os.path.join(self.oldcwd, os_helper.TESTFN) + ".py"
1938 with open(startup, "w", encoding="utf-8") as f:
1939 print("pass", file=f)
1940 try:
1941 env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
1942 self.run_embedded_interpreter("test_audit_run_startup",
1943 timeout=support.SHORT_TIMEOUT,
1944 returncode=10, env=env)
1945 finally:
1946 os.unlink(startup)
1947
1948 def test_audit_run_stdin(self):
1949 self.run_embedded_interpreter("test_audit_run_stdin",
1950 timeout=support.SHORT_TIMEOUT,
1951 returncode=1)
1952
1953 def test_get_incomplete_frame(self):
1954 self.run_embedded_interpreter("test_get_incomplete_frame")
1955
1956
1957 class MiscTests(EmbeddingTestsMixin, unittest.TestCase):
1958 def test_unicode_id_init(self):
1959 # bpo-42882: Test that _PyUnicode_FromId() works
1960 # when Python is initialized multiples times.
1961 self.run_embedded_interpreter("test_unicode_id_init")
1962
1963 # See bpo-44133
1964 @unittest.skipIf(os.name == 'nt',
1965 'Py_FrozenMain is not exported on Windows')
1966 @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
1967 def test_frozenmain(self):
1968 env = dict(os.environ)
1969 env['PYTHONUNBUFFERED'] = '1'
1970 out, err = self.run_embedded_interpreter("test_frozenmain", env=env)
1971 executable = os.path.realpath('./argv0')
1972 expected = textwrap.dedent(f"""
1973 Frozen Hello World
1974 sys.argv ['./argv0', '-E', 'arg1', 'arg2']
1975 config program_name: ./argv0
1976 config executable: {executable}
1977 config use_environment: True
1978 config configure_c_stdio: True
1979 config buffered_stdio: False
1980 """).lstrip()
1981 self.assertEqual(out, expected)
1982
1983 @unittest.skipUnless(support.Py_DEBUG,
1984 '-X showrefcount requires a Python debug build')
1985 def test_no_memleak(self):
1986 # bpo-1635741: Python must release all memory at exit
1987 tests = (
1988 ('off', 'pass'),
1989 ('on', 'pass'),
1990 ('off', 'import __hello__'),
1991 ('on', 'import __hello__'),
1992 )
1993 for flag, stmt in tests:
1994 xopt = f"frozen_modules={flag}"
1995 cmd = [sys.executable, "-I", "-X", "showrefcount", "-X", xopt, "-c", stmt]
1996 proc = subprocess.run(cmd,
1997 stdout=subprocess.PIPE,
1998 stderr=subprocess.STDOUT,
1999 text=True)
2000 self.assertEqual(proc.returncode, 0)
2001 out = proc.stdout.rstrip()
2002 match = re.match(r'^\[(-?\d+) refs, (-?\d+) blocks\]', out)
2003 if not match:
2004 self.fail(f"unexpected output: {out!a}")
2005 refs = int(match.group(1))
2006 blocks = int(match.group(2))
2007 with self.subTest(frozen_modules=flag, stmt=stmt):
2008 self.assertEqual(refs, 0, out)
2009 self.assertEqual(blocks, 0, out)
2010
2011 @unittest.skipUnless(support.Py_DEBUG,
2012 '-X presite requires a Python debug build')
2013 def test_presite(self):
2014 cmd = [
2015 sys.executable,
2016 "-I", "-X", "presite=test._test_embed_structseq",
2017 "-c", "print('unique-python-message')",
2018 ]
2019 proc = subprocess.run(
2020 cmd,
2021 stdout=subprocess.PIPE,
2022 stderr=subprocess.STDOUT,
2023 text=True,
2024 )
2025 self.assertEqual(proc.returncode, 0)
2026 out = proc.stdout.strip()
2027 self.assertIn("Tests passed", out)
2028 self.assertIn("unique-python-message", out)
2029
2030
2031 if __name__ == "__main__":
2032 unittest.main()