blob: 9155c40f405ed08599634ae5c9d094006f6beabc [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
6import os
7import re
8import subprocess
9import sys
10
11
Victor Stinner56b29b62018-07-26 18:57:56 +020012class EmbeddingTestsMixin:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100013 def setUp(self):
14 here = os.path.abspath(__file__)
15 basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
16 exename = "_testembed"
17 if sys.platform.startswith("win"):
18 ext = ("_d" if "_d" in sys.executable else "") + ".exe"
19 exename += ext
20 exepath = os.path.dirname(sys.executable)
21 else:
22 exepath = os.path.join(basepath, "Programs")
23 self.test_exe = exe = os.path.join(exepath, exename)
24 if not os.path.exists(exe):
25 self.skipTest("%r doesn't exist" % exe)
26 # This is needed otherwise we get a fatal error:
27 # "Py_Initialize: Unable to get the locale encoding
28 # LookupError: no codec search functions registered: can't find encoding"
29 self.oldcwd = os.getcwd()
30 os.chdir(basepath)
31
32 def tearDown(self):
33 os.chdir(self.oldcwd)
34
35 def run_embedded_interpreter(self, *args, env=None):
36 """Runs a test in the embedded interpreter"""
37 cmd = [self.test_exe]
38 cmd.extend(args)
39 if env is not None and sys.platform == 'win32':
40 # Windows requires at least the SYSTEMROOT environment variable to
41 # start Python.
42 env = env.copy()
43 env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
44
45 p = subprocess.Popen(cmd,
46 stdout=subprocess.PIPE,
47 stderr=subprocess.PIPE,
48 universal_newlines=True,
49 env=env)
50 (out, err) = p.communicate()
51 if p.returncode != 0 and support.verbose:
52 print(f"--- {cmd} failed ---")
53 print(f"stdout:\n{out}")
Nick Coghlanbc77eff2018-03-25 20:44:30 +100054 print(f"stderr:\n{err}")
Nick Coghlan39f0bb52017-11-28 08:11:51 +100055 print(f"------")
56
57 self.assertEqual(p.returncode, 0,
58 "bad returncode %d, stderr is %r" %
59 (p.returncode, err))
60 return out, err
61
62 def run_repeated_init_and_subinterpreters(self):
63 out, err = self.run_embedded_interpreter("repeated_init_and_subinterpreters")
64 self.assertEqual(err, "")
65
66 # The output from _testembed looks like this:
67 # --- Pass 0 ---
68 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
69 # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
70 # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
71 # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
72 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
73 # --- Pass 1 ---
74 # ...
75
76 interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
77 r"thread state <(0x[\dA-F]+)>: "
78 r"id\(modules\) = ([\d]+)$")
79 Interp = namedtuple("Interp", "id interp tstate modules")
80
81 numloops = 0
82 current_run = []
83 for line in out.splitlines():
84 if line == "--- Pass {} ---".format(numloops):
85 self.assertEqual(len(current_run), 0)
Nick Coghlanbc77eff2018-03-25 20:44:30 +100086 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100087 print(line)
88 numloops += 1
89 continue
90
91 self.assertLess(len(current_run), 5)
92 match = re.match(interp_pat, line)
93 if match is None:
94 self.assertRegex(line, interp_pat)
95
96 # Parse the line from the loop. The first line is the main
97 # interpreter and the 3 afterward are subinterpreters.
98 interp = Interp(*match.groups())
Nick Coghlanbc77eff2018-03-25 20:44:30 +100099 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000100 print(interp)
101 self.assertTrue(interp.interp)
102 self.assertTrue(interp.tstate)
103 self.assertTrue(interp.modules)
104 current_run.append(interp)
105
106 # The last line in the loop should be the same as the first.
107 if len(current_run) == 5:
108 main = current_run[0]
109 self.assertEqual(interp, main)
110 yield current_run
111 current_run = []
112
Victor Stinner56b29b62018-07-26 18:57:56 +0200113
114class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000115 def test_subinterps_main(self):
116 for run in self.run_repeated_init_and_subinterpreters():
117 main = run[0]
118
119 self.assertEqual(main.id, '0')
120
121 def test_subinterps_different_ids(self):
122 for run in self.run_repeated_init_and_subinterpreters():
123 main, *subs, _ = run
124
125 mainid = int(main.id)
126 for i, sub in enumerate(subs):
127 self.assertEqual(sub.id, str(mainid + i + 1))
128
129 def test_subinterps_distinct_state(self):
130 for run in self.run_repeated_init_and_subinterpreters():
131 main, *subs, _ = run
132
133 if '0x0' in main:
134 # XXX Fix on Windows (and other platforms): something
135 # is going on with the pointers in Programs/_testembed.c.
136 # interp.interp is 0x0 and interp.modules is the same
137 # between interpreters.
138 raise unittest.SkipTest('platform prints pointers as 0x0')
139
140 for sub in subs:
141 # A new subinterpreter may have the same
142 # PyInterpreterState pointer as a previous one if
143 # the earlier one has already been destroyed. So
144 # we compare with the main interpreter. The same
145 # applies to tstate.
146 self.assertNotEqual(sub.interp, main.interp)
147 self.assertNotEqual(sub.tstate, main.tstate)
148 self.assertNotEqual(sub.modules, main.modules)
149
150 def test_forced_io_encoding(self):
151 # Checks forced configuration of embedded interpreter IO streams
152 env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
153 out, err = self.run_embedded_interpreter("forced_io_encoding", env=env)
154 if support.verbose > 1:
155 print()
156 print(out)
157 print(err)
158 expected_stream_encoding = "utf-8"
159 expected_errors = "surrogateescape"
160 expected_output = '\n'.join([
161 "--- Use defaults ---",
162 "Expected encoding: default",
163 "Expected errors: default",
164 "stdin: {in_encoding}:{errors}",
165 "stdout: {out_encoding}:{errors}",
166 "stderr: {out_encoding}:backslashreplace",
167 "--- Set errors only ---",
168 "Expected encoding: default",
169 "Expected errors: ignore",
170 "stdin: {in_encoding}:ignore",
171 "stdout: {out_encoding}:ignore",
172 "stderr: {out_encoding}:backslashreplace",
173 "--- Set encoding only ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200174 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000175 "Expected errors: default",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200176 "stdin: iso8859-1:{errors}",
177 "stdout: iso8859-1:{errors}",
178 "stderr: iso8859-1:backslashreplace",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000179 "--- Set encoding and errors ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200180 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000181 "Expected errors: replace",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200182 "stdin: iso8859-1:replace",
183 "stdout: iso8859-1:replace",
184 "stderr: iso8859-1:backslashreplace"])
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000185 expected_output = expected_output.format(
186 in_encoding=expected_stream_encoding,
187 out_encoding=expected_stream_encoding,
188 errors=expected_errors)
189 # This is useful if we ever trip over odd platform behaviour
190 self.maxDiff = None
191 self.assertEqual(out.strip(), expected_output)
192
193 def test_pre_initialization_api(self):
194 """
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000195 Checks some key parts of the C-API that need to work before the runtine
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000196 is initialized (via Py_Initialize()).
197 """
198 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
199 out, err = self.run_embedded_interpreter("pre_initialization_api", env=env)
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000200 if sys.platform == "win32":
201 expected_path = self.test_exe
202 else:
203 expected_path = os.path.join(os.getcwd(), "spam")
204 expected_output = f"sys.executable: {expected_path}\n"
205 self.assertIn(expected_output, out)
206 self.assertEqual(err, '')
207
208 def test_pre_initialization_sys_options(self):
209 """
210 Checks that sys.warnoptions and sys._xoptions can be set before the
211 runtime is initialized (otherwise they won't be effective).
212 """
Pablo Galindo41148462018-04-27 13:23:13 +0100213 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000214 out, err = self.run_embedded_interpreter(
215 "pre_initialization_sys_options", env=env)
216 expected_output = (
217 "sys.warnoptions: ['once', 'module', 'default']\n"
218 "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
219 "warnings.filters[:3]: ['default', 'module', 'once']\n"
220 )
221 self.assertIn(expected_output, out)
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000222 self.assertEqual(err, '')
223
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100224 def test_bpo20891(self):
225 """
226 bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
227 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
228 call PyEval_InitThreads() for us in this case.
229 """
230 out, err = self.run_embedded_interpreter("bpo20891")
231 self.assertEqual(out, '')
232 self.assertEqual(err, '')
233
Victor Stinner209abf72018-06-22 19:14:51 +0200234 def test_initialize_twice(self):
235 """
236 bpo-33932: Calling Py_Initialize() twice should do nothing (and not
237 crash!).
238 """
239 out, err = self.run_embedded_interpreter("initialize_twice")
240 self.assertEqual(out, '')
241 self.assertEqual(err, '')
242
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200243 def test_initialize_pymain(self):
244 """
245 bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
246 """
247 out, err = self.run_embedded_interpreter("initialize_pymain")
248 self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
249 self.assertEqual(err, '')
250
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000251
Victor Stinner56b29b62018-07-26 18:57:56 +0200252class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
253 maxDiff = 4096
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200254 UTF8_MODE_ERRORS = ('surrogatepass' if sys.platform == 'win32'
255 else 'surrogateescape')
Victor Stinner56b29b62018-07-26 18:57:56 +0200256 DEFAULT_CONFIG = {
257 'install_signal_handlers': 1,
258 'use_environment': 1,
259 'use_hash_seed': 0,
260 'hash_seed': 0,
261 'allocator': '(null)',
262 'dev_mode': 0,
263 'faulthandler': 0,
264 'tracemalloc': 0,
265 'import_time': 0,
266 'show_ref_count': 0,
267 'show_alloc_count': 0,
268 'dump_refs': 0,
269 'malloc_stats': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200270
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200271 # None means that the value is get by get_locale_encoding()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200272 'filesystem_encoding': None,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200273 'filesystem_errors': None,
274
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200275 'utf8_mode': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200276 'coerce_c_locale': 0,
277 'coerce_c_locale_warn': 0,
278
279 'pycache_prefix': '(null)',
280 'program_name': './_testembed',
Victor Stinnerea68d832018-08-01 03:07:18 +0200281 'argc': 0,
282 'argv': '[]',
Victor Stinner56b29b62018-07-26 18:57:56 +0200283 'program': '(null)',
284
285 'isolated': 0,
286 'site_import': 1,
287 'bytes_warning': 0,
288 'inspect': 0,
289 'interactive': 0,
290 'optimization_level': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200291 'parser_debug': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200292 'write_bytecode': 1,
293 'verbose': 0,
294 'quiet': 0,
295 'user_site_directory': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200296 'buffered_stdio': 1,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200297
298 # None means that the value is get by get_stdio_encoding()
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200299 'stdio_encoding': None,
300 'stdio_errors': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200301
302 '_install_importlib': 1,
303 '_check_hash_pycs_mode': 'default',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200304 '_frozen': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200305 }
306
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200307 def get_stdio_encoding(self, env):
308 code = 'import sys; print(sys.stdout.encoding, sys.stdout.errors)'
309 args = (sys.executable, '-c', code)
310 proc = subprocess.run(args, env=env, text=True,
311 stdout=subprocess.PIPE,
312 stderr=subprocess.STDOUT)
313 if proc.returncode:
314 raise Exception(f"failed to get the stdio encoding: stdout={proc.stdout!r}")
315 out = proc.stdout.rstrip()
316 return out.split()
317
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200318 def get_filesystem_encoding(self, isolated, env):
319 code = ('import codecs, locale, sys; '
320 'print(sys.getfilesystemencoding(), '
321 'sys.getfilesystemencodeerrors())')
322 args = (sys.executable, '-c', code)
323 env = dict(env)
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200324 if not isolated:
325 env['PYTHONCOERCECLOCALE'] = '0'
326 env['PYTHONUTF8'] = '0'
327 proc = subprocess.run(args, text=True, env=env,
328 stdout=subprocess.PIPE,
329 stderr=subprocess.PIPE)
330 if proc.returncode:
331 raise Exception(f"failed to get the locale encoding: "
332 f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200333 out = proc.stdout.rstrip()
334 return out.split()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200335
Victor Stinner56b29b62018-07-26 18:57:56 +0200336 def check_config(self, testname, expected):
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200337 expected = dict(self.DEFAULT_CONFIG, **expected)
338
Victor Stinner56b29b62018-07-26 18:57:56 +0200339 env = dict(os.environ)
340 for key in list(env):
341 if key.startswith('PYTHON'):
342 del env[key]
343 # Disable C locale coercion and UTF-8 mode to not depend
344 # on the current locale
345 env['PYTHONCOERCECLOCALE'] = '0'
346 env['PYTHONUTF8'] = '0'
Victor Stinner56b29b62018-07-26 18:57:56 +0200347
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200348 if expected['stdio_encoding'] is None or expected['stdio_errors'] is None:
349 res = self.get_stdio_encoding(env)
350 if expected['stdio_encoding'] is None:
351 expected['stdio_encoding'] = res[0]
352 if expected['stdio_errors'] is None:
353 expected['stdio_errors'] = res[1]
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200354 if expected['filesystem_encoding'] is None or expected['filesystem_errors'] is None:
355 res = self.get_filesystem_encoding(expected['isolated'], env)
356 if expected['filesystem_encoding'] is None:
357 expected['filesystem_encoding'] = res[0]
358 if expected['filesystem_errors'] is None:
359 expected['filesystem_errors'] = res[1]
Victor Stinner56b29b62018-07-26 18:57:56 +0200360 for key, value in expected.items():
361 expected[key] = str(value)
362
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200363 out, err = self.run_embedded_interpreter(testname, env=env)
364 # Ignore err
365
Victor Stinner56b29b62018-07-26 18:57:56 +0200366 config = {}
367 for line in out.splitlines():
368 key, value = line.split(' = ', 1)
369 config[key] = value
370 self.assertEqual(config, expected)
371
372 def test_init_default_config(self):
373 self.check_config("init_default_config", {})
374
375 def test_init_global_config(self):
376 config = {
377 'program_name': './globalvar',
378 'site_import': 0,
379 'bytes_warning': 1,
380 'inspect': 1,
381 'interactive': 1,
382 'optimization_level': 2,
383 'write_bytecode': 0,
384 'verbose': 1,
385 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200386 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200387
Victor Stinner56b29b62018-07-26 18:57:56 +0200388 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200389 'stdio_encoding': 'utf-8',
390 'stdio_errors': 'surrogateescape',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200391 'filesystem_encoding': 'utf-8',
392 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200393 'user_site_directory': 0,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200394 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200395 }
396 self.check_config("init_global_config", config)
397
398 def test_init_from_config(self):
399 config = {
400 'install_signal_handlers': 0,
401 'use_hash_seed': 1,
402 'hash_seed': 123,
403 'allocator': 'malloc_debug',
404 'tracemalloc': 2,
405 'import_time': 1,
406 'show_ref_count': 1,
407 'show_alloc_count': 1,
408 'malloc_stats': 1,
409
410 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200411 'stdio_encoding': 'iso8859-1',
412 'stdio_errors': 'replace',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200413 'filesystem_encoding': 'utf-8',
414 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200415
416 'pycache_prefix': 'conf_pycache_prefix',
417 'program_name': './conf_program_name',
418 'program': 'conf_program',
419
420 'site_import': 0,
421 'bytes_warning': 1,
422 'inspect': 1,
423 'interactive': 1,
424 'optimization_level': 2,
425 'write_bytecode': 0,
426 'verbose': 1,
427 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200428 'buffered_stdio': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200429 'user_site_directory': 0,
430 'faulthandler': 1,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200431
Victor Stinner56b29b62018-07-26 18:57:56 +0200432 '_check_hash_pycs_mode': 'always',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200433 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200434 }
435 self.check_config("init_from_config", config)
436
437 def test_init_env(self):
438 config = {
439 'use_hash_seed': 1,
440 'hash_seed': 42,
441 'allocator': 'malloc_debug',
442 'tracemalloc': 2,
443 'import_time': 1,
444 'malloc_stats': 1,
445 'utf8_mode': 1,
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200446 'filesystem_encoding': 'utf-8',
447 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200448 'inspect': 1,
449 'optimization_level': 2,
450 'pycache_prefix': 'env_pycache_prefix',
451 'write_bytecode': 0,
452 'verbose': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200453 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200454 'stdio_encoding': 'iso8859-1',
455 'stdio_errors': 'replace',
Victor Stinner56b29b62018-07-26 18:57:56 +0200456 'user_site_directory': 0,
457 'faulthandler': 1,
458 'dev_mode': 1,
459 }
460 self.check_config("init_env", config)
461
462 def test_init_dev_mode(self):
463 config = {
464 'dev_mode': 1,
465 'faulthandler': 1,
466 'allocator': 'debug',
467 }
468 self.check_config("init_dev_mode", config)
469
470 def test_init_isolated(self):
471 config = {
472 'isolated': 1,
473 'use_environment': 0,
474 'user_site_directory': 0,
475 }
476 self.check_config("init_isolated", config)
477
478
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000479if __name__ == "__main__":
480 unittest.main()