blob: da79d7af0502372c7fc6230a5698f0aace679696 [file] [log] [blame]
Nick Coghlan39f0bb52017-11-28 08:11:51 +10001# Run the tests in Programs/_testembed.c (tests for the CPython embedding APIs)
2from test import support
3import unittest
4
5from collections import namedtuple
Victor Stinner96c84752019-09-26 16:17:34 +02006import contextlib
Victor Stinner7ddd56f2018-11-14 00:24:28 +01007import json
Nick Coghlan39f0bb52017-11-28 08:11:51 +10008import os
9import re
Victor Stinner96c84752019-09-26 16:17:34 +020010import shutil
Nick Coghlan39f0bb52017-11-28 08:11:51 +100011import subprocess
12import sys
Victor Stinner96c84752019-09-26 16:17:34 +020013import tempfile
Victor Stinnera6537fb2018-11-26 11:54:12 +010014import textwrap
Nick Coghlan39f0bb52017-11-28 08:11:51 +100015
16
Victor Stinner01de89c2018-11-14 17:39:45 +010017MS_WINDOWS = (os.name == 'nt')
Victor Stinner96c84752019-09-26 16:17:34 +020018MACOS = (sys.platform == 'darwin')
Victor Stinner022be022019-05-22 23:58:50 +020019
Victor Stinnerb16b4e42019-05-17 15:20:52 +020020PYMEM_ALLOCATOR_NOT_SET = 0
21PYMEM_ALLOCATOR_DEBUG = 2
22PYMEM_ALLOCATOR_MALLOC = 3
Victor Stinner01de89c2018-11-14 17:39:45 +010023
Victor Stinner022be022019-05-22 23:58:50 +020024# _PyCoreConfig_InitCompatConfig()
25API_COMPAT = 1
26# _PyCoreConfig_InitPythonConfig()
27API_PYTHON = 2
28# _PyCoreConfig_InitIsolatedConfig()
29API_ISOLATED = 3
Victor Stinner6d1c4672019-05-20 11:02:00 +020030
Victor Stinner01de89c2018-11-14 17:39:45 +010031
Victor Stinner96c84752019-09-26 16:17:34 +020032def debug_build(program):
33 program = os.path.basename(program)
34 name = os.path.splitext(program)[0]
35 return name.endswith("_d")
36
37
Miss Islington (bot)69610f82019-06-17 15:31:43 -070038def remove_python_envvars():
39 env = dict(os.environ)
40 # Remove PYTHON* environment variables to get deterministic environment
41 for key in list(env):
42 if key.startswith('PYTHON'):
43 del env[key]
44 return env
45
46
Victor Stinner56b29b62018-07-26 18:57:56 +020047class EmbeddingTestsMixin:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100048 def setUp(self):
49 here = os.path.abspath(__file__)
50 basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
51 exename = "_testembed"
Victor Stinner01de89c2018-11-14 17:39:45 +010052 if MS_WINDOWS:
Victor Stinner96c84752019-09-26 16:17:34 +020053 ext = ("_d" if debug_build(sys.executable) else "") + ".exe"
Nick Coghlan39f0bb52017-11-28 08:11:51 +100054 exename += ext
55 exepath = os.path.dirname(sys.executable)
56 else:
57 exepath = os.path.join(basepath, "Programs")
58 self.test_exe = exe = os.path.join(exepath, exename)
59 if not os.path.exists(exe):
60 self.skipTest("%r doesn't exist" % exe)
61 # This is needed otherwise we get a fatal error:
62 # "Py_Initialize: Unable to get the locale encoding
63 # LookupError: no codec search functions registered: can't find encoding"
64 self.oldcwd = os.getcwd()
65 os.chdir(basepath)
66
67 def tearDown(self):
68 os.chdir(self.oldcwd)
69
Miss Islington (bot)746992c2019-07-01 16:22:29 -070070 def run_embedded_interpreter(self, *args, env=None,
Victor Stinner96c84752019-09-26 16:17:34 +020071 timeout=None, returncode=0, input=None,
72 cwd=None):
Nick Coghlan39f0bb52017-11-28 08:11:51 +100073 """Runs a test in the embedded interpreter"""
74 cmd = [self.test_exe]
75 cmd.extend(args)
Victor Stinner01de89c2018-11-14 17:39:45 +010076 if env is not None and MS_WINDOWS:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100077 # Windows requires at least the SYSTEMROOT environment variable to
78 # start Python.
79 env = env.copy()
80 env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
81
82 p = subprocess.Popen(cmd,
83 stdout=subprocess.PIPE,
84 stderr=subprocess.PIPE,
85 universal_newlines=True,
Victor Stinner96c84752019-09-26 16:17:34 +020086 env=env,
87 cwd=cwd)
Victor Stinner2f549082019-03-29 15:13:46 +010088 try:
Miss Islington (bot)746992c2019-07-01 16:22:29 -070089 (out, err) = p.communicate(input=input, timeout=timeout)
Victor Stinner2f549082019-03-29 15:13:46 +010090 except:
91 p.terminate()
92 p.wait()
93 raise
Miss Islington (bot)746992c2019-07-01 16:22:29 -070094 if p.returncode != returncode and support.verbose:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100095 print(f"--- {cmd} failed ---")
96 print(f"stdout:\n{out}")
Nick Coghlanbc77eff2018-03-25 20:44:30 +100097 print(f"stderr:\n{err}")
Nick Coghlan39f0bb52017-11-28 08:11:51 +100098 print(f"------")
99
Miss Islington (bot)746992c2019-07-01 16:22:29 -0700100 self.assertEqual(p.returncode, returncode,
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000101 "bad returncode %d, stderr is %r" %
102 (p.returncode, err))
103 return out, err
104
105 def run_repeated_init_and_subinterpreters(self):
Victor Stinner5edcf262019-05-23 00:57:57 +0200106 out, err = self.run_embedded_interpreter("test_repeated_init_and_subinterpreters")
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000107 self.assertEqual(err, "")
108
109 # The output from _testembed looks like this:
110 # --- Pass 0 ---
111 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
112 # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
113 # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
114 # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
115 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
116 # --- Pass 1 ---
117 # ...
118
119 interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
120 r"thread state <(0x[\dA-F]+)>: "
121 r"id\(modules\) = ([\d]+)$")
122 Interp = namedtuple("Interp", "id interp tstate modules")
123
124 numloops = 0
125 current_run = []
126 for line in out.splitlines():
127 if line == "--- Pass {} ---".format(numloops):
128 self.assertEqual(len(current_run), 0)
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000129 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000130 print(line)
131 numloops += 1
132 continue
133
134 self.assertLess(len(current_run), 5)
135 match = re.match(interp_pat, line)
136 if match is None:
137 self.assertRegex(line, interp_pat)
138
139 # Parse the line from the loop. The first line is the main
140 # interpreter and the 3 afterward are subinterpreters.
141 interp = Interp(*match.groups())
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000142 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000143 print(interp)
144 self.assertTrue(interp.interp)
145 self.assertTrue(interp.tstate)
146 self.assertTrue(interp.modules)
147 current_run.append(interp)
148
149 # The last line in the loop should be the same as the first.
150 if len(current_run) == 5:
151 main = current_run[0]
152 self.assertEqual(interp, main)
153 yield current_run
154 current_run = []
155
Victor Stinner56b29b62018-07-26 18:57:56 +0200156
157class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000158 def test_subinterps_main(self):
159 for run in self.run_repeated_init_and_subinterpreters():
160 main = run[0]
161
162 self.assertEqual(main.id, '0')
163
164 def test_subinterps_different_ids(self):
165 for run in self.run_repeated_init_and_subinterpreters():
166 main, *subs, _ = run
167
168 mainid = int(main.id)
169 for i, sub in enumerate(subs):
170 self.assertEqual(sub.id, str(mainid + i + 1))
171
172 def test_subinterps_distinct_state(self):
173 for run in self.run_repeated_init_and_subinterpreters():
174 main, *subs, _ = run
175
176 if '0x0' in main:
177 # XXX Fix on Windows (and other platforms): something
178 # is going on with the pointers in Programs/_testembed.c.
179 # interp.interp is 0x0 and interp.modules is the same
180 # between interpreters.
181 raise unittest.SkipTest('platform prints pointers as 0x0')
182
183 for sub in subs:
184 # A new subinterpreter may have the same
185 # PyInterpreterState pointer as a previous one if
186 # the earlier one has already been destroyed. So
187 # we compare with the main interpreter. The same
188 # applies to tstate.
189 self.assertNotEqual(sub.interp, main.interp)
190 self.assertNotEqual(sub.tstate, main.tstate)
191 self.assertNotEqual(sub.modules, main.modules)
192
193 def test_forced_io_encoding(self):
194 # Checks forced configuration of embedded interpreter IO streams
195 env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
Victor Stinner5edcf262019-05-23 00:57:57 +0200196 out, err = self.run_embedded_interpreter("test_forced_io_encoding", env=env)
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000197 if support.verbose > 1:
198 print()
199 print(out)
200 print(err)
201 expected_stream_encoding = "utf-8"
202 expected_errors = "surrogateescape"
203 expected_output = '\n'.join([
204 "--- Use defaults ---",
205 "Expected encoding: default",
206 "Expected errors: default",
207 "stdin: {in_encoding}:{errors}",
208 "stdout: {out_encoding}:{errors}",
209 "stderr: {out_encoding}:backslashreplace",
210 "--- Set errors only ---",
211 "Expected encoding: default",
212 "Expected errors: ignore",
213 "stdin: {in_encoding}:ignore",
214 "stdout: {out_encoding}:ignore",
215 "stderr: {out_encoding}:backslashreplace",
216 "--- Set encoding only ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200217 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000218 "Expected errors: default",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200219 "stdin: iso8859-1:{errors}",
220 "stdout: iso8859-1:{errors}",
221 "stderr: iso8859-1:backslashreplace",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000222 "--- Set encoding and errors ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200223 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000224 "Expected errors: replace",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200225 "stdin: iso8859-1:replace",
226 "stdout: iso8859-1:replace",
227 "stderr: iso8859-1:backslashreplace"])
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000228 expected_output = expected_output.format(
229 in_encoding=expected_stream_encoding,
230 out_encoding=expected_stream_encoding,
231 errors=expected_errors)
232 # This is useful if we ever trip over odd platform behaviour
233 self.maxDiff = None
234 self.assertEqual(out.strip(), expected_output)
235
236 def test_pre_initialization_api(self):
237 """
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000238 Checks some key parts of the C-API that need to work before the runtine
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000239 is initialized (via Py_Initialize()).
240 """
241 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
Victor Stinner5edcf262019-05-23 00:57:57 +0200242 out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env)
Victor Stinner01de89c2018-11-14 17:39:45 +0100243 if MS_WINDOWS:
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000244 expected_path = self.test_exe
245 else:
246 expected_path = os.path.join(os.getcwd(), "spam")
247 expected_output = f"sys.executable: {expected_path}\n"
248 self.assertIn(expected_output, out)
249 self.assertEqual(err, '')
250
251 def test_pre_initialization_sys_options(self):
252 """
253 Checks that sys.warnoptions and sys._xoptions can be set before the
254 runtime is initialized (otherwise they won't be effective).
255 """
Miss Islington (bot)69610f82019-06-17 15:31:43 -0700256 env = remove_python_envvars()
257 env['PYTHONPATH'] = os.pathsep.join(sys.path)
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000258 out, err = self.run_embedded_interpreter(
Victor Stinner5edcf262019-05-23 00:57:57 +0200259 "test_pre_initialization_sys_options", env=env)
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000260 expected_output = (
261 "sys.warnoptions: ['once', 'module', 'default']\n"
262 "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
263 "warnings.filters[:3]: ['default', 'module', 'once']\n"
264 )
265 self.assertIn(expected_output, out)
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000266 self.assertEqual(err, '')
267
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100268 def test_bpo20891(self):
269 """
270 bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
271 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
272 call PyEval_InitThreads() for us in this case.
273 """
Victor Stinner5edcf262019-05-23 00:57:57 +0200274 out, err = self.run_embedded_interpreter("test_bpo20891")
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100275 self.assertEqual(out, '')
276 self.assertEqual(err, '')
277
Victor Stinner209abf72018-06-22 19:14:51 +0200278 def test_initialize_twice(self):
279 """
280 bpo-33932: Calling Py_Initialize() twice should do nothing (and not
281 crash!).
282 """
Victor Stinner5edcf262019-05-23 00:57:57 +0200283 out, err = self.run_embedded_interpreter("test_initialize_twice")
Victor Stinner209abf72018-06-22 19:14:51 +0200284 self.assertEqual(out, '')
285 self.assertEqual(err, '')
286
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200287 def test_initialize_pymain(self):
288 """
289 bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
290 """
Victor Stinner5edcf262019-05-23 00:57:57 +0200291 out, err = self.run_embedded_interpreter("test_initialize_pymain")
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200292 self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
293 self.assertEqual(err, '')
294
Victor Stinner2f549082019-03-29 15:13:46 +0100295 def test_run_main(self):
Victor Stinner5edcf262019-05-23 00:57:57 +0200296 out, err = self.run_embedded_interpreter("test_run_main")
Victor Stinner331a6a52019-05-27 16:39:22 +0200297 self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']")
Victor Stinner2f549082019-03-29 15:13:46 +0100298 self.assertEqual(err, '')
299
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000300
Victor Stinner56b29b62018-07-26 18:57:56 +0200301class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
302 maxDiff = 4096
Victor Stinner01de89c2018-11-14 17:39:45 +0100303 UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
304
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200305 # Marker to read the default configuration: get_default_config()
Victor Stinnera6537fb2018-11-26 11:54:12 +0100306 GET_DEFAULT_CONFIG = object()
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200307
308 # Marker to ignore a configuration parameter
309 IGNORE_CONFIG = object()
310
Victor Stinner022be022019-05-22 23:58:50 +0200311 PRE_CONFIG_COMPAT = {
Victor Stinner20e1e252019-05-23 04:12:27 +0200312 '_config_init': API_COMPAT,
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200313 'allocator': PYMEM_ALLOCATOR_NOT_SET,
Victor Stinner6d1c4672019-05-20 11:02:00 +0200314 'parse_argv': 0,
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200315 'configure_locale': 1,
Victor Stinner1075d162019-03-25 23:19:57 +0100316 'coerce_c_locale': 0,
317 'coerce_c_locale_warn': 0,
Victor Stinner1075d162019-03-25 23:19:57 +0100318 'utf8_mode': 0,
319 }
Victor Stinner6d1c4672019-05-20 11:02:00 +0200320 if MS_WINDOWS:
Victor Stinner022be022019-05-22 23:58:50 +0200321 PRE_CONFIG_COMPAT.update({
Victor Stinner6d1c4672019-05-20 11:02:00 +0200322 'legacy_windows_fs_encoding': 0,
323 })
Victor Stinner022be022019-05-22 23:58:50 +0200324 PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT,
Victor Stinner20e1e252019-05-23 04:12:27 +0200325 _config_init=API_PYTHON,
Victor Stinner6d1c4672019-05-20 11:02:00 +0200326 parse_argv=1,
Victor Stinner425717f2019-05-20 16:38:48 +0200327 coerce_c_locale=GET_DEFAULT_CONFIG,
328 utf8_mode=GET_DEFAULT_CONFIG,
Victor Stinner6d1c4672019-05-20 11:02:00 +0200329 )
Victor Stinner022be022019-05-22 23:58:50 +0200330 PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT,
Victor Stinner20e1e252019-05-23 04:12:27 +0200331 _config_init=API_ISOLATED,
Victor Stinnerbab0db62019-05-18 03:21:27 +0200332 configure_locale=0,
333 isolated=1,
334 use_environment=0,
335 utf8_mode=0,
336 dev_mode=0,
Victor Stinner425717f2019-05-20 16:38:48 +0200337 coerce_c_locale=0,
Victor Stinnerbab0db62019-05-18 03:21:27 +0200338 )
Victor Stinnerbab0db62019-05-18 03:21:27 +0200339
Victor Stinner20004952019-03-26 02:31:11 +0100340 COPY_PRE_CONFIG = [
341 'dev_mode',
342 'isolated',
343 'use_environment',
344 ]
345
Victor Stinner331a6a52019-05-27 16:39:22 +0200346 CONFIG_COMPAT = {
Victor Stinner022be022019-05-22 23:58:50 +0200347 '_config_init': API_COMPAT,
Victor Stinner20004952019-03-26 02:31:11 +0100348 'isolated': 0,
349 'use_environment': 1,
350 'dev_mode': 0,
351
Victor Stinner56b29b62018-07-26 18:57:56 +0200352 'install_signal_handlers': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200353 'use_hash_seed': 0,
354 'hash_seed': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200355 'faulthandler': 0,
356 'tracemalloc': 0,
357 'import_time': 0,
358 'show_ref_count': 0,
359 'show_alloc_count': 0,
360 'dump_refs': 0,
361 'malloc_stats': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200362
Victor Stinnera6537fb2018-11-26 11:54:12 +0100363 'filesystem_encoding': GET_DEFAULT_CONFIG,
364 'filesystem_errors': GET_DEFAULT_CONFIG,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200365
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100366 'pycache_prefix': None,
Victor Stinner91c99872019-05-14 22:01:51 +0200367 'program_name': GET_DEFAULT_CONFIG,
Victor Stinnercab5d072019-05-17 19:01:14 +0200368 'parse_argv': 0,
Victor Stinner62599762019-03-15 16:03:23 +0100369 'argv': [""],
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100370
371 'xoptions': [],
372 'warnoptions': [],
Victor Stinner56b29b62018-07-26 18:57:56 +0200373
Victor Stinner331a6a52019-05-27 16:39:22 +0200374 'pythonpath_env': None,
Victor Stinner01de89c2018-11-14 17:39:45 +0100375 'home': None,
Victor Stinner91c99872019-05-14 22:01:51 +0200376 'executable': GET_DEFAULT_CONFIG,
Steve Dower323e7432019-06-29 14:28:59 -0700377 'base_executable': GET_DEFAULT_CONFIG,
Victor Stinnera6537fb2018-11-26 11:54:12 +0100378
379 'prefix': GET_DEFAULT_CONFIG,
380 'base_prefix': GET_DEFAULT_CONFIG,
381 'exec_prefix': GET_DEFAULT_CONFIG,
382 'base_exec_prefix': GET_DEFAULT_CONFIG,
Victor Stinner5eb8b072019-05-15 02:12:48 +0200383 'module_search_paths': GET_DEFAULT_CONFIG,
Victor Stinner01de89c2018-11-14 17:39:45 +0100384
Victor Stinner56b29b62018-07-26 18:57:56 +0200385 'site_import': 1,
386 'bytes_warning': 0,
387 'inspect': 0,
388 'interactive': 0,
389 'optimization_level': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200390 'parser_debug': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200391 'write_bytecode': 1,
392 'verbose': 0,
393 'quiet': 0,
394 'user_site_directory': 1,
Victor Stinnercab5d072019-05-17 19:01:14 +0200395 'configure_c_stdio': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200396 'buffered_stdio': 1,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200397
Victor Stinnera6537fb2018-11-26 11:54:12 +0100398 'stdio_encoding': GET_DEFAULT_CONFIG,
399 'stdio_errors': GET_DEFAULT_CONFIG,
Victor Stinner56b29b62018-07-26 18:57:56 +0200400
Victor Stinner62be7632019-03-01 13:10:14 +0100401 'skip_source_first_line': 0,
402 'run_command': None,
403 'run_module': None,
404 'run_filename': None,
405
Victor Stinner56b29b62018-07-26 18:57:56 +0200406 '_install_importlib': 1,
Victor Stinnercb9fbd32019-05-01 23:51:56 -0400407 'check_hash_pycs_mode': 'default',
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200408 'pathconfig_warnings': 1,
409 '_init_main': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200410 }
Victor Stinner01de89c2018-11-14 17:39:45 +0100411 if MS_WINDOWS:
Victor Stinner331a6a52019-05-27 16:39:22 +0200412 CONFIG_COMPAT.update({
Victor Stinner01de89c2018-11-14 17:39:45 +0100413 'legacy_windows_stdio': 0,
414 })
415
Victor Stinner331a6a52019-05-27 16:39:22 +0200416 CONFIG_PYTHON = dict(CONFIG_COMPAT,
Victor Stinner20e1e252019-05-23 04:12:27 +0200417 _config_init=API_PYTHON,
Victor Stinnerbab0db62019-05-18 03:21:27 +0200418 configure_c_stdio=1,
419 parse_argv=1,
420 )
Victor Stinner331a6a52019-05-27 16:39:22 +0200421 CONFIG_ISOLATED = dict(CONFIG_COMPAT,
Victor Stinner20e1e252019-05-23 04:12:27 +0200422 _config_init=API_ISOLATED,
Victor Stinnerbab0db62019-05-18 03:21:27 +0200423 isolated=1,
424 use_environment=0,
425 user_site_directory=0,
426 dev_mode=0,
427 install_signal_handlers=0,
428 use_hash_seed=0,
429 faulthandler=0,
430 tracemalloc=0,
431 pathconfig_warnings=0,
432 )
433 if MS_WINDOWS:
Victor Stinner331a6a52019-05-27 16:39:22 +0200434 CONFIG_ISOLATED['legacy_windows_stdio'] = 0
Victor Stinnerbab0db62019-05-18 03:21:27 +0200435
Victor Stinner01de89c2018-11-14 17:39:45 +0100436 # global config
437 DEFAULT_GLOBAL_CONFIG = {
438 'Py_HasFileSystemDefaultEncoding': 0,
439 'Py_HashRandomizationFlag': 1,
440 '_Py_HasFileSystemDefaultEncodeErrors': 0,
441 }
Victor Stinner1075d162019-03-25 23:19:57 +0100442 COPY_GLOBAL_PRE_CONFIG = [
Victor Stinner1075d162019-03-25 23:19:57 +0100443 ('Py_UTF8Mode', 'utf8_mode'),
444 ]
Victor Stinner01de89c2018-11-14 17:39:45 +0100445 COPY_GLOBAL_CONFIG = [
446 # Copy core config to global config for expected values
447 # True means that the core config value is inverted (0 => 1 and 1 => 0)
448 ('Py_BytesWarningFlag', 'bytes_warning'),
449 ('Py_DebugFlag', 'parser_debug'),
450 ('Py_DontWriteBytecodeFlag', 'write_bytecode', True),
451 ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),
452 ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200453 ('Py_FrozenFlag', 'pathconfig_warnings', True),
Victor Stinner20004952019-03-26 02:31:11 +0100454 ('Py_IgnoreEnvironmentFlag', 'use_environment', True),
Victor Stinner01de89c2018-11-14 17:39:45 +0100455 ('Py_InspectFlag', 'inspect'),
456 ('Py_InteractiveFlag', 'interactive'),
Victor Stinner20004952019-03-26 02:31:11 +0100457 ('Py_IsolatedFlag', 'isolated'),
Victor Stinner01de89c2018-11-14 17:39:45 +0100458 ('Py_NoSiteFlag', 'site_import', True),
459 ('Py_NoUserSiteDirectory', 'user_site_directory', True),
460 ('Py_OptimizeFlag', 'optimization_level'),
461 ('Py_QuietFlag', 'quiet'),
Victor Stinner01de89c2018-11-14 17:39:45 +0100462 ('Py_UnbufferedStdioFlag', 'buffered_stdio', True),
463 ('Py_VerboseFlag', 'verbose'),
464 ]
465 if MS_WINDOWS:
Victor Stinner1075d162019-03-25 23:19:57 +0100466 COPY_GLOBAL_PRE_CONFIG.extend((
Victor Stinner01de89c2018-11-14 17:39:45 +0100467 ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'),
Victor Stinner1075d162019-03-25 23:19:57 +0100468 ))
469 COPY_GLOBAL_CONFIG.extend((
Victor Stinner01de89c2018-11-14 17:39:45 +0100470 ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'),
471 ))
Victor Stinner56b29b62018-07-26 18:57:56 +0200472
Victor Stinner425717f2019-05-20 16:38:48 +0200473 EXPECTED_CONFIG = None
474
Victor Stinner96c84752019-09-26 16:17:34 +0200475 @classmethod
476 def tearDownClass(cls):
477 # clear cache
478 cls.EXPECTED_CONFIG = None
479
Victor Stinner01de89c2018-11-14 17:39:45 +0100480 def main_xoptions(self, xoptions_list):
481 xoptions = {}
482 for opt in xoptions_list:
483 if '=' in opt:
484 key, value = opt.split('=', 1)
485 xoptions[key] = value
486 else:
487 xoptions[opt] = True
488 return xoptions
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200489
Victor Stinner96c84752019-09-26 16:17:34 +0200490 def _get_expected_config_impl(self):
491 env = remove_python_envvars()
Victor Stinnera6537fb2018-11-26 11:54:12 +0100492 code = textwrap.dedent('''
493 import json
Victor Stinnera6537fb2018-11-26 11:54:12 +0100494 import sys
Victor Stinner5eb8b072019-05-15 02:12:48 +0200495 import _testinternalcapi
Victor Stinnera6537fb2018-11-26 11:54:12 +0100496
Victor Stinner5eb8b072019-05-15 02:12:48 +0200497 configs = _testinternalcapi.get_configs()
Victor Stinnera6537fb2018-11-26 11:54:12 +0100498
Victor Stinner425717f2019-05-20 16:38:48 +0200499 data = json.dumps(configs)
Victor Stinnera6537fb2018-11-26 11:54:12 +0100500 data = data.encode('utf-8')
501 sys.stdout.buffer.write(data)
502 sys.stdout.buffer.flush()
503 ''')
504
505 # Use -S to not import the site module: get the proper configuration
506 # when test_embed is run from a venv (bpo-35313)
Victor Stinner5eb8b072019-05-15 02:12:48 +0200507 args = [sys.executable, '-S', '-c', code]
Victor Stinnera6537fb2018-11-26 11:54:12 +0100508 proc = subprocess.run(args, env=env,
509 stdout=subprocess.PIPE,
Victor Stinner96c84752019-09-26 16:17:34 +0200510 stderr=subprocess.PIPE)
Victor Stinnera6537fb2018-11-26 11:54:12 +0100511 if proc.returncode:
512 raise Exception(f"failed to get the default config: "
513 f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
514 stdout = proc.stdout.decode('utf-8')
Victor Stinner96c84752019-09-26 16:17:34 +0200515 # ignore stderr
Victor Stinner4631da12019-05-02 15:30:21 -0400516 try:
Victor Stinner425717f2019-05-20 16:38:48 +0200517 return json.loads(stdout)
Victor Stinner4631da12019-05-02 15:30:21 -0400518 except json.JSONDecodeError:
519 self.fail(f"fail to decode stdout: {stdout!r}")
Victor Stinnera6537fb2018-11-26 11:54:12 +0100520
Victor Stinner96c84752019-09-26 16:17:34 +0200521 def _get_expected_config(self):
522 cls = InitConfigTests
523 if cls.EXPECTED_CONFIG is None:
524 cls.EXPECTED_CONFIG = self._get_expected_config_impl()
525
526 # get a copy
527 configs = {}
528 for config_key, config_value in cls.EXPECTED_CONFIG.items():
529 config = {}
530 for key, value in config_value.items():
531 if isinstance(value, list):
532 value = value.copy()
533 config[key] = value
534 configs[config_key] = config
535 return configs
536
Victor Stinner425717f2019-05-20 16:38:48 +0200537 def get_expected_config(self, expected_preconfig, expected, env, api,
Miss Islington (bot)a6427cb2019-08-23 09:24:42 -0700538 modify_path_cb=None):
Victor Stinner425717f2019-05-20 16:38:48 +0200539 cls = self.__class__
Victor Stinner96c84752019-09-26 16:17:34 +0200540 configs = self._get_expected_config()
Victor Stinner425717f2019-05-20 16:38:48 +0200541
542 pre_config = configs['pre_config']
543 for key, value in expected_preconfig.items():
544 if value is self.GET_DEFAULT_CONFIG:
545 expected_preconfig[key] = pre_config[key]
546
Victor Stinner022be022019-05-22 23:58:50 +0200547 if not expected_preconfig['configure_locale'] or api == API_COMPAT:
Victor Stinner425717f2019-05-20 16:38:48 +0200548 # there is no easy way to get the locale encoding before
549 # setlocale(LC_CTYPE, "") is called: don't test encodings
550 for key in ('filesystem_encoding', 'filesystem_errors',
551 'stdio_encoding', 'stdio_errors'):
552 expected[key] = self.IGNORE_CONFIG
553
554 if not expected_preconfig['configure_locale']:
555 # UTF-8 Mode depends on the locale. There is no easy way
556 # to guess if UTF-8 Mode will be enabled or not if the locale
557 # is not configured.
558 expected_preconfig['utf8_mode'] = self.IGNORE_CONFIG
559
560 if expected_preconfig['utf8_mode'] == 1:
561 if expected['filesystem_encoding'] is self.GET_DEFAULT_CONFIG:
562 expected['filesystem_encoding'] = 'utf-8'
563 if expected['filesystem_errors'] is self.GET_DEFAULT_CONFIG:
564 expected['filesystem_errors'] = self.UTF8_MODE_ERRORS
565 if expected['stdio_encoding'] is self.GET_DEFAULT_CONFIG:
566 expected['stdio_encoding'] = 'utf-8'
567 if expected['stdio_errors'] is self.GET_DEFAULT_CONFIG:
568 expected['stdio_errors'] = 'surrogateescape'
569
Steve Dower323e7432019-06-29 14:28:59 -0700570 if sys.platform == 'win32':
571 default_executable = self.test_exe
572 elif expected['program_name'] is not self.GET_DEFAULT_CONFIG:
573 default_executable = os.path.abspath(expected['program_name'])
574 else:
575 default_executable = os.path.join(os.getcwd(), '_testembed')
Victor Stinner91c99872019-05-14 22:01:51 +0200576 if expected['executable'] is self.GET_DEFAULT_CONFIG:
Steve Dower323e7432019-06-29 14:28:59 -0700577 expected['executable'] = default_executable
578 if expected['base_executable'] is self.GET_DEFAULT_CONFIG:
579 expected['base_executable'] = default_executable
Victor Stinner91c99872019-05-14 22:01:51 +0200580 if expected['program_name'] is self.GET_DEFAULT_CONFIG:
581 expected['program_name'] = './_testembed'
582
Victor Stinner331a6a52019-05-27 16:39:22 +0200583 config = configs['config']
Victor Stinnera6537fb2018-11-26 11:54:12 +0100584 for key, value in expected.items():
585 if value is self.GET_DEFAULT_CONFIG:
Victor Stinner331a6a52019-05-27 16:39:22 +0200586 expected[key] = config[key]
Victor Stinner5eb8b072019-05-15 02:12:48 +0200587
Victor Stinner96c84752019-09-26 16:17:34 +0200588 pythonpath_env = expected['pythonpath_env']
589 if pythonpath_env is not None:
590 paths = pythonpath_env.split(os.path.pathsep)
591 expected['module_search_paths'] = [*paths, *expected['module_search_paths']]
Miss Islington (bot)a6427cb2019-08-23 09:24:42 -0700592 if modify_path_cb is not None:
593 expected['module_search_paths'] = expected['module_search_paths'].copy()
594 modify_path_cb(expected['module_search_paths'])
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200595
Victor Stinner425717f2019-05-20 16:38:48 +0200596 for key in self.COPY_PRE_CONFIG:
597 if key not in expected_preconfig:
598 expected_preconfig[key] = expected[key]
Victor Stinner01de89c2018-11-14 17:39:45 +0100599
Victor Stinner331a6a52019-05-27 16:39:22 +0200600 def check_pre_config(self, configs, expected):
601 pre_config = dict(configs['pre_config'])
Victor Stinner425717f2019-05-20 16:38:48 +0200602 for key, value in list(expected.items()):
603 if value is self.IGNORE_CONFIG:
604 del pre_config[key]
605 del expected[key]
606 self.assertEqual(pre_config, expected)
Victor Stinner1075d162019-03-25 23:19:57 +0100607
Victor Stinner331a6a52019-05-27 16:39:22 +0200608 def check_config(self, configs, expected):
609 config = dict(configs['config'])
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200610 for key, value in list(expected.items()):
611 if value is self.IGNORE_CONFIG:
Victor Stinner331a6a52019-05-27 16:39:22 +0200612 del config[key]
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200613 del expected[key]
Victor Stinner331a6a52019-05-27 16:39:22 +0200614 self.assertEqual(config, expected)
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200615
Victor Stinner331a6a52019-05-27 16:39:22 +0200616 def check_global_config(self, configs):
617 pre_config = configs['pre_config']
618 config = configs['config']
Victor Stinner00b137c2018-11-13 19:59:26 +0100619
Victor Stinnera6537fb2018-11-26 11:54:12 +0100620 expected = dict(self.DEFAULT_GLOBAL_CONFIG)
Victor Stinner01de89c2018-11-14 17:39:45 +0100621 for item in self.COPY_GLOBAL_CONFIG:
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100622 if len(item) == 3:
623 global_key, core_key, opposite = item
Victor Stinner331a6a52019-05-27 16:39:22 +0200624 expected[global_key] = 0 if config[core_key] else 1
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100625 else:
626 global_key, core_key = item
Victor Stinner331a6a52019-05-27 16:39:22 +0200627 expected[global_key] = config[core_key]
Victor Stinner1075d162019-03-25 23:19:57 +0100628 for item in self.COPY_GLOBAL_PRE_CONFIG:
629 if len(item) == 3:
630 global_key, core_key, opposite = item
631 expected[global_key] = 0 if pre_config[core_key] else 1
632 else:
633 global_key, core_key = item
634 expected[global_key] = pre_config[core_key]
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100635
Victor Stinner331a6a52019-05-27 16:39:22 +0200636 self.assertEqual(configs['global_config'], expected)
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100637
Victor Stinner331a6a52019-05-27 16:39:22 +0200638 def check_all_configs(self, testname, expected_config=None,
Victor Stinner96c84752019-09-26 16:17:34 +0200639 expected_preconfig=None, modify_path_cb=None,
640 stderr=None, *, api, preconfig_api=None,
641 env=None, ignore_stderr=False, cwd=None):
642 new_env = remove_python_envvars()
643 if env is not None:
644 new_env.update(env)
645 env = new_env
Victor Stinner01de89c2018-11-14 17:39:45 +0100646
Victor Stinner96c84752019-09-26 16:17:34 +0200647 if preconfig_api is None:
648 preconfig_api = api
649 if preconfig_api == API_ISOLATED:
Victor Stinner022be022019-05-22 23:58:50 +0200650 default_preconfig = self.PRE_CONFIG_ISOLATED
Victor Stinner96c84752019-09-26 16:17:34 +0200651 elif preconfig_api == API_PYTHON:
Victor Stinner022be022019-05-22 23:58:50 +0200652 default_preconfig = self.PRE_CONFIG_PYTHON
Victor Stinnerbab0db62019-05-18 03:21:27 +0200653 else:
Victor Stinner022be022019-05-22 23:58:50 +0200654 default_preconfig = self.PRE_CONFIG_COMPAT
Victor Stinnerbab0db62019-05-18 03:21:27 +0200655 if expected_preconfig is None:
656 expected_preconfig = {}
657 expected_preconfig = dict(default_preconfig, **expected_preconfig)
658 if expected_config is None:
659 expected_config = {}
Victor Stinner425717f2019-05-20 16:38:48 +0200660
Victor Stinner022be022019-05-22 23:58:50 +0200661 if api == API_PYTHON:
Victor Stinner331a6a52019-05-27 16:39:22 +0200662 default_config = self.CONFIG_PYTHON
Victor Stinner022be022019-05-22 23:58:50 +0200663 elif api == API_ISOLATED:
Victor Stinner331a6a52019-05-27 16:39:22 +0200664 default_config = self.CONFIG_ISOLATED
Victor Stinner425717f2019-05-20 16:38:48 +0200665 else:
Victor Stinner331a6a52019-05-27 16:39:22 +0200666 default_config = self.CONFIG_COMPAT
Victor Stinner425717f2019-05-20 16:38:48 +0200667 expected_config = dict(default_config, **expected_config)
Victor Stinner425717f2019-05-20 16:38:48 +0200668
669 self.get_expected_config(expected_preconfig,
670 expected_config, env,
Miss Islington (bot)a6427cb2019-08-23 09:24:42 -0700671 api, modify_path_cb)
Victor Stinner1075d162019-03-25 23:19:57 +0100672
Victor Stinner96c84752019-09-26 16:17:34 +0200673 out, err = self.run_embedded_interpreter(testname,
674 env=env, cwd=cwd)
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200675 if stderr is None and not expected_config['verbose']:
676 stderr = ""
Victor Stinner96c84752019-09-26 16:17:34 +0200677 if stderr is not None and not ignore_stderr:
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200678 self.assertEqual(err.rstrip(), stderr)
679 try:
Victor Stinner331a6a52019-05-27 16:39:22 +0200680 configs = json.loads(out)
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200681 except json.JSONDecodeError:
682 self.fail(f"fail to decode stdout: {out!r}")
683
Victor Stinner331a6a52019-05-27 16:39:22 +0200684 self.check_pre_config(configs, expected_preconfig)
685 self.check_config(configs, expected_config)
686 self.check_global_config(configs)
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100687
Victor Stinner56b29b62018-07-26 18:57:56 +0200688 def test_init_default_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200689 self.check_all_configs("test_init_initialize_config", api=API_COMPAT)
Victor Stinner022be022019-05-22 23:58:50 +0200690
691 def test_preinit_compat_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200692 self.check_all_configs("test_preinit_compat_config", api=API_COMPAT)
Victor Stinner022be022019-05-22 23:58:50 +0200693
694 def test_init_compat_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200695 self.check_all_configs("test_init_compat_config", api=API_COMPAT)
Victor Stinner56b29b62018-07-26 18:57:56 +0200696
697 def test_init_global_config(self):
Victor Stinner1075d162019-03-25 23:19:57 +0100698 preconfig = {
699 'utf8_mode': 1,
700 }
Victor Stinner56b29b62018-07-26 18:57:56 +0200701 config = {
702 'program_name': './globalvar',
703 'site_import': 0,
704 'bytes_warning': 1,
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100705 'warnoptions': ['default::BytesWarning'],
Victor Stinner56b29b62018-07-26 18:57:56 +0200706 'inspect': 1,
707 'interactive': 1,
708 'optimization_level': 2,
709 'write_bytecode': 0,
710 'verbose': 1,
711 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200712 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200713
Victor Stinner56b29b62018-07-26 18:57:56 +0200714 'user_site_directory': 0,
Victor Stinner54b43bb2019-05-16 18:30:15 +0200715 'pathconfig_warnings': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200716 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200717 self.check_all_configs("test_init_global_config", config, preconfig,
718 api=API_COMPAT)
Victor Stinner56b29b62018-07-26 18:57:56 +0200719
720 def test_init_from_config(self):
Victor Stinner1075d162019-03-25 23:19:57 +0100721 preconfig = {
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200722 'allocator': PYMEM_ALLOCATOR_MALLOC,
Victor Stinner1075d162019-03-25 23:19:57 +0100723 'utf8_mode': 1,
724 }
Victor Stinner56b29b62018-07-26 18:57:56 +0200725 config = {
726 'install_signal_handlers': 0,
727 'use_hash_seed': 1,
728 'hash_seed': 123,
Victor Stinner56b29b62018-07-26 18:57:56 +0200729 'tracemalloc': 2,
730 'import_time': 1,
731 'show_ref_count': 1,
732 'show_alloc_count': 1,
733 'malloc_stats': 1,
734
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200735 'stdio_encoding': 'iso8859-1',
736 'stdio_errors': 'replace',
Victor Stinner56b29b62018-07-26 18:57:56 +0200737
738 'pycache_prefix': 'conf_pycache_prefix',
739 'program_name': './conf_program_name',
Miss Islington (bot)4c227e62019-07-01 11:28:55 -0700740 'argv': ['-c', 'arg2', ],
Victor Stinnercab5d072019-05-17 19:01:14 +0200741 'parse_argv': 1,
Miss Islington (bot)4c227e62019-07-01 11:28:55 -0700742 'xoptions': [
743 'config_xoption1=3',
744 'config_xoption2=',
745 'config_xoption3',
746 'cmdline_xoption',
747 ],
748 'warnoptions': [
Miss Islington (bot)4c227e62019-07-01 11:28:55 -0700749 'cmdline_warnoption',
750 'default::BytesWarning',
Miss Islington (bot)c9ed9e62019-09-29 16:58:57 -0700751 'config_warnoption',
Miss Islington (bot)4c227e62019-07-01 11:28:55 -0700752 ],
Victor Stinner2f549082019-03-29 15:13:46 +0100753 'run_command': 'pass\n',
Victor Stinner56b29b62018-07-26 18:57:56 +0200754
755 'site_import': 0,
756 'bytes_warning': 1,
757 'inspect': 1,
758 'interactive': 1,
759 'optimization_level': 2,
760 'write_bytecode': 0,
761 'verbose': 1,
762 'quiet': 1,
Victor Stinnercab5d072019-05-17 19:01:14 +0200763 'configure_c_stdio': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200764 'buffered_stdio': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200765 'user_site_directory': 0,
766 'faulthandler': 1,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200767
Victor Stinnercb9fbd32019-05-01 23:51:56 -0400768 'check_hash_pycs_mode': 'always',
Victor Stinner54b43bb2019-05-16 18:30:15 +0200769 'pathconfig_warnings': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200770 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200771 self.check_all_configs("test_init_from_config", config, preconfig,
772 api=API_COMPAT)
Victor Stinner56b29b62018-07-26 18:57:56 +0200773
Victor Stinner20e1e252019-05-23 04:12:27 +0200774 def test_init_compat_env(self):
775 preconfig = {
776 'allocator': PYMEM_ALLOCATOR_MALLOC,
777 }
778 config = {
779 'use_hash_seed': 1,
780 'hash_seed': 42,
781 'tracemalloc': 2,
782 'import_time': 1,
783 'malloc_stats': 1,
784 'inspect': 1,
785 'optimization_level': 2,
Victor Stinner331a6a52019-05-27 16:39:22 +0200786 'pythonpath_env': '/my/path',
Victor Stinner20e1e252019-05-23 04:12:27 +0200787 'pycache_prefix': 'env_pycache_prefix',
788 'write_bytecode': 0,
789 'verbose': 1,
790 'buffered_stdio': 0,
791 'stdio_encoding': 'iso8859-1',
792 'stdio_errors': 'replace',
793 'user_site_directory': 0,
794 'faulthandler': 1,
795 'warnoptions': ['EnvVar'],
796 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200797 self.check_all_configs("test_init_compat_env", config, preconfig,
798 api=API_COMPAT)
Victor Stinner20e1e252019-05-23 04:12:27 +0200799
800 def test_init_python_env(self):
Victor Stinner425717f2019-05-20 16:38:48 +0200801 preconfig = {
802 'allocator': PYMEM_ALLOCATOR_MALLOC,
Victor Stinner022be022019-05-22 23:58:50 +0200803 'utf8_mode': 1,
Victor Stinner425717f2019-05-20 16:38:48 +0200804 }
805 config = {
806 'use_hash_seed': 1,
807 'hash_seed': 42,
808 'tracemalloc': 2,
809 'import_time': 1,
810 'malloc_stats': 1,
811 'inspect': 1,
812 'optimization_level': 2,
Victor Stinner331a6a52019-05-27 16:39:22 +0200813 'pythonpath_env': '/my/path',
Victor Stinner425717f2019-05-20 16:38:48 +0200814 'pycache_prefix': 'env_pycache_prefix',
815 'write_bytecode': 0,
816 'verbose': 1,
817 'buffered_stdio': 0,
818 'stdio_encoding': 'iso8859-1',
819 'stdio_errors': 'replace',
820 'user_site_directory': 0,
821 'faulthandler': 1,
822 'warnoptions': ['EnvVar'],
823 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200824 self.check_all_configs("test_init_python_env", config, preconfig,
825 api=API_PYTHON)
Victor Stinnerb35be4b2019-03-05 17:37:44 +0100826
827 def test_init_env_dev_mode(self):
Victor Stinner425717f2019-05-20 16:38:48 +0200828 preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG)
829 config = dict(dev_mode=1,
830 faulthandler=1,
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100831 warnoptions=['default'])
Victor Stinner331a6a52019-05-27 16:39:22 +0200832 self.check_all_configs("test_init_env_dev_mode", config, preconfig,
833 api=API_COMPAT)
Victor Stinner56b29b62018-07-26 18:57:56 +0200834
Victor Stinner20004952019-03-26 02:31:11 +0100835 def test_init_env_dev_mode_alloc(self):
Victor Stinner425717f2019-05-20 16:38:48 +0200836 preconfig = dict(allocator=PYMEM_ALLOCATOR_MALLOC)
837 config = dict(dev_mode=1,
838 faulthandler=1,
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100839 warnoptions=['default'])
Victor Stinner331a6a52019-05-27 16:39:22 +0200840 self.check_all_configs("test_init_env_dev_mode_alloc", config, preconfig,
841 api=API_COMPAT)
Victor Stinner25d13f32019-03-06 12:51:53 +0100842
Victor Stinner56b29b62018-07-26 18:57:56 +0200843 def test_init_dev_mode(self):
Victor Stinner1075d162019-03-25 23:19:57 +0100844 preconfig = {
Victor Stinnerb16b4e42019-05-17 15:20:52 +0200845 'allocator': PYMEM_ALLOCATOR_DEBUG,
Victor Stinner56b29b62018-07-26 18:57:56 +0200846 }
Victor Stinner1075d162019-03-25 23:19:57 +0100847 config = {
848 'faulthandler': 1,
Victor Stinner20004952019-03-26 02:31:11 +0100849 'dev_mode': 1,
Victor Stinnerf8ba6f52019-03-26 16:58:50 +0100850 'warnoptions': ['default'],
Victor Stinner1075d162019-03-25 23:19:57 +0100851 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200852 self.check_all_configs("test_init_dev_mode", config, preconfig,
853 api=API_PYTHON)
Victor Stinner6d1c4672019-05-20 11:02:00 +0200854
855 def test_preinit_parse_argv(self):
856 # Pre-initialize implicitly using argv: make sure that -X dev
857 # is used to configure the allocation in preinitialization
858 preconfig = {
859 'allocator': PYMEM_ALLOCATOR_DEBUG,
860 }
861 config = {
862 'argv': ['script.py'],
863 'run_filename': 'script.py',
864 'dev_mode': 1,
865 'faulthandler': 1,
866 'warnoptions': ['default'],
867 'xoptions': ['dev'],
868 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200869 self.check_all_configs("test_preinit_parse_argv", config, preconfig,
870 api=API_PYTHON)
Victor Stinner6d1c4672019-05-20 11:02:00 +0200871
872 def test_preinit_dont_parse_argv(self):
873 # -X dev must be ignored by isolated preconfiguration
874 preconfig = {
875 'isolated': 0,
876 }
877 config = {
878 'argv': ["python3", "-E", "-I",
879 "-X", "dev", "-X", "utf8", "script.py"],
880 'isolated': 0,
881 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200882 self.check_all_configs("test_preinit_dont_parse_argv", config, preconfig,
883 api=API_ISOLATED)
Victor Stinner56b29b62018-07-26 18:57:56 +0200884
Victor Stinnercab5d072019-05-17 19:01:14 +0200885 def test_init_isolated_flag(self):
Victor Stinner1075d162019-03-25 23:19:57 +0100886 config = {
Victor Stinner20004952019-03-26 02:31:11 +0100887 'isolated': 1,
888 'use_environment': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200889 'user_site_directory': 0,
890 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200891 self.check_all_configs("test_init_isolated_flag", config, api=API_PYTHON)
Victor Stinner56b29b62018-07-26 18:57:56 +0200892
Victor Stinner6da20a42019-03-27 00:26:18 +0100893 def test_preinit_isolated1(self):
894 # _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set
Victor Stinner6da20a42019-03-27 00:26:18 +0100895 config = {
896 'isolated': 1,
897 'use_environment': 0,
898 'user_site_directory': 0,
899 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200900 self.check_all_configs("test_preinit_isolated1", config, api=API_COMPAT)
Victor Stinner6da20a42019-03-27 00:26:18 +0100901
902 def test_preinit_isolated2(self):
903 # _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1
Victor Stinner6da20a42019-03-27 00:26:18 +0100904 config = {
905 'isolated': 1,
906 'use_environment': 0,
907 'user_site_directory': 0,
908 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200909 self.check_all_configs("test_preinit_isolated2", config, api=API_COMPAT)
Victor Stinner6da20a42019-03-27 00:26:18 +0100910
Victor Stinner6d1c4672019-05-20 11:02:00 +0200911 def test_preinit_isolated_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200912 self.check_all_configs("test_preinit_isolated_config", api=API_ISOLATED)
Victor Stinner6d1c4672019-05-20 11:02:00 +0200913
Victor Stinnercab5d072019-05-17 19:01:14 +0200914 def test_init_isolated_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200915 self.check_all_configs("test_init_isolated_config", api=API_ISOLATED)
Victor Stinner5edcf262019-05-23 00:57:57 +0200916
917 def test_preinit_python_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200918 self.check_all_configs("test_preinit_python_config", api=API_PYTHON)
Victor Stinnercab5d072019-05-17 19:01:14 +0200919
920 def test_init_python_config(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200921 self.check_all_configs("test_init_python_config", api=API_PYTHON)
Victor Stinnercab5d072019-05-17 19:01:14 +0200922
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200923 def test_init_dont_configure_locale(self):
924 # _PyPreConfig.configure_locale=0
925 preconfig = {
926 'configure_locale': 0,
Victor Stinner425717f2019-05-20 16:38:48 +0200927 'coerce_c_locale': 0,
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200928 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200929 self.check_all_configs("test_init_dont_configure_locale", {}, preconfig,
930 api=API_PYTHON)
Victor Stinnerbcfbbd72019-05-17 22:44:16 +0200931
Victor Stinner91c99872019-05-14 22:01:51 +0200932 def test_init_read_set(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200933 config = {
Victor Stinner91c99872019-05-14 22:01:51 +0200934 'program_name': './init_read_set',
935 'executable': 'my_executable',
936 }
Miss Islington (bot)a6427cb2019-08-23 09:24:42 -0700937 def modify_path(path):
938 path.insert(1, "test_path_insert1")
939 path.append("test_path_append")
Victor Stinner331a6a52019-05-27 16:39:22 +0200940 self.check_all_configs("test_init_read_set", config,
941 api=API_PYTHON,
Miss Islington (bot)a6427cb2019-08-23 09:24:42 -0700942 modify_path_cb=modify_path)
Victor Stinner91c99872019-05-14 22:01:51 +0200943
Victor Stinneraf84a882019-08-23 21:16:51 +0200944 def test_init_sys_add(self):
945 config = {
946 'faulthandler': 1,
947 'xoptions': [
948 'config_xoption',
949 'cmdline_xoption',
950 'sysadd_xoption',
951 'faulthandler',
952 ],
953 'warnoptions': [
Victor Stinneraf84a882019-08-23 21:16:51 +0200954 'ignore:::cmdline_warnoption',
955 'ignore:::sysadd_warnoption',
Miss Islington (bot)c9ed9e62019-09-29 16:58:57 -0700956 'ignore:::config_warnoption',
Victor Stinneraf84a882019-08-23 21:16:51 +0200957 ],
958 }
959 self.check_all_configs("test_init_sys_add", config, api=API_PYTHON)
960
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200961 def test_init_run_main(self):
Victor Stinner5eb8b072019-05-15 02:12:48 +0200962 code = ('import _testinternalcapi, json; '
963 'print(json.dumps(_testinternalcapi.get_configs()))')
Victor Stinner331a6a52019-05-27 16:39:22 +0200964 config = {
Victor Stinner5eb8b072019-05-15 02:12:48 +0200965 'argv': ['-c', 'arg2'],
Victor Stinner5eb8b072019-05-15 02:12:48 +0200966 'program_name': './python3',
967 'run_command': code + '\n',
Victor Stinnercab5d072019-05-17 19:01:14 +0200968 'parse_argv': 1,
Victor Stinner5eb8b072019-05-15 02:12:48 +0200969 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200970 self.check_all_configs("test_init_run_main", config, api=API_PYTHON)
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200971
972 def test_init_main(self):
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200973 code = ('import _testinternalcapi, json; '
974 'print(json.dumps(_testinternalcapi.get_configs()))')
Victor Stinner331a6a52019-05-27 16:39:22 +0200975 config = {
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200976 'argv': ['-c', 'arg2'],
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200977 'program_name': './python3',
978 'run_command': code + '\n',
Victor Stinnercab5d072019-05-17 19:01:14 +0200979 'parse_argv': 1,
Victor Stinner9ef5dca2019-05-16 17:38:16 +0200980 '_init_main': 0,
981 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200982 self.check_all_configs("test_init_main", config,
983 api=API_PYTHON,
984 stderr="Run Python code before _Py_InitializeMain")
Victor Stinner5eb8b072019-05-15 02:12:48 +0200985
Victor Stinnercab5d072019-05-17 19:01:14 +0200986 def test_init_parse_argv(self):
Victor Stinner331a6a52019-05-27 16:39:22 +0200987 config = {
Victor Stinnerbab0db62019-05-18 03:21:27 +0200988 'parse_argv': 1,
Victor Stinnercab5d072019-05-17 19:01:14 +0200989 'argv': ['-c', 'arg1', '-v', 'arg3'],
990 'program_name': './argv0',
Victor Stinnercab5d072019-05-17 19:01:14 +0200991 'run_command': 'pass\n',
992 'use_environment': 0,
993 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200994 self.check_all_configs("test_init_parse_argv", config, api=API_PYTHON)
Victor Stinnercab5d072019-05-17 19:01:14 +0200995
Victor Stinnerae239f62019-05-16 17:02:56 +0200996 def test_init_dont_parse_argv(self):
Victor Stinner6d1c4672019-05-20 11:02:00 +0200997 pre_config = {
998 'parse_argv': 0,
999 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001000 config = {
Victor Stinnerbab0db62019-05-18 03:21:27 +02001001 'parse_argv': 0,
Victor Stinnercab5d072019-05-17 19:01:14 +02001002 'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'],
1003 'program_name': './argv0',
Victor Stinnerae239f62019-05-16 17:02:56 +02001004 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001005 self.check_all_configs("test_init_dont_parse_argv", config, pre_config,
1006 api=API_PYTHON)
Victor Stinnerae239f62019-05-16 17:02:56 +02001007
Victor Stinner96c84752019-09-26 16:17:34 +02001008 def default_program_name(self, config):
1009 if MS_WINDOWS:
1010 program_name = 'python'
1011 executable = self.test_exe
1012 else:
1013 program_name = 'python3'
1014 if MACOS:
1015 executable = self.test_exe
1016 else:
1017 executable = shutil.which(program_name) or ''
1018 config.update({
1019 'program_name': program_name,
1020 'base_executable': executable,
1021 'executable': executable,
1022 })
1023
1024 def test_init_setpath(self):
1025 # Test Py_SetPath()
1026 config = self._get_expected_config()
1027 paths = config['config']['module_search_paths']
1028
1029 config = {
1030 'module_search_paths': paths,
1031 'prefix': '',
1032 'base_prefix': '',
1033 'exec_prefix': '',
1034 'base_exec_prefix': '',
1035 }
1036 self.default_program_name(config)
1037 env = {'TESTPATH': os.path.pathsep.join(paths)}
1038 self.check_all_configs("test_init_setpath", config,
1039 api=API_COMPAT, env=env,
1040 ignore_stderr=True)
1041
1042 def test_init_setpath_config(self):
1043 # Test Py_SetPath() with PyConfig
1044 config = self._get_expected_config()
1045 paths = config['config']['module_search_paths']
1046
1047 config = {
1048 # set by Py_SetPath()
1049 'module_search_paths': paths,
1050 'prefix': '',
1051 'base_prefix': '',
1052 'exec_prefix': '',
1053 'base_exec_prefix': '',
1054 # overriden by PyConfig
1055 'program_name': 'conf_program_name',
1056 'base_executable': 'conf_executable',
1057 'executable': 'conf_executable',
1058 }
1059 env = {'TESTPATH': os.path.pathsep.join(paths)}
1060 self.check_all_configs("test_init_setpath_config", config,
1061 api=API_PYTHON, env=env, ignore_stderr=True)
1062
1063 def module_search_paths(self, prefix=None, exec_prefix=None):
1064 config = self._get_expected_config()
1065 if prefix is None:
1066 prefix = config['config']['prefix']
1067 if exec_prefix is None:
1068 exec_prefix = config['config']['prefix']
1069 if MS_WINDOWS:
1070 return config['config']['module_search_paths']
1071 else:
1072 ver = sys.version_info
1073 return [
1074 os.path.join(prefix, 'lib',
1075 f'python{ver.major}{ver.minor}.zip'),
1076 os.path.join(prefix, 'lib',
1077 f'python{ver.major}.{ver.minor}'),
1078 os.path.join(exec_prefix, 'lib',
1079 f'python{ver.major}.{ver.minor}', 'lib-dynload'),
1080 ]
1081
1082 @contextlib.contextmanager
1083 def tmpdir_with_python(self):
1084 # Temporary directory with a copy of the Python program
1085 with tempfile.TemporaryDirectory() as tmpdir:
1086 # bpo-38234: On macOS and FreeBSD, the temporary directory
1087 # can be symbolic link. For example, /tmp can be a symbolic link
1088 # to /var/tmp. Call realpath() to resolve all symbolic links.
1089 tmpdir = os.path.realpath(tmpdir)
1090
1091 if MS_WINDOWS:
1092 # Copy pythonXY.dll (or pythonXY_d.dll)
1093 ver = sys.version_info
1094 dll = f'python{ver.major}{ver.minor}'
1095 if debug_build(sys.executable):
1096 dll += '_d'
1097 dll += '.dll'
1098 dll = os.path.join(os.path.dirname(self.test_exe), dll)
1099 dll_copy = os.path.join(tmpdir, os.path.basename(dll))
1100 shutil.copyfile(dll, dll_copy)
1101
1102 # Copy Python program
1103 exec_copy = os.path.join(tmpdir, os.path.basename(self.test_exe))
1104 shutil.copyfile(self.test_exe, exec_copy)
1105 shutil.copystat(self.test_exe, exec_copy)
1106 self.test_exe = exec_copy
1107
1108 yield tmpdir
1109
1110 def test_init_setpythonhome(self):
1111 # Test Py_SetPythonHome(home) with PYTHONPATH env var
1112 config = self._get_expected_config()
1113 paths = config['config']['module_search_paths']
1114 paths_str = os.path.pathsep.join(paths)
1115
1116 for path in paths:
1117 if not os.path.isdir(path):
1118 continue
1119 if os.path.exists(os.path.join(path, 'os.py')):
1120 home = os.path.dirname(path)
1121 break
1122 else:
1123 self.fail(f"Unable to find home in {paths!r}")
1124
1125 prefix = exec_prefix = home
1126 ver = sys.version_info
1127 expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)
1128
1129 config = {
1130 'home': home,
1131 'module_search_paths': expected_paths,
1132 'prefix': prefix,
1133 'base_prefix': prefix,
1134 'exec_prefix': exec_prefix,
1135 'base_exec_prefix': exec_prefix,
1136 'pythonpath_env': paths_str,
1137 }
1138 self.default_program_name(config)
1139 env = {'TESTHOME': home, 'PYTHONPATH': paths_str}
1140 self.check_all_configs("test_init_setpythonhome", config,
1141 api=API_COMPAT, env=env)
1142
1143 def copy_paths_by_env(self, config):
1144 all_configs = self._get_expected_config()
1145 paths = all_configs['config']['module_search_paths']
1146 paths_str = os.path.pathsep.join(paths)
1147 config['pythonpath_env'] = paths_str
1148 env = {'PYTHONPATH': paths_str}
1149 return env
1150
1151 @unittest.skipIf(MS_WINDOWS, 'Windows does not use pybuilddir.txt')
1152 def test_init_pybuilddir(self):
1153 # Test path configuration with pybuilddir.txt configuration file
1154
1155 with self.tmpdir_with_python() as tmpdir:
1156 # pybuilddir.txt is a sub-directory relative to the current
1157 # directory (tmpdir)
1158 subdir = 'libdir'
1159 libdir = os.path.join(tmpdir, subdir)
1160 os.mkdir(libdir)
1161
1162 filename = os.path.join(tmpdir, 'pybuilddir.txt')
1163 with open(filename, "w", encoding="utf8") as fp:
1164 fp.write(subdir)
1165
1166 module_search_paths = self.module_search_paths()
1167 module_search_paths[-1] = libdir
1168
1169 executable = self.test_exe
1170 config = {
1171 'base_executable': executable,
1172 'executable': executable,
1173 'module_search_paths': module_search_paths,
1174 }
1175 env = self.copy_paths_by_env(config)
1176 self.check_all_configs("test_init_compat_config", config,
1177 api=API_COMPAT, env=env,
1178 ignore_stderr=True, cwd=tmpdir)
1179
1180 def test_init_pyvenv_cfg(self):
1181 # Test path configuration with pyvenv.cfg configuration file
1182
1183 with self.tmpdir_with_python() as tmpdir, \
1184 tempfile.TemporaryDirectory() as pyvenv_home:
1185 ver = sys.version_info
1186
1187 if not MS_WINDOWS:
1188 lib_dynload = os.path.join(pyvenv_home,
1189 'lib',
1190 f'python{ver.major}.{ver.minor}',
1191 'lib-dynload')
1192 os.makedirs(lib_dynload)
1193 else:
1194 lib_dynload = os.path.join(pyvenv_home, 'lib')
1195 os.makedirs(lib_dynload)
1196 # getpathp.c uses Lib\os.py as the LANDMARK
1197 shutil.copyfile(os.__file__, os.path.join(lib_dynload, 'os.py'))
1198
1199 filename = os.path.join(tmpdir, 'pyvenv.cfg')
1200 with open(filename, "w", encoding="utf8") as fp:
1201 print("home = %s" % pyvenv_home, file=fp)
1202 print("include-system-site-packages = false", file=fp)
1203
1204 paths = self.module_search_paths()
1205 if not MS_WINDOWS:
1206 paths[-1] = lib_dynload
1207 else:
1208 for index, path in enumerate(paths):
1209 if index == 0:
1210 paths[index] = os.path.join(tmpdir, os.path.basename(path))
1211 else:
1212 paths[index] = os.path.join(pyvenv_home, os.path.basename(path))
1213 paths[-1] = pyvenv_home
1214
1215 executable = self.test_exe
1216 exec_prefix = pyvenv_home
1217 config = {
1218 'base_exec_prefix': exec_prefix,
1219 'exec_prefix': exec_prefix,
1220 'base_executable': executable,
1221 'executable': executable,
1222 'module_search_paths': paths,
1223 }
1224 if MS_WINDOWS:
1225 config['base_prefix'] = pyvenv_home
1226 config['prefix'] = pyvenv_home
1227 env = self.copy_paths_by_env(config)
1228 self.check_all_configs("test_init_compat_config", config,
1229 api=API_COMPAT, env=env,
1230 ignore_stderr=True, cwd=tmpdir)
1231
1232 def test_global_pathconfig(self):
1233 # Test C API functions getting the path configuration:
1234 #
1235 # - Py_GetExecPrefix()
1236 # - Py_GetPath()
1237 # - Py_GetPrefix()
1238 # - Py_GetProgramFullPath()
1239 # - Py_GetProgramName()
1240 # - Py_GetPythonHome()
1241 #
1242 # The global path configuration (_Py_path_config) must be a copy
1243 # of the path configuration of PyInterpreter.config (PyConfig).
1244 ctypes = support.import_module('ctypes')
1245 _testinternalcapi = support.import_module('_testinternalcapi')
1246
1247 def get_func(name):
1248 func = getattr(ctypes.pythonapi, name)
1249 func.argtypes = ()
1250 func.restype = ctypes.c_wchar_p
1251 return func
1252
1253 Py_GetPath = get_func('Py_GetPath')
1254 Py_GetPrefix = get_func('Py_GetPrefix')
1255 Py_GetExecPrefix = get_func('Py_GetExecPrefix')
1256 Py_GetProgramName = get_func('Py_GetProgramName')
1257 Py_GetProgramFullPath = get_func('Py_GetProgramFullPath')
1258 Py_GetPythonHome = get_func('Py_GetPythonHome')
1259
1260 config = _testinternalcapi.get_configs()['config']
1261
1262 self.assertEqual(Py_GetPath().split(os.path.pathsep),
1263 config['module_search_paths'])
1264 self.assertEqual(Py_GetPrefix(), config['prefix'])
1265 self.assertEqual(Py_GetExecPrefix(), config['exec_prefix'])
1266 self.assertEqual(Py_GetProgramName(), config['program_name'])
1267 self.assertEqual(Py_GetProgramFullPath(), config['executable'])
1268 self.assertEqual(Py_GetPythonHome(), config['home'])
1269
Miss Islington (bot)c9ed9e62019-09-29 16:58:57 -07001270 def test_init_warnoptions(self):
1271 # lowest to highest priority
1272 warnoptions = [
1273 'ignore:::PyConfig_Insert0', # PyWideStringList_Insert(0)
1274 'default', # PyConfig.dev_mode=1
1275 'ignore:::env1', # PYTHONWARNINGS env var
1276 'ignore:::env2', # PYTHONWARNINGS env var
1277 'ignore:::cmdline1', # -W opt command line option
1278 'ignore:::cmdline2', # -W opt command line option
1279 'default::BytesWarning', # PyConfig.bytes_warnings=1
1280 'ignore:::PySys_AddWarnOption1', # PySys_AddWarnOption()
1281 'ignore:::PySys_AddWarnOption2', # PySys_AddWarnOption()
1282 'ignore:::PyConfig_BeforeRead', # PyConfig.warnoptions
1283 'ignore:::PyConfig_AfterRead'] # PyWideStringList_Append()
1284 preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG)
1285 config = {
1286 'dev_mode': 1,
1287 'faulthandler': 1,
1288 'bytes_warning': 1,
1289 'warnoptions': warnoptions,
1290 }
1291 self.check_all_configs("test_init_warnoptions", config, preconfig,
1292 api=API_PYTHON)
1293
Victor Stinner56b29b62018-07-26 18:57:56 +02001294
Steve Dowerb82e17e2019-05-23 08:45:22 -07001295class AuditingTests(EmbeddingTestsMixin, unittest.TestCase):
1296 def test_open_code_hook(self):
1297 self.run_embedded_interpreter("test_open_code_hook")
1298
1299 def test_audit(self):
1300 self.run_embedded_interpreter("test_audit")
1301
1302 def test_audit_subinterpreter(self):
1303 self.run_embedded_interpreter("test_audit_subinterpreter")
1304
Miss Islington (bot)746992c2019-07-01 16:22:29 -07001305 def test_audit_run_command(self):
1306 self.run_embedded_interpreter("test_audit_run_command", timeout=3, returncode=1)
1307
1308 def test_audit_run_file(self):
1309 self.run_embedded_interpreter("test_audit_run_file", timeout=3, returncode=1)
1310
1311 def test_audit_run_interactivehook(self):
1312 startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
1313 with open(startup, "w", encoding="utf-8") as f:
1314 print("import sys", file=f)
1315 print("sys.__interactivehook__ = lambda: None", file=f)
1316 try:
1317 env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
1318 self.run_embedded_interpreter("test_audit_run_interactivehook", timeout=5,
1319 returncode=10, env=env)
1320 finally:
1321 os.unlink(startup)
1322
1323 def test_audit_run_startup(self):
1324 startup = os.path.join(self.oldcwd, support.TESTFN) + ".py"
1325 with open(startup, "w", encoding="utf-8") as f:
1326 print("pass", file=f)
1327 try:
1328 env = {**remove_python_envvars(), "PYTHONSTARTUP": startup}
1329 self.run_embedded_interpreter("test_audit_run_startup", timeout=5,
1330 returncode=10, env=env)
1331 finally:
1332 os.unlink(startup)
1333
1334 def test_audit_run_stdin(self):
1335 self.run_embedded_interpreter("test_audit_run_stdin", timeout=3, returncode=1)
Steve Dowerb82e17e2019-05-23 08:45:22 -07001336
Nick Coghlan39f0bb52017-11-28 08:11:51 +10001337if __name__ == "__main__":
1338 unittest.main()