blob: e531fd49d5a36baa649ba6fe7ceae52e227a6d4d [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 Stinner56b29b62018-07-26 18:57:56 +0200280
Michael Feltd2067312018-09-15 11:28:31 +0200281 'pycache_prefix': NULL_STR,
Victor Stinner56b29b62018-07-26 18:57:56 +0200282 'program_name': './_testembed',
Victor Stinnerea68d832018-08-01 03:07:18 +0200283 'argc': 0,
284 'argv': '[]',
Michael Feltd2067312018-09-15 11:28:31 +0200285 'program': NULL_STR,
Victor Stinner56b29b62018-07-26 18:57:56 +0200286
287 'isolated': 0,
288 'site_import': 1,
289 'bytes_warning': 0,
290 'inspect': 0,
291 'interactive': 0,
292 'optimization_level': 0,
Victor Stinner98512272018-08-01 03:07:00 +0200293 'parser_debug': 0,
Victor Stinner56b29b62018-07-26 18:57:56 +0200294 'write_bytecode': 1,
295 'verbose': 0,
296 'quiet': 0,
297 'user_site_directory': 1,
Victor Stinner98512272018-08-01 03:07:00 +0200298 'buffered_stdio': 1,
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200299
300 # None means that the value is get by get_stdio_encoding()
Victor Stinnerdfe0dc72018-08-29 11:47:29 +0200301 'stdio_encoding': None,
302 'stdio_errors': None,
Victor Stinner56b29b62018-07-26 18:57:56 +0200303
304 '_install_importlib': 1,
305 '_check_hash_pycs_mode': 'default',
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200306 '_frozen': 0,
Victor Stinner188ebfa2018-09-17 15:13:17 -0700307 '_coerce_c_locale': 0,
308 '_coerce_c_locale_warn': 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 Stinnerb2457ef2018-08-29 13:25:36 +0200327 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()