| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | # Run the tests in Programs/_testembed.c (tests for the CPython embedding APIs) | 
					
						
							|  |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2024-08-17 16:04:08 -04:00
										 |  |  | from test.support import import_helper, os_helper, threading_helper, MS_WINDOWS | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from collections import namedtuple | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | import contextlib | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2021-09-17 16:31:31 -06:00
										 |  |  | import os.path | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | import re | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | import subprocess | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | import sysconfig | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-25 09:09:06 +02:00
										 |  |  | if not support.has_subprocess_support: | 
					
						
							|  |  |  |     raise unittest.SkipTest("test module requires subprocess") | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 15:11:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     import _testinternalcapi | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     _testinternalcapi = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 04:01:49 +02:00
										 |  |  | MACOS = (sys.platform == 'darwin') | 
					
						
							| 
									
										
										
										
											2019-05-17 15:20:52 +02:00
										 |  |  | PYMEM_ALLOCATOR_NOT_SET = 0 | 
					
						
							|  |  |  | PYMEM_ALLOCATOR_DEBUG = 2 | 
					
						
							|  |  |  | PYMEM_ALLOCATOR_MALLOC = 3 | 
					
						
							| 
									
										
										
										
											2023-12-11 19:04:48 -05:00
										 |  |  | PYMEM_ALLOCATOR_MIMALLOC = 7 | 
					
						
							|  |  |  | if support.Py_GIL_DISABLED: | 
					
						
							|  |  |  |     ALLOCATOR_FOR_CONFIG = PYMEM_ALLOCATOR_MIMALLOC | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     ALLOCATOR_FOR_CONFIG = PYMEM_ALLOCATOR_MALLOC | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 17:54:59 +02:00
										 |  |  | Py_STATS = hasattr(sys, '_stats_on') | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  | # _PyCoreConfig_InitCompatConfig() | 
					
						
							|  |  |  | API_COMPAT = 1 | 
					
						
							|  |  |  | # _PyCoreConfig_InitPythonConfig() | 
					
						
							|  |  |  | API_PYTHON = 2 | 
					
						
							|  |  |  | # _PyCoreConfig_InitIsolatedConfig() | 
					
						
							|  |  |  | API_ISOLATED = 3 | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 02:12:18 +01:00
										 |  |  | INIT_LOOPS = 4 | 
					
						
							| 
									
										
										
										
											2020-11-05 18:12:33 +01:00
										 |  |  | MAX_HASH_SEED = 4294967295 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  | ABI_THREAD = 't' if sysconfig.get_config_var('Py_GIL_DISABLED') else '' | 
					
						
							| 
									
										
										
										
											2025-02-16 10:33:14 +09:00
										 |  |  | # PLATSTDLIB_LANDMARK copied from Modules/getpath.py | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  | if os.name == 'nt': | 
					
						
							|  |  |  |     PLATSTDLIB_LANDMARK = f'{sys.platlibdir}' | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     VERSION_MAJOR = sys.version_info.major | 
					
						
							|  |  |  |     VERSION_MINOR = sys.version_info.minor | 
					
						
							|  |  |  |     PLATSTDLIB_LANDMARK = (f'{sys.platlibdir}/python{VERSION_MAJOR}.' | 
					
						
							|  |  |  |                            f'{VERSION_MINOR}{ABI_THREAD}/lib-dynload') | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 00:11:40 +00:00
										 |  |  | # If we are running from a build dir, but the stdlib has been installed, | 
					
						
							|  |  |  | # some tests need to expect different results. | 
					
						
							|  |  |  | STDLIB_INSTALL = os.path.join(sys.prefix, sys.platlibdir, | 
					
						
							|  |  |  |     f'python{sys.version_info.major}.{sys.version_info.minor}') | 
					
						
							|  |  |  | if not os.path.isfile(os.path.join(STDLIB_INSTALL, 'os.py')): | 
					
						
							|  |  |  |     STDLIB_INSTALL = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | def debug_build(program): | 
					
						
							|  |  |  |     program = os.path.basename(program) | 
					
						
							|  |  |  |     name = os.path.splitext(program)[0] | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  |     return name.casefold().endswith("_d".casefold()) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-18 00:11:00 +02:00
										 |  |  | def remove_python_envvars(): | 
					
						
							|  |  |  |     env = dict(os.environ) | 
					
						
							|  |  |  |     # Remove PYTHON* environment variables to get deterministic environment | 
					
						
							|  |  |  |     for key in list(env): | 
					
						
							|  |  |  |         if key.startswith('PYTHON'): | 
					
						
							|  |  |  |             del env[key] | 
					
						
							|  |  |  |     return env | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | class EmbeddingTestsMixin: | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |     def setUp(self): | 
					
						
							|  |  |  |         exename = "_testembed" | 
					
						
							| 
									
										
										
										
											2021-10-16 13:16:08 -06:00
										 |  |  |         builddir = os.path.dirname(sys.executable) | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             ext = ("_d" if debug_build(sys.executable) else "") + ".exe" | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |             exename += ext | 
					
						
							| 
									
										
										
										
											2021-10-16 13:16:08 -06:00
										 |  |  |             exepath = builddir | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2021-10-16 13:16:08 -06:00
										 |  |  |             exepath = os.path.join(builddir, 'Programs') | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         self.test_exe = exe = os.path.join(exepath, exename) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:54:17 +05:30
										 |  |  |         if not os.path.exists(exe): | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |             self.skipTest("%r doesn't exist" % exe) | 
					
						
							|  |  |  |         # This is needed otherwise we get a fatal error: | 
					
						
							|  |  |  |         # "Py_Initialize: Unable to get the locale encoding | 
					
						
							|  |  |  |         # LookupError: no codec search functions registered: can't find encoding" | 
					
						
							|  |  |  |         self.oldcwd = os.getcwd() | 
					
						
							| 
									
										
										
										
											2022-06-17 21:54:17 +05:30
										 |  |  |         os.chdir(builddir) | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         os.chdir(self.oldcwd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |     def run_embedded_interpreter(self, *args, env=None, | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                                  timeout=None, returncode=0, input=None, | 
					
						
							|  |  |  |                                  cwd=None): | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         """Runs a test in the embedded interpreter""" | 
					
						
							|  |  |  |         cmd = [self.test_exe] | 
					
						
							|  |  |  |         cmd.extend(args) | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         if env is not None and MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |             # Windows requires at least the SYSTEMROOT environment variable to | 
					
						
							|  |  |  |             # start Python. | 
					
						
							|  |  |  |             env = env.copy() | 
					
						
							|  |  |  |             env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         p = subprocess.Popen(cmd, | 
					
						
							|  |  |  |                              stdout=subprocess.PIPE, | 
					
						
							|  |  |  |                              stderr=subprocess.PIPE, | 
					
						
							|  |  |  |                              universal_newlines=True, | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                              env=env, | 
					
						
							|  |  |  |                              cwd=cwd) | 
					
						
							| 
									
										
										
										
											2019-03-29 15:13:46 +01:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |             (out, err) = p.communicate(input=input, timeout=timeout) | 
					
						
							| 
									
										
										
										
											2019-03-29 15:13:46 +01:00
										 |  |  |         except: | 
					
						
							|  |  |  |             p.terminate() | 
					
						
							|  |  |  |             p.wait() | 
					
						
							|  |  |  |             raise | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |         if p.returncode != returncode and support.verbose: | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |             print(f"--- {cmd} failed ---") | 
					
						
							|  |  |  |             print(f"stdout:\n{out}") | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |             print(f"stderr:\n{err}") | 
					
						
							| 
									
										
										
										
											2023-04-24 17:24:49 -06:00
										 |  |  |             print("------") | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |         self.assertEqual(p.returncode, returncode, | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |                          "bad returncode %d, stderr is %r" % | 
					
						
							|  |  |  |                          (p.returncode, err)) | 
					
						
							|  |  |  |         return out, err | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run_repeated_init_and_subinterpreters(self): | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_and_subinterpreters") | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         self.assertEqual(err, "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # The output from _testembed looks like this: | 
					
						
							| 
									
										
										
										
											2021-06-23 14:13:27 +02:00
										 |  |  |         # --- Pass 1 --- | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728 | 
					
						
							|  |  |  |         # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784 | 
					
						
							|  |  |  |         # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368 | 
					
						
							|  |  |  |         # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200 | 
					
						
							|  |  |  |         # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728 | 
					
						
							| 
									
										
										
										
											2021-06-23 14:13:27 +02:00
										 |  |  |         # --- Pass 2 --- | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         # ... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, " | 
					
						
							|  |  |  |                       r"thread state <(0x[\dA-F]+)>: " | 
					
						
							|  |  |  |                       r"id\(modules\) = ([\d]+)$") | 
					
						
							|  |  |  |         Interp = namedtuple("Interp", "id interp tstate modules") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 14:13:27 +02:00
										 |  |  |         numloops = 1 | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         current_run = [] | 
					
						
							|  |  |  |         for line in out.splitlines(): | 
					
						
							|  |  |  |             if line == "--- Pass {} ---".format(numloops): | 
					
						
							|  |  |  |                 self.assertEqual(len(current_run), 0) | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |                 if support.verbose > 1: | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |                     print(line) | 
					
						
							|  |  |  |                 numloops += 1 | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             self.assertLess(len(current_run), 5) | 
					
						
							|  |  |  |             match = re.match(interp_pat, line) | 
					
						
							|  |  |  |             if match is None: | 
					
						
							|  |  |  |                 self.assertRegex(line, interp_pat) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Parse the line from the loop.  The first line is the main | 
					
						
							|  |  |  |             # interpreter and the 3 afterward are subinterpreters. | 
					
						
							|  |  |  |             interp = Interp(*match.groups()) | 
					
						
							| 
									
										
										
										
											2024-10-08 18:34:11 +10:00
										 |  |  |             if support.verbose > 2: | 
					
						
							|  |  |  |                 # 5 lines per pass is super-spammy, so limit that to -vvv | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |                 print(interp) | 
					
						
							|  |  |  |             self.assertTrue(interp.interp) | 
					
						
							|  |  |  |             self.assertTrue(interp.tstate) | 
					
						
							|  |  |  |             self.assertTrue(interp.modules) | 
					
						
							|  |  |  |             current_run.append(interp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # The last line in the loop should be the same as the first. | 
					
						
							|  |  |  |             if len(current_run) == 5: | 
					
						
							|  |  |  |                 main = current_run[0] | 
					
						
							|  |  |  |                 self.assertEqual(interp, main) | 
					
						
							|  |  |  |                 yield current_run | 
					
						
							|  |  |  |                 current_run = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2021-06-23 14:13:27 +02:00
										 |  |  |     maxDiff = 100 * 50 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |     def test_subinterps_main(self): | 
					
						
							|  |  |  |         for run in self.run_repeated_init_and_subinterpreters(): | 
					
						
							|  |  |  |             main = run[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             self.assertEqual(main.id, '0') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_subinterps_different_ids(self): | 
					
						
							|  |  |  |         for run in self.run_repeated_init_and_subinterpreters(): | 
					
						
							|  |  |  |             main, *subs, _ = run | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             mainid = int(main.id) | 
					
						
							|  |  |  |             for i, sub in enumerate(subs): | 
					
						
							|  |  |  |                 self.assertEqual(sub.id, str(mainid + i + 1)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_subinterps_distinct_state(self): | 
					
						
							|  |  |  |         for run in self.run_repeated_init_and_subinterpreters(): | 
					
						
							|  |  |  |             main, *subs, _ = run | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if '0x0' in main: | 
					
						
							|  |  |  |                 # XXX Fix on Windows (and other platforms): something | 
					
						
							|  |  |  |                 # is going on with the pointers in Programs/_testembed.c. | 
					
						
							|  |  |  |                 # interp.interp is 0x0 and interp.modules is the same | 
					
						
							|  |  |  |                 # between interpreters. | 
					
						
							|  |  |  |                 raise unittest.SkipTest('platform prints pointers as 0x0') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for sub in subs: | 
					
						
							|  |  |  |                 # A new subinterpreter may have the same | 
					
						
							|  |  |  |                 # PyInterpreterState pointer as a previous one if | 
					
						
							|  |  |  |                 # the earlier one has already been destroyed.  So | 
					
						
							|  |  |  |                 # we compare with the main interpreter.  The same | 
					
						
							|  |  |  |                 # applies to tstate. | 
					
						
							|  |  |  |                 self.assertNotEqual(sub.interp, main.interp) | 
					
						
							|  |  |  |                 self.assertNotEqual(sub.tstate, main.tstate) | 
					
						
							|  |  |  |                 self.assertNotEqual(sub.modules, main.modules) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 14:13:27 +02:00
										 |  |  |     def test_repeated_init_and_inittab(self): | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_and_inittab") | 
					
						
							|  |  |  |         self.assertEqual(err, "") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         lines = [f"--- Pass {i} ---" for i in range(1, INIT_LOOPS+1)] | 
					
						
							|  |  |  |         lines = "\n".join(lines) + "\n" | 
					
						
							|  |  |  |         self.assertEqual(out, lines) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |     def test_forced_io_encoding(self): | 
					
						
							|  |  |  |         # Checks forced configuration of embedded interpreter IO streams | 
					
						
							|  |  |  |         env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape") | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_forced_io_encoding", env=env) | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         if support.verbose > 1: | 
					
						
							|  |  |  |             print() | 
					
						
							|  |  |  |             print(out) | 
					
						
							|  |  |  |             print(err) | 
					
						
							|  |  |  |         expected_stream_encoding = "utf-8" | 
					
						
							|  |  |  |         expected_errors = "surrogateescape" | 
					
						
							|  |  |  |         expected_output = '\n'.join([ | 
					
						
							|  |  |  |         "--- Use defaults ---", | 
					
						
							|  |  |  |         "Expected encoding: default", | 
					
						
							|  |  |  |         "Expected errors: default", | 
					
						
							|  |  |  |         "stdin: {in_encoding}:{errors}", | 
					
						
							|  |  |  |         "stdout: {out_encoding}:{errors}", | 
					
						
							|  |  |  |         "stderr: {out_encoding}:backslashreplace", | 
					
						
							|  |  |  |         "--- Set errors only ---", | 
					
						
							|  |  |  |         "Expected encoding: default", | 
					
						
							|  |  |  |         "Expected errors: ignore", | 
					
						
							|  |  |  |         "stdin: {in_encoding}:ignore", | 
					
						
							|  |  |  |         "stdout: {out_encoding}:ignore", | 
					
						
							|  |  |  |         "stderr: {out_encoding}:backslashreplace", | 
					
						
							|  |  |  |         "--- Set encoding only ---", | 
					
						
							| 
									
										
										
										
											2018-08-28 23:26:33 +02:00
										 |  |  |         "Expected encoding: iso8859-1", | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         "Expected errors: default", | 
					
						
							| 
									
										
										
										
											2018-08-28 23:26:33 +02:00
										 |  |  |         "stdin: iso8859-1:{errors}", | 
					
						
							|  |  |  |         "stdout: iso8859-1:{errors}", | 
					
						
							|  |  |  |         "stderr: iso8859-1:backslashreplace", | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         "--- Set encoding and errors ---", | 
					
						
							| 
									
										
										
										
											2018-08-28 23:26:33 +02:00
										 |  |  |         "Expected encoding: iso8859-1", | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         "Expected errors: replace", | 
					
						
							| 
									
										
										
										
											2018-08-28 23:26:33 +02:00
										 |  |  |         "stdin: iso8859-1:replace", | 
					
						
							|  |  |  |         "stdout: iso8859-1:replace", | 
					
						
							|  |  |  |         "stderr: iso8859-1:backslashreplace"]) | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         expected_output = expected_output.format( | 
					
						
							|  |  |  |                                 in_encoding=expected_stream_encoding, | 
					
						
							|  |  |  |                                 out_encoding=expected_stream_encoding, | 
					
						
							|  |  |  |                                 errors=expected_errors) | 
					
						
							|  |  |  |         # This is useful if we ever trip over odd platform behaviour | 
					
						
							|  |  |  |         self.maxDiff = None | 
					
						
							|  |  |  |         self.assertEqual(out.strip(), expected_output) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_pre_initialization_api(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2021-10-07 01:13:48 +02:00
										 |  |  |         Checks some key parts of the C-API that need to work before the runtime | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         is initialized (via Py_Initialize()). | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path)) | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env) | 
					
						
							| 
									
										
										
										
											2024-10-08 18:34:11 +10:00
										 |  |  |         if support.verbose > 1: | 
					
						
							|  |  |  |             print() | 
					
						
							|  |  |  |             print(out) | 
					
						
							|  |  |  |             print(err) | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |             expected_path = self.test_exe | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             expected_path = os.path.join(os.getcwd(), "spam") | 
					
						
							|  |  |  |         expected_output = f"sys.executable: {expected_path}\n" | 
					
						
							|  |  |  |         self.assertIn(expected_output, out) | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_pre_initialization_sys_options(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         Checks that sys.warnoptions and sys._xoptions can be set before the | 
					
						
							|  |  |  |         runtime is initialized (otherwise they won't be effective). | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-06-18 00:11:00 +02:00
										 |  |  |         env = remove_python_envvars() | 
					
						
							|  |  |  |         env['PYTHONPATH'] = os.pathsep.join(sys.path) | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |         out, err = self.run_embedded_interpreter( | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |                         "test_pre_initialization_sys_options", env=env) | 
					
						
							| 
									
										
										
										
											2024-10-08 18:34:11 +10:00
										 |  |  |         if support.verbose > 1: | 
					
						
							|  |  |  |             print() | 
					
						
							|  |  |  |             print(out) | 
					
						
							|  |  |  |             print(err) | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |         expected_output = ( | 
					
						
							|  |  |  |             "sys.warnoptions: ['once', 'module', 'default']\n" | 
					
						
							| 
									
										
										
										
											2022-07-31 16:33:56 +01:00
										 |  |  |             "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n" | 
					
						
							| 
									
										
										
										
											2018-03-25 20:44:30 +10:00
										 |  |  |             "warnings.filters[:3]: ['default', 'module', 'once']\n" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertIn(expected_output, out) | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 22:05:00 +01:00
										 |  |  |     def test_bpo20891(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2020-03-09 20:56:57 +01:00
										 |  |  |         bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not | 
					
						
							|  |  |  |         crash. | 
					
						
							| 
									
										
										
										
											2017-11-30 22:05:00 +01:00
										 |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_bpo20891") | 
					
						
							| 
									
										
										
										
											2017-11-30 22:05:00 +01:00
										 |  |  |         self.assertEqual(out, '') | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 19:14:51 +02:00
										 |  |  |     def test_initialize_twice(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         bpo-33932: Calling Py_Initialize() twice should do nothing (and not | 
					
						
							|  |  |  |         crash!). | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_initialize_twice") | 
					
						
							| 
									
										
										
										
											2018-06-22 19:14:51 +02:00
										 |  |  |         self.assertEqual(out, '') | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 17:34:23 +02:00
										 |  |  |     def test_initialize_pymain(self): | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         bpo-34008: Calling Py_Main() after Py_Initialize() must not fail. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_initialize_pymain") | 
					
						
							| 
									
										
										
										
											2018-07-20 17:34:23 +02:00
										 |  |  |         self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']") | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 15:13:46 +01:00
										 |  |  |     def test_run_main(self): | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter("test_run_main") | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']") | 
					
						
							| 
									
										
										
										
											2019-03-29 15:13:46 +01:00
										 |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 10:30:02 +02:00
										 |  |  |     def test_run_main_loop(self): | 
					
						
							|  |  |  |         # bpo-40413: Calling Py_InitializeFromConfig()+Py_RunMain() multiple | 
					
						
							|  |  |  |         # times must not crash. | 
					
						
							|  |  |  |         nloop = 5 | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_run_main_loop") | 
					
						
							|  |  |  |         self.assertEqual(out, "Py_RunMain(): sys.argv=['-c', 'arg2']\n" * nloop) | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-21 01:42:25 +01:00
										 |  |  |     def test_finalize_structseq(self): | 
					
						
							|  |  |  |         # bpo-46417: Py_Finalize() clears structseq static types. Check that | 
					
						
							|  |  |  |         # sys attributes using struct types still work when | 
					
						
							|  |  |  |         # Py_Finalize()/Py_Initialize() is called multiple times. | 
					
						
							|  |  |  |         # print() calls type->tp_repr(instance) and so checks that the types | 
					
						
							|  |  |  |         # are still working properly. | 
					
						
							|  |  |  |         script = support.findfile('_test_embed_structseq.py') | 
					
						
							|  |  |  |         with open(script, encoding="utf-8") as fp: | 
					
						
							|  |  |  |             code = fp.read() | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_exec", code) | 
					
						
							|  |  |  |         self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 22:01:30 +10:00
										 |  |  |     def test_simple_initialization_api(self): | 
					
						
							|  |  |  |         # _testembed now uses Py_InitializeFromConfig by default | 
					
						
							|  |  |  |         # This case specifically checks Py_Initialize(Ex) still works | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_simple_init") | 
					
						
							|  |  |  |         self.assertEqual(out, 'Finalized\n' * INIT_LOOPS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-06 02:37:28 +02:00
										 |  |  |     @support.requires_specialization | 
					
						
							| 
									
										
										
										
											2024-04-03 15:11:36 +02:00
										 |  |  |     @unittest.skipUnless(support.TEST_MODULES_ENABLED, "requires test modules") | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |     def test_specialized_static_code_gets_unspecialized_at_Py_FINALIZE(self): | 
					
						
							| 
									
										
										
										
											2022-05-03 10:59:12 -04:00
										 |  |  |         # https://github.com/python/cpython/issues/92031 | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 10:49:58 -08:00
										 |  |  |         _testinternalcapi = import_helper.import_module("_testinternalcapi") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         code = textwrap.dedent(f"""\
 | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |             import dis | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  |             import importlib._bootstrap | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |             import opcode | 
					
						
							|  |  |  |             import test.test_dis | 
					
						
							| 
									
										
										
										
											2025-01-31 02:50:54 -08:00
										 |  |  |             import test.support | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             def is_specialized(f): | 
					
						
							|  |  |  |                 for instruction in dis.get_instructions(f, adaptive=True): | 
					
						
							|  |  |  |                     opname = instruction.opname | 
					
						
							|  |  |  |                     if ( | 
					
						
							| 
									
										
										
										
											2023-08-16 23:25:18 +01:00
										 |  |  |                         opname in opcode._specialized_opmap | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |                         # Exclude superinstructions: | 
					
						
							|  |  |  |                         and "__" not in opname | 
					
						
							| 
									
										
										
										
											2024-10-29 11:15:42 +00:00
										 |  |  |                         # LOAD_CONST_IMMORTAL is "specialized", but is | 
					
						
							|  |  |  |                         # inserted during quickening. | 
					
						
							|  |  |  |                         and opname != "LOAD_CONST_IMMORTAL" | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |                     ): | 
					
						
							|  |  |  |                         return True | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  |             func = importlib._bootstrap._handle_fromlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |             # "copy" the code to un-specialize it: | 
					
						
							| 
									
										
										
										
											2025-01-31 02:50:54 -08:00
										 |  |  |             test.support.reset_code(func) | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             assert not is_specialized(func), "specialized instructions found" | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-29 10:49:58 -08:00
										 |  |  |             for _ in range({_testinternalcapi.SPECIALIZATION_THRESHOLD}): | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  |                 func(importlib._bootstrap, ["x"], lambda *args: None) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 10:42:57 -07:00
										 |  |  |             assert is_specialized(func), "no specialized instructions found" | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             print("Tests passed") | 
					
						
							|  |  |  |         """)
 | 
					
						
							| 
									
										
										
										
											2022-05-03 10:59:12 -04:00
										 |  |  |         run = self.run_embedded_interpreter | 
					
						
							| 
									
										
										
										
											2022-05-11 18:54:00 -04:00
										 |  |  |         out, err = run("test_repeated_init_exec", code) | 
					
						
							|  |  |  |         self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS) | 
					
						
							| 
									
										
										
										
											2022-05-03 10:59:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 18:14:50 +03:00
										 |  |  |     def test_ucnhash_capi_reset(self): | 
					
						
							|  |  |  |         # bpo-47182: unicodeobject.c:ucnhash_capi was not reset on shutdown. | 
					
						
							|  |  |  |         code = "print('\\N{digit nine}')" | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_exec", code) | 
					
						
							|  |  |  |         self.assertEqual(out, '9\n' * INIT_LOOPS) | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-13 01:46:39 +09:00
										 |  |  |     def test_datetime_reset_strptime(self): | 
					
						
							|  |  |  |         code = ( | 
					
						
							|  |  |  |             "import datetime;" | 
					
						
							|  |  |  |             "d = datetime.datetime.strptime('2000-01-01', '%Y-%m-%d');" | 
					
						
							|  |  |  |             "print(d.strftime('%Y%m%d'))" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_exec", code) | 
					
						
							|  |  |  |         self.assertEqual(out, '20000101\n' * INIT_LOOPS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 13:57:26 -06:00
										 |  |  |     def test_static_types_inherited_slots(self): | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  |         script = textwrap.dedent("""
 | 
					
						
							|  |  |  |             import test.support | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |             results = [] | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  |             for cls in test.support.iter_builtin_types(): | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |                 for attr, _ in test.support.iter_slot_wrappers(cls): | 
					
						
							|  |  |  |                     wrapper = getattr(cls, attr) | 
					
						
							|  |  |  |                     res = (cls, attr, wrapper) | 
					
						
							|  |  |  |                     results.append(res) | 
					
						
							|  |  |  |             results = ((repr(c), a, repr(w)) for c, a, w in results) | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  |             """)
 | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |         def collate_results(raw): | 
					
						
							|  |  |  |             results = {} | 
					
						
							|  |  |  |             for cls, attr, wrapper in raw: | 
					
						
							|  |  |  |                 key = cls, attr | 
					
						
							|  |  |  |                 assert key not in results, (results, key, wrapper) | 
					
						
							|  |  |  |                 results[key] = wrapper | 
					
						
							|  |  |  |             return results | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ns = {} | 
					
						
							|  |  |  |         exec(script, ns, ns) | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |         main_results = collate_results(ns['results']) | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  |         del ns | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         script += textwrap.dedent("""
 | 
					
						
							|  |  |  |             import json | 
					
						
							|  |  |  |             import sys | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |             text = json.dumps(list(results)) | 
					
						
							| 
									
										
										
										
											2024-07-29 10:23:23 -06:00
										 |  |  |             print(text, file=sys.stderr) | 
					
						
							|  |  |  |             """)
 | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter( | 
					
						
							|  |  |  |                 "test_repeated_init_exec", script, script) | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |         _results = err.split('--- Loop #')[1:] | 
					
						
							|  |  |  |         (_embedded, _reinit, | 
					
						
							|  |  |  |          ) = [json.loads(res.rpartition(' ---\n')[-1]) for res in _results] | 
					
						
							|  |  |  |         embedded_results = collate_results(_embedded) | 
					
						
							|  |  |  |         reinit_results = collate_results(_reinit) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for key, expected in main_results.items(): | 
					
						
							|  |  |  |             cls, attr = key | 
					
						
							|  |  |  |             for src, results in [ | 
					
						
							|  |  |  |                 ('embedded', embedded_results), | 
					
						
							|  |  |  |                 ('reinit', reinit_results), | 
					
						
							|  |  |  |             ]: | 
					
						
							|  |  |  |                 with self.subTest(src, cls=cls, slotattr=attr): | 
					
						
							|  |  |  |                     actual = results.pop(key) | 
					
						
							|  |  |  |                     self.assertEqual(actual, expected) | 
					
						
							| 
									
										
										
										
											2024-07-23 13:57:26 -06:00
										 |  |  |         self.maxDiff = None | 
					
						
							| 
									
										
										
										
											2024-08-12 13:19:33 -06:00
										 |  |  |         self.assertEqual(embedded_results, {}) | 
					
						
							|  |  |  |         self.assertEqual(reinit_results, {}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-23 13:57:26 -06:00
										 |  |  |         self.assertEqual(out, '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-02 22:36:20 +09:00
										 |  |  |     def test_getargs_reset_static_parser(self): | 
					
						
							|  |  |  |         # Test _PyArg_Parser initializations via _PyArg_UnpackKeywords() | 
					
						
							|  |  |  |         # https://github.com/python/cpython/issues/122334 | 
					
						
							|  |  |  |         code = textwrap.dedent("""
 | 
					
						
							| 
									
										
										
										
											2024-08-03 22:15:26 +09:00
										 |  |  |             try: | 
					
						
							|  |  |  |                 import _ssl | 
					
						
							|  |  |  |             except ModuleNotFoundError: | 
					
						
							|  |  |  |                 _ssl = None | 
					
						
							|  |  |  |             if _ssl is not None: | 
					
						
							|  |  |  |                 _ssl.txt2obj(txt='1.3') | 
					
						
							| 
									
										
										
										
											2024-08-02 22:36:20 +09:00
										 |  |  |             print('1') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             import _queue | 
					
						
							|  |  |  |             _queue.SimpleQueue().put_nowait(item=None) | 
					
						
							|  |  |  |             print('2') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             import _zoneinfo | 
					
						
							|  |  |  |             _zoneinfo.ZoneInfo.clear_cache(only_keys=['Foo/Bar']) | 
					
						
							|  |  |  |             print('3') | 
					
						
							|  |  |  |         """)
 | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_repeated_init_exec", code) | 
					
						
							|  |  |  |         self.assertEqual(out, '1\n2\n3\n' * INIT_LOOPS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 15:11:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  | def config_dev_mode(preconfig, config): | 
					
						
							|  |  |  |     preconfig['allocator'] = PYMEM_ALLOCATOR_DEBUG | 
					
						
							|  |  |  |     preconfig['dev_mode'] = 1 | 
					
						
							|  |  |  |     config['dev_mode'] = 1 | 
					
						
							|  |  |  |     config['warnoptions'] = ['default'] | 
					
						
							|  |  |  |     config['faulthandler'] = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-03 15:11:36 +02:00
										 |  |  | @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): | 
					
						
							|  |  |  |     maxDiff = 4096 | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |     UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  |     # Marker to read the default configuration: get_default_config() | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |     GET_DEFAULT_CONFIG = object() | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Marker to ignore a configuration parameter | 
					
						
							|  |  |  |     IGNORE_CONFIG = object() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |     PRE_CONFIG_COMPAT = { | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         '_config_init': API_COMPAT, | 
					
						
							| 
									
										
										
										
											2019-05-17 15:20:52 +02:00
										 |  |  |         'allocator': PYMEM_ALLOCATOR_NOT_SET, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         'parse_argv': False, | 
					
						
							|  |  |  |         'configure_locale': True, | 
					
						
							|  |  |  |         'coerce_c_locale': False, | 
					
						
							|  |  |  |         'coerce_c_locale_warn': False, | 
					
						
							|  |  |  |         'utf8_mode': False, | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |     if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |         PRE_CONFIG_COMPAT.update({ | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'legacy_windows_fs_encoding': False, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |     PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         _config_init=API_PYTHON, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         parse_argv=True, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         coerce_c_locale=GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         utf8_mode=GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |     PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         _config_init=API_ISOLATED, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         configure_locale=False, | 
					
						
							|  |  |  |         isolated=True, | 
					
						
							|  |  |  |         use_environment=False, | 
					
						
							|  |  |  |         utf8_mode=False, | 
					
						
							|  |  |  |         dev_mode=False, | 
					
						
							|  |  |  |         coerce_c_locale=False, | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 02:31:11 +01:00
										 |  |  |     COPY_PRE_CONFIG = [ | 
					
						
							|  |  |  |         'dev_mode', | 
					
						
							|  |  |  |         'isolated', | 
					
						
							|  |  |  |         'use_environment', | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     CONFIG_COMPAT = { | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |         '_config_init': API_COMPAT, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'isolated': False, | 
					
						
							|  |  |  |         'use_environment': True, | 
					
						
							|  |  |  |         'dev_mode': False, | 
					
						
							| 
									
										
										
										
											2019-03-26 02:31:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'install_signal_handlers': True, | 
					
						
							|  |  |  |         'use_hash_seed': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         'hash_seed': 0, | 
					
						
							| 
									
										
										
										
											2022-10-03 13:55:45 -07:00
										 |  |  |         'int_max_str_digits': sys.int_info.default_max_str_digits, | 
					
						
							| 
									
										
										
										
											2023-10-10 19:00:09 +09:00
										 |  |  |         'cpu_count': -1, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'faulthandler': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         'tracemalloc': 0, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |         'perf_profiling': 0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'import_time': False, | 
					
						
							|  |  |  |         'code_debug_ranges': True, | 
					
						
							|  |  |  |         'show_ref_count': False, | 
					
						
							|  |  |  |         'dump_refs': False, | 
					
						
							| 
									
										
										
										
											2023-09-30 19:24:06 +02:00
										 |  |  |         'dump_refs_file': None, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'malloc_stats': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         'filesystem_encoding': GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         'filesystem_errors': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  |         'pycache_prefix': None, | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |         'program_name': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'parse_argv': False, | 
					
						
							| 
									
										
										
										
											2019-03-15 16:03:23 +01:00
										 |  |  |         'argv': [""], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |         'orig_argv': [], | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         'xoptions': {}, | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  |         'warnoptions': [], | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         'pythonpath_env': None, | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         'home': None, | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |         'executable': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2019-06-29 10:34:11 -07:00
										 |  |  |         'base_executable': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         'prefix': GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         'base_prefix': GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         'exec_prefix': GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         'base_exec_prefix': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2019-05-15 02:12:48 +02:00
										 |  |  |         'module_search_paths': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'module_search_paths_set': True, | 
					
						
							| 
									
										
										
										
											2020-06-08 17:28:11 +02:00
										 |  |  |         'platlibdir': sys.platlibdir, | 
					
						
							| 
									
										
										
										
											2021-09-28 12:18:28 -06:00
										 |  |  |         'stdlib_dir': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'site_import': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         'bytes_warning': 0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'warn_default_encoding': False, | 
					
						
							|  |  |  |         'inspect': False, | 
					
						
							|  |  |  |         'interactive': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         'optimization_level': 0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'parser_debug': False, | 
					
						
							|  |  |  |         'write_bytecode': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         'verbose': 0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'quiet': False, | 
					
						
							|  |  |  |         'user_site_directory': True, | 
					
						
							|  |  |  |         'configure_c_stdio': False, | 
					
						
							|  |  |  |         'buffered_stdio': True, | 
					
						
							| 
									
										
										
										
											2018-08-29 19:32:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         'stdio_encoding': GET_DEFAULT_CONFIG, | 
					
						
							|  |  |  |         'stdio_errors': GET_DEFAULT_CONFIG, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'skip_source_first_line': False, | 
					
						
							| 
									
										
										
										
											2019-03-01 13:10:14 +01:00
										 |  |  |         'run_command': None, | 
					
						
							|  |  |  |         'run_module': None, | 
					
						
							|  |  |  |         'run_filename': None, | 
					
						
							| 
									
										
										
										
											2023-10-02 13:59:05 -06:00
										 |  |  |         'sys_path_0': None, | 
					
						
							| 
									
										
										
										
											2019-03-01 13:10:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         '_install_importlib': True, | 
					
						
							| 
									
										
										
										
											2019-05-01 23:51:56 -04:00
										 |  |  |         'check_hash_pycs_mode': 'default', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'pathconfig_warnings': True, | 
					
						
							|  |  |  |         '_init_main': True, | 
					
						
							| 
									
										
										
										
											2022-05-26 00:12:54 +02:00
										 |  |  |         'use_frozen_modules': not support.Py_DEBUG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         'safe_path': False, | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |         '_is_python_build': IGNORE_CONFIG, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-06 17:54:59 +02:00
										 |  |  |     if Py_STATS: | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         CONFIG_COMPAT['_pystats'] = False | 
					
						
							| 
									
										
										
										
											2023-10-14 23:32:57 +02:00
										 |  |  |     if support.Py_DEBUG: | 
					
						
							|  |  |  |         CONFIG_COMPAT['run_presite'] = None | 
					
						
							| 
									
										
										
										
											2024-03-11 11:02:58 -04:00
										 |  |  |     if support.Py_GIL_DISABLED: | 
					
						
							|  |  |  |         CONFIG_COMPAT['enable_gil'] = -1 | 
					
						
							| 
									
										
										
										
											2024-11-04 11:13:32 -08:00
										 |  |  |         CONFIG_COMPAT['tlbc_enabled'] = GET_DEFAULT_CONFIG | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |     if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         CONFIG_COMPAT.update({ | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'legacy_windows_stdio': False, | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											2024-12-09 13:28:57 +08:00
										 |  |  |     if support.is_apple: | 
					
						
							|  |  |  |         CONFIG_COMPAT['use_system_logger'] = False | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     CONFIG_PYTHON = dict(CONFIG_COMPAT, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         _config_init=API_PYTHON, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         configure_c_stdio=True, | 
					
						
							|  |  |  |         parse_argv=True, | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     CONFIG_ISOLATED = dict(CONFIG_COMPAT, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         _config_init=API_ISOLATED, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         isolated=True, | 
					
						
							|  |  |  |         use_environment=False, | 
					
						
							|  |  |  |         user_site_directory=False, | 
					
						
							|  |  |  |         safe_path=True, | 
					
						
							|  |  |  |         dev_mode=False, | 
					
						
							|  |  |  |         install_signal_handlers=False, | 
					
						
							|  |  |  |         use_hash_seed=False, | 
					
						
							|  |  |  |         faulthandler=False, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         tracemalloc=False, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |         perf_profiling=0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |         pathconfig_warnings=False, | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  |     if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         CONFIG_ISOLATED['legacy_windows_stdio'] = False | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |     # global config | 
					
						
							|  |  |  |     DEFAULT_GLOBAL_CONFIG = { | 
					
						
							|  |  |  |         'Py_HasFileSystemDefaultEncoding': 0, | 
					
						
							|  |  |  |         'Py_HashRandomizationFlag': 1, | 
					
						
							|  |  |  |         '_Py_HasFileSystemDefaultEncodeErrors': 0, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |     COPY_GLOBAL_PRE_CONFIG = [ | 
					
						
							|  |  |  |         ('Py_UTF8Mode', 'utf8_mode'), | 
					
						
							|  |  |  |     ] | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |     COPY_GLOBAL_CONFIG = [ | 
					
						
							|  |  |  |         # Copy core config to global config for expected values | 
					
						
							|  |  |  |         # True means that the core config value is inverted (0 => 1 and 1 => 0) | 
					
						
							|  |  |  |         ('Py_BytesWarningFlag', 'bytes_warning'), | 
					
						
							|  |  |  |         ('Py_DebugFlag', 'parser_debug'), | 
					
						
							|  |  |  |         ('Py_DontWriteBytecodeFlag', 'write_bytecode', True), | 
					
						
							|  |  |  |         ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'), | 
					
						
							|  |  |  |         ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'), | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |         ('Py_FrozenFlag', 'pathconfig_warnings', True), | 
					
						
							| 
									
										
										
										
											2019-03-26 02:31:11 +01:00
										 |  |  |         ('Py_IgnoreEnvironmentFlag', 'use_environment', True), | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         ('Py_InspectFlag', 'inspect'), | 
					
						
							|  |  |  |         ('Py_InteractiveFlag', 'interactive'), | 
					
						
							| 
									
										
										
										
											2019-03-26 02:31:11 +01:00
										 |  |  |         ('Py_IsolatedFlag', 'isolated'), | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         ('Py_NoSiteFlag', 'site_import', True), | 
					
						
							|  |  |  |         ('Py_NoUserSiteDirectory', 'user_site_directory', True), | 
					
						
							|  |  |  |         ('Py_OptimizeFlag', 'optimization_level'), | 
					
						
							|  |  |  |         ('Py_QuietFlag', 'quiet'), | 
					
						
							|  |  |  |         ('Py_UnbufferedStdioFlag', 'buffered_stdio', True), | 
					
						
							|  |  |  |         ('Py_VerboseFlag', 'verbose'), | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         COPY_GLOBAL_PRE_CONFIG.extend(( | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |             ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'), | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         )) | 
					
						
							|  |  |  |         COPY_GLOBAL_CONFIG.extend(( | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |             ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'), | 
					
						
							|  |  |  |         )) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |     EXPECTED_CONFIG = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def tearDownClass(cls): | 
					
						
							|  |  |  |         # clear cache | 
					
						
							|  |  |  |         cls.EXPECTED_CONFIG = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |     def main_xoptions(self, xoptions_list): | 
					
						
							|  |  |  |         xoptions = {} | 
					
						
							|  |  |  |         for opt in xoptions_list: | 
					
						
							|  |  |  |             if '=' in opt: | 
					
						
							|  |  |  |                 key, value = opt.split('=', 1) | 
					
						
							|  |  |  |                 xoptions[key] = value | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 xoptions[opt] = True | 
					
						
							|  |  |  |         return xoptions | 
					
						
							| 
									
										
										
										
											2018-08-29 11:47:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |     def _get_expected_config_impl(self): | 
					
						
							|  |  |  |         env = remove_python_envvars() | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         code = textwrap.dedent('''
 | 
					
						
							|  |  |  |             import json | 
					
						
							|  |  |  |             import sys | 
					
						
							| 
									
										
										
										
											2019-05-15 02:12:48 +02:00
										 |  |  |             import _testinternalcapi | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-15 02:12:48 +02:00
										 |  |  |             configs = _testinternalcapi.get_configs() | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             data = json.dumps(configs) | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |             data = data.encode('utf-8') | 
					
						
							|  |  |  |             sys.stdout.buffer.write(data) | 
					
						
							|  |  |  |             sys.stdout.buffer.flush() | 
					
						
							|  |  |  |         ''')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Use -S to not import the site module: get the proper configuration | 
					
						
							|  |  |  |         # when test_embed is run from a venv (bpo-35313) | 
					
						
							| 
									
										
										
										
											2019-05-15 02:12:48 +02:00
										 |  |  |         args = [sys.executable, '-S', '-c', code] | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         proc = subprocess.run(args, env=env, | 
					
						
							|  |  |  |                               stdout=subprocess.PIPE, | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                               stderr=subprocess.PIPE) | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         if proc.returncode: | 
					
						
							|  |  |  |             raise Exception(f"failed to get the default config: " | 
					
						
							|  |  |  |                             f"stdout={proc.stdout!r} stderr={proc.stderr!r}") | 
					
						
							|  |  |  |         stdout = proc.stdout.decode('utf-8') | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |         # ignore stderr | 
					
						
							| 
									
										
										
										
											2019-05-02 15:30:21 -04:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             return json.loads(stdout) | 
					
						
							| 
									
										
										
										
											2019-05-02 15:30:21 -04:00
										 |  |  |         except json.JSONDecodeError: | 
					
						
							|  |  |  |             self.fail(f"fail to decode stdout: {stdout!r}") | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |     def _get_expected_config(self): | 
					
						
							|  |  |  |         cls = InitConfigTests | 
					
						
							|  |  |  |         if cls.EXPECTED_CONFIG is None: | 
					
						
							|  |  |  |             cls.EXPECTED_CONFIG = self._get_expected_config_impl() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # get a copy | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |         configs = {} | 
					
						
							|  |  |  |         for config_key, config_value in cls.EXPECTED_CONFIG.items(): | 
					
						
							|  |  |  |             config = {} | 
					
						
							|  |  |  |             for key, value in config_value.items(): | 
					
						
							|  |  |  |                 if isinstance(value, list): | 
					
						
							|  |  |  |                     value = value.copy() | 
					
						
							|  |  |  |                 config[key] = value | 
					
						
							|  |  |  |             configs[config_key] = config | 
					
						
							|  |  |  |         return configs | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-08 00:20:37 +02:00
										 |  |  |     def get_expected_config(self, expected_preconfig, expected, | 
					
						
							| 
									
										
										
										
											2022-06-17 19:06:49 +09:00
										 |  |  |                             env, api, modify_path_cb=None): | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         configs = self._get_expected_config() | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         pre_config = configs['pre_config'] | 
					
						
							|  |  |  |         for key, value in expected_preconfig.items(): | 
					
						
							|  |  |  |             if value is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |                 expected_preconfig[key] = pre_config[key] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |         if not expected_preconfig['configure_locale'] or api == API_COMPAT: | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             # there is no easy way to get the locale encoding before | 
					
						
							|  |  |  |             # setlocale(LC_CTYPE, "") is called: don't test encodings | 
					
						
							|  |  |  |             for key in ('filesystem_encoding', 'filesystem_errors', | 
					
						
							|  |  |  |                         'stdio_encoding', 'stdio_errors'): | 
					
						
							|  |  |  |                 expected[key] = self.IGNORE_CONFIG | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if not expected_preconfig['configure_locale']: | 
					
						
							|  |  |  |             # UTF-8 Mode depends on the locale. There is no easy way | 
					
						
							|  |  |  |             # to guess if UTF-8 Mode will be enabled or not if the locale | 
					
						
							|  |  |  |             # is not configured. | 
					
						
							|  |  |  |             expected_preconfig['utf8_mode'] = self.IGNORE_CONFIG | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if expected_preconfig['utf8_mode'] == 1: | 
					
						
							|  |  |  |             if expected['filesystem_encoding'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |                 expected['filesystem_encoding'] = 'utf-8' | 
					
						
							|  |  |  |             if expected['filesystem_errors'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |                 expected['filesystem_errors'] = self.UTF8_MODE_ERRORS | 
					
						
							|  |  |  |             if expected['stdio_encoding'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |                 expected['stdio_encoding'] = 'utf-8' | 
					
						
							|  |  |  |             if expected['stdio_errors'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |                 expected['stdio_errors'] = 'surrogateescape' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  |         if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2019-06-29 10:34:11 -07:00
										 |  |  |             default_executable = self.test_exe | 
					
						
							|  |  |  |         elif expected['program_name'] is not self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |             default_executable = os.path.abspath(expected['program_name']) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             default_executable = os.path.join(os.getcwd(), '_testembed') | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |         if expected['executable'] is self.GET_DEFAULT_CONFIG: | 
					
						
							| 
									
										
										
										
											2019-06-29 10:34:11 -07:00
										 |  |  |             expected['executable'] = default_executable | 
					
						
							|  |  |  |         if expected['base_executable'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |             expected['base_executable'] = default_executable | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |         if expected['program_name'] is self.GET_DEFAULT_CONFIG: | 
					
						
							|  |  |  |             expected['program_name'] = './_testembed' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         config = configs['config'] | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         for key, value in expected.items(): | 
					
						
							|  |  |  |             if value is self.GET_DEFAULT_CONFIG: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |                 expected[key] = config[key] | 
					
						
							| 
									
										
										
										
											2019-05-15 02:12:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 17:28:11 +02:00
										 |  |  |         if expected['module_search_paths'] is not self.IGNORE_CONFIG: | 
					
						
							|  |  |  |             pythonpath_env = expected['pythonpath_env'] | 
					
						
							|  |  |  |             if pythonpath_env is not None: | 
					
						
							|  |  |  |                 paths = pythonpath_env.split(os.path.pathsep) | 
					
						
							|  |  |  |                 expected['module_search_paths'] = [*paths, *expected['module_search_paths']] | 
					
						
							|  |  |  |             if modify_path_cb is not None: | 
					
						
							|  |  |  |                 expected['module_search_paths'] = expected['module_search_paths'].copy() | 
					
						
							|  |  |  |                 modify_path_cb(expected['module_search_paths']) | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         for key in self.COPY_PRE_CONFIG: | 
					
						
							|  |  |  |             if key not in expected_preconfig: | 
					
						
							|  |  |  |                 expected_preconfig[key] = expected[key] | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     def check_pre_config(self, configs, expected): | 
					
						
							|  |  |  |         pre_config = dict(configs['pre_config']) | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         for key, value in list(expected.items()): | 
					
						
							|  |  |  |             if value is self.IGNORE_CONFIG: | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  |                 pre_config.pop(key, None) | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |                 del expected[key] | 
					
						
							|  |  |  |         self.assertEqual(pre_config, expected) | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     def check_config(self, configs, expected): | 
					
						
							|  |  |  |         config = dict(configs['config']) | 
					
						
							| 
									
										
										
										
											2022-06-17 19:06:49 +09:00
										 |  |  |         if MS_WINDOWS: | 
					
						
							|  |  |  |             value = config.get(key := 'program_name') | 
					
						
							|  |  |  |             if value and isinstance(value, str): | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:39 +00:00
										 |  |  |                 value = value[:len(value.lower().removesuffix('.exe'))] | 
					
						
							|  |  |  |                 if debug_build(sys.executable): | 
					
						
							|  |  |  |                     value = value[:len(value.lower().removesuffix('_d'))] | 
					
						
							|  |  |  |                 config[key] = value | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  |         for key, value in list(expected.items()): | 
					
						
							|  |  |  |             if value is self.IGNORE_CONFIG: | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  |                 config.pop(key, None) | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  |                 del expected[key] | 
					
						
							| 
									
										
										
										
											2024-04-24 23:00:55 +01:00
										 |  |  |             # Resolve bool/int mismatches to reduce noise in diffs | 
					
						
							|  |  |  |             if isinstance(value, (bool, int)) and isinstance(config.get(key), (bool, int)): | 
					
						
							|  |  |  |                 expected[key] = type(config[key])(expected[key]) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.assertEqual(config, expected) | 
					
						
							| 
									
										
										
										
											2018-08-29 11:47:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     def check_global_config(self, configs): | 
					
						
							|  |  |  |         pre_config = configs['pre_config'] | 
					
						
							|  |  |  |         config = configs['config'] | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 11:54:12 +01:00
										 |  |  |         expected = dict(self.DEFAULT_GLOBAL_CONFIG) | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  |         for item in self.COPY_GLOBAL_CONFIG: | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  |             if len(item) == 3: | 
					
						
							|  |  |  |                 global_key, core_key, opposite = item | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |                 expected[global_key] = 0 if config[core_key] else 1 | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 global_key, core_key = item | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |                 expected[global_key] = config[core_key] | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         for item in self.COPY_GLOBAL_PRE_CONFIG: | 
					
						
							|  |  |  |             if len(item) == 3: | 
					
						
							|  |  |  |                 global_key, core_key, opposite = item | 
					
						
							|  |  |  |                 expected[global_key] = 0 if pre_config[core_key] else 1 | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 global_key, core_key = item | 
					
						
							|  |  |  |                 expected[global_key] = pre_config[core_key] | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.assertEqual(configs['global_config'], expected) | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |     def check_all_configs(self, testname, expected_config=None, | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                           expected_preconfig=None, | 
					
						
							| 
									
										
										
										
											2020-07-08 00:20:37 +02:00
										 |  |  |                           modify_path_cb=None, | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |                           stderr=None, *, api, preconfig_api=None, | 
					
						
							|  |  |  |                           env=None, ignore_stderr=False, cwd=None): | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         new_env = remove_python_envvars() | 
					
						
							|  |  |  |         if env is not None: | 
					
						
							|  |  |  |             new_env.update(env) | 
					
						
							|  |  |  |         env = new_env | 
					
						
							| 
									
										
										
										
											2018-11-14 17:39:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         if preconfig_api is None: | 
					
						
							|  |  |  |             preconfig_api = api | 
					
						
							|  |  |  |         if preconfig_api == API_ISOLATED: | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |             default_preconfig = self.PRE_CONFIG_ISOLATED | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         elif preconfig_api == API_PYTHON: | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |             default_preconfig = self.PRE_CONFIG_PYTHON | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |             default_preconfig = self.PRE_CONFIG_COMPAT | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |         if expected_preconfig is None: | 
					
						
							|  |  |  |             expected_preconfig = {} | 
					
						
							|  |  |  |         expected_preconfig = dict(default_preconfig, **expected_preconfig) | 
					
						
							| 
									
										
										
										
											2020-07-08 00:20:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 03:21:27 +02:00
										 |  |  |         if expected_config is None: | 
					
						
							|  |  |  |             expected_config = {} | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |         if api == API_PYTHON: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             default_config = self.CONFIG_PYTHON | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  |         elif api == API_ISOLATED: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             default_config = self.CONFIG_ISOLATED | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             default_config = self.CONFIG_COMPAT | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         expected_config = dict(default_config, **expected_config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.get_expected_config(expected_preconfig, | 
					
						
							| 
									
										
										
										
											2020-07-08 00:20:37 +02:00
										 |  |  |                                  expected_config, | 
					
						
							|  |  |  |                                  env, | 
					
						
							| 
									
										
										
										
											2022-06-17 19:06:49 +09:00
										 |  |  |                                  api, modify_path_cb) | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |         out, err = self.run_embedded_interpreter(testname, | 
					
						
							|  |  |  |                                                  env=env, cwd=cwd) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |         if stderr is None and not expected_config['verbose']: | 
					
						
							|  |  |  |             stderr = "" | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         if stderr is not None and not ignore_stderr: | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |             self.assertEqual(err.rstrip(), stderr) | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             configs = json.loads(out) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |         except json.JSONDecodeError: | 
					
						
							|  |  |  |             self.fail(f"fail to decode stdout: {out!r}") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_pre_config(configs, expected_preconfig) | 
					
						
							|  |  |  |         self.check_config(configs, expected_config) | 
					
						
							|  |  |  |         self.check_global_config(configs) | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  |         return configs | 
					
						
							| 
									
										
										
										
											2018-11-14 00:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 19:49:02 -06:00
										 |  |  |     @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries") | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |     def test_init_default_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_initialize_config", api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_preinit_compat_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_compat_config", api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-05-22 23:58:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_init_compat_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_compat_config", api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_init_global_config(self): | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         preconfig = { | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'utf8_mode': True, | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         config = { | 
					
						
							|  |  |  |             'program_name': './globalvar', | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'site_import': False, | 
					
						
							|  |  |  |             'bytes_warning': True, | 
					
						
							| 
									
										
										
										
											2019-03-26 16:58:50 +01:00
										 |  |  |             'warnoptions': ['default::BytesWarning'], | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'inspect': True, | 
					
						
							|  |  |  |             'interactive': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |             'optimization_level': 2, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'write_bytecode': False, | 
					
						
							|  |  |  |             'verbose': True, | 
					
						
							|  |  |  |             'quiet': True, | 
					
						
							|  |  |  |             'buffered_stdio': False, | 
					
						
							| 
									
										
										
										
											2018-08-29 11:47:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'user_site_directory': False, | 
					
						
							|  |  |  |             'pathconfig_warnings': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_global_config", config, preconfig, | 
					
						
							|  |  |  |                                api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_init_from_config(self): | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         preconfig = { | 
					
						
							| 
									
										
										
										
											2023-12-11 19:04:48 -05:00
										 |  |  |             'allocator': ALLOCATOR_FOR_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'utf8_mode': True, | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'install_signal_handlers': False, | 
					
						
							|  |  |  |             'use_hash_seed': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |             'hash_seed': 123, | 
					
						
							|  |  |  |             'tracemalloc': 2, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |             'perf_profiling': 0, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'import_time': True, | 
					
						
							|  |  |  |             'code_debug_ranges': False, | 
					
						
							|  |  |  |             'show_ref_count': True, | 
					
						
							|  |  |  |             'malloc_stats': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-29 11:47:29 +02:00
										 |  |  |             'stdio_encoding': 'iso8859-1', | 
					
						
							|  |  |  |             'stdio_errors': 'replace', | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             'pycache_prefix': 'conf_pycache_prefix', | 
					
						
							|  |  |  |             'program_name': './conf_program_name', | 
					
						
							| 
									
										
										
										
											2020-06-08 18:12:59 +02:00
										 |  |  |             'argv': ['-c', 'arg2'], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['python3', | 
					
						
							|  |  |  |                           '-W', 'cmdline_warnoption', | 
					
						
							| 
									
										
										
										
											2022-07-31 16:33:56 +01:00
										 |  |  |                           '-X', 'cmdline_xoption', | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |                           '-c', 'pass', | 
					
						
							|  |  |  |                           'arg2'], | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'parse_argv': True, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'xoptions': { | 
					
						
							|  |  |  |                 'config_xoption1': '3', | 
					
						
							|  |  |  |                 'config_xoption2': '', | 
					
						
							|  |  |  |                 'config_xoption3': True, | 
					
						
							|  |  |  |                 'cmdline_xoption': True, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2019-07-01 19:52:45 +02:00
										 |  |  |             'warnoptions': [ | 
					
						
							|  |  |  |                 'cmdline_warnoption', | 
					
						
							|  |  |  |                 'default::BytesWarning', | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |                 'config_warnoption', | 
					
						
							| 
									
										
										
										
											2019-07-01 19:52:45 +02:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2019-03-29 15:13:46 +01:00
										 |  |  |             'run_command': 'pass\n', | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'site_import': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |             'bytes_warning': 1, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'inspect': True, | 
					
						
							|  |  |  |             'interactive': True, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |             'optimization_level': 2, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'write_bytecode': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |             'verbose': 1, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'quiet': True, | 
					
						
							|  |  |  |             'configure_c_stdio': True, | 
					
						
							|  |  |  |             'buffered_stdio': False, | 
					
						
							|  |  |  |             'user_site_directory': False, | 
					
						
							|  |  |  |             'faulthandler': True, | 
					
						
							| 
									
										
										
										
											2020-06-08 17:28:11 +02:00
										 |  |  |             'platlibdir': 'my_platlibdir', | 
					
						
							|  |  |  |             'module_search_paths': self.IGNORE_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'safe_path': True, | 
					
						
							| 
									
										
										
										
											2022-10-03 13:55:45 -07:00
										 |  |  |             'int_max_str_digits': 31337, | 
					
						
							| 
									
										
										
										
											2023-10-10 19:00:09 +09:00
										 |  |  |             'cpu_count': 4321, | 
					
						
							| 
									
										
										
										
											2018-08-01 02:13:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 23:51:56 -04:00
										 |  |  |             'check_hash_pycs_mode': 'always', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'pathconfig_warnings': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-06 17:54:59 +02:00
										 |  |  |         if Py_STATS: | 
					
						
							|  |  |  |             config['_pystats'] = 1 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_from_config", config, preconfig, | 
					
						
							|  |  |  |                                api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 19:49:02 -06:00
										 |  |  |     @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries") | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |     def test_init_compat_env(self): | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         preconfig = { | 
					
						
							| 
									
										
										
										
											2023-12-11 19:04:48 -05:00
										 |  |  |             'allocator': ALLOCATOR_FOR_CONFIG, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'use_hash_seed': True, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'hash_seed': 42, | 
					
						
							|  |  |  |             'tracemalloc': 2, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'import_time': True, | 
					
						
							|  |  |  |             'code_debug_ranges': False, | 
					
						
							|  |  |  |             'malloc_stats': True, | 
					
						
							|  |  |  |             'inspect': True, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'optimization_level': 2, | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             'pythonpath_env': '/my/path', | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'pycache_prefix': 'env_pycache_prefix', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'write_bytecode': False, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'verbose': 1, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'buffered_stdio': False, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'stdio_encoding': 'iso8859-1', | 
					
						
							|  |  |  |             'stdio_errors': 'replace', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'user_site_directory': False, | 
					
						
							|  |  |  |             'faulthandler': True, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'warnoptions': ['EnvVar'], | 
					
						
							| 
									
										
										
										
											2020-06-08 17:28:11 +02:00
										 |  |  |             'platlibdir': 'env_platlibdir', | 
					
						
							|  |  |  |             'module_search_paths': self.IGNORE_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'safe_path': True, | 
					
						
							| 
									
										
										
										
											2022-10-03 13:55:45 -07:00
										 |  |  |             'int_max_str_digits': 4567, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |             'perf_profiling': 1, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-06 17:54:59 +02:00
										 |  |  |         if Py_STATS: | 
					
						
							|  |  |  |             config['_pystats'] = 1 | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_compat_env", config, preconfig, | 
					
						
							|  |  |  |                                api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-03-05 17:37:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 19:49:02 -06:00
										 |  |  |     @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries") | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |     def test_init_python_env(self): | 
					
						
							|  |  |  |         preconfig = { | 
					
						
							| 
									
										
										
										
											2023-12-11 19:04:48 -05:00
										 |  |  |             'allocator': ALLOCATOR_FOR_CONFIG, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'utf8_mode': 1, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'use_hash_seed': True, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'hash_seed': 42, | 
					
						
							|  |  |  |             'tracemalloc': 2, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'import_time': True, | 
					
						
							|  |  |  |             'code_debug_ranges': False, | 
					
						
							|  |  |  |             'malloc_stats': True, | 
					
						
							|  |  |  |             'inspect': True, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'optimization_level': 2, | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |             'pythonpath_env': '/my/path', | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'pycache_prefix': 'env_pycache_prefix', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'write_bytecode': False, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'verbose': 1, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'buffered_stdio': False, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'stdio_encoding': 'iso8859-1', | 
					
						
							|  |  |  |             'stdio_errors': 'replace', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'user_site_directory': False, | 
					
						
							|  |  |  |             'faulthandler': True, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |             'warnoptions': ['EnvVar'], | 
					
						
							| 
									
										
										
										
											2020-06-08 17:28:11 +02:00
										 |  |  |             'platlibdir': 'env_platlibdir', | 
					
						
							|  |  |  |             'module_search_paths': self.IGNORE_CONFIG, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'safe_path': True, | 
					
						
							| 
									
										
										
										
											2022-10-03 13:55:45 -07:00
										 |  |  |             'int_max_str_digits': 4567, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |             'perf_profiling': 1, | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-09-06 17:54:59 +02:00
										 |  |  |         if Py_STATS: | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             config['_pystats'] = True | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_python_env", config, preconfig, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-23 04:12:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 17:37:44 +01:00
										 |  |  |     def test_init_env_dev_mode(self): | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG) | 
					
						
							|  |  |  |         config = dict(dev_mode=1, | 
					
						
							|  |  |  |                       faulthandler=1, | 
					
						
							| 
									
										
										
										
											2019-03-26 16:58:50 +01:00
										 |  |  |                       warnoptions=['default']) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_env_dev_mode", config, preconfig, | 
					
						
							|  |  |  |                                api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 02:31:11 +01:00
										 |  |  |     def test_init_env_dev_mode_alloc(self): | 
					
						
							| 
									
										
										
										
											2023-12-11 19:04:48 -05:00
										 |  |  |         preconfig = dict(allocator=ALLOCATOR_FOR_CONFIG) | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |         config = dict(dev_mode=1, | 
					
						
							|  |  |  |                       faulthandler=1, | 
					
						
							| 
									
										
										
										
											2019-03-26 16:58:50 +01:00
										 |  |  |                       warnoptions=['default']) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_env_dev_mode_alloc", config, preconfig, | 
					
						
							|  |  |  |                                api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-03-06 12:51:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |     def test_init_dev_mode(self): | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         preconfig = {} | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'faulthandler': True, | 
					
						
							|  |  |  |             'dev_mode': True, | 
					
						
							| 
									
										
										
										
											2019-03-26 16:58:50 +01:00
										 |  |  |             'warnoptions': ['default'], | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         config_dev_mode(preconfig, config) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_dev_mode", config, preconfig, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_preinit_parse_argv(self): | 
					
						
							|  |  |  |         # Pre-initialize implicitly using argv: make sure that -X dev | 
					
						
							|  |  |  |         # is used to configure the allocation in preinitialization | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         preconfig = {} | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2019-12-09 17:34:02 +01:00
										 |  |  |             'argv': ['script.py'], | 
					
						
							| 
									
										
										
										
											2022-05-06 01:34:11 +02:00
										 |  |  |             'orig_argv': ['python3', '-X', 'dev', '-P', 'script.py'], | 
					
						
							| 
									
										
										
										
											2019-12-09 17:34:02 +01:00
										 |  |  |             'run_filename': os.path.abspath('script.py'), | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'dev_mode': True, | 
					
						
							|  |  |  |             'faulthandler': True, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |             'warnoptions': ['default'], | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'xoptions': {'dev': True}, | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'safe_path': True, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         config_dev_mode(preconfig, config) | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_parse_argv", config, preconfig, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_preinit_dont_parse_argv(self): | 
					
						
							|  |  |  |         # -X dev must be ignored by isolated preconfiguration | 
					
						
							|  |  |  |         preconfig = { | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'isolated': False, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-06-08 18:12:59 +02:00
										 |  |  |         argv = ["python3", | 
					
						
							| 
									
										
										
										
											2022-05-06 01:34:11 +02:00
										 |  |  |                "-E", "-I", "-P", | 
					
						
							| 
									
										
										
										
											2020-06-08 18:12:59 +02:00
										 |  |  |                "-X", "dev", | 
					
						
							|  |  |  |                "-X", "utf8", | 
					
						
							|  |  |  |                "script.py"] | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2020-06-08 18:12:59 +02:00
										 |  |  |             'argv': argv, | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': argv, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'isolated': False, | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_dont_parse_argv", config, preconfig, | 
					
						
							|  |  |  |                                api=API_ISOLATED) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |     def test_init_isolated_flag(self): | 
					
						
							| 
									
										
										
										
											2019-03-25 23:19:57 +01:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'isolated': True, | 
					
						
							|  |  |  |             'safe_path': True, | 
					
						
							|  |  |  |             'use_environment': False, | 
					
						
							|  |  |  |             'user_site_directory': False, | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_isolated_flag", config, api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-27 00:26:18 +01:00
										 |  |  |     def test_preinit_isolated1(self): | 
					
						
							|  |  |  |         # _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set | 
					
						
							|  |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'isolated': True, | 
					
						
							|  |  |  |             'safe_path': True, | 
					
						
							|  |  |  |             'use_environment': False, | 
					
						
							|  |  |  |             'user_site_directory': False, | 
					
						
							| 
									
										
										
										
											2019-03-27 00:26:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_isolated1", config, api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-03-27 00:26:18 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_preinit_isolated2(self): | 
					
						
							|  |  |  |         # _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1 | 
					
						
							|  |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'isolated': True, | 
					
						
							|  |  |  |             'safe_path': True, | 
					
						
							|  |  |  |             'use_environment': False, | 
					
						
							|  |  |  |             'user_site_directory': False, | 
					
						
							| 
									
										
										
										
											2019-03-27 00:26:18 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_isolated2", config, api=API_COMPAT) | 
					
						
							| 
									
										
										
										
											2019-03-27 00:26:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |     def test_preinit_isolated_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_isolated_config", api=API_ISOLATED) | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |     def test_init_isolated_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_isolated_config", api=API_ISOLATED) | 
					
						
							| 
									
										
										
										
											2019-05-23 00:57:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_preinit_python_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_preinit_python_config", api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_init_python_config(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_python_config", api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  |     def test_init_dont_configure_locale(self): | 
					
						
							|  |  |  |         # _PyPreConfig.configure_locale=0 | 
					
						
							|  |  |  |         preconfig = { | 
					
						
							|  |  |  |             'configure_locale': 0, | 
					
						
							| 
									
										
										
										
											2019-05-20 16:38:48 +02:00
										 |  |  |             'coerce_c_locale': 0, | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_dont_configure_locale", {}, preconfig, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-17 22:44:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     @unittest.skip('as of 3.11 this test no longer works because ' | 
					
						
							|  |  |  |                    'path calculations do not occur on read') | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |     def test_init_read_set(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |             'program_name': './init_read_set', | 
					
						
							|  |  |  |             'executable': 'my_executable', | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             'base_executable': 'my_executable', | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-23 16:57:54 +01:00
										 |  |  |         def modify_path(path): | 
					
						
							|  |  |  |             path.insert(1, "test_path_insert1") | 
					
						
							|  |  |  |             path.append("test_path_append") | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_read_set", config, | 
					
						
							|  |  |  |                                api=API_PYTHON, | 
					
						
							| 
									
										
										
										
											2019-08-23 16:57:54 +01:00
										 |  |  |                                modify_path_cb=modify_path) | 
					
						
							| 
									
										
										
										
											2019-05-14 22:01:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 18:03:08 +01:00
										 |  |  |     def test_init_sys_add(self): | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             'faulthandler': 1, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             'xoptions': { | 
					
						
							|  |  |  |                 'config_xoption': True, | 
					
						
							|  |  |  |                 'cmdline_xoption': True, | 
					
						
							|  |  |  |                 'sysadd_xoption': True, | 
					
						
							|  |  |  |                 'faulthandler': True, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2019-08-23 18:03:08 +01:00
										 |  |  |             'warnoptions': [ | 
					
						
							|  |  |  |                 'ignore:::cmdline_warnoption', | 
					
						
							|  |  |  |                 'ignore:::sysadd_warnoption', | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |                 'ignore:::config_warnoption', | 
					
						
							| 
									
										
										
										
											2019-08-23 18:03:08 +01:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['python3', | 
					
						
							|  |  |  |                           '-W', 'ignore:::cmdline_warnoption', | 
					
						
							| 
									
										
										
										
											2022-07-31 16:33:56 +01:00
										 |  |  |                           '-X', 'cmdline_xoption'], | 
					
						
							| 
									
										
										
										
											2019-08-23 18:03:08 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-31 16:33:56 +01:00
										 |  |  |         self.check_all_configs("test_init_sys_add", config, api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-08-23 18:03:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |     def test_init_run_main(self): | 
					
						
							|  |  |  |         code = ('import _testinternalcapi, json; ' | 
					
						
							|  |  |  |                 'print(json.dumps(_testinternalcapi.get_configs()))') | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |             'argv': ['-c', 'arg2'], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['python3', '-c', code, 'arg2'], | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |             'program_name': './python3', | 
					
						
							|  |  |  |             'run_command': code + '\n', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'parse_argv': True, | 
					
						
							| 
									
										
										
										
											2023-10-02 13:59:05 -06:00
										 |  |  |             'sys_path_0': '', | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_run_main", config, api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:38:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |     def test_init_parse_argv(self): | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'parse_argv': True, | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |             'argv': ['-c', 'arg1', '-v', 'arg3'], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'], | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |             'program_name': './argv0', | 
					
						
							|  |  |  |             'run_command': 'pass\n', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'use_environment': False, | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_parse_argv", config, api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-16 17:02:56 +02:00
										 |  |  |     def test_init_dont_parse_argv(self): | 
					
						
							| 
									
										
										
										
											2019-05-20 11:02:00 +02:00
										 |  |  |         pre_config = { | 
					
						
							|  |  |  |             'parse_argv': 0, | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             'parse_argv': False, | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |             'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'], | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'], | 
					
						
							| 
									
										
										
										
											2019-05-17 19:01:14 +02:00
										 |  |  |             'program_name': './argv0', | 
					
						
							| 
									
										
										
										
											2019-05-16 17:02:56 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-27 16:39:22 +02:00
										 |  |  |         self.check_all_configs("test_init_dont_parse_argv", config, pre_config, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							| 
									
										
										
										
											2019-05-16 17:02:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |     def default_program_name(self, config): | 
					
						
							|  |  |  |         if MS_WINDOWS: | 
					
						
							|  |  |  |             program_name = 'python' | 
					
						
							|  |  |  |             executable = self.test_exe | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             program_name = 'python3' | 
					
						
							| 
									
										
										
										
											2019-09-26 04:01:49 +02:00
										 |  |  |             if MACOS: | 
					
						
							|  |  |  |                 executable = self.test_exe | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 executable = shutil.which(program_name) or '' | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         config.update({ | 
					
						
							|  |  |  |             'program_name': program_name, | 
					
						
							|  |  |  |             'base_executable': executable, | 
					
						
							|  |  |  |             'executable': executable, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |     def test_init_setpath(self): | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         # Test Py_SetPath() | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         config = self._get_expected_config() | 
					
						
							|  |  |  |         paths = config['config']['module_search_paths'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             'module_search_paths': paths, | 
					
						
							|  |  |  |             'prefix': '', | 
					
						
							|  |  |  |             'base_prefix': '', | 
					
						
							|  |  |  |             'exec_prefix': '', | 
					
						
							|  |  |  |             'base_exec_prefix': '', | 
					
						
							| 
									
										
										
										
											2021-09-28 12:18:28 -06:00
										 |  |  |              # The current getpath.c doesn't determine the stdlib dir | 
					
						
							|  |  |  |              # in this case. | 
					
						
							|  |  |  |             'stdlib_dir': '', | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         self.default_program_name(config) | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         env = {'TESTPATH': os.path.pathsep.join(paths)} | 
					
						
							| 
									
										
										
										
											2020-07-06 17:32:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         self.check_all_configs("test_init_setpath", config, | 
					
						
							|  |  |  |                                api=API_COMPAT, env=env, | 
					
						
							|  |  |  |                                ignore_stderr=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |     def test_init_setpath_config(self): | 
					
						
							|  |  |  |         # Test Py_SetPath() with PyConfig | 
					
						
							|  |  |  |         config = self._get_expected_config() | 
					
						
							|  |  |  |         paths = config['config']['module_search_paths'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             # set by Py_SetPath() | 
					
						
							|  |  |  |             'module_search_paths': paths, | 
					
						
							|  |  |  |             'prefix': '', | 
					
						
							|  |  |  |             'base_prefix': '', | 
					
						
							|  |  |  |             'exec_prefix': '', | 
					
						
							|  |  |  |             'base_exec_prefix': '', | 
					
						
							| 
									
										
										
										
											2021-09-28 12:18:28 -06:00
										 |  |  |              # The current getpath.c doesn't determine the stdlib dir | 
					
						
							|  |  |  |              # in this case. | 
					
						
							|  |  |  |             'stdlib_dir': '', | 
					
						
							| 
									
										
										
										
											2022-05-26 00:12:54 +02:00
										 |  |  |             'use_frozen_modules': not support.Py_DEBUG, | 
					
						
							| 
									
										
										
										
											2021-10-07 01:13:48 +02:00
										 |  |  |             # overridden by PyConfig | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |             'program_name': 'conf_program_name', | 
					
						
							|  |  |  |             'base_executable': 'conf_executable', | 
					
						
							|  |  |  |             'executable': 'conf_executable', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         env = {'TESTPATH': os.path.pathsep.join(paths)} | 
					
						
							|  |  |  |         self.check_all_configs("test_init_setpath_config", config, | 
					
						
							| 
									
										
										
										
											2019-09-26 04:01:49 +02:00
										 |  |  |                                api=API_PYTHON, env=env, ignore_stderr=True) | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |     def module_search_paths(self, prefix=None, exec_prefix=None): | 
					
						
							|  |  |  |         config = self._get_expected_config() | 
					
						
							|  |  |  |         if prefix is None: | 
					
						
							|  |  |  |             prefix = config['config']['prefix'] | 
					
						
							|  |  |  |         if exec_prefix is None: | 
					
						
							|  |  |  |             exec_prefix = config['config']['prefix'] | 
					
						
							|  |  |  |         if MS_WINDOWS: | 
					
						
							|  |  |  |             return config['config']['module_search_paths'] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             ver = sys.version_info | 
					
						
							|  |  |  |             return [ | 
					
						
							| 
									
										
										
										
											2020-03-10 09:53:09 +01:00
										 |  |  |                 os.path.join(prefix, sys.platlibdir, | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |                              f'python{ver.major}{ver.minor}{ABI_THREAD}.zip'), | 
					
						
							| 
									
										
										
										
											2020-03-10 09:53:09 +01:00
										 |  |  |                 os.path.join(prefix, sys.platlibdir, | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |                              f'python{ver.major}.{ver.minor}{ABI_THREAD}'), | 
					
						
							| 
									
										
										
										
											2020-03-10 09:53:09 +01:00
										 |  |  |                 os.path.join(exec_prefix, sys.platlibdir, | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |                              f'python{ver.major}.{ver.minor}{ABI_THREAD}', 'lib-dynload'), | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @contextlib.contextmanager | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     def tmpdir_with_python(self, subdir=None): | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |         # Temporary directory with a copy of the Python program | 
					
						
							|  |  |  |         with tempfile.TemporaryDirectory() as tmpdir: | 
					
						
							| 
									
										
										
										
											2019-09-25 16:30:36 +02:00
										 |  |  |             # bpo-38234: On macOS and FreeBSD, the temporary directory | 
					
						
							|  |  |  |             # can be symbolic link. For example, /tmp can be a symbolic link | 
					
						
							|  |  |  |             # to /var/tmp. Call realpath() to resolve all symbolic links. | 
					
						
							|  |  |  |             tmpdir = os.path.realpath(tmpdir) | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             if subdir: | 
					
						
							|  |  |  |                 tmpdir = os.path.normpath(os.path.join(tmpdir, subdir)) | 
					
						
							|  |  |  |                 os.makedirs(tmpdir) | 
					
						
							| 
									
										
										
										
											2019-09-25 16:30:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             if MS_WINDOWS: | 
					
						
							|  |  |  |                 # Copy pythonXY.dll (or pythonXY_d.dll) | 
					
						
							| 
									
										
										
										
											2022-04-06 11:56:31 +01:00
										 |  |  |                 import fnmatch | 
					
						
							|  |  |  |                 exedir = os.path.dirname(self.test_exe) | 
					
						
							|  |  |  |                 for f in os.listdir(exedir): | 
					
						
							|  |  |  |                     if fnmatch.fnmatch(f, '*.dll'): | 
					
						
							|  |  |  |                         shutil.copyfile(os.path.join(exedir, f), os.path.join(tmpdir, f)) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             # Copy Python program | 
					
						
							|  |  |  |             exec_copy = os.path.join(tmpdir, os.path.basename(self.test_exe)) | 
					
						
							|  |  |  |             shutil.copyfile(self.test_exe, exec_copy) | 
					
						
							|  |  |  |             shutil.copystat(self.test_exe, exec_copy) | 
					
						
							|  |  |  |             self.test_exe = exec_copy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             yield tmpdir | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |     def test_init_setpythonhome(self): | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         # Test Py_SetPythonHome(home) with PYTHONPATH env var | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         config = self._get_expected_config() | 
					
						
							|  |  |  |         paths = config['config']['module_search_paths'] | 
					
						
							|  |  |  |         paths_str = os.path.pathsep.join(paths) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for path in paths: | 
					
						
							|  |  |  |             if not os.path.isdir(path): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if os.path.exists(os.path.join(path, 'os.py')): | 
					
						
							|  |  |  |                 home = os.path.dirname(path) | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail(f"Unable to find home in {paths!r}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         prefix = exec_prefix = home | 
					
						
							| 
									
										
										
										
											2021-10-14 14:48:32 -06:00
										 |  |  |         if MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             stdlib = os.path.join(home, "Lib") | 
					
						
							|  |  |  |             # Because we are specifying 'home', module search paths | 
					
						
							|  |  |  |             # are fairly static | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:39 +00:00
										 |  |  |             expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib] | 
					
						
							| 
									
										
										
										
											2021-10-14 14:48:32 -06:00
										 |  |  |         else: | 
					
						
							|  |  |  |             version = f'{sys.version_info.major}.{sys.version_info.minor}' | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |             stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}') | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             expected_paths = self.module_search_paths(prefix=home, exec_prefix=home) | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             'home': home, | 
					
						
							|  |  |  |             'module_search_paths': expected_paths, | 
					
						
							|  |  |  |             'prefix': prefix, | 
					
						
							|  |  |  |             'base_prefix': prefix, | 
					
						
							|  |  |  |             'exec_prefix': exec_prefix, | 
					
						
							|  |  |  |             'base_exec_prefix': exec_prefix, | 
					
						
							|  |  |  |             'pythonpath_env': paths_str, | 
					
						
							| 
									
										
										
										
											2021-10-14 14:48:32 -06:00
										 |  |  |             'stdlib_dir': stdlib, | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-26 02:22:35 +02:00
										 |  |  |         self.default_program_name(config) | 
					
						
							|  |  |  |         env = {'TESTHOME': home, 'PYTHONPATH': paths_str} | 
					
						
							| 
									
										
										
										
											2019-09-24 18:21:02 +02:00
										 |  |  |         self.check_all_configs("test_init_setpythonhome", config, | 
					
						
							|  |  |  |                                api=API_COMPAT, env=env) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-17 06:41:57 +09:00
										 |  |  |     def test_init_is_python_build_with_home(self): | 
					
						
							|  |  |  |         # Test _Py_path_config._is_python_build configuration (gh-91985) | 
					
						
							|  |  |  |         config = self._get_expected_config() | 
					
						
							|  |  |  |         paths = config['config']['module_search_paths'] | 
					
						
							|  |  |  |         paths_str = os.path.pathsep.join(paths) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for path in paths: | 
					
						
							|  |  |  |             if not os.path.isdir(path): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if os.path.exists(os.path.join(path, 'os.py')): | 
					
						
							|  |  |  |                 home = os.path.dirname(path) | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.fail(f"Unable to find home in {paths!r}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         prefix = exec_prefix = home | 
					
						
							|  |  |  |         if MS_WINDOWS: | 
					
						
							|  |  |  |             stdlib = os.path.join(home, "Lib") | 
					
						
							|  |  |  |             # Because we are specifying 'home', module search paths | 
					
						
							|  |  |  |             # are fairly static | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:39 +00:00
										 |  |  |             expected_paths = [paths[0], os.path.join(home, 'DLLs'), stdlib] | 
					
						
							| 
									
										
										
										
											2022-06-17 06:41:57 +09:00
										 |  |  |         else: | 
					
						
							|  |  |  |             version = f'{sys.version_info.major}.{sys.version_info.minor}' | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |             stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}') | 
					
						
							| 
									
										
										
										
											2022-06-17 06:41:57 +09:00
										 |  |  |             expected_paths = self.module_search_paths(prefix=home, exec_prefix=home) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             'home': home, | 
					
						
							|  |  |  |             'module_search_paths': expected_paths, | 
					
						
							|  |  |  |             'prefix': prefix, | 
					
						
							|  |  |  |             'base_prefix': prefix, | 
					
						
							|  |  |  |             'exec_prefix': exec_prefix, | 
					
						
							|  |  |  |             'base_exec_prefix': exec_prefix, | 
					
						
							|  |  |  |             'pythonpath_env': paths_str, | 
					
						
							|  |  |  |             'stdlib_dir': stdlib, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         # The code above is taken from test_init_setpythonhome() | 
					
						
							|  |  |  |         env = {'TESTHOME': home, 'PYTHONPATH': paths_str} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         env['NEGATIVE_ISPYTHONBUILD'] = '1' | 
					
						
							|  |  |  |         config['_is_python_build'] = 0 | 
					
						
							|  |  |  |         self.check_all_configs("test_init_is_python_build", config, | 
					
						
							|  |  |  |                                api=API_COMPAT, env=env) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         env['NEGATIVE_ISPYTHONBUILD'] = '0' | 
					
						
							|  |  |  |         config['_is_python_build'] = 1 | 
					
						
							|  |  |  |         exedir = os.path.dirname(sys.executable) | 
					
						
							|  |  |  |         with open(os.path.join(exedir, 'pybuilddir.txt'), encoding='utf8') as f: | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:39 +00:00
										 |  |  |             expected_paths[1 if MS_WINDOWS else 2] = os.path.normpath( | 
					
						
							| 
									
										
										
										
											2022-06-17 06:41:57 +09:00
										 |  |  |                 os.path.join(exedir, f'{f.read()}\n$'.splitlines()[0])) | 
					
						
							|  |  |  |         if not MS_WINDOWS: | 
					
						
							|  |  |  |             # PREFIX (default) is set when running in build directory | 
					
						
							|  |  |  |             prefix = exec_prefix = sys.prefix | 
					
						
							|  |  |  |             # stdlib calculation (/Lib) is not yet supported | 
					
						
							|  |  |  |             expected_paths[0] = self.module_search_paths(prefix=prefix)[0] | 
					
						
							|  |  |  |             config.update(prefix=prefix, base_prefix=prefix, | 
					
						
							|  |  |  |                           exec_prefix=exec_prefix, base_exec_prefix=exec_prefix) | 
					
						
							|  |  |  |         self.check_all_configs("test_init_is_python_build", config, | 
					
						
							|  |  |  |                                api=API_COMPAT, env=env) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |     def copy_paths_by_env(self, config): | 
					
						
							|  |  |  |         all_configs = self._get_expected_config() | 
					
						
							|  |  |  |         paths = all_configs['config']['module_search_paths'] | 
					
						
							|  |  |  |         paths_str = os.path.pathsep.join(paths) | 
					
						
							|  |  |  |         config['pythonpath_env'] = paths_str | 
					
						
							|  |  |  |         env = {'PYTHONPATH': paths_str} | 
					
						
							|  |  |  |         return env | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     @unittest.skipIf(MS_WINDOWS, 'See test_init_pybuilddir_win32') | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |     def test_init_pybuilddir(self): | 
					
						
							|  |  |  |         # Test path configuration with pybuilddir.txt configuration file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.tmpdir_with_python() as tmpdir: | 
					
						
							|  |  |  |             # pybuilddir.txt is a sub-directory relative to the current | 
					
						
							|  |  |  |             # directory (tmpdir) | 
					
						
							| 
									
										
										
										
											2022-06-17 21:54:17 +05:30
										 |  |  |             vpath = sysconfig.get_config_var("VPATH") or '' | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             subdir = 'libdir' | 
					
						
							|  |  |  |             libdir = os.path.join(tmpdir, subdir) | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             # The stdlib dir is dirname(executable) + VPATH + 'Lib' | 
					
						
							| 
									
										
										
										
											2022-06-17 21:54:17 +05:30
										 |  |  |             stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib')) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             os.mkdir(libdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             filename = os.path.join(tmpdir, 'pybuilddir.txt') | 
					
						
							|  |  |  |             with open(filename, "w", encoding="utf8") as fp: | 
					
						
							|  |  |  |                 fp.write(subdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             module_search_paths = self.module_search_paths() | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             module_search_paths[-2] = stdlibdir | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             module_search_paths[-1] = libdir | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             executable = self.test_exe | 
					
						
							|  |  |  |             config = { | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 'base_exec_prefix': sysconfig.get_config_var("exec_prefix"), | 
					
						
							|  |  |  |                 'base_prefix': sysconfig.get_config_var("prefix"), | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                 'base_executable': executable, | 
					
						
							|  |  |  |                 'executable': executable, | 
					
						
							|  |  |  |                 'module_search_paths': module_search_paths, | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 'stdlib_dir': stdlibdir, | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             env = self.copy_paths_by_env(config) | 
					
						
							|  |  |  |             self.check_all_configs("test_init_compat_config", config, | 
					
						
							|  |  |  |                                    api=API_COMPAT, env=env, | 
					
						
							|  |  |  |                                    ignore_stderr=True, cwd=tmpdir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |     @unittest.skipUnless(MS_WINDOWS, 'See test_init_pybuilddir') | 
					
						
							|  |  |  |     def test_init_pybuilddir_win32(self): | 
					
						
							|  |  |  |         # Test path configuration with pybuilddir.txt configuration file | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 02:13:55 +09:00
										 |  |  |         vpath = sysconfig.get_config_var("VPATH") | 
					
						
							|  |  |  |         subdir = r'PCbuild\arch' | 
					
						
							|  |  |  |         if os.path.normpath(vpath).count(os.sep) == 2: | 
					
						
							|  |  |  |             subdir = os.path.join(subdir, 'instrumented') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.tmpdir_with_python(subdir) as tmpdir: | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             # The prefix is dirname(executable) + VPATH | 
					
						
							| 
									
										
										
										
											2021-12-11 02:13:55 +09:00
										 |  |  |             prefix = os.path.normpath(os.path.join(tmpdir, vpath)) | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             # The stdlib dir is dirname(executable) + VPATH + 'Lib' | 
					
						
							| 
									
										
										
										
											2021-12-11 02:13:55 +09:00
										 |  |  |             stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib')) | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             filename = os.path.join(tmpdir, 'pybuilddir.txt') | 
					
						
							|  |  |  |             with open(filename, "w", encoding="utf8") as fp: | 
					
						
							|  |  |  |                 fp.write(tmpdir) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             module_search_paths = self.module_search_paths() | 
					
						
							|  |  |  |             module_search_paths[-3] = os.path.join(tmpdir, os.path.basename(module_search_paths[-3])) | 
					
						
							| 
									
										
										
										
											2023-01-16 16:05:39 +00:00
										 |  |  |             module_search_paths[-2] = tmpdir | 
					
						
							|  |  |  |             module_search_paths[-1] = stdlibdir | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             executable = self.test_exe | 
					
						
							|  |  |  |             config = { | 
					
						
							|  |  |  |                 'base_exec_prefix': prefix, | 
					
						
							|  |  |  |                 'base_prefix': prefix, | 
					
						
							|  |  |  |                 'base_executable': executable, | 
					
						
							|  |  |  |                 'executable': executable, | 
					
						
							|  |  |  |                 'prefix': prefix, | 
					
						
							|  |  |  |                 'exec_prefix': prefix, | 
					
						
							|  |  |  |                 'module_search_paths': module_search_paths, | 
					
						
							|  |  |  |                 'stdlib_dir': stdlibdir, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             env = self.copy_paths_by_env(config) | 
					
						
							|  |  |  |             self.check_all_configs("test_init_compat_config", config, | 
					
						
							|  |  |  |                                    api=API_COMPAT, env=env, | 
					
						
							|  |  |  |                                    ignore_stderr=False, cwd=tmpdir) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |     def test_init_pyvenv_cfg(self): | 
					
						
							|  |  |  |         # Test path configuration with pyvenv.cfg configuration file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with self.tmpdir_with_python() as tmpdir, \ | 
					
						
							|  |  |  |              tempfile.TemporaryDirectory() as pyvenv_home: | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             ver = sys.version_info | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  |             base_prefix = sysconfig.get_config_var("prefix") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # gh-128690: base_exec_prefix depends if PLATSTDLIB_LANDMARK exists | 
					
						
							|  |  |  |             platstdlib = os.path.join(base_prefix, PLATSTDLIB_LANDMARK) | 
					
						
							|  |  |  |             change_exec_prefix = not os.path.isdir(platstdlib) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not MS_WINDOWS: | 
					
						
							|  |  |  |                 lib_dynload = os.path.join(pyvenv_home, | 
					
						
							| 
									
										
										
										
											2020-03-10 09:53:09 +01:00
										 |  |  |                                            sys.platlibdir, | 
					
						
							| 
									
										
										
										
											2024-07-11 16:21:37 -04:00
										 |  |  |                                            f'python{ver.major}.{ver.minor}{ABI_THREAD}', | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                                            'lib-dynload') | 
					
						
							|  |  |  |                 os.makedirs(lib_dynload) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 lib_folder = os.path.join(pyvenv_home, 'Lib') | 
					
						
							|  |  |  |                 os.makedirs(lib_folder) | 
					
						
							|  |  |  |                 # getpath.py uses Lib\os.py as the LANDMARK | 
					
						
							| 
									
										
										
										
											2021-09-17 16:31:31 -06:00
										 |  |  |                 shutil.copyfile( | 
					
						
							|  |  |  |                     os.path.join(support.STDLIB_DIR, 'os.py'), | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                     os.path.join(lib_folder, 'os.py'), | 
					
						
							| 
									
										
										
										
											2021-09-17 16:31:31 -06:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             filename = os.path.join(tmpdir, 'pyvenv.cfg') | 
					
						
							|  |  |  |             with open(filename, "w", encoding="utf8") as fp: | 
					
						
							|  |  |  |                 print("home = %s" % pyvenv_home, file=fp) | 
					
						
							|  |  |  |                 print("include-system-site-packages = false", file=fp) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             paths = self.module_search_paths() | 
					
						
							|  |  |  |             if not MS_WINDOWS: | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  |                 if change_exec_prefix: | 
					
						
							|  |  |  |                     paths[-1] = lib_dynload | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2022-11-02 18:38:40 +00:00
										 |  |  |                 paths = [ | 
					
						
							|  |  |  |                     os.path.join(tmpdir, os.path.basename(paths[0])), | 
					
						
							|  |  |  |                     pyvenv_home, | 
					
						
							|  |  |  |                     os.path.join(pyvenv_home, "Lib"), | 
					
						
							|  |  |  |                 ] | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             executable = self.test_exe | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |             base_executable = os.path.join(pyvenv_home, os.path.basename(executable)) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             config = { | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  |                 'base_prefix': base_prefix, | 
					
						
							| 
									
										
										
										
											2024-11-26 13:46:33 +00:00
										 |  |  |                 'exec_prefix': tmpdir, | 
					
						
							|  |  |  |                 'prefix': tmpdir, | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 'base_executable': base_executable, | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |                 'executable': executable, | 
					
						
							|  |  |  |                 'module_search_paths': paths, | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2025-01-23 15:50:04 +01:00
										 |  |  |             if change_exec_prefix: | 
					
						
							|  |  |  |                 config['base_exec_prefix'] = pyvenv_home | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  |             if MS_WINDOWS: | 
					
						
							|  |  |  |                 config['base_prefix'] = pyvenv_home | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 config['stdlib_dir'] = os.path.join(pyvenv_home, 'Lib') | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |                 config['use_frozen_modules'] = bool(not support.Py_DEBUG) | 
					
						
							| 
									
										
										
										
											2021-09-28 12:18:28 -06:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2021-12-03 00:08:42 +00:00
										 |  |  |                 # cannot reliably assume stdlib_dir here because it | 
					
						
							|  |  |  |                 # depends too much on our build. But it ought to be found | 
					
						
							|  |  |  |                 config['stdlib_dir'] = self.IGNORE_CONFIG | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |                 config['use_frozen_modules'] = bool(not support.Py_DEBUG) | 
					
						
							| 
									
										
										
										
											2020-07-08 00:20:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             env = self.copy_paths_by_env(config) | 
					
						
							|  |  |  |             self.check_all_configs("test_init_compat_config", config, | 
					
						
							|  |  |  |                                    api=API_COMPAT, env=env, | 
					
						
							|  |  |  |                                    ignore_stderr=True, cwd=tmpdir) | 
					
						
							| 
									
										
										
										
											2019-09-25 02:10:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 08:35:42 +09:00
										 |  |  |     @unittest.skipUnless(MS_WINDOWS, 'specific to Windows') | 
					
						
							|  |  |  |     def test_getpath_abspath_win32(self): | 
					
						
							|  |  |  |         # Check _Py_abspath() is passed a backslashed path not to fall back to | 
					
						
							|  |  |  |         # GetFullPathNameW() on startup, which (re-)normalizes the path overly. | 
					
						
							|  |  |  |         # Currently, _Py_normpath() doesn't trim trailing dots and spaces. | 
					
						
							|  |  |  |         CASES = [ | 
					
						
							|  |  |  |             ("C:/a. . .",  "C:\\a. . ."), | 
					
						
							|  |  |  |             ("C:\\a. . .", "C:\\a. . ."), | 
					
						
							|  |  |  |             ("\\\\?\\C:////a////b. . .", "\\\\?\\C:\\a\\b. . ."), | 
					
						
							|  |  |  |             ("//a/b/c. . .", "\\\\a\\b\\c. . ."), | 
					
						
							|  |  |  |             ("\\\\a\\b\\c. . .", "\\\\a\\b\\c. . ."), | 
					
						
							|  |  |  |             ("a. . .", f"{os.getcwd()}\\a"),  # relpath gets fully normalized | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter( | 
					
						
							|  |  |  |             "test_init_initialize_config", | 
					
						
							| 
									
										
										
										
											2022-01-15 00:31:15 +09:00
										 |  |  |             env={**remove_python_envvars(), | 
					
						
							|  |  |  |                  "PYTHONPATH": os.path.pathsep.join(c[0] for c in CASES)} | 
					
						
							| 
									
										
										
										
											2022-01-14 08:35:42 +09:00
										 |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual(err, "") | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             out = json.loads(out) | 
					
						
							|  |  |  |         except json.JSONDecodeError: | 
					
						
							|  |  |  |             self.fail(f"fail to decode stdout: {out!r}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results = out['config']["module_search_paths"] | 
					
						
							|  |  |  |         for (_, expected), result in zip(CASES, results): | 
					
						
							|  |  |  |             self.assertEqual(result, expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-26 15:51:50 +02:00
										 |  |  |     def test_global_pathconfig(self): | 
					
						
							|  |  |  |         # Test C API functions getting the path configuration: | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # - Py_GetExecPrefix() | 
					
						
							|  |  |  |         # - Py_GetPath() | 
					
						
							|  |  |  |         # - Py_GetPrefix() | 
					
						
							|  |  |  |         # - Py_GetProgramFullPath() | 
					
						
							|  |  |  |         # - Py_GetProgramName() | 
					
						
							|  |  |  |         # - Py_GetPythonHome() | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         # The global path configuration (_Py_path_config) must be a copy | 
					
						
							|  |  |  |         # of the path configuration of PyInterpreter.config (PyConfig). | 
					
						
							| 
									
										
										
										
											2020-08-04 00:47:42 +08:00
										 |  |  |         ctypes = import_helper.import_module('ctypes') | 
					
						
							| 
									
										
										
										
											2019-09-26 15:51:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def get_func(name): | 
					
						
							|  |  |  |             func = getattr(ctypes.pythonapi, name) | 
					
						
							|  |  |  |             func.argtypes = () | 
					
						
							|  |  |  |             func.restype = ctypes.c_wchar_p | 
					
						
							|  |  |  |             return func | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Py_GetPath = get_func('Py_GetPath') | 
					
						
							|  |  |  |         Py_GetPrefix = get_func('Py_GetPrefix') | 
					
						
							|  |  |  |         Py_GetExecPrefix = get_func('Py_GetExecPrefix') | 
					
						
							|  |  |  |         Py_GetProgramName = get_func('Py_GetProgramName') | 
					
						
							|  |  |  |         Py_GetProgramFullPath = get_func('Py_GetProgramFullPath') | 
					
						
							|  |  |  |         Py_GetPythonHome = get_func('Py_GetPythonHome') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config = _testinternalcapi.get_configs()['config'] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |         self.assertEqual(tuple(Py_GetPath().split(os.path.pathsep)), | 
					
						
							| 
									
										
										
										
											2019-09-26 15:51:50 +02:00
										 |  |  |                          config['module_search_paths']) | 
					
						
							|  |  |  |         self.assertEqual(Py_GetPrefix(), config['prefix']) | 
					
						
							|  |  |  |         self.assertEqual(Py_GetExecPrefix(), config['exec_prefix']) | 
					
						
							|  |  |  |         self.assertEqual(Py_GetProgramName(), config['program_name']) | 
					
						
							|  |  |  |         self.assertEqual(Py_GetProgramFullPath(), config['executable']) | 
					
						
							|  |  |  |         self.assertEqual(Py_GetPythonHome(), config['home']) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |     def test_init_warnoptions(self): | 
					
						
							|  |  |  |         # lowest to highest priority | 
					
						
							|  |  |  |         warnoptions = [ | 
					
						
							|  |  |  |             'ignore:::PyConfig_Insert0',      # PyWideStringList_Insert(0) | 
					
						
							|  |  |  |             'default',                        # PyConfig.dev_mode=1 | 
					
						
							|  |  |  |             'ignore:::env1',                  # PYTHONWARNINGS env var | 
					
						
							|  |  |  |             'ignore:::env2',                  # PYTHONWARNINGS env var | 
					
						
							|  |  |  |             'ignore:::cmdline1',              # -W opt command line option | 
					
						
							|  |  |  |             'ignore:::cmdline2',              # -W opt command line option | 
					
						
							|  |  |  |             'default::BytesWarning',          # PyConfig.bytes_warnings=1 | 
					
						
							|  |  |  |             'ignore:::PySys_AddWarnOption1',  # PySys_AddWarnOption() | 
					
						
							|  |  |  |             'ignore:::PySys_AddWarnOption2',  # PySys_AddWarnOption() | 
					
						
							|  |  |  |             'ignore:::PyConfig_BeforeRead',   # PyConfig.warnoptions | 
					
						
							|  |  |  |             'ignore:::PyConfig_AfterRead']    # PyWideStringList_Append() | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         preconfig = {} | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |         config = { | 
					
						
							|  |  |  |             'bytes_warning': 1, | 
					
						
							| 
									
										
										
										
											2020-06-30 00:49:03 +02:00
										 |  |  |             'orig_argv': ['python3', | 
					
						
							|  |  |  |                           '-Wignore:::cmdline1', | 
					
						
							|  |  |  |                           '-Wignore:::cmdline2'], | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         config_dev_mode(preconfig, config) | 
					
						
							|  |  |  |         config['warnoptions'] = warnoptions | 
					
						
							| 
									
										
										
										
											2019-09-30 01:40:17 +02:00
										 |  |  |         self.check_all_configs("test_init_warnoptions", config, preconfig, | 
					
						
							|  |  |  |                                api=API_PYTHON) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-14 19:49:02 -06:00
										 |  |  |     @unittest.skipIf(support.check_bolt_optimized, "segfaults on BOLT instrumented binaries") | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |     def test_initconfig_api(self): | 
					
						
							|  |  |  |         preconfig = { | 
					
						
							|  |  |  |             'configure_locale': True, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         config = { | 
					
						
							|  |  |  |             'pycache_prefix': 'conf_pycache_prefix', | 
					
						
							|  |  |  |             'xoptions': {'faulthandler': True}, | 
					
						
							|  |  |  |             'hash_seed': 10, | 
					
						
							|  |  |  |             'use_hash_seed': True, | 
					
						
							| 
									
										
										
										
											2024-09-27 16:50:16 -07:00
										 |  |  |             'perf_profiling': 2, | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         config_dev_mode(preconfig, config) | 
					
						
							| 
									
										
										
										
											2024-11-01 16:10:58 -07:00
										 |  |  |         # Temporarily enable ignore_stderr=True to ignore warnings on JIT builds | 
					
						
							|  |  |  |         # See gh-126255 for more information | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  |         self.check_all_configs("test_initconfig_api", config, preconfig, | 
					
						
							| 
									
										
										
										
											2024-11-01 16:10:58 -07:00
										 |  |  |                                api=API_ISOLATED, ignore_stderr=True) | 
					
						
							| 
									
										
										
										
											2024-09-03 14:33:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_initconfig_get_api(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_initconfig_get_api") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_initconfig_exit(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_initconfig_exit") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-04 13:41:57 +02:00
										 |  |  |     def test_initconfig_module(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_initconfig_module") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 18:12:59 +02:00
										 |  |  |     def test_get_argc_argv(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_get_argc_argv") | 
					
						
							|  |  |  |         # ignore output | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-25 15:26:41 -06:00
										 |  |  |     def test_init_use_frozen_modules(self): | 
					
						
							|  |  |  |         tests = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             ('=on', True), | 
					
						
							|  |  |  |             ('=off', False), | 
					
						
							|  |  |  |             ('=', True), | 
					
						
							|  |  |  |             ('', True), | 
					
						
							| 
									
										
										
										
											2021-10-25 15:26:41 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |         for raw, expected in tests: | 
					
						
							|  |  |  |             optval = f'frozen_modules{raw}' | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |             if raw.startswith('='): | 
					
						
							|  |  |  |                 xoption_value = raw[1:] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 xoption_value = True | 
					
						
							| 
									
										
										
										
											2021-10-25 15:26:41 -06:00
										 |  |  |             config = { | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |                 'parse_argv': True, | 
					
						
							| 
									
										
										
										
											2021-10-25 15:26:41 -06:00
										 |  |  |                 'argv': ['-c'], | 
					
						
							|  |  |  |                 'orig_argv': ['./argv0', '-X', optval, '-c', 'pass'], | 
					
						
							|  |  |  |                 'program_name': './argv0', | 
					
						
							|  |  |  |                 'run_command': 'pass\n', | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |                 'use_environment': True, | 
					
						
							| 
									
										
										
										
											2024-09-02 23:25:08 +02:00
										 |  |  |                 'xoptions': {'frozen_modules': xoption_value}, | 
					
						
							| 
									
										
										
										
											2021-10-25 15:26:41 -06:00
										 |  |  |                 'use_frozen_modules': expected, | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             env = {'TESTFROZEN': raw[1:]} if raw else None | 
					
						
							|  |  |  |             with self.subTest(repr(raw)): | 
					
						
							|  |  |  |                 self.check_all_configs("test_init_use_frozen_modules", config, | 
					
						
							|  |  |  |                                        api=API_PYTHON, env=env) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-26 11:16:30 -06:00
										 |  |  |     def test_init_main_interpreter_settings(self): | 
					
						
							| 
									
										
										
										
											2023-04-24 17:23:57 -06:00
										 |  |  |         OBMALLOC = 1<<5 | 
					
						
							| 
									
										
										
										
											2023-02-15 18:16:00 -07:00
										 |  |  |         EXTENSIONS = 1<<8 | 
					
						
							| 
									
										
										
										
											2022-10-26 11:16:30 -06:00
										 |  |  |         THREADS = 1<<10 | 
					
						
							| 
									
										
										
											
												gh-98610: Adjust the Optional Restrictions on Subinterpreters (GH-98618)
Previously, the optional restrictions on subinterpreters were: disallow fork, subprocess, and threads.  By default, we were disallowing all three for "isolated" interpreters.  We always allowed all three for the main interpreter and those created through the legacy `Py_NewInterpreter()` API.
Those settings were a bit conservative, so here we've adjusted the optional restrictions to: fork, exec, threads, and daemon threads.  The default for "isolated" interpreters disables fork, exec, and daemon threads.  Regular threads are allowed by default.  We continue always allowing everything For the main interpreter and the legacy API.
In the code, we add `_PyInterpreterConfig.allow_exec` and  `_PyInterpreterConfig.allow_daemon_threads`.  We also add `Py_RTFLAGS_DAEMON_THREADS` and `Py_RTFLAGS_EXEC`.
											
										 
											2022-10-31 13:35:54 -06:00
										 |  |  |         DAEMON_THREADS = 1<<11 | 
					
						
							| 
									
										
										
										
											2022-10-26 11:16:30 -06:00
										 |  |  |         FORK = 1<<15 | 
					
						
							| 
									
										
										
											
												gh-98610: Adjust the Optional Restrictions on Subinterpreters (GH-98618)
Previously, the optional restrictions on subinterpreters were: disallow fork, subprocess, and threads.  By default, we were disallowing all three for "isolated" interpreters.  We always allowed all three for the main interpreter and those created through the legacy `Py_NewInterpreter()` API.
Those settings were a bit conservative, so here we've adjusted the optional restrictions to: fork, exec, threads, and daemon threads.  The default for "isolated" interpreters disables fork, exec, and daemon threads.  Regular threads are allowed by default.  We continue always allowing everything For the main interpreter and the legacy API.
In the code, we add `_PyInterpreterConfig.allow_exec` and  `_PyInterpreterConfig.allow_daemon_threads`.  We also add `Py_RTFLAGS_DAEMON_THREADS` and `Py_RTFLAGS_EXEC`.
											
										 
											2022-10-31 13:35:54 -06:00
										 |  |  |         EXEC = 1<<16 | 
					
						
							| 
									
										
										
										
											2022-10-26 11:16:30 -06:00
										 |  |  |         expected = { | 
					
						
							|  |  |  |             # All optional features should be enabled. | 
					
						
							| 
									
										
										
										
											2023-02-15 18:16:00 -07:00
										 |  |  |             'feature_flags': | 
					
						
							| 
									
										
										
										
											2023-04-24 17:23:57 -06:00
										 |  |  |                 OBMALLOC | FORK | EXEC | THREADS | DAEMON_THREADS, | 
					
						
							| 
									
										
										
										
											2023-05-05 15:59:20 -06:00
										 |  |  |             'own_gil': True, | 
					
						
							| 
									
										
										
										
											2022-10-26 11:16:30 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter( | 
					
						
							|  |  |  |             'test_init_main_interpreter_settings', | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual(err, '') | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             out = json.loads(out) | 
					
						
							|  |  |  |         except json.JSONDecodeError: | 
					
						
							|  |  |  |             self.fail(f'fail to decode stdout: {out!r}') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(out, expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-17 16:04:08 -04:00
										 |  |  |     @threading_helper.requires_working_threading() | 
					
						
							|  |  |  |     def test_init_in_background_thread(self): | 
					
						
							|  |  |  |         # gh-123022: Check that running Py_Initialize() in a background | 
					
						
							|  |  |  |         # thread doesn't crash. | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_init_in_background_thread") | 
					
						
							|  |  |  |         self.assertEqual(err, "") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 18:57:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  | class AuditingTests(EmbeddingTestsMixin, unittest.TestCase): | 
					
						
							|  |  |  |     def test_open_code_hook(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_open_code_hook") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_audit(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_audit") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-05 23:59:35 +02:00
										 |  |  |     def test_audit_tuple(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_audit_tuple") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  |     def test_audit_subinterpreter(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_audit_subinterpreter") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |     def test_audit_run_command(self): | 
					
						
							| 
									
										
										
										
											2019-12-11 22:17:04 +01:00
										 |  |  |         self.run_embedded_interpreter("test_audit_run_command", | 
					
						
							|  |  |  |                                       timeout=support.SHORT_TIMEOUT, | 
					
						
							|  |  |  |                                       returncode=1) | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_audit_run_file(self): | 
					
						
							| 
									
										
										
										
											2019-12-11 22:17:04 +01:00
										 |  |  |         self.run_embedded_interpreter("test_audit_run_file", | 
					
						
							|  |  |  |                                       timeout=support.SHORT_TIMEOUT, | 
					
						
							|  |  |  |                                       returncode=1) | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_audit_run_interactivehook(self): | 
					
						
							| 
									
										
										
										
											2020-08-04 00:47:42 +08:00
										 |  |  |         startup = os.path.join(self.oldcwd, os_helper.TESTFN) + ".py" | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |         with open(startup, "w", encoding="utf-8") as f: | 
					
						
							|  |  |  |             print("import sys", file=f) | 
					
						
							|  |  |  |             print("sys.__interactivehook__ = lambda: None", file=f) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             env = {**remove_python_envvars(), "PYTHONSTARTUP": startup} | 
					
						
							| 
									
										
										
										
											2019-12-11 22:17:04 +01:00
										 |  |  |             self.run_embedded_interpreter("test_audit_run_interactivehook", | 
					
						
							|  |  |  |                                           timeout=support.SHORT_TIMEOUT, | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |                                           returncode=10, env=env) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.unlink(startup) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_audit_run_startup(self): | 
					
						
							| 
									
										
										
										
											2020-08-04 00:47:42 +08:00
										 |  |  |         startup = os.path.join(self.oldcwd, os_helper.TESTFN) + ".py" | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |         with open(startup, "w", encoding="utf-8") as f: | 
					
						
							|  |  |  |             print("pass", file=f) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             env = {**remove_python_envvars(), "PYTHONSTARTUP": startup} | 
					
						
							| 
									
										
										
										
											2019-12-11 22:17:04 +01:00
										 |  |  |             self.run_embedded_interpreter("test_audit_run_startup", | 
					
						
							|  |  |  |                                           timeout=support.SHORT_TIMEOUT, | 
					
						
							| 
									
										
										
										
											2019-07-01 16:03:53 -07:00
										 |  |  |                                           returncode=10, env=env) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.unlink(startup) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_audit_run_stdin(self): | 
					
						
							| 
									
										
										
										
											2019-12-11 22:17:04 +01:00
										 |  |  |         self.run_embedded_interpreter("test_audit_run_stdin", | 
					
						
							|  |  |  |                                       timeout=support.SHORT_TIMEOUT, | 
					
						
							|  |  |  |                                       returncode=1) | 
					
						
							| 
									
										
										
										
											2019-05-23 08:45:22 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-22 09:16:52 -07:00
										 |  |  |     def test_get_incomplete_frame(self): | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_get_incomplete_frame") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 11:26:26 +01:00
										 |  |  | class MiscTests(EmbeddingTestsMixin, unittest.TestCase): | 
					
						
							|  |  |  |     def test_unicode_id_init(self): | 
					
						
							|  |  |  |         # bpo-42882: Test that _PyUnicode_FromId() works | 
					
						
							|  |  |  |         # when Python is initialized multiples times. | 
					
						
							|  |  |  |         self.run_embedded_interpreter("test_unicode_id_init") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 23:48:35 +02:00
										 |  |  |     # See bpo-44133 | 
					
						
							|  |  |  |     @unittest.skipIf(os.name == 'nt', | 
					
						
							|  |  |  |                      'Py_FrozenMain is not exported on Windows') | 
					
						
							| 
									
										
										
										
											2024-04-03 15:11:36 +02:00
										 |  |  |     @unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi") | 
					
						
							| 
									
										
										
										
											2021-05-17 23:48:35 +02:00
										 |  |  |     def test_frozenmain(self): | 
					
						
							| 
									
										
										
										
											2021-05-20 12:08:05 +02:00
										 |  |  |         env = dict(os.environ) | 
					
						
							|  |  |  |         env['PYTHONUNBUFFERED'] = '1' | 
					
						
							|  |  |  |         out, err = self.run_embedded_interpreter("test_frozenmain", env=env) | 
					
						
							|  |  |  |         executable = os.path.realpath('./argv0') | 
					
						
							| 
									
										
										
										
											2021-05-17 23:48:35 +02:00
										 |  |  |         expected = textwrap.dedent(f"""
 | 
					
						
							|  |  |  |             Frozen Hello World | 
					
						
							|  |  |  |             sys.argv ['./argv0', '-E', 'arg1', 'arg2'] | 
					
						
							|  |  |  |             config program_name: ./argv0 | 
					
						
							| 
									
										
										
										
											2021-05-20 12:08:05 +02:00
										 |  |  |             config executable: {executable} | 
					
						
							| 
									
										
										
										
											2024-03-06 10:29:27 +01:00
										 |  |  |             config use_environment: True | 
					
						
							|  |  |  |             config configure_c_stdio: True | 
					
						
							|  |  |  |             config buffered_stdio: False | 
					
						
							| 
									
										
										
										
											2021-05-17 23:48:35 +02:00
										 |  |  |         """).lstrip()
 | 
					
						
							|  |  |  |         self.assertEqual(out, expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-26 00:12:54 +02:00
										 |  |  |     @unittest.skipUnless(support.Py_DEBUG, | 
					
						
							| 
									
										
										
										
											2022-02-25 00:03:20 +01:00
										 |  |  |                          '-X showrefcount requires a Python debug build') | 
					
						
							|  |  |  |     def test_no_memleak(self): | 
					
						
							|  |  |  |         # bpo-1635741: Python must release all memory at exit | 
					
						
							| 
									
										
										
										
											2022-03-22 06:53:51 -06:00
										 |  |  |         tests = ( | 
					
						
							|  |  |  |             ('off', 'pass'), | 
					
						
							|  |  |  |             ('on', 'pass'), | 
					
						
							|  |  |  |             ('off', 'import __hello__'), | 
					
						
							|  |  |  |             ('on', 'import __hello__'), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         for flag, stmt in tests: | 
					
						
							|  |  |  |             xopt = f"frozen_modules={flag}" | 
					
						
							|  |  |  |             cmd = [sys.executable, "-I", "-X", "showrefcount", "-X", xopt, "-c", stmt] | 
					
						
							|  |  |  |             proc = subprocess.run(cmd, | 
					
						
							|  |  |  |                                   stdout=subprocess.PIPE, | 
					
						
							|  |  |  |                                   stderr=subprocess.STDOUT, | 
					
						
							|  |  |  |                                   text=True) | 
					
						
							|  |  |  |             self.assertEqual(proc.returncode, 0) | 
					
						
							|  |  |  |             out = proc.stdout.rstrip() | 
					
						
							|  |  |  |             match = re.match(r'^\[(-?\d+) refs, (-?\d+) blocks\]', out) | 
					
						
							|  |  |  |             if not match: | 
					
						
							|  |  |  |                 self.fail(f"unexpected output: {out!a}") | 
					
						
							|  |  |  |             refs = int(match.group(1)) | 
					
						
							|  |  |  |             blocks = int(match.group(2)) | 
					
						
							|  |  |  |             with self.subTest(frozen_modules=flag, stmt=stmt): | 
					
						
							|  |  |  |                 self.assertEqual(refs, 0, out) | 
					
						
							|  |  |  |                 self.assertEqual(blocks, 0, out) | 
					
						
							| 
									
										
										
										
											2022-02-25 00:03:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-14 23:32:57 +02:00
										 |  |  |     @unittest.skipUnless(support.Py_DEBUG, | 
					
						
							|  |  |  |                          '-X presite requires a Python debug build') | 
					
						
							|  |  |  |     def test_presite(self): | 
					
						
							| 
									
										
										
										
											2024-09-13 14:13:52 +03:00
										 |  |  |         cmd = [ | 
					
						
							|  |  |  |             sys.executable, | 
					
						
							|  |  |  |             "-I", "-X", "presite=test._test_embed_structseq", | 
					
						
							|  |  |  |             "-c", "print('unique-python-message')", | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2023-10-14 23:32:57 +02:00
										 |  |  |         proc = subprocess.run( | 
					
						
							|  |  |  |             cmd, | 
					
						
							|  |  |  |             stdout=subprocess.PIPE, | 
					
						
							|  |  |  |             stderr=subprocess.STDOUT, | 
					
						
							|  |  |  |             text=True, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertEqual(proc.returncode, 0) | 
					
						
							|  |  |  |         out = proc.stdout.strip() | 
					
						
							| 
									
										
										
										
											2024-09-13 14:13:52 +03:00
										 |  |  |         self.assertIn("Tests passed", out) | 
					
						
							|  |  |  |         self.assertIn("unique-python-message", out) | 
					
						
							| 
									
										
										
										
											2023-10-14 23:32:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 14:56:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 08:11:51 +10:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     unittest.main() |