Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 1 | # Run the tests in Programs/_testembed.c (tests for the CPython embedding APIs) |
| 2 | from test import support |
| 3 | import unittest |
| 4 | |
| 5 | from collections import namedtuple |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 6 | import json |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 7 | import os |
| 8 | import re |
| 9 | import subprocess |
| 10 | import sys |
| 11 | |
| 12 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 13 | MS_WINDOWS = (os.name == 'nt') |
| 14 | |
| 15 | |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 16 | class EmbeddingTestsMixin: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 17 | def setUp(self): |
| 18 | here = os.path.abspath(__file__) |
| 19 | basepath = os.path.dirname(os.path.dirname(os.path.dirname(here))) |
| 20 | exename = "_testembed" |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 21 | if MS_WINDOWS: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 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) |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 43 | if env is not None and MS_WINDOWS: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 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}") |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 58 | print(f"stderr:\n{err}") |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 59 | 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) |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 90 | if support.verbose > 1: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 91 | 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()) |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 103 | if support.verbose > 1: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 104 | 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 Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 117 | |
| 118 | class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase): |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 119 | 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 ---", |
| 178 | "Expected encoding: latin-1", |
| 179 | "Expected errors: default", |
| 180 | "stdin: latin-1:{errors}", |
| 181 | "stdout: latin-1:{errors}", |
| 182 | "stderr: latin-1:backslashreplace", |
| 183 | "--- Set encoding and errors ---", |
| 184 | "Expected encoding: latin-1", |
| 185 | "Expected errors: replace", |
| 186 | "stdin: latin-1:replace", |
| 187 | "stdout: latin-1:replace", |
| 188 | "stderr: latin-1:backslashreplace"]) |
| 189 | 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 | """ |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 199 | Checks some key parts of the C-API that need to work before the runtine |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 200 | 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) |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 204 | if MS_WINDOWS: |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 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 | """ |
Miss Islington (bot) | dd3ede7 | 2018-04-27 05:41:25 -0700 | [diff] [blame] | 217 | env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path)) |
Miss Islington (bot) | c6d94c3 | 2018-03-25 04:27:57 -0700 | [diff] [blame] | 218 | 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 Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 226 | self.assertEqual(err, '') |
| 227 | |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 228 | 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 | |
Miss Islington (bot) | 3747dd1 | 2018-06-22 10:33:48 -0700 | [diff] [blame] | 238 | 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 | |
Miss Islington (bot) | 03ec4df | 2018-07-20 17:16:22 -0700 | [diff] [blame] | 247 | 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 Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 255 | |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 256 | class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): |
| 257 | maxDiff = 4096 |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 258 | UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape') |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 259 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 260 | # core config |
| 261 | UNTESTED_CORE_CONFIG = ( |
| 262 | # FIXME: untested core configuration variables |
| 263 | 'dll_path', |
| 264 | 'executable', |
| 265 | 'module_search_paths', |
| 266 | ) |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 267 | DEFAULT_CORE_CONFIG = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 268 | 'install_signal_handlers': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 269 | 'ignore_environment': 0, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 270 | 'use_hash_seed': 0, |
| 271 | 'hash_seed': 0, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 272 | 'allocator': None, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 273 | 'dev_mode': 0, |
| 274 | 'faulthandler': 0, |
| 275 | 'tracemalloc': 0, |
| 276 | 'import_time': 0, |
| 277 | 'show_ref_count': 0, |
| 278 | 'show_alloc_count': 0, |
| 279 | 'dump_refs': 0, |
| 280 | 'malloc_stats': 0, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 281 | |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 282 | 'utf8_mode': 0, |
Victor Stinner | 95cc3ee | 2018-09-19 12:01:52 -0700 | [diff] [blame] | 283 | 'coerce_c_locale': 0, |
| 284 | 'coerce_c_locale_warn': 0, |
| 285 | |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 286 | 'program_name': './_testembed', |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 287 | 'argv': [], |
| 288 | 'program': None, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 289 | |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 290 | 'xoptions': [], |
| 291 | 'warnoptions': [], |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 292 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 293 | 'module_search_path_env': None, |
| 294 | 'home': None, |
| 295 | 'prefix': sys.prefix, |
| 296 | 'base_prefix': sys.base_prefix, |
| 297 | 'exec_prefix': sys.exec_prefix, |
| 298 | 'base_exec_prefix': sys.base_exec_prefix, |
| 299 | |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 300 | '_disable_importlib': 0, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 303 | # main config |
| 304 | UNTESTED_MAIN_CONFIG = ( |
| 305 | # FIXME: untested main configuration variables |
| 306 | 'module_search_path', |
| 307 | ) |
| 308 | COPY_MAIN_CONFIG = ( |
| 309 | # Copy core config to main config for expected values |
| 310 | 'argv', |
| 311 | 'base_exec_prefix', |
| 312 | 'base_prefix', |
| 313 | 'exec_prefix', |
| 314 | 'executable', |
| 315 | 'install_signal_handlers', |
| 316 | 'prefix', |
| 317 | 'warnoptions', |
| 318 | # xoptions is created from core_config in check_main_config() |
| 319 | ) |
| 320 | |
| 321 | # global config |
| 322 | UNTESTED_GLOBAL_CONFIG = ( |
| 323 | # Py_HasFileSystemDefaultEncoding value depends on the LC_CTYPE locale |
| 324 | # and the platform. It is complex to test it, and it's value doesn't |
| 325 | # really matter. |
| 326 | 'Py_HasFileSystemDefaultEncoding', |
| 327 | ) |
| 328 | DEFAULT_GLOBAL_CONFIG = { |
| 329 | 'Py_BytesWarningFlag': 0, |
| 330 | 'Py_DebugFlag': 0, |
| 331 | 'Py_DontWriteBytecodeFlag': 0, |
| 332 | # None means that the value is get by get_filesystem_encoding() |
| 333 | 'Py_FileSystemDefaultEncodeErrors': None, |
| 334 | 'Py_FileSystemDefaultEncoding': None, |
| 335 | 'Py_FrozenFlag': 0, |
| 336 | 'Py_HashRandomizationFlag': 1, |
| 337 | 'Py_InspectFlag': 0, |
| 338 | 'Py_InteractiveFlag': 0, |
| 339 | 'Py_IsolatedFlag': 0, |
| 340 | 'Py_NoSiteFlag': 0, |
| 341 | 'Py_NoUserSiteDirectory': 0, |
| 342 | 'Py_OptimizeFlag': 0, |
| 343 | 'Py_QuietFlag': 0, |
| 344 | 'Py_UnbufferedStdioFlag': 0, |
| 345 | 'Py_VerboseFlag': 0, |
| 346 | } |
| 347 | if MS_WINDOWS: |
| 348 | DEFAULT_GLOBAL_CONFIG.update({ |
| 349 | 'Py_LegacyWindowsFSEncodingFlag': 0, |
| 350 | 'Py_LegacyWindowsStdioFlag': 0, |
| 351 | }) |
| 352 | COPY_GLOBAL_CONFIG = [ |
| 353 | # Copy core config to global config for expected values |
| 354 | # True means that the core config value is inverted (0 => 1 and 1 => 0) |
| 355 | ('Py_IgnoreEnvironmentFlag', 'ignore_environment'), |
| 356 | ('Py_UTF8Mode', 'utf8_mode'), |
| 357 | ] |
| 358 | |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 359 | def get_filesystem_encoding(self, isolated, env): |
| 360 | code = ('import codecs, locale, sys; ' |
| 361 | 'print(sys.getfilesystemencoding(), ' |
| 362 | 'sys.getfilesystemencodeerrors())') |
| 363 | args = (sys.executable, '-c', code) |
| 364 | env = dict(env) |
| 365 | if not isolated: |
| 366 | env['PYTHONCOERCECLOCALE'] = '0' |
| 367 | env['PYTHONUTF8'] = '0' |
| 368 | proc = subprocess.run(args, text=True, env=env, |
| 369 | stdout=subprocess.PIPE, |
| 370 | stderr=subprocess.PIPE) |
| 371 | if proc.returncode: |
| 372 | raise Exception(f"failed to get the locale encoding: " |
| 373 | f"stdout={proc.stdout!r} stderr={proc.stderr!r}") |
| 374 | out = proc.stdout.rstrip() |
| 375 | return out.split() |
| 376 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 377 | def main_xoptions(self, xoptions_list): |
| 378 | xoptions = {} |
| 379 | for opt in xoptions_list: |
| 380 | if '=' in opt: |
| 381 | key, value = opt.split('=', 1) |
| 382 | xoptions[key] = value |
| 383 | else: |
| 384 | xoptions[opt] = True |
| 385 | return xoptions |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 386 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 387 | def check_main_config(self, config): |
| 388 | core_config = config['core_config'] |
| 389 | main_config = config['main_config'] |
| 390 | |
| 391 | # main config |
| 392 | for key in self.UNTESTED_MAIN_CONFIG: |
| 393 | del main_config[key] |
| 394 | |
| 395 | expected_main = {} |
| 396 | for key in self.COPY_MAIN_CONFIG: |
| 397 | expected_main[key] = core_config[key] |
| 398 | expected_main['xoptions'] = self.main_xoptions(core_config['xoptions']) |
| 399 | self.assertEqual(main_config, expected_main) |
| 400 | |
| 401 | def check_core_config(self, config, expected): |
| 402 | expected = dict(self.DEFAULT_CORE_CONFIG, **expected) |
| 403 | core_config = dict(config['core_config']) |
| 404 | for key in self.UNTESTED_CORE_CONFIG: |
| 405 | core_config.pop(key, None) |
| 406 | self.assertEqual(core_config, expected) |
| 407 | |
| 408 | def check_global_config(self, config, expected, env): |
| 409 | expected = dict(self.DEFAULT_GLOBAL_CONFIG, **expected) |
| 410 | |
| 411 | if expected['Py_FileSystemDefaultEncoding'] is None or expected['Py_FileSystemDefaultEncodeErrors'] is None: |
| 412 | res = self.get_filesystem_encoding(expected['Py_IsolatedFlag'], env) |
| 413 | if expected['Py_FileSystemDefaultEncoding'] is None: |
| 414 | expected['Py_FileSystemDefaultEncoding'] = res[0] |
| 415 | if expected['Py_FileSystemDefaultEncodeErrors'] is None: |
| 416 | expected['Py_FileSystemDefaultEncodeErrors'] = res[1] |
| 417 | |
| 418 | core_config = config['core_config'] |
| 419 | |
| 420 | for item in self.COPY_GLOBAL_CONFIG: |
| 421 | if len(item) == 3: |
| 422 | global_key, core_key, opposite = item |
| 423 | expected[global_key] = 0 if core_config[core_key] else 1 |
| 424 | else: |
| 425 | global_key, core_key = item |
| 426 | expected[global_key] = core_config[core_key] |
| 427 | |
| 428 | global_config = dict(config['global_config']) |
| 429 | for key in self.UNTESTED_GLOBAL_CONFIG: |
| 430 | del global_config[key] |
| 431 | self.assertEqual(global_config, expected) |
| 432 | |
| 433 | def check_config(self, testname, expected_core, expected_global): |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 434 | env = dict(os.environ) |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 435 | # Remove PYTHON* environment variables to get deterministic environment |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 436 | for key in list(env): |
| 437 | if key.startswith('PYTHON'): |
| 438 | del env[key] |
| 439 | # Disable C locale coercion and UTF-8 mode to not depend |
| 440 | # on the current locale |
| 441 | env['PYTHONCOERCECLOCALE'] = '0' |
| 442 | env['PYTHONUTF8'] = '0' |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 443 | |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 444 | out, err = self.run_embedded_interpreter(testname, env=env) |
| 445 | # Ignore err |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 446 | config = json.loads(out) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 447 | |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 448 | self.check_core_config(config, expected_core) |
| 449 | self.check_main_config(config) |
| 450 | self.check_global_config(config, expected_global, env) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 451 | |
| 452 | def test_init_default_config(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 453 | self.check_config("init_default_config", {}, {}) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 454 | |
| 455 | def test_init_global_config(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 456 | core_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 457 | 'program_name': './globalvar', |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 458 | 'utf8_mode': 1, |
| 459 | } |
| 460 | global_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 461 | 'Py_BytesWarningFlag': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 462 | 'Py_DontWriteBytecodeFlag': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 463 | 'Py_FileSystemDefaultEncodeErrors': self.UTF8_MODE_ERRORS, |
| 464 | 'Py_FileSystemDefaultEncoding': 'utf-8', |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 465 | 'Py_InspectFlag': 1, |
| 466 | 'Py_InteractiveFlag': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 467 | 'Py_NoSiteFlag': 1, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 468 | 'Py_NoUserSiteDirectory': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 469 | 'Py_OptimizeFlag': 2, |
| 470 | 'Py_QuietFlag': 1, |
| 471 | 'Py_VerboseFlag': 1, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 472 | 'Py_FrozenFlag': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 473 | 'Py_UnbufferedStdioFlag': 1, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 474 | } |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 475 | self.check_config("init_global_config", core_config, global_config) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 476 | |
| 477 | def test_init_from_config(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 478 | core_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 479 | 'install_signal_handlers': 0, |
| 480 | 'use_hash_seed': 1, |
| 481 | 'hash_seed': 123, |
| 482 | 'allocator': 'malloc_debug', |
| 483 | 'tracemalloc': 2, |
| 484 | 'import_time': 1, |
| 485 | 'show_ref_count': 1, |
| 486 | 'show_alloc_count': 1, |
| 487 | 'malloc_stats': 1, |
| 488 | |
| 489 | 'utf8_mode': 1, |
| 490 | |
| 491 | 'program_name': './conf_program_name', |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 492 | 'argv': ['-c', 'pass'], |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 493 | 'program': 'conf_program', |
Victor Stinner | 9ee1d42 | 2018-11-14 18:58:01 +0100 | [diff] [blame^] | 494 | 'xoptions': ['core_xoption1=3', 'core_xoption2=', 'core_xoption3'], |
| 495 | 'warnoptions': ['default', 'error::ResourceWarning'], |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 496 | |
| 497 | 'faulthandler': 1, |
| 498 | } |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 499 | global_config = { |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 500 | 'Py_NoUserSiteDirectory': 0, |
| 501 | } |
| 502 | self.check_config("init_from_config", core_config, global_config) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 503 | |
| 504 | def test_init_env(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 505 | core_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 506 | 'use_hash_seed': 1, |
| 507 | 'hash_seed': 42, |
| 508 | 'allocator': 'malloc_debug', |
| 509 | 'tracemalloc': 2, |
| 510 | 'import_time': 1, |
| 511 | 'malloc_stats': 1, |
| 512 | 'utf8_mode': 1, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 513 | 'faulthandler': 1, |
| 514 | 'dev_mode': 1, |
| 515 | } |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 516 | global_config = { |
| 517 | 'Py_DontWriteBytecodeFlag': 1, |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 518 | 'Py_InspectFlag': 1, |
| 519 | 'Py_NoUserSiteDirectory': 1, |
| 520 | 'Py_OptimizeFlag': 2, |
| 521 | 'Py_UnbufferedStdioFlag': 1, |
| 522 | 'Py_VerboseFlag': 1, |
| 523 | 'Py_FileSystemDefaultEncoding': 'utf-8', |
| 524 | 'Py_FileSystemDefaultEncodeErrors': self.UTF8_MODE_ERRORS, |
| 525 | } |
| 526 | self.check_config("init_env", core_config, global_config) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 527 | |
| 528 | def test_init_dev_mode(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 529 | core_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 530 | 'dev_mode': 1, |
| 531 | 'faulthandler': 1, |
| 532 | 'allocator': 'debug', |
| 533 | } |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 534 | self.check_config("init_dev_mode", core_config, {}) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 535 | |
| 536 | def test_init_isolated(self): |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 537 | core_config = { |
| 538 | 'ignore_environment': 1, |
| 539 | } |
| 540 | global_config = { |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 541 | 'Py_IsolatedFlag': 1, |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 542 | 'Py_NoUserSiteDirectory': 1, |
| 543 | } |
Victor Stinner | 35c28d5 | 2018-11-14 02:01:52 +0100 | [diff] [blame] | 544 | self.check_config("init_isolated", core_config, global_config) |
Victor Stinner | 0c90d6f | 2018-08-05 12:31:59 +0200 | [diff] [blame] | 545 | |
| 546 | |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 547 | if __name__ == "__main__": |
| 548 | unittest.main() |