blob: 29274580339f0a01039358a146562a9dc9238633 [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 Stinner0c90d6f2018-08-05 12:31:59 +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}")
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -070054 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)
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -070086 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())
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -070099 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 Stinner0c90d6f2018-08-05 12:31:59 +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 ---",
174 "Expected encoding: latin-1",
175 "Expected errors: default",
176 "stdin: latin-1:{errors}",
177 "stdout: latin-1:{errors}",
178 "stderr: latin-1:backslashreplace",
179 "--- Set encoding and errors ---",
180 "Expected encoding: latin-1",
181 "Expected errors: replace",
182 "stdin: latin-1:replace",
183 "stdout: latin-1:replace",
184 "stderr: latin-1:backslashreplace"])
185 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 """
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700195 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)
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700200 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 """
Miss Islington (bot)dd3ede72018-04-27 05:41:25 -0700213 env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path))
Miss Islington (bot)c6d94c32018-03-25 04:27:57 -0700214 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
Miss Islington (bot)3747dd12018-06-22 10:33:48 -0700234 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
Miss Islington (bot)03ec4df2018-07-20 17:16:22 -0700243 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 Stinner0c90d6f2018-08-05 12:31:59 +0200252class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
253 maxDiff = 4096
254 DEFAULT_CONFIG = {
255 'install_signal_handlers': 1,
256 'Py_IgnoreEnvironmentFlag': 0,
257 'use_hash_seed': 0,
258 'hash_seed': 0,
259 'allocator': '(null)',
260 'dev_mode': 0,
261 'faulthandler': 0,
262 'tracemalloc': 0,
263 'import_time': 0,
264 'show_ref_count': 0,
265 'show_alloc_count': 0,
266 'dump_refs': 0,
267 'malloc_stats': 0,
268 'utf8_mode': 0,
269
Victor Stinner95cc3ee2018-09-19 12:01:52 -0700270 'coerce_c_locale': 0,
271 'coerce_c_locale_warn': 0,
272
Victor Stinner0c90d6f2018-08-05 12:31:59 +0200273 'program_name': './_testembed',
274 'argc': 0,
275 'argv': '[]',
276 'program': '(null)',
277
278 'Py_IsolatedFlag': 0,
279 'Py_NoSiteFlag': 0,
280 'Py_BytesWarningFlag': 0,
281 'Py_InspectFlag': 0,
282 'Py_InteractiveFlag': 0,
283 'Py_OptimizeFlag': 0,
284 'Py_DebugFlag': 0,
285 'Py_DontWriteBytecodeFlag': 0,
286 'Py_VerboseFlag': 0,
287 'Py_QuietFlag': 0,
288 'Py_NoUserSiteDirectory': 0,
289 'Py_UnbufferedStdioFlag': 0,
290
291 '_disable_importlib': 0,
292 'Py_FrozenFlag': 0,
293 }
294
295 def check_config(self, testname, expected):
296 env = dict(os.environ)
297 for key in list(env):
298 if key.startswith('PYTHON'):
299 del env[key]
300 # Disable C locale coercion and UTF-8 mode to not depend
301 # on the current locale
302 env['PYTHONCOERCECLOCALE'] = '0'
303 env['PYTHONUTF8'] = '0'
304 out, err = self.run_embedded_interpreter(testname, env=env)
305 # Ignore err
306
307 expected = dict(self.DEFAULT_CONFIG, **expected)
308 for key, value in expected.items():
309 expected[key] = str(value)
310
311 config = {}
312 for line in out.splitlines():
313 key, value = line.split(' = ', 1)
314 config[key] = value
315 self.assertEqual(config, expected)
316
317 def test_init_default_config(self):
318 self.check_config("init_default_config", {})
319
320 def test_init_global_config(self):
321 config = {
322 'program_name': './globalvar',
323 'Py_NoSiteFlag': 1,
324 'Py_BytesWarningFlag': 1,
325 'Py_InspectFlag': 1,
326 'Py_InteractiveFlag': 1,
327 'Py_OptimizeFlag': 2,
328 'Py_DontWriteBytecodeFlag': 1,
329 'Py_VerboseFlag': 1,
330 'Py_QuietFlag': 1,
331 'Py_UnbufferedStdioFlag': 1,
332 'utf8_mode': 1,
333 'Py_NoUserSiteDirectory': 1,
334 'Py_FrozenFlag': 1,
335 }
336 self.check_config("init_global_config", config)
337
338 def test_init_from_config(self):
339 config = {
340 'install_signal_handlers': 0,
341 'use_hash_seed': 1,
342 'hash_seed': 123,
343 'allocator': 'malloc_debug',
344 'tracemalloc': 2,
345 'import_time': 1,
346 'show_ref_count': 1,
347 'show_alloc_count': 1,
348 'malloc_stats': 1,
349
350 'utf8_mode': 1,
351
352 'program_name': './conf_program_name',
353 'program': 'conf_program',
354
355 'faulthandler': 1,
356 }
357 self.check_config("init_from_config", config)
358
359 def test_init_env(self):
360 config = {
361 'use_hash_seed': 1,
362 'hash_seed': 42,
363 'allocator': 'malloc_debug',
364 'tracemalloc': 2,
365 'import_time': 1,
366 'malloc_stats': 1,
367 'utf8_mode': 1,
368 'Py_InspectFlag': 1,
369 'Py_OptimizeFlag': 2,
370 'Py_DontWriteBytecodeFlag': 1,
371 'Py_VerboseFlag': 1,
372 'Py_UnbufferedStdioFlag': 1,
373 'Py_NoUserSiteDirectory': 1,
374 'faulthandler': 1,
375 'dev_mode': 1,
376 }
377 self.check_config("init_env", config)
378
379 def test_init_dev_mode(self):
380 config = {
381 'dev_mode': 1,
382 'faulthandler': 1,
383 'allocator': 'debug',
384 }
385 self.check_config("init_dev_mode", config)
386
387 def test_init_isolated(self):
388 config = {
389 'Py_IsolatedFlag': 1,
390 'Py_IgnoreEnvironmentFlag': 1,
391 'Py_NoUserSiteDirectory': 1,
392 }
393 self.check_config("init_isolated", config)
394
395
Nick Coghlan39f0bb52017-11-28 08:11:51 +1000396if __name__ == "__main__":
397 unittest.main()