blob: 80233a54b0b02a13e96cf0866fe53e850c55f54b [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
Michael Feltd2067312018-09-15 11:28:31 +02007import platform
Nick Coghlan39f0bb52017-11-28 08:11:51 +10008import re
9import subprocess
10import sys
11
12
Michael Feltd2067312018-09-15 11:28:31 +020013# AIX libc prints an empty string as '' rather than the string '(null)'
14NULL_STR = '' if platform.system() == 'AIX' else '(null)'
15
Victor Stinner56b29b62018-07-26 18:57:56 +020016class EmbeddingTestsMixin:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100017 def setUp(self):
18 here = os.path.abspath(__file__)
19 basepath = os.path.dirname(os.path.dirname(os.path.dirname(here)))
20 exename = "_testembed"
21 if sys.platform.startswith("win"):
22 ext = ("_d" if "_d" in sys.executable else "") + ".exe"
23 exename += ext
24 exepath = os.path.dirname(sys.executable)
25 else:
26 exepath = os.path.join(basepath, "Programs")
27 self.test_exe = exe = os.path.join(exepath, exename)
28 if not os.path.exists(exe):
29 self.skipTest("%r doesn't exist" % exe)
30 # This is needed otherwise we get a fatal error:
31 # "Py_Initialize: Unable to get the locale encoding
32 # LookupError: no codec search functions registered: can't find encoding"
33 self.oldcwd = os.getcwd()
34 os.chdir(basepath)
35
36 def tearDown(self):
37 os.chdir(self.oldcwd)
38
39 def run_embedded_interpreter(self, *args, env=None):
40 """Runs a test in the embedded interpreter"""
41 cmd = [self.test_exe]
42 cmd.extend(args)
43 if env is not None and sys.platform == 'win32':
44 # Windows requires at least the SYSTEMROOT environment variable to
45 # start Python.
46 env = env.copy()
47 env['SYSTEMROOT'] = os.environ['SYSTEMROOT']
48
49 p = subprocess.Popen(cmd,
50 stdout=subprocess.PIPE,
51 stderr=subprocess.PIPE,
52 universal_newlines=True,
53 env=env)
54 (out, err) = p.communicate()
55 if p.returncode != 0 and support.verbose:
56 print(f"--- {cmd} failed ---")
57 print(f"stdout:\n{out}")
Nick Coghlanbc77eff2018-03-25 20:44:30 +100058 print(f"stderr:\n{err}")
Nick Coghlan39f0bb52017-11-28 08:11:51 +100059 print(f"------")
60
61 self.assertEqual(p.returncode, 0,
62 "bad returncode %d, stderr is %r" %
63 (p.returncode, err))
64 return out, err
65
66 def run_repeated_init_and_subinterpreters(self):
67 out, err = self.run_embedded_interpreter("repeated_init_and_subinterpreters")
68 self.assertEqual(err, "")
69
70 # The output from _testembed looks like this:
71 # --- Pass 0 ---
72 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
73 # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784
74 # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368
75 # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200
76 # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728
77 # --- Pass 1 ---
78 # ...
79
80 interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, "
81 r"thread state <(0x[\dA-F]+)>: "
82 r"id\(modules\) = ([\d]+)$")
83 Interp = namedtuple("Interp", "id interp tstate modules")
84
85 numloops = 0
86 current_run = []
87 for line in out.splitlines():
88 if line == "--- Pass {} ---".format(numloops):
89 self.assertEqual(len(current_run), 0)
Nick Coghlanbc77eff2018-03-25 20:44:30 +100090 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +100091 print(line)
92 numloops += 1
93 continue
94
95 self.assertLess(len(current_run), 5)
96 match = re.match(interp_pat, line)
97 if match is None:
98 self.assertRegex(line, interp_pat)
99
100 # Parse the line from the loop. The first line is the main
101 # interpreter and the 3 afterward are subinterpreters.
102 interp = Interp(*match.groups())
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000103 if support.verbose > 1:
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000104 print(interp)
105 self.assertTrue(interp.interp)
106 self.assertTrue(interp.tstate)
107 self.assertTrue(interp.modules)
108 current_run.append(interp)
109
110 # The last line in the loop should be the same as the first.
111 if len(current_run) == 5:
112 main = current_run[0]
113 self.assertEqual(interp, main)
114 yield current_run
115 current_run = []
116
Victor Stinner56b29b62018-07-26 18:57:56 +0200117
118class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000119 def test_subinterps_main(self):
120 for run in self.run_repeated_init_and_subinterpreters():
121 main = run[0]
122
123 self.assertEqual(main.id, '0')
124
125 def test_subinterps_different_ids(self):
126 for run in self.run_repeated_init_and_subinterpreters():
127 main, *subs, _ = run
128
129 mainid = int(main.id)
130 for i, sub in enumerate(subs):
131 self.assertEqual(sub.id, str(mainid + i + 1))
132
133 def test_subinterps_distinct_state(self):
134 for run in self.run_repeated_init_and_subinterpreters():
135 main, *subs, _ = run
136
137 if '0x0' in main:
138 # XXX Fix on Windows (and other platforms): something
139 # is going on with the pointers in Programs/_testembed.c.
140 # interp.interp is 0x0 and interp.modules is the same
141 # between interpreters.
142 raise unittest.SkipTest('platform prints pointers as 0x0')
143
144 for sub in subs:
145 # A new subinterpreter may have the same
146 # PyInterpreterState pointer as a previous one if
147 # the earlier one has already been destroyed. So
148 # we compare with the main interpreter. The same
149 # applies to tstate.
150 self.assertNotEqual(sub.interp, main.interp)
151 self.assertNotEqual(sub.tstate, main.tstate)
152 self.assertNotEqual(sub.modules, main.modules)
153
154 def test_forced_io_encoding(self):
155 # Checks forced configuration of embedded interpreter IO streams
156 env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape")
157 out, err = self.run_embedded_interpreter("forced_io_encoding", env=env)
158 if support.verbose > 1:
159 print()
160 print(out)
161 print(err)
162 expected_stream_encoding = "utf-8"
163 expected_errors = "surrogateescape"
164 expected_output = '\n'.join([
165 "--- Use defaults ---",
166 "Expected encoding: default",
167 "Expected errors: default",
168 "stdin: {in_encoding}:{errors}",
169 "stdout: {out_encoding}:{errors}",
170 "stderr: {out_encoding}:backslashreplace",
171 "--- Set errors only ---",
172 "Expected encoding: default",
173 "Expected errors: ignore",
174 "stdin: {in_encoding}:ignore",
175 "stdout: {out_encoding}:ignore",
176 "stderr: {out_encoding}:backslashreplace",
177 "--- Set encoding only ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200178 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000179 "Expected errors: default",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200180 "stdin: iso8859-1:{errors}",
181 "stdout: iso8859-1:{errors}",
182 "stderr: iso8859-1:backslashreplace",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000183 "--- Set encoding and errors ---",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200184 "Expected encoding: iso8859-1",
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000185 "Expected errors: replace",
Victor Stinner9e4994d2018-08-28 23:26:33 +0200186 "stdin: iso8859-1:replace",
187 "stdout: iso8859-1:replace",
188 "stderr: iso8859-1:backslashreplace"])
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000189 expected_output = expected_output.format(
190 in_encoding=expected_stream_encoding,
191 out_encoding=expected_stream_encoding,
192 errors=expected_errors)
193 # This is useful if we ever trip over odd platform behaviour
194 self.maxDiff = None
195 self.assertEqual(out.strip(), expected_output)
196
197 def test_pre_initialization_api(self):
198 """
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000199 Checks some key parts of the C-API that need to work before the runtine
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000200 is initialized (via Py_Initialize()).
201 """
202 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
203 out, err = self.run_embedded_interpreter("pre_initialization_api", env=env)
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000204 if sys.platform == "win32":
205 expected_path = self.test_exe
206 else:
207 expected_path = os.path.join(os.getcwd(), "spam")
208 expected_output = f"sys.executable: {expected_path}\n"
209 self.assertIn(expected_output, out)
210 self.assertEqual(err, '')
211
212 def test_pre_initialization_sys_options(self):
213 """
214 Checks that sys.warnoptions and sys._xoptions can be set before the
215 runtime is initialized (otherwise they won't be effective).
216 """
Pablo Galindo41148462018-04-27 13:23:13 +0100217 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000218 out, err = self.run_embedded_interpreter(
219 "pre_initialization_sys_options", env=env)
220 expected_output = (
221 "sys.warnoptions: ['once', 'module', 'default']\n"
222 "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n"
223 "warnings.filters[:3]: ['default', 'module', 'once']\n"
224 )
225 self.assertIn(expected_output, out)
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000226 self.assertEqual(err, '')
227
Victor Stinnerb4d1e1f2017-11-30 22:05:00 +0100228 def test_bpo20891(self):
229 """
230 bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
231 calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
232 call PyEval_InitThreads() for us in this case.
233 """
234 out, err = self.run_embedded_interpreter("bpo20891")
235 self.assertEqual(out, '')
236 self.assertEqual(err, '')
237
Victor Stinner209abf72018-06-22 19:14:51 +0200238 def test_initialize_twice(self):
239 """
240 bpo-33932: Calling Py_Initialize() twice should do nothing (and not
241 crash!).
242 """
243 out, err = self.run_embedded_interpreter("initialize_twice")
244 self.assertEqual(out, '')
245 self.assertEqual(err, '')
246
Victor Stinnerfb47bca2018-07-20 17:34:23 +0200247 def test_initialize_pymain(self):
248 """
249 bpo-34008: Calling Py_Main() after Py_Initialize() must not fail.
250 """
251 out, err = self.run_embedded_interpreter("initialize_pymain")
252 self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']")
253 self.assertEqual(err, '')
254
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000255
Victor Stinner56b29b62018-07-26 18:57:56 +0200256class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
257 maxDiff = 4096
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200258 UTF8_MODE_ERRORS = ('surrogatepass' if sys.platform == 'win32'
259 else 'surrogateescape')
Victor Stinner56b29b62018-07-26 18:57:56 +0200260 DEFAULT_CONFIG = {
261 'install_signal_handlers': 1,
262 'use_environment': 1,
263 'use_hash_seed': 0,
264 'hash_seed': 0,
Michael Feltd2067312018-09-15 11:28:31 +0200265 'allocator': NULL_STR,
Victor Stinner56b29b62018-07-26 18:57:56 +0200266 'dev_mode': 0,
267 'faulthandler': 0,
268 'tracemalloc': 0,
269 'import_time': 0,
270 'show_ref_count': 0,
271 'show_alloc_count': 0,
272 'dump_refs': 0,
273 'malloc_stats': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200274
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200275 # None means that the value is get by get_locale_encoding()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200276 'filesystem_encoding': None,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200277 'filesystem_errors': None,
278
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200279 'utf8_mode': 0,
Victor Stinner06e76082018-09-19 14:56:36 -0700280 'coerce_c_locale': 0,
281 'coerce_c_locale_warn': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200282
Michael Feltd2067312018-09-15 11:28:31 +0200283 'pycache_prefix': NULL_STR,
Victor Stinner56b29b62018-07-26 18:57:56 +0200284 'program_name': './_testembed',
Victor Stinnerea68d832018-08-01 03:07:18 +0200285 'argc': 0,
286 'argv': '[]',
Michael Feltd2067312018-09-15 11:28:31 +0200287 'program': NULL_STR,
Victor Stinner56b29b62018-07-26 18:57:56 +0200288
289 'isolated': 0,
290 'site_import': 1,
291 'bytes_warning': 0,
292 'inspect': 0,
293 'interactive': 0,
294 'optimization_level': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200295 'parser_debug': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200296 'write_bytecode': 1,
297 'verbose': 0,
298 'quiet': 0,
299 'user_site_directory': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200300 'buffered_stdio': 1,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200301
302 # None means that the value is get by get_stdio_encoding()
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200303 'stdio_encoding': None,
304 'stdio_errors': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200305
306 '_install_importlib': 1,
307 '_check_hash_pycs_mode': 'default',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200308 '_frozen': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200309 }
310
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200311 def get_stdio_encoding(self, env):
312 code = 'import sys; print(sys.stdout.encoding, sys.stdout.errors)'
313 args = (sys.executable, '-c', code)
314 proc = subprocess.run(args, env=env, text=True,
315 stdout=subprocess.PIPE,
316 stderr=subprocess.STDOUT)
317 if proc.returncode:
318 raise Exception(f"failed to get the stdio encoding: stdout={proc.stdout!r}")
319 out = proc.stdout.rstrip()
320 return out.split()
321
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200322 def get_filesystem_encoding(self, isolated, env):
323 code = ('import codecs, locale, sys; '
324 'print(sys.getfilesystemencoding(), '
325 'sys.getfilesystemencodeerrors())')
326 args = (sys.executable, '-c', code)
Victor Stinner06e76082018-09-19 14:56:36 -0700327 env = dict(env)
328 if not isolated:
329 env['PYTHONCOERCECLOCALE'] = '0'
330 env['PYTHONUTF8'] = '0'
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200331 proc = subprocess.run(args, text=True, env=env,
332 stdout=subprocess.PIPE,
333 stderr=subprocess.PIPE)
334 if proc.returncode:
335 raise Exception(f"failed to get the locale encoding: "
336 f"stdout={proc.stdout!r} stderr={proc.stderr!r}")
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200337 out = proc.stdout.rstrip()
338 return out.split()
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200339
Victor Stinner56b29b62018-07-26 18:57:56 +0200340 def check_config(self, testname, expected):
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200341 expected = dict(self.DEFAULT_CONFIG, **expected)
342
Victor Stinner56b29b62018-07-26 18:57:56 +0200343 env = dict(os.environ)
344 for key in list(env):
345 if key.startswith('PYTHON'):
346 del env[key]
347 # Disable C locale coercion and UTF-8 mode to not depend
348 # on the current locale
349 env['PYTHONCOERCECLOCALE'] = '0'
350 env['PYTHONUTF8'] = '0'
Victor Stinner56b29b62018-07-26 18:57:56 +0200351
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200352 if expected['stdio_encoding'] is None or expected['stdio_errors'] is None:
353 res = self.get_stdio_encoding(env)
354 if expected['stdio_encoding'] is None:
355 expected['stdio_encoding'] = res[0]
356 if expected['stdio_errors'] is None:
357 expected['stdio_errors'] = res[1]
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200358 if expected['filesystem_encoding'] is None or expected['filesystem_errors'] is None:
359 res = self.get_filesystem_encoding(expected['isolated'], env)
360 if expected['filesystem_encoding'] is None:
361 expected['filesystem_encoding'] = res[0]
362 if expected['filesystem_errors'] is None:
363 expected['filesystem_errors'] = res[1]
Victor Stinner56b29b62018-07-26 18:57:56 +0200364 for key, value in expected.items():
365 expected[key] = str(value)
366
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200367 out, err = self.run_embedded_interpreter(testname, env=env)
368 # Ignore err
369
Victor Stinner56b29b62018-07-26 18:57:56 +0200370 config = {}
371 for line in out.splitlines():
372 key, value = line.split(' = ', 1)
373 config[key] = value
374 self.assertEqual(config, expected)
375
376 def test_init_default_config(self):
377 self.check_config("init_default_config", {})
378
379 def test_init_global_config(self):
380 config = {
381 'program_name': './globalvar',
382 'site_import': 0,
383 'bytes_warning': 1,
384 'inspect': 1,
385 'interactive': 1,
386 'optimization_level': 2,
387 'write_bytecode': 0,
388 'verbose': 1,
389 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200390 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200391
Victor Stinner56b29b62018-07-26 18:57:56 +0200392 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200393 'stdio_encoding': 'utf-8',
394 'stdio_errors': 'surrogateescape',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200395 'filesystem_encoding': 'utf-8',
396 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200397 'user_site_directory': 0,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200398 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200399 }
400 self.check_config("init_global_config", config)
401
402 def test_init_from_config(self):
403 config = {
404 'install_signal_handlers': 0,
405 'use_hash_seed': 1,
406 'hash_seed': 123,
407 'allocator': 'malloc_debug',
408 'tracemalloc': 2,
409 'import_time': 1,
410 'show_ref_count': 1,
411 'show_alloc_count': 1,
412 'malloc_stats': 1,
413
414 'utf8_mode': 1,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200415 'stdio_encoding': 'iso8859-1',
416 'stdio_errors': 'replace',
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200417 'filesystem_encoding': 'utf-8',
418 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200419
420 'pycache_prefix': 'conf_pycache_prefix',
421 'program_name': './conf_program_name',
422 'program': 'conf_program',
423
424 'site_import': 0,
425 'bytes_warning': 1,
426 'inspect': 1,
427 'interactive': 1,
428 'optimization_level': 2,
429 'write_bytecode': 0,
430 'verbose': 1,
431 'quiet': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200432 'buffered_stdio': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200433 'user_site_directory': 0,
434 'faulthandler': 1,
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200435
Victor Stinner56b29b62018-07-26 18:57:56 +0200436 '_check_hash_pycs_mode': 'always',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200437 '_frozen': 1,
Victor Stinner56b29b62018-07-26 18:57:56 +0200438 }
439 self.check_config("init_from_config", config)
440
441 def test_init_env(self):
442 config = {
443 'use_hash_seed': 1,
444 'hash_seed': 42,
445 'allocator': 'malloc_debug',
446 'tracemalloc': 2,
447 'import_time': 1,
448 'malloc_stats': 1,
449 'utf8_mode': 1,
Victor Stinnerb2457ef2018-08-29 13:25:36 +0200450 'filesystem_encoding': 'utf-8',
451 'filesystem_errors': self.UTF8_MODE_ERRORS,
Victor Stinner56b29b62018-07-26 18:57:56 +0200452 'inspect': 1,
453 'optimization_level': 2,
454 'pycache_prefix': 'env_pycache_prefix',
455 'write_bytecode': 0,
456 'verbose': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200457 'buffered_stdio': 0,
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200458 'stdio_encoding': 'iso8859-1',
459 'stdio_errors': 'replace',
Victor Stinner56b29b62018-07-26 18:57:56 +0200460 'user_site_directory': 0,
461 'faulthandler': 1,
462 'dev_mode': 1,
463 }
464 self.check_config("init_env", config)
465
466 def test_init_dev_mode(self):
467 config = {
468 'dev_mode': 1,
469 'faulthandler': 1,
470 'allocator': 'debug',
471 }
472 self.check_config("init_dev_mode", config)
473
474 def test_init_isolated(self):
475 config = {
476 'isolated': 1,
477 'use_environment': 0,
478 'user_site_directory': 0,
479 }
480 self.check_config("init_isolated", config)
481
482
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000483if __name__ == "__main__":
484 unittest.main()