blob: 3f383d7fd50c8f4f6821d3a657b346e9e6a12940 [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 Stinner7ddd56f2018-11-14 00:24:28 +01006import json
Nick Coghlan39f0bb52017-11-28 08:11:51 +10007import os
Michael Feltd2067312018-09-15 11:28:31 +02008import platform
Nick Coghlan39f0bb52017-11-28 08:11:51 +10009import re
10import subprocess
11import sys
12
13
Victor Stinner01de89c2018-11-14 17:39:45 +010014MS_WINDOWS = (os.name == 'nt')
15
16
Victor Stinner56b29b62018-07-26 18:57:56 +020017class EmbeddingTestsMixin:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100018 def setUp(self):
19 here = os.path.abspath(__file__)
20 basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
21 exename = "_testembed"
Victor Stinner01de89c2018-11-14 17:39:45 +010022 if MS_WINDOWS:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100023 ext = ("_d" if "_d" in sys.executable else "") + ".exe"
24 exename += ext
25 exepath = os.path.dirname(sys.executable)
26 else:
27 exepath = os.path.join(basepath, "Programs")
28 self.test_exe = exe = os.path.join(exepath, exename)
29 if not os.path.exists(exe):
30 self.skipTest("%r doesn't exist" % exe)
31 # This is needed otherwise we get a fatal error:
32 # "Py_Initialize: Unable to get the locale encoding
33 # LookupError: no codec search functions registered: can't find encoding"
34 self.oldcwd = os.getcwd()
35 os.chdir(basepath)
36
37 def tearDown(self):
38 os.chdir(self.oldcwd)
39
40 def run_embedded_interpreter(self, *args, env=None):
41 """Runs a test in the embedded interpreter"""
42 cmd = [self.test_exe]
43 cmd.extend(args)
Victor Stinner01de89c2018-11-14 17:39:45 +010044 if env is not None and MS_WINDOWS:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100045 # Windows requires at least the SYSTEMROOT environment variable to
46 # start Python.
47 env = env.copy()
48 env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
49
50 p = subprocess.Popen(cmd,
51 stdout=subprocess.PIPE,
52 stderr=subprocess.PIPE,
53 universal_newlines=True,
54 env=env)
55 (out, err) = p.communicate()
56 if p.returncode != 0 and support.verbose:
57 print(f"--- {cmd} failed ---")
58 print(f"stdout:\n{out}")
Nick Coghlanbc77eff2018-03-25 20:44:30 +100059 print(f"stderr:\n{err}")
Nick Coghlan39f0bb52017-11-28 08:11:51 +100060 print(f"------")
61
62 self.assertEqual(p.returncode, 0,
63 "bad returncode %d, stderr is %r" %
64 (p.returncode, err))
65 return out, err
66
67 def run_repeated_init_and_subinterpreters(self):
68 out, err = self.run_embedded_interpreter("repeated_init_and_subinterpreters")
69 self.assertEqual(err, "")
70
71 # The output from _testembed looks like this:
72 # --- Pass 0 ---
73 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
74 # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
75 # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
76 # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
77 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
78 # --- Pass 1 ---
79 # ...
80
81 interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
82 r"thread state <(0x[\dA-F]+)>: "
83 r"id\(modules\) = ([\d]+)$")
84 Interp = namedtuple("Interp", "id interp tstate modules")
85
86 numloops = 0
87 current_run = []
88 for line in out.splitlines():
89 if line == "--- Pass {} ---".format(numloops):
90 self.assertEqual(len(current_run), 0)
Nick Coghlanbc77eff2018-03-25 20:44:30 +100091 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100092 print(line)
93 numloops += 1
94 continue
95
96 self.assertLess(len(current_run), 5)
97 match = re.match(interp_pat, line)
98 if match is None:
99 self.assertRegex(line, interp_pat)
100
101 # Parse the line from the loop. The first line is the main
102 # interpreter and the 3 afterward are subinterpreters.
103 interp = Interp(*match.groups())
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000104 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000105 print(interp)
106 self.assertTrue(interp.interp)
107 self.assertTrue(interp.tstate)
108 self.assertTrue(interp.modules)
109 current_run.append(interp)
110
111 # The last line in the loop should be the same as the first.
112 if len(current_run) == 5:
113 main = current_run[0]
114 self.assertEqual(interp, main)
115 yield current_run
116 current_run = []
117
Victor Stinner56b29b62018-07-26 18:57:56 +0200118
119class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000120 def test_subinterps_main(self):
121 for run in self.run_repeated_init_and_subinterpreters():
122 main = run[0]
123
124 self.assertEqual(main.id, '0')
125
126 def test_subinterps_different_ids(self):
127 for run in self.run_repeated_init_and_subinterpreters():
128 main, *subs, _ = run
129
130 mainid = int(main.id)
131 for i, sub in enumerate(subs):
132 self.assertEqual(sub.id, str(mainid + i + 1))
133
134 def test_subinterps_distinct_state(self):
135 for run in self.run_repeated_init_and_subinterpreters():
136 main, *subs, _ = run
137
138 if '0x0' in main:
139 # XXX Fix on Windows (and other platforms): something
140 # is going on with the pointers in Programs/_testembed.c.
141 # interp.interp is 0x0 and interp.modules is the same
142 # between interpreters.
143 raise unittest.SkipTest('platform prints pointers as 0x0')
144
145 for sub in subs:
146 # A new subinterpreter may have the same
147 # PyInterpreterState pointer as a previous one if
148 # the earlier one has already been destroyed. So
149 # we compare with the main interpreter. The same
150 # applies to tstate.
151 self.assertNotEqual(sub.interp, main.interp)
152 self.assertNotEqual(sub.tstate, main.tstate)
153 self.assertNotEqual(sub.modules, main.modules)
154
155 def test_forced_io_encoding(self):
156 # Checks forced configuration of embedded interpreter IO streams
157 env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
158 out, err = self.run_embedded_interpreter("forced_io_encoding", env=env)
159 if support.verbose > 1:
160 print()
161 print(out)
162 print(err)
163 expected_stream_encoding = "utf-8"
164 expected_errors = "surrogateescape"
165 expected_output = '\n'.join([
166 "--- Use defaults ---",
167 "Expected encoding: default",
168 "Expected errors: default",
169 "stdin: {in_encoding}:{errors}",
170 "stdout: {out_encoding}:{errors}",
171 "stderr: {out_encoding}:backslashreplace",
172 "--- Set errors only ---",
173 "Expected encoding: default",
174 "Expected errors: ignore",
175 "stdin: {in_encoding}:ignore",
176 "stdout: {out_encoding}:ignore",
177 "stderr: {out_encoding}:backslashreplace",
178 "--- Set encoding only ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200179 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000180 "Expected errors: default",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200181 "stdin: iso8859-1:{errors}",
182 "stdout: iso8859-1:{errors}",
183 "stderr: iso8859-1:backslashreplace",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000184 "--- Set encoding and errors ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200185 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000186 "Expected errors: replace",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200187 "stdin: iso8859-1:replace",
188 "stdout: iso8859-1:replace",
189 "stderr: iso8859-1:backslashreplace"])
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000190 expected_output = expected_output.format(
191 in_encoding=expected_stream_encoding,
192 out_encoding=expected_stream_encoding,
193 errors=expected_errors)
194 # This is useful if we ever trip over odd platform behaviour
195 self.maxDiff = None
196 self.assertEqual(out.strip(), expected_output)
197
198 def test_pre_initialization_api(self):
199 """
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000200 Checks some key parts of the C-API that need to work before the runtine
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000201 is initialized (via Py_Initialize()).
202 """
203 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
204 out, err = self.run_embedded_interpreter("pre_initialization_api", env=env)
Victor Stinner01de89c2018-11-14 17:39:45 +0100205 if MS_WINDOWS:
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000206 expected_path = self.test_exe
207 else:
208 expected_path = os.path.join(os.getcwd(), "spam")
209 expected_output = f"sys.executable: {expected_path}\n"
210 self.assertIn(expected_output, out)
211 self.assertEqual(err, '')
212
213 def test_pre_initialization_sys_options(self):
214 """
215 Checks that sys.warnoptions and sys._xoptions can be set before the
216 runtime is initialized (otherwise they won't be effective).
217 """
Pablo Galindo41148462018-04-27 13:23:13 +0100218 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000219 out, err = self.run_embedded_interpreter(
220 "pre_initialization_sys_options", env=env)
221 expected_output = (
222 "sys.warnoptions: ['once', 'module', 'default']\n"
223 "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
224 "warnings.filters[:3]: ['default', 'module', 'once']\n"
225 )
226 self.assertIn(expected_output, out)
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000227 self.assertEqual(err, '')
228
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100229 def test_bpo20891(self):
230 """
231 bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
232 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
233 call PyEval_InitThreads() for us in this case.
234 """
235 out, err = self.run_embedded_interpreter("bpo20891")
236 self.assertEqual(out, '')
237 self.assertEqual(err, '')
238
Victor Stinner209abf72018-06-22 19:14:51 +0200239 def test_initialize_twice(self):
240 """
241 bpo-33932: Calling Py_Initialize() twice should do nothing (and not
242 crash!).
243 """
244 out, err = self.run_embedded_interpreter("initialize_twice")
245 self.assertEqual(out, '')
246 self.assertEqual(err, '')
247
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200248 def test_initialize_pymain(self):
249 """
250 bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
251 """
252 out, err = self.run_embedded_interpreter("initialize_pymain")
253 self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
254 self.assertEqual(err, '')
255
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000256
Victor Stinner56b29b62018-07-26 18:57:56 +0200257class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
258 maxDiff = 4096
Victor Stinner01de89c2018-11-14 17:39:45 +0100259 UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape')
260
261 # core config
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100262 UNTESTED_CORE_CONFIG = (
Victor Stinner01de89c2018-11-14 17:39:45 +0100263 # FIXME: untested core configuration variables
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100264 'dll_path',
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100265 'executable',
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100266 'module_search_paths',
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100267 )
Victor Stinner00b137c2018-11-13 19:59:26 +0100268 DEFAULT_CORE_CONFIG = {
Victor Stinner56b29b62018-07-26 18:57:56 +0200269 'install_signal_handlers': 1,
270 'use_environment': 1,
271 'use_hash_seed': 0,
272 'hash_seed': 0,
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100273 'allocator': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200274 'dev_mode': 0,
275 'faulthandler': 0,
276 'tracemalloc': 0,
277 'import_time': 0,
278 'show_ref_count': 0,
279 'show_alloc_count': 0,
280 'dump_refs': 0,
281 'malloc_stats': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200282
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200283 # None means that the value is get by get_locale_encoding()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200284 'filesystem_encoding': None,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200285 'filesystem_errors': None,
286
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200287 'utf8_mode': 0,
Victor Stinner06e76082018-09-19 14:56:36 -0700288 'coerce_c_locale': 0,
289 'coerce_c_locale_warn': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200290
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100291 'pycache_prefix': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200292 'program_name': './_testembed',
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100293 'argv': [],
294 'program': None,
295
296 'xoptions': [],
297 'warnoptions': [],
Victor Stinner56b29b62018-07-26 18:57:56 +0200298
Victor Stinner01de89c2018-11-14 17:39:45 +0100299 'module_search_path_env': None,
300 'home': None,
301 'prefix': sys.prefix,
302 'base_prefix': sys.base_prefix,
303 'exec_prefix': sys.exec_prefix,
304 'base_exec_prefix': sys.base_exec_prefix,
305
Victor Stinner56b29b62018-07-26 18:57:56 +0200306 'isolated': 0,
307 'site_import': 1,
308 'bytes_warning': 0,
309 'inspect': 0,
310 'interactive': 0,
311 'optimization_level': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200312 'parser_debug': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200313 'write_bytecode': 1,
314 'verbose': 0,
315 'quiet': 0,
316 'user_site_directory': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200317 'buffered_stdio': 1,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200318
319 # None means that the value is get by get_stdio_encoding()
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200320 'stdio_encoding': None,
321 'stdio_errors': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200322
323 '_install_importlib': 1,
324 '_check_hash_pycs_mode': 'default',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200325 '_frozen': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200326 }
Victor Stinner01de89c2018-11-14 17:39:45 +0100327 if MS_WINDOWS:
328 DEFAULT_CORE_CONFIG.update({
329 'legacy_windows_fs_encoding': 0,
330 'legacy_windows_stdio': 0,
331 })
332
333 # main config
Victor Stinner01de89c2018-11-14 17:39:45 +0100334 COPY_MAIN_CONFIG = (
335 # Copy core config to main config for expected values
336 'argv',
337 'base_exec_prefix',
338 'base_prefix',
339 'exec_prefix',
340 'executable',
341 'install_signal_handlers',
342 'prefix',
343 'pycache_prefix',
344 'warnoptions',
Victor Stinner37cd9822018-11-16 11:55:35 +0100345 # xoptions is created from core_config in check_main_config().
346 # 'module_search_paths' is copied to 'module_search_path'.
Victor Stinner01de89c2018-11-14 17:39:45 +0100347 )
348
349 # global config
350 DEFAULT_GLOBAL_CONFIG = {
351 'Py_HasFileSystemDefaultEncoding': 0,
352 'Py_HashRandomizationFlag': 1,
353 '_Py_HasFileSystemDefaultEncodeErrors': 0,
354 }
355 COPY_GLOBAL_CONFIG = [
356 # Copy core config to global config for expected values
357 # True means that the core config value is inverted (0 => 1 and 1 => 0)
358 ('Py_BytesWarningFlag', 'bytes_warning'),
359 ('Py_DebugFlag', 'parser_debug'),
360 ('Py_DontWriteBytecodeFlag', 'write_bytecode', True),
361 ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),
362 ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),
363 ('Py_FrozenFlag', '_frozen'),
364 ('Py_IgnoreEnvironmentFlag', 'use_environment', True),
365 ('Py_InspectFlag', 'inspect'),
366 ('Py_InteractiveFlag', 'interactive'),
367 ('Py_IsolatedFlag', 'isolated'),
368 ('Py_NoSiteFlag', 'site_import', True),
369 ('Py_NoUserSiteDirectory', 'user_site_directory', True),
370 ('Py_OptimizeFlag', 'optimization_level'),
371 ('Py_QuietFlag', 'quiet'),
372 ('Py_UTF8Mode', 'utf8_mode'),
373 ('Py_UnbufferedStdioFlag', 'buffered_stdio', True),
374 ('Py_VerboseFlag', 'verbose'),
375 ]
376 if MS_WINDOWS:
377 COPY_GLOBAL_CONFIG.extend((
378 ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'),
379 ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'),
380 ))
Victor Stinner56b29b62018-07-26 18:57:56 +0200381
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200382 def get_stdio_encoding(self, env):
383 code = 'import sys; print(sys.stdout.encoding, sys.stdout.errors)'
384 args = (sys.executable, '-c', code)
385 proc = subprocess.run(args, env=env, text=True,
386 stdout=subprocess.PIPE,
387 stderr=subprocess.STDOUT)
388 if proc.returncode:
389 raise Exception(f"failed to get the stdio encoding: stdout={proc.stdout!r}")
390 out = proc.stdout.rstrip()
391 return out.split()
392
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200393 def get_filesystem_encoding(self, isolated, env):
394 code = ('import codecs, locale, sys; '
395 'print(sys.getfilesystemencoding(), '
396 'sys.getfilesystemencodeerrors())')
397 args = (sys.executable, '-c', code)
Victor Stinner06e76082018-09-19 14:56:36 -0700398 env = dict(env)
399 if not isolated:
400 env['PYTHONCOERCECLOCALE'] = '0'
401 env['PYTHONUTF8'] = '0'
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200402 proc = subprocess.run(args, text=True, env=env,
403 stdout=subprocess.PIPE,
404 stderr=subprocess.PIPE)
405 if proc.returncode:
406 raise Exception(f"failed to get the locale encoding: "
407 f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200408 out = proc.stdout.rstrip()
409 return out.split()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200410
Victor Stinner01de89c2018-11-14 17:39:45 +0100411 def main_xoptions(self, xoptions_list):
412 xoptions = {}
413 for opt in xoptions_list:
414 if '=' in opt:
415 key, value = opt.split('=', 1)
416 xoptions[key] = value
417 else:
418 xoptions[opt] = True
419 return xoptions
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200420
Victor Stinner01de89c2018-11-14 17:39:45 +0100421 def check_main_config(self, config):
422 core_config = config['core_config']
423 main_config = config['main_config']
Victor Stinner56b29b62018-07-26 18:57:56 +0200424
Victor Stinner01de89c2018-11-14 17:39:45 +0100425 # main config
Victor Stinner01de89c2018-11-14 17:39:45 +0100426 expected_main = {}
427 for key in self.COPY_MAIN_CONFIG:
428 expected_main[key] = core_config[key]
Victor Stinner37cd9822018-11-16 11:55:35 +0100429 expected_main['module_search_path'] = core_config['module_search_paths']
Victor Stinner01de89c2018-11-14 17:39:45 +0100430 expected_main['xoptions'] = self.main_xoptions(core_config['xoptions'])
431 self.assertEqual(main_config, expected_main)
432
433 def check_core_config(self, config, expected, env):
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200434 if expected['stdio_encoding'] is None or expected['stdio_errors'] is None:
435 res = self.get_stdio_encoding(env)
436 if expected['stdio_encoding'] is None:
437 expected['stdio_encoding'] = res[0]
438 if expected['stdio_errors'] is None:
439 expected['stdio_errors'] = res[1]
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200440 if expected['filesystem_encoding'] is None or expected['filesystem_errors'] is None:
441 res = self.get_filesystem_encoding(expected['isolated'], env)
442 if expected['filesystem_encoding'] is None:
443 expected['filesystem_encoding'] = res[0]
444 if expected['filesystem_errors'] is None:
445 expected['filesystem_errors'] = res[1]
Victor Stinner56b29b62018-07-26 18:57:56 +0200446
Victor Stinner01de89c2018-11-14 17:39:45 +0100447 core_config = dict(config['core_config'])
448 for key in self.UNTESTED_CORE_CONFIG:
449 core_config.pop(key, None)
450 self.assertEqual(core_config, expected)
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200451
Victor Stinner01de89c2018-11-14 17:39:45 +0100452 def check_global_config(self, config):
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100453 core_config = config['core_config']
Victor Stinner00b137c2018-11-13 19:59:26 +0100454
Victor Stinner01de89c2018-11-14 17:39:45 +0100455 expected_global = dict(self.DEFAULT_GLOBAL_CONFIG)
456 for item in self.COPY_GLOBAL_CONFIG:
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100457 if len(item) == 3:
458 global_key, core_key, opposite = item
459 expected_global[global_key] = 0 if core_config[core_key] else 1
460 else:
461 global_key, core_key = item
462 expected_global[global_key] = core_config[core_key]
463
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100464 self.assertEqual(config['global_config'], expected_global)
465
Victor Stinner01de89c2018-11-14 17:39:45 +0100466 def check_config(self, testname, expected):
467 expected = dict(self.DEFAULT_CORE_CONFIG, **expected)
468
469 env = dict(os.environ)
470 # Remove PYTHON* environment variables to get deterministic environment
471 for key in list(env):
472 if key.startswith('PYTHON'):
473 del env[key]
474 # Disable C locale coercion and UTF-8 mode to not depend
475 # on the current locale
476 env['PYTHONCOERCECLOCALE'] = '0'
477 env['PYTHONUTF8'] = '0'
478
479 out, err = self.run_embedded_interpreter(testname, env=env)
480 # Ignore err
481 config = json.loads(out)
482
483 self.check_core_config(config, expected, env)
484 self.check_main_config(config)
485 self.check_global_config(config)
Victor Stinner7ddd56f2018-11-14 00:24:28 +0100486
Victor Stinner56b29b62018-07-26 18:57:56 +0200487 def test_init_default_config(self):
488 self.check_config("init_default_config", {})
489
490 def test_init_global_config(self):
491 config = {
492 'program_name': './globalvar',
493 'site_import': 0,
494 'bytes_warning': 1,
495 'inspect': 1,
496 'interactive': 1,
497 'optimization_level': 2,
498 'write_bytecode': 0,
499 'verbose': 1,
500 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200501 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200502
Victor Stinner56b29b62018-07-26 18:57:56 +0200503 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200504 'stdio_encoding': 'utf-8',
505 'stdio_errors': 'surrogateescape',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200506 'filesystem_encoding': 'utf-8',
507 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200508 'user_site_directory': 0,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200509 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200510 }
511 self.check_config("init_global_config", config)
512
513 def test_init_from_config(self):
514 config = {
515 'install_signal_handlers': 0,
516 'use_hash_seed': 1,
517 'hash_seed': 123,
518 'allocator': 'malloc_debug',
519 'tracemalloc': 2,
520 'import_time': 1,
521 'show_ref_count': 1,
522 'show_alloc_count': 1,
523 'malloc_stats': 1,
524
525 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200526 'stdio_encoding': 'iso8859-1',
527 'stdio_errors': 'replace',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200528 'filesystem_encoding': 'utf-8',
529 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200530
531 'pycache_prefix': 'conf_pycache_prefix',
532 'program_name': './conf_program_name',
Victor Stinner01de89c2018-11-14 17:39:45 +0100533 'argv': ['-c', 'pass'],
Victor Stinner56b29b62018-07-26 18:57:56 +0200534 'program': 'conf_program',
Victor Stinner01de89c2018-11-14 17:39:45 +0100535 'xoptions': ['core_xoption1=3', 'core_xoption2=', 'core_xoption3'],
536 'warnoptions': ['default', 'error::ResourceWarning'],
Victor Stinner56b29b62018-07-26 18:57:56 +0200537
538 'site_import': 0,
539 'bytes_warning': 1,
540 'inspect': 1,
541 'interactive': 1,
542 'optimization_level': 2,
543 'write_bytecode': 0,
544 'verbose': 1,
545 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200546 'buffered_stdio': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200547 'user_site_directory': 0,
548 'faulthandler': 1,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200549
Victor Stinner56b29b62018-07-26 18:57:56 +0200550 '_check_hash_pycs_mode': 'always',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200551 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200552 }
553 self.check_config("init_from_config", config)
554
555 def test_init_env(self):
556 config = {
557 'use_hash_seed': 1,
558 'hash_seed': 42,
559 'allocator': 'malloc_debug',
560 'tracemalloc': 2,
561 'import_time': 1,
562 'malloc_stats': 1,
563 'utf8_mode': 1,
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200564 'filesystem_encoding': 'utf-8',
565 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200566 'inspect': 1,
567 'optimization_level': 2,
568 'pycache_prefix': 'env_pycache_prefix',
569 'write_bytecode': 0,
570 'verbose': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200571 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200572 'stdio_encoding': 'iso8859-1',
573 'stdio_errors': 'replace',
Victor Stinner56b29b62018-07-26 18:57:56 +0200574 'user_site_directory': 0,
575 'faulthandler': 1,
576 'dev_mode': 1,
577 }
578 self.check_config("init_env", config)
579
580 def test_init_dev_mode(self):
581 config = {
582 'dev_mode': 1,
583 'faulthandler': 1,
584 'allocator': 'debug',
585 }
586 self.check_config("init_dev_mode", config)
587
588 def test_init_isolated(self):
589 config = {
590 'isolated': 1,
591 'use_environment': 0,
592 'user_site_directory': 0,
593 }
594 self.check_config("init_isolated", config)
595
596
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000597if __name__ == "__main__":
598 unittest.main()