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 | 7ddd56f | 2018-11-14 00:24:28 +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 |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 11 | import textwrap |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 12 | |
| 13 | |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 14 | MS_WINDOWS = (os.name == 'nt') |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 15 | |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 16 | PYMEM_ALLOCATOR_NOT_SET = 0 |
| 17 | PYMEM_ALLOCATOR_DEBUG = 2 |
| 18 | PYMEM_ALLOCATOR_MALLOC = 3 |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 19 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 20 | # _PyCoreConfig_InitCompatConfig() |
| 21 | API_COMPAT = 1 |
| 22 | # _PyCoreConfig_InitPythonConfig() |
| 23 | API_PYTHON = 2 |
| 24 | # _PyCoreConfig_InitIsolatedConfig() |
| 25 | API_ISOLATED = 3 |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 26 | |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 27 | |
Miss Islington (bot) | 69610f8 | 2019-06-17 15:31:43 -0700 | [diff] [blame] | 28 | def remove_python_envvars(): |
| 29 | env = dict(os.environ) |
| 30 | # Remove PYTHON* environment variables to get deterministic environment |
| 31 | for key in list(env): |
| 32 | if key.startswith('PYTHON'): |
| 33 | del env[key] |
| 34 | return env |
| 35 | |
| 36 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 37 | class EmbeddingTestsMixin: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 38 | def setUp(self): |
| 39 | here = os.path.abspath(__file__) |
| 40 | basepath = os.path.dirname(os.path.dirname(os.path.dirname(here))) |
| 41 | exename = "_testembed" |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 42 | if MS_WINDOWS: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 43 | ext = ("_d" if "_d" in sys.executable else "") + ".exe" |
| 44 | exename += ext |
| 45 | exepath = os.path.dirname(sys.executable) |
| 46 | else: |
| 47 | exepath = os.path.join(basepath, "Programs") |
| 48 | self.test_exe = exe = os.path.join(exepath, exename) |
| 49 | if not os.path.exists(exe): |
| 50 | self.skipTest("%r doesn't exist" % exe) |
| 51 | # This is needed otherwise we get a fatal error: |
| 52 | # "Py_Initialize: Unable to get the locale encoding |
| 53 | # LookupError: no codec search functions registered: can't find encoding" |
| 54 | self.oldcwd = os.getcwd() |
| 55 | os.chdir(basepath) |
| 56 | |
| 57 | def tearDown(self): |
| 58 | os.chdir(self.oldcwd) |
| 59 | |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame^] | 60 | def run_embedded_interpreter(self, *args, env=None, |
| 61 | timeout=None, returncode=0, input=None): |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 62 | """Runs a test in the embedded interpreter""" |
| 63 | cmd = [self.test_exe] |
| 64 | cmd.extend(args) |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 65 | if env is not None and MS_WINDOWS: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 66 | # Windows requires at least the SYSTEMROOT environment variable to |
| 67 | # start Python. |
| 68 | env = env.copy() |
| 69 | env['SYSTEMROOT'] = os.environ['SYSTEMROOT'] |
| 70 | |
| 71 | p = subprocess.Popen(cmd, |
| 72 | stdout=subprocess.PIPE, |
| 73 | stderr=subprocess.PIPE, |
| 74 | universal_newlines=True, |
| 75 | env=env) |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 76 | try: |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame^] | 77 | (out, err) = p.communicate(input=input, timeout=timeout) |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 78 | except: |
| 79 | p.terminate() |
| 80 | p.wait() |
| 81 | raise |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame^] | 82 | if p.returncode != returncode and support.verbose: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 83 | print(f"--- {cmd} failed ---") |
| 84 | print(f"stdout:\n{out}") |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 85 | print(f"stderr:\n{err}") |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 86 | print(f"------") |
| 87 | |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame^] | 88 | self.assertEqual(p.returncode, returncode, |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 89 | "bad returncode %d, stderr is %r" % |
| 90 | (p.returncode, err)) |
| 91 | return out, err |
| 92 | |
| 93 | def run_repeated_init_and_subinterpreters(self): |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 94 | out, err = self.run_embedded_interpreter("test_repeated_init_and_subinterpreters") |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 95 | self.assertEqual(err, "") |
| 96 | |
| 97 | # The output from _testembed looks like this: |
| 98 | # --- Pass 0 --- |
| 99 | # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728 |
| 100 | # interp 1 <0x1d4f690>, thread state <0x1d35350>: id(modules) = 139650431165784 |
| 101 | # interp 2 <0x1d5a690>, thread state <0x1d99ed0>: id(modules) = 139650413140368 |
| 102 | # interp 3 <0x1d4f690>, thread state <0x1dc3340>: id(modules) = 139650412862200 |
| 103 | # interp 0 <0x1cf9330>, thread state <0x1cf9700>: id(modules) = 139650431942728 |
| 104 | # --- Pass 1 --- |
| 105 | # ... |
| 106 | |
| 107 | interp_pat = (r"^interp (\d+) <(0x[\dA-F]+)>, " |
| 108 | r"thread state <(0x[\dA-F]+)>: " |
| 109 | r"id\(modules\) = ([\d]+)$") |
| 110 | Interp = namedtuple("Interp", "id interp tstate modules") |
| 111 | |
| 112 | numloops = 0 |
| 113 | current_run = [] |
| 114 | for line in out.splitlines(): |
| 115 | if line == "--- Pass {} ---".format(numloops): |
| 116 | self.assertEqual(len(current_run), 0) |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 117 | if support.verbose > 1: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 118 | print(line) |
| 119 | numloops += 1 |
| 120 | continue |
| 121 | |
| 122 | self.assertLess(len(current_run), 5) |
| 123 | match = re.match(interp_pat, line) |
| 124 | if match is None: |
| 125 | self.assertRegex(line, interp_pat) |
| 126 | |
| 127 | # Parse the line from the loop. The first line is the main |
| 128 | # interpreter and the 3 afterward are subinterpreters. |
| 129 | interp = Interp(*match.groups()) |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 130 | if support.verbose > 1: |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 131 | print(interp) |
| 132 | self.assertTrue(interp.interp) |
| 133 | self.assertTrue(interp.tstate) |
| 134 | self.assertTrue(interp.modules) |
| 135 | current_run.append(interp) |
| 136 | |
| 137 | # The last line in the loop should be the same as the first. |
| 138 | if len(current_run) == 5: |
| 139 | main = current_run[0] |
| 140 | self.assertEqual(interp, main) |
| 141 | yield current_run |
| 142 | current_run = [] |
| 143 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 144 | |
| 145 | class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase): |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 146 | def test_subinterps_main(self): |
| 147 | for run in self.run_repeated_init_and_subinterpreters(): |
| 148 | main = run[0] |
| 149 | |
| 150 | self.assertEqual(main.id, '0') |
| 151 | |
| 152 | def test_subinterps_different_ids(self): |
| 153 | for run in self.run_repeated_init_and_subinterpreters(): |
| 154 | main, *subs, _ = run |
| 155 | |
| 156 | mainid = int(main.id) |
| 157 | for i, sub in enumerate(subs): |
| 158 | self.assertEqual(sub.id, str(mainid + i + 1)) |
| 159 | |
| 160 | def test_subinterps_distinct_state(self): |
| 161 | for run in self.run_repeated_init_and_subinterpreters(): |
| 162 | main, *subs, _ = run |
| 163 | |
| 164 | if '0x0' in main: |
| 165 | # XXX Fix on Windows (and other platforms): something |
| 166 | # is going on with the pointers in Programs/_testembed.c. |
| 167 | # interp.interp is 0x0 and interp.modules is the same |
| 168 | # between interpreters. |
| 169 | raise unittest.SkipTest('platform prints pointers as 0x0') |
| 170 | |
| 171 | for sub in subs: |
| 172 | # A new subinterpreter may have the same |
| 173 | # PyInterpreterState pointer as a previous one if |
| 174 | # the earlier one has already been destroyed. So |
| 175 | # we compare with the main interpreter. The same |
| 176 | # applies to tstate. |
| 177 | self.assertNotEqual(sub.interp, main.interp) |
| 178 | self.assertNotEqual(sub.tstate, main.tstate) |
| 179 | self.assertNotEqual(sub.modules, main.modules) |
| 180 | |
| 181 | def test_forced_io_encoding(self): |
| 182 | # Checks forced configuration of embedded interpreter IO streams |
| 183 | env = dict(os.environ, PYTHONIOENCODING="utf-8:surrogateescape") |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 184 | out, err = self.run_embedded_interpreter("test_forced_io_encoding", env=env) |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 185 | if support.verbose > 1: |
| 186 | print() |
| 187 | print(out) |
| 188 | print(err) |
| 189 | expected_stream_encoding = "utf-8" |
| 190 | expected_errors = "surrogateescape" |
| 191 | expected_output = '\n'.join([ |
| 192 | "--- Use defaults ---", |
| 193 | "Expected encoding: default", |
| 194 | "Expected errors: default", |
| 195 | "stdin: {in_encoding}:{errors}", |
| 196 | "stdout: {out_encoding}:{errors}", |
| 197 | "stderr: {out_encoding}:backslashreplace", |
| 198 | "--- Set errors only ---", |
| 199 | "Expected encoding: default", |
| 200 | "Expected errors: ignore", |
| 201 | "stdin: {in_encoding}:ignore", |
| 202 | "stdout: {out_encoding}:ignore", |
| 203 | "stderr: {out_encoding}:backslashreplace", |
| 204 | "--- Set encoding only ---", |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 205 | "Expected encoding: iso8859-1", |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 206 | "Expected errors: default", |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 207 | "stdin: iso8859-1:{errors}", |
| 208 | "stdout: iso8859-1:{errors}", |
| 209 | "stderr: iso8859-1:backslashreplace", |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 210 | "--- Set encoding and errors ---", |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 211 | "Expected encoding: iso8859-1", |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 212 | "Expected errors: replace", |
Victor Stinner | 9e4994d | 2018-08-28 23:26:33 +0200 | [diff] [blame] | 213 | "stdin: iso8859-1:replace", |
| 214 | "stdout: iso8859-1:replace", |
| 215 | "stderr: iso8859-1:backslashreplace"]) |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 216 | expected_output = expected_output.format( |
| 217 | in_encoding=expected_stream_encoding, |
| 218 | out_encoding=expected_stream_encoding, |
| 219 | errors=expected_errors) |
| 220 | # This is useful if we ever trip over odd platform behaviour |
| 221 | self.maxDiff = None |
| 222 | self.assertEqual(out.strip(), expected_output) |
| 223 | |
| 224 | def test_pre_initialization_api(self): |
| 225 | """ |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 226 | 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] | 227 | is initialized (via Py_Initialize()). |
| 228 | """ |
| 229 | env = dict(os.environ, PYTHONPATH=os.pathsep.join(sys.path)) |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 230 | out, err = self.run_embedded_interpreter("test_pre_initialization_api", env=env) |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 231 | if MS_WINDOWS: |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 232 | expected_path = self.test_exe |
| 233 | else: |
| 234 | expected_path = os.path.join(os.getcwd(), "spam") |
| 235 | expected_output = f"sys.executable: {expected_path}\n" |
| 236 | self.assertIn(expected_output, out) |
| 237 | self.assertEqual(err, '') |
| 238 | |
| 239 | def test_pre_initialization_sys_options(self): |
| 240 | """ |
| 241 | Checks that sys.warnoptions and sys._xoptions can be set before the |
| 242 | runtime is initialized (otherwise they won't be effective). |
| 243 | """ |
Miss Islington (bot) | 69610f8 | 2019-06-17 15:31:43 -0700 | [diff] [blame] | 244 | env = remove_python_envvars() |
| 245 | env['PYTHONPATH'] = os.pathsep.join(sys.path) |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 246 | out, err = self.run_embedded_interpreter( |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 247 | "test_pre_initialization_sys_options", env=env) |
Nick Coghlan | bc77eff | 2018-03-25 20:44:30 +1000 | [diff] [blame] | 248 | expected_output = ( |
| 249 | "sys.warnoptions: ['once', 'module', 'default']\n" |
| 250 | "sys._xoptions: {'not_an_option': '1', 'also_not_an_option': '2'}\n" |
| 251 | "warnings.filters[:3]: ['default', 'module', 'once']\n" |
| 252 | ) |
| 253 | self.assertIn(expected_output, out) |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 254 | self.assertEqual(err, '') |
| 255 | |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 256 | def test_bpo20891(self): |
| 257 | """ |
| 258 | bpo-20891: Calling PyGILState_Ensure in a non-Python thread before |
| 259 | calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must |
| 260 | call PyEval_InitThreads() for us in this case. |
| 261 | """ |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 262 | out, err = self.run_embedded_interpreter("test_bpo20891") |
Victor Stinner | b4d1e1f | 2017-11-30 22:05:00 +0100 | [diff] [blame] | 263 | self.assertEqual(out, '') |
| 264 | self.assertEqual(err, '') |
| 265 | |
Victor Stinner | 209abf7 | 2018-06-22 19:14:51 +0200 | [diff] [blame] | 266 | def test_initialize_twice(self): |
| 267 | """ |
| 268 | bpo-33932: Calling Py_Initialize() twice should do nothing (and not |
| 269 | crash!). |
| 270 | """ |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 271 | out, err = self.run_embedded_interpreter("test_initialize_twice") |
Victor Stinner | 209abf7 | 2018-06-22 19:14:51 +0200 | [diff] [blame] | 272 | self.assertEqual(out, '') |
| 273 | self.assertEqual(err, '') |
| 274 | |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 275 | def test_initialize_pymain(self): |
| 276 | """ |
| 277 | bpo-34008: Calling Py_Main() after Py_Initialize() must not fail. |
| 278 | """ |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 279 | out, err = self.run_embedded_interpreter("test_initialize_pymain") |
Victor Stinner | fb47bca | 2018-07-20 17:34:23 +0200 | [diff] [blame] | 280 | self.assertEqual(out.rstrip(), "Py_Main() after Py_Initialize: sys.argv=['-c', 'arg2']") |
| 281 | self.assertEqual(err, '') |
| 282 | |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 283 | def test_run_main(self): |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 284 | out, err = self.run_embedded_interpreter("test_run_main") |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 285 | self.assertEqual(out.rstrip(), "Py_RunMain(): sys.argv=['-c', 'arg2']") |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 286 | self.assertEqual(err, '') |
| 287 | |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 288 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 289 | class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): |
| 290 | maxDiff = 4096 |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 291 | UTF8_MODE_ERRORS = ('surrogatepass' if MS_WINDOWS else 'surrogateescape') |
| 292 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 293 | # Marker to read the default configuration: get_default_config() |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 294 | GET_DEFAULT_CONFIG = object() |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 295 | |
| 296 | # Marker to ignore a configuration parameter |
| 297 | IGNORE_CONFIG = object() |
| 298 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 299 | PRE_CONFIG_COMPAT = { |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 300 | '_config_init': API_COMPAT, |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 301 | 'allocator': PYMEM_ALLOCATOR_NOT_SET, |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 302 | 'parse_argv': 0, |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 303 | 'configure_locale': 1, |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 304 | 'coerce_c_locale': 0, |
| 305 | 'coerce_c_locale_warn': 0, |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 306 | 'utf8_mode': 0, |
| 307 | } |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 308 | if MS_WINDOWS: |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 309 | PRE_CONFIG_COMPAT.update({ |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 310 | 'legacy_windows_fs_encoding': 0, |
| 311 | }) |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 312 | PRE_CONFIG_PYTHON = dict(PRE_CONFIG_COMPAT, |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 313 | _config_init=API_PYTHON, |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 314 | parse_argv=1, |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 315 | coerce_c_locale=GET_DEFAULT_CONFIG, |
| 316 | utf8_mode=GET_DEFAULT_CONFIG, |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 317 | ) |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 318 | PRE_CONFIG_ISOLATED = dict(PRE_CONFIG_COMPAT, |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 319 | _config_init=API_ISOLATED, |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 320 | configure_locale=0, |
| 321 | isolated=1, |
| 322 | use_environment=0, |
| 323 | utf8_mode=0, |
| 324 | dev_mode=0, |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 325 | coerce_c_locale=0, |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 326 | ) |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 327 | |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 328 | COPY_PRE_CONFIG = [ |
| 329 | 'dev_mode', |
| 330 | 'isolated', |
| 331 | 'use_environment', |
| 332 | ] |
| 333 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 334 | CONFIG_COMPAT = { |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 335 | '_config_init': API_COMPAT, |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 336 | 'isolated': 0, |
| 337 | 'use_environment': 1, |
| 338 | 'dev_mode': 0, |
| 339 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 340 | 'install_signal_handlers': 1, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 341 | 'use_hash_seed': 0, |
| 342 | 'hash_seed': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 343 | 'faulthandler': 0, |
| 344 | 'tracemalloc': 0, |
| 345 | 'import_time': 0, |
| 346 | 'show_ref_count': 0, |
| 347 | 'show_alloc_count': 0, |
| 348 | 'dump_refs': 0, |
| 349 | 'malloc_stats': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 350 | |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 351 | 'filesystem_encoding': GET_DEFAULT_CONFIG, |
| 352 | 'filesystem_errors': GET_DEFAULT_CONFIG, |
Victor Stinner | c5989cd | 2018-08-29 19:32:47 +0200 | [diff] [blame] | 353 | |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 354 | 'pycache_prefix': None, |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 355 | 'program_name': GET_DEFAULT_CONFIG, |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 356 | 'parse_argv': 0, |
Victor Stinner | 6259976 | 2019-03-15 16:03:23 +0100 | [diff] [blame] | 357 | 'argv': [""], |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 358 | |
| 359 | 'xoptions': [], |
| 360 | 'warnoptions': [], |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 361 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 362 | 'pythonpath_env': None, |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 363 | 'home': None, |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 364 | 'executable': GET_DEFAULT_CONFIG, |
Steve Dower | 323e743 | 2019-06-29 14:28:59 -0700 | [diff] [blame] | 365 | 'base_executable': GET_DEFAULT_CONFIG, |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 366 | |
| 367 | 'prefix': GET_DEFAULT_CONFIG, |
| 368 | 'base_prefix': GET_DEFAULT_CONFIG, |
| 369 | 'exec_prefix': GET_DEFAULT_CONFIG, |
| 370 | 'base_exec_prefix': GET_DEFAULT_CONFIG, |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 371 | 'module_search_paths': GET_DEFAULT_CONFIG, |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 372 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 373 | 'site_import': 1, |
| 374 | 'bytes_warning': 0, |
| 375 | 'inspect': 0, |
| 376 | 'interactive': 0, |
| 377 | 'optimization_level': 0, |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 378 | 'parser_debug': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 379 | 'write_bytecode': 1, |
| 380 | 'verbose': 0, |
| 381 | 'quiet': 0, |
| 382 | 'user_site_directory': 1, |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 383 | 'configure_c_stdio': 0, |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 384 | 'buffered_stdio': 1, |
Victor Stinner | c5989cd | 2018-08-29 19:32:47 +0200 | [diff] [blame] | 385 | |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 386 | 'stdio_encoding': GET_DEFAULT_CONFIG, |
| 387 | 'stdio_errors': GET_DEFAULT_CONFIG, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 388 | |
Victor Stinner | 62be763 | 2019-03-01 13:10:14 +0100 | [diff] [blame] | 389 | 'skip_source_first_line': 0, |
| 390 | 'run_command': None, |
| 391 | 'run_module': None, |
| 392 | 'run_filename': None, |
| 393 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 394 | '_install_importlib': 1, |
Victor Stinner | cb9fbd3 | 2019-05-01 23:51:56 -0400 | [diff] [blame] | 395 | 'check_hash_pycs_mode': 'default', |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 396 | 'pathconfig_warnings': 1, |
| 397 | '_init_main': 1, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 398 | } |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 399 | if MS_WINDOWS: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 400 | CONFIG_COMPAT.update({ |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 401 | 'legacy_windows_stdio': 0, |
| 402 | }) |
| 403 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 404 | CONFIG_PYTHON = dict(CONFIG_COMPAT, |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 405 | _config_init=API_PYTHON, |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 406 | configure_c_stdio=1, |
| 407 | parse_argv=1, |
| 408 | ) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 409 | CONFIG_ISOLATED = dict(CONFIG_COMPAT, |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 410 | _config_init=API_ISOLATED, |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 411 | isolated=1, |
| 412 | use_environment=0, |
| 413 | user_site_directory=0, |
| 414 | dev_mode=0, |
| 415 | install_signal_handlers=0, |
| 416 | use_hash_seed=0, |
| 417 | faulthandler=0, |
| 418 | tracemalloc=0, |
| 419 | pathconfig_warnings=0, |
| 420 | ) |
| 421 | if MS_WINDOWS: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 422 | CONFIG_ISOLATED['legacy_windows_stdio'] = 0 |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 423 | |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 424 | # global config |
| 425 | DEFAULT_GLOBAL_CONFIG = { |
| 426 | 'Py_HasFileSystemDefaultEncoding': 0, |
| 427 | 'Py_HashRandomizationFlag': 1, |
| 428 | '_Py_HasFileSystemDefaultEncodeErrors': 0, |
| 429 | } |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 430 | COPY_GLOBAL_PRE_CONFIG = [ |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 431 | ('Py_UTF8Mode', 'utf8_mode'), |
| 432 | ] |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 433 | COPY_GLOBAL_CONFIG = [ |
| 434 | # Copy core config to global config for expected values |
| 435 | # True means that the core config value is inverted (0 => 1 and 1 => 0) |
| 436 | ('Py_BytesWarningFlag', 'bytes_warning'), |
| 437 | ('Py_DebugFlag', 'parser_debug'), |
| 438 | ('Py_DontWriteBytecodeFlag', 'write_bytecode', True), |
| 439 | ('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'), |
| 440 | ('Py_FileSystemDefaultEncoding', 'filesystem_encoding'), |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 441 | ('Py_FrozenFlag', 'pathconfig_warnings', True), |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 442 | ('Py_IgnoreEnvironmentFlag', 'use_environment', True), |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 443 | ('Py_InspectFlag', 'inspect'), |
| 444 | ('Py_InteractiveFlag', 'interactive'), |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 445 | ('Py_IsolatedFlag', 'isolated'), |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 446 | ('Py_NoSiteFlag', 'site_import', True), |
| 447 | ('Py_NoUserSiteDirectory', 'user_site_directory', True), |
| 448 | ('Py_OptimizeFlag', 'optimization_level'), |
| 449 | ('Py_QuietFlag', 'quiet'), |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 450 | ('Py_UnbufferedStdioFlag', 'buffered_stdio', True), |
| 451 | ('Py_VerboseFlag', 'verbose'), |
| 452 | ] |
| 453 | if MS_WINDOWS: |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 454 | COPY_GLOBAL_PRE_CONFIG.extend(( |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 455 | ('Py_LegacyWindowsFSEncodingFlag', 'legacy_windows_fs_encoding'), |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 456 | )) |
| 457 | COPY_GLOBAL_CONFIG.extend(( |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 458 | ('Py_LegacyWindowsStdioFlag', 'legacy_windows_stdio'), |
| 459 | )) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 460 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 461 | EXPECTED_CONFIG = None |
| 462 | |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 463 | def main_xoptions(self, xoptions_list): |
| 464 | xoptions = {} |
| 465 | for opt in xoptions_list: |
| 466 | if '=' in opt: |
| 467 | key, value = opt.split('=', 1) |
| 468 | xoptions[key] = value |
| 469 | else: |
| 470 | xoptions[opt] = True |
| 471 | return xoptions |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 472 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 473 | def _get_expected_config(self, env): |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 474 | code = textwrap.dedent(''' |
| 475 | import json |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 476 | import sys |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 477 | import _testinternalcapi |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 478 | |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 479 | configs = _testinternalcapi.get_configs() |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 480 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 481 | data = json.dumps(configs) |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 482 | data = data.encode('utf-8') |
| 483 | sys.stdout.buffer.write(data) |
| 484 | sys.stdout.buffer.flush() |
| 485 | ''') |
| 486 | |
| 487 | # Use -S to not import the site module: get the proper configuration |
| 488 | # when test_embed is run from a venv (bpo-35313) |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 489 | args = [sys.executable, '-S', '-c', code] |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 490 | proc = subprocess.run(args, env=env, |
| 491 | stdout=subprocess.PIPE, |
| 492 | stderr=subprocess.STDOUT) |
| 493 | if proc.returncode: |
| 494 | raise Exception(f"failed to get the default config: " |
| 495 | f"stdout={proc.stdout!r} stderr={proc.stderr!r}") |
| 496 | stdout = proc.stdout.decode('utf-8') |
Victor Stinner | 4631da1 | 2019-05-02 15:30:21 -0400 | [diff] [blame] | 497 | try: |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 498 | return json.loads(stdout) |
Victor Stinner | 4631da1 | 2019-05-02 15:30:21 -0400 | [diff] [blame] | 499 | except json.JSONDecodeError: |
| 500 | self.fail(f"fail to decode stdout: {stdout!r}") |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 501 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 502 | def get_expected_config(self, expected_preconfig, expected, env, api, |
| 503 | add_path=None): |
| 504 | cls = self.__class__ |
| 505 | if cls.EXPECTED_CONFIG is None: |
| 506 | cls.EXPECTED_CONFIG = self._get_expected_config(env) |
| 507 | configs = {key: dict(value) |
| 508 | for key, value in self.EXPECTED_CONFIG.items()} |
| 509 | |
| 510 | pre_config = configs['pre_config'] |
| 511 | for key, value in expected_preconfig.items(): |
| 512 | if value is self.GET_DEFAULT_CONFIG: |
| 513 | expected_preconfig[key] = pre_config[key] |
| 514 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 515 | if not expected_preconfig['configure_locale'] or api == API_COMPAT: |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 516 | # there is no easy way to get the locale encoding before |
| 517 | # setlocale(LC_CTYPE, "") is called: don't test encodings |
| 518 | for key in ('filesystem_encoding', 'filesystem_errors', |
| 519 | 'stdio_encoding', 'stdio_errors'): |
| 520 | expected[key] = self.IGNORE_CONFIG |
| 521 | |
| 522 | if not expected_preconfig['configure_locale']: |
| 523 | # UTF-8 Mode depends on the locale. There is no easy way |
| 524 | # to guess if UTF-8 Mode will be enabled or not if the locale |
| 525 | # is not configured. |
| 526 | expected_preconfig['utf8_mode'] = self.IGNORE_CONFIG |
| 527 | |
| 528 | if expected_preconfig['utf8_mode'] == 1: |
| 529 | if expected['filesystem_encoding'] is self.GET_DEFAULT_CONFIG: |
| 530 | expected['filesystem_encoding'] = 'utf-8' |
| 531 | if expected['filesystem_errors'] is self.GET_DEFAULT_CONFIG: |
| 532 | expected['filesystem_errors'] = self.UTF8_MODE_ERRORS |
| 533 | if expected['stdio_encoding'] is self.GET_DEFAULT_CONFIG: |
| 534 | expected['stdio_encoding'] = 'utf-8' |
| 535 | if expected['stdio_errors'] is self.GET_DEFAULT_CONFIG: |
| 536 | expected['stdio_errors'] = 'surrogateescape' |
| 537 | |
Steve Dower | 323e743 | 2019-06-29 14:28:59 -0700 | [diff] [blame] | 538 | if sys.platform == 'win32': |
| 539 | default_executable = self.test_exe |
| 540 | elif expected['program_name'] is not self.GET_DEFAULT_CONFIG: |
| 541 | default_executable = os.path.abspath(expected['program_name']) |
| 542 | else: |
| 543 | default_executable = os.path.join(os.getcwd(), '_testembed') |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 544 | if expected['executable'] is self.GET_DEFAULT_CONFIG: |
Steve Dower | 323e743 | 2019-06-29 14:28:59 -0700 | [diff] [blame] | 545 | expected['executable'] = default_executable |
| 546 | if expected['base_executable'] is self.GET_DEFAULT_CONFIG: |
| 547 | expected['base_executable'] = default_executable |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 548 | if expected['program_name'] is self.GET_DEFAULT_CONFIG: |
| 549 | expected['program_name'] = './_testembed' |
| 550 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 551 | config = configs['config'] |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 552 | for key, value in expected.items(): |
| 553 | if value is self.GET_DEFAULT_CONFIG: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 554 | expected[key] = config[key] |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 555 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 556 | prepend_path = expected['pythonpath_env'] |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 557 | if prepend_path is not None: |
| 558 | expected['module_search_paths'] = [prepend_path, *expected['module_search_paths']] |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 559 | if add_path is not None: |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 560 | expected['module_search_paths'] = [*expected['module_search_paths'], add_path] |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 561 | |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 562 | for key in self.COPY_PRE_CONFIG: |
| 563 | if key not in expected_preconfig: |
| 564 | expected_preconfig[key] = expected[key] |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 565 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 566 | def check_pre_config(self, configs, expected): |
| 567 | pre_config = dict(configs['pre_config']) |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 568 | for key, value in list(expected.items()): |
| 569 | if value is self.IGNORE_CONFIG: |
| 570 | del pre_config[key] |
| 571 | del expected[key] |
| 572 | self.assertEqual(pre_config, expected) |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 573 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 574 | def check_config(self, configs, expected): |
| 575 | config = dict(configs['config']) |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 576 | for key, value in list(expected.items()): |
| 577 | if value is self.IGNORE_CONFIG: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 578 | del config[key] |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 579 | del expected[key] |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 580 | self.assertEqual(config, expected) |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 581 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 582 | def check_global_config(self, configs): |
| 583 | pre_config = configs['pre_config'] |
| 584 | config = configs['config'] |
Victor Stinner | 00b137c | 2018-11-13 19:59:26 +0100 | [diff] [blame] | 585 | |
Victor Stinner | a6537fb | 2018-11-26 11:54:12 +0100 | [diff] [blame] | 586 | expected = dict(self.DEFAULT_GLOBAL_CONFIG) |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 587 | for item in self.COPY_GLOBAL_CONFIG: |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 588 | if len(item) == 3: |
| 589 | global_key, core_key, opposite = item |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 590 | expected[global_key] = 0 if config[core_key] else 1 |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 591 | else: |
| 592 | global_key, core_key = item |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 593 | expected[global_key] = config[core_key] |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 594 | for item in self.COPY_GLOBAL_PRE_CONFIG: |
| 595 | if len(item) == 3: |
| 596 | global_key, core_key, opposite = item |
| 597 | expected[global_key] = 0 if pre_config[core_key] else 1 |
| 598 | else: |
| 599 | global_key, core_key = item |
| 600 | expected[global_key] = pre_config[core_key] |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 601 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 602 | self.assertEqual(configs['global_config'], expected) |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 603 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 604 | def check_all_configs(self, testname, expected_config=None, |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 605 | expected_preconfig=None, add_path=None, stderr=None, |
| 606 | *, api): |
Miss Islington (bot) | 69610f8 | 2019-06-17 15:31:43 -0700 | [diff] [blame] | 607 | env = remove_python_envvars() |
Victor Stinner | 01de89c | 2018-11-14 17:39:45 +0100 | [diff] [blame] | 608 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 609 | if api == API_ISOLATED: |
| 610 | default_preconfig = self.PRE_CONFIG_ISOLATED |
| 611 | elif api == API_PYTHON: |
| 612 | default_preconfig = self.PRE_CONFIG_PYTHON |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 613 | else: |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 614 | default_preconfig = self.PRE_CONFIG_COMPAT |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 615 | if expected_preconfig is None: |
| 616 | expected_preconfig = {} |
| 617 | expected_preconfig = dict(default_preconfig, **expected_preconfig) |
| 618 | if expected_config is None: |
| 619 | expected_config = {} |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 620 | |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 621 | if api == API_PYTHON: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 622 | default_config = self.CONFIG_PYTHON |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 623 | elif api == API_ISOLATED: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 624 | default_config = self.CONFIG_ISOLATED |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 625 | else: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 626 | default_config = self.CONFIG_COMPAT |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 627 | expected_config = dict(default_config, **expected_config) |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 628 | |
| 629 | self.get_expected_config(expected_preconfig, |
| 630 | expected_config, env, |
| 631 | api, add_path) |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 632 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 633 | out, err = self.run_embedded_interpreter(testname, env=env) |
| 634 | if stderr is None and not expected_config['verbose']: |
| 635 | stderr = "" |
| 636 | if stderr is not None: |
| 637 | self.assertEqual(err.rstrip(), stderr) |
| 638 | try: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 639 | configs = json.loads(out) |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 640 | except json.JSONDecodeError: |
| 641 | self.fail(f"fail to decode stdout: {out!r}") |
| 642 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 643 | self.check_pre_config(configs, expected_preconfig) |
| 644 | self.check_config(configs, expected_config) |
| 645 | self.check_global_config(configs) |
Victor Stinner | 7ddd56f | 2018-11-14 00:24:28 +0100 | [diff] [blame] | 646 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 647 | def test_init_default_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 648 | self.check_all_configs("test_init_initialize_config", api=API_COMPAT) |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 649 | |
| 650 | def test_preinit_compat_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 651 | self.check_all_configs("test_preinit_compat_config", api=API_COMPAT) |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 652 | |
| 653 | def test_init_compat_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 654 | self.check_all_configs("test_init_compat_config", api=API_COMPAT) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 655 | |
| 656 | def test_init_global_config(self): |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 657 | preconfig = { |
| 658 | 'utf8_mode': 1, |
| 659 | } |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 660 | config = { |
| 661 | 'program_name': './globalvar', |
| 662 | 'site_import': 0, |
| 663 | 'bytes_warning': 1, |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 664 | 'warnoptions': ['default::BytesWarning'], |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 665 | 'inspect': 1, |
| 666 | 'interactive': 1, |
| 667 | 'optimization_level': 2, |
| 668 | 'write_bytecode': 0, |
| 669 | 'verbose': 1, |
| 670 | 'quiet': 1, |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 671 | 'buffered_stdio': 0, |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 672 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 673 | 'user_site_directory': 0, |
Victor Stinner | 54b43bb | 2019-05-16 18:30:15 +0200 | [diff] [blame] | 674 | 'pathconfig_warnings': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 675 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 676 | self.check_all_configs("test_init_global_config", config, preconfig, |
| 677 | api=API_COMPAT) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 678 | |
| 679 | def test_init_from_config(self): |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 680 | preconfig = { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 681 | 'allocator': PYMEM_ALLOCATOR_MALLOC, |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 682 | 'utf8_mode': 1, |
| 683 | } |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 684 | config = { |
| 685 | 'install_signal_handlers': 0, |
| 686 | 'use_hash_seed': 1, |
| 687 | 'hash_seed': 123, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 688 | 'tracemalloc': 2, |
| 689 | 'import_time': 1, |
| 690 | 'show_ref_count': 1, |
| 691 | 'show_alloc_count': 1, |
| 692 | 'malloc_stats': 1, |
| 693 | |
Victor Stinner | dfe0dc7 | 2018-08-29 11:47:29 +0200 | [diff] [blame] | 694 | 'stdio_encoding': 'iso8859-1', |
| 695 | 'stdio_errors': 'replace', |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 696 | |
| 697 | 'pycache_prefix': 'conf_pycache_prefix', |
| 698 | 'program_name': './conf_program_name', |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 699 | 'argv': ['-c', 'arg2', ], |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 700 | 'parse_argv': 1, |
Miss Islington (bot) | 4c227e6 | 2019-07-01 11:28:55 -0700 | [diff] [blame] | 701 | 'xoptions': [ |
| 702 | 'config_xoption1=3', |
| 703 | 'config_xoption2=', |
| 704 | 'config_xoption3', |
| 705 | 'cmdline_xoption', |
| 706 | ], |
| 707 | 'warnoptions': [ |
| 708 | 'config_warnoption', |
| 709 | 'cmdline_warnoption', |
| 710 | 'default::BytesWarning', |
| 711 | ], |
Victor Stinner | 2f54908 | 2019-03-29 15:13:46 +0100 | [diff] [blame] | 712 | 'run_command': 'pass\n', |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 713 | |
| 714 | 'site_import': 0, |
| 715 | 'bytes_warning': 1, |
| 716 | 'inspect': 1, |
| 717 | 'interactive': 1, |
| 718 | 'optimization_level': 2, |
| 719 | 'write_bytecode': 0, |
| 720 | 'verbose': 1, |
| 721 | 'quiet': 1, |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 722 | 'configure_c_stdio': 1, |
Victor Stinner | 9851227 | 2018-08-01 03:07:00 +0200 | [diff] [blame] | 723 | 'buffered_stdio': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 724 | 'user_site_directory': 0, |
| 725 | 'faulthandler': 1, |
Victor Stinner | b75d7e2 | 2018-08-01 02:13:04 +0200 | [diff] [blame] | 726 | |
Victor Stinner | cb9fbd3 | 2019-05-01 23:51:56 -0400 | [diff] [blame] | 727 | 'check_hash_pycs_mode': 'always', |
Victor Stinner | 54b43bb | 2019-05-16 18:30:15 +0200 | [diff] [blame] | 728 | 'pathconfig_warnings': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 729 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 730 | self.check_all_configs("test_init_from_config", config, preconfig, |
| 731 | api=API_COMPAT) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 732 | |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 733 | def test_init_compat_env(self): |
| 734 | preconfig = { |
| 735 | 'allocator': PYMEM_ALLOCATOR_MALLOC, |
| 736 | } |
| 737 | config = { |
| 738 | 'use_hash_seed': 1, |
| 739 | 'hash_seed': 42, |
| 740 | 'tracemalloc': 2, |
| 741 | 'import_time': 1, |
| 742 | 'malloc_stats': 1, |
| 743 | 'inspect': 1, |
| 744 | 'optimization_level': 2, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 745 | 'pythonpath_env': '/my/path', |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 746 | 'pycache_prefix': 'env_pycache_prefix', |
| 747 | 'write_bytecode': 0, |
| 748 | 'verbose': 1, |
| 749 | 'buffered_stdio': 0, |
| 750 | 'stdio_encoding': 'iso8859-1', |
| 751 | 'stdio_errors': 'replace', |
| 752 | 'user_site_directory': 0, |
| 753 | 'faulthandler': 1, |
| 754 | 'warnoptions': ['EnvVar'], |
| 755 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 756 | self.check_all_configs("test_init_compat_env", config, preconfig, |
| 757 | api=API_COMPAT) |
Victor Stinner | 20e1e25 | 2019-05-23 04:12:27 +0200 | [diff] [blame] | 758 | |
| 759 | def test_init_python_env(self): |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 760 | preconfig = { |
| 761 | 'allocator': PYMEM_ALLOCATOR_MALLOC, |
Victor Stinner | 022be02 | 2019-05-22 23:58:50 +0200 | [diff] [blame] | 762 | 'utf8_mode': 1, |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 763 | } |
| 764 | config = { |
| 765 | 'use_hash_seed': 1, |
| 766 | 'hash_seed': 42, |
| 767 | 'tracemalloc': 2, |
| 768 | 'import_time': 1, |
| 769 | 'malloc_stats': 1, |
| 770 | 'inspect': 1, |
| 771 | 'optimization_level': 2, |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 772 | 'pythonpath_env': '/my/path', |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 773 | 'pycache_prefix': 'env_pycache_prefix', |
| 774 | 'write_bytecode': 0, |
| 775 | 'verbose': 1, |
| 776 | 'buffered_stdio': 0, |
| 777 | 'stdio_encoding': 'iso8859-1', |
| 778 | 'stdio_errors': 'replace', |
| 779 | 'user_site_directory': 0, |
| 780 | 'faulthandler': 1, |
| 781 | 'warnoptions': ['EnvVar'], |
| 782 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 783 | self.check_all_configs("test_init_python_env", config, preconfig, |
| 784 | api=API_PYTHON) |
Victor Stinner | b35be4b | 2019-03-05 17:37:44 +0100 | [diff] [blame] | 785 | |
| 786 | def test_init_env_dev_mode(self): |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 787 | preconfig = dict(allocator=PYMEM_ALLOCATOR_DEBUG) |
| 788 | config = dict(dev_mode=1, |
| 789 | faulthandler=1, |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 790 | warnoptions=['default']) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 791 | self.check_all_configs("test_init_env_dev_mode", config, preconfig, |
| 792 | api=API_COMPAT) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 793 | |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 794 | def test_init_env_dev_mode_alloc(self): |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 795 | preconfig = dict(allocator=PYMEM_ALLOCATOR_MALLOC) |
| 796 | config = dict(dev_mode=1, |
| 797 | faulthandler=1, |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 798 | warnoptions=['default']) |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 799 | self.check_all_configs("test_init_env_dev_mode_alloc", config, preconfig, |
| 800 | api=API_COMPAT) |
Victor Stinner | 25d13f3 | 2019-03-06 12:51:53 +0100 | [diff] [blame] | 801 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 802 | def test_init_dev_mode(self): |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 803 | preconfig = { |
Victor Stinner | b16b4e4 | 2019-05-17 15:20:52 +0200 | [diff] [blame] | 804 | 'allocator': PYMEM_ALLOCATOR_DEBUG, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 805 | } |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 806 | config = { |
| 807 | 'faulthandler': 1, |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 808 | 'dev_mode': 1, |
Victor Stinner | f8ba6f5 | 2019-03-26 16:58:50 +0100 | [diff] [blame] | 809 | 'warnoptions': ['default'], |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 810 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 811 | self.check_all_configs("test_init_dev_mode", config, preconfig, |
| 812 | api=API_PYTHON) |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 813 | |
| 814 | def test_preinit_parse_argv(self): |
| 815 | # Pre-initialize implicitly using argv: make sure that -X dev |
| 816 | # is used to configure the allocation in preinitialization |
| 817 | preconfig = { |
| 818 | 'allocator': PYMEM_ALLOCATOR_DEBUG, |
| 819 | } |
| 820 | config = { |
| 821 | 'argv': ['script.py'], |
| 822 | 'run_filename': 'script.py', |
| 823 | 'dev_mode': 1, |
| 824 | 'faulthandler': 1, |
| 825 | 'warnoptions': ['default'], |
| 826 | 'xoptions': ['dev'], |
| 827 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 828 | self.check_all_configs("test_preinit_parse_argv", config, preconfig, |
| 829 | api=API_PYTHON) |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 830 | |
| 831 | def test_preinit_dont_parse_argv(self): |
| 832 | # -X dev must be ignored by isolated preconfiguration |
| 833 | preconfig = { |
| 834 | 'isolated': 0, |
| 835 | } |
| 836 | config = { |
| 837 | 'argv': ["python3", "-E", "-I", |
| 838 | "-X", "dev", "-X", "utf8", "script.py"], |
| 839 | 'isolated': 0, |
| 840 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 841 | self.check_all_configs("test_preinit_dont_parse_argv", config, preconfig, |
| 842 | api=API_ISOLATED) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 843 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 844 | def test_init_isolated_flag(self): |
Victor Stinner | 1075d16 | 2019-03-25 23:19:57 +0100 | [diff] [blame] | 845 | config = { |
Victor Stinner | 2000495 | 2019-03-26 02:31:11 +0100 | [diff] [blame] | 846 | 'isolated': 1, |
| 847 | 'use_environment': 0, |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 848 | 'user_site_directory': 0, |
| 849 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 850 | self.check_all_configs("test_init_isolated_flag", config, api=API_PYTHON) |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 851 | |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 852 | def test_preinit_isolated1(self): |
| 853 | # _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 854 | config = { |
| 855 | 'isolated': 1, |
| 856 | 'use_environment': 0, |
| 857 | 'user_site_directory': 0, |
| 858 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 859 | self.check_all_configs("test_preinit_isolated1", config, api=API_COMPAT) |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 860 | |
| 861 | def test_preinit_isolated2(self): |
| 862 | # _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1 |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 863 | config = { |
| 864 | 'isolated': 1, |
| 865 | 'use_environment': 0, |
| 866 | 'user_site_directory': 0, |
| 867 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 868 | self.check_all_configs("test_preinit_isolated2", config, api=API_COMPAT) |
Victor Stinner | 6da20a4 | 2019-03-27 00:26:18 +0100 | [diff] [blame] | 869 | |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 870 | def test_preinit_isolated_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 871 | self.check_all_configs("test_preinit_isolated_config", api=API_ISOLATED) |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 872 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 873 | def test_init_isolated_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 874 | self.check_all_configs("test_init_isolated_config", api=API_ISOLATED) |
Victor Stinner | 5edcf26 | 2019-05-23 00:57:57 +0200 | [diff] [blame] | 875 | |
| 876 | def test_preinit_python_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 877 | self.check_all_configs("test_preinit_python_config", api=API_PYTHON) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 878 | |
| 879 | def test_init_python_config(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 880 | self.check_all_configs("test_init_python_config", api=API_PYTHON) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 881 | |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 882 | def test_init_dont_configure_locale(self): |
| 883 | # _PyPreConfig.configure_locale=0 |
| 884 | preconfig = { |
| 885 | 'configure_locale': 0, |
Victor Stinner | 425717f | 2019-05-20 16:38:48 +0200 | [diff] [blame] | 886 | 'coerce_c_locale': 0, |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 887 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 888 | self.check_all_configs("test_init_dont_configure_locale", {}, preconfig, |
| 889 | api=API_PYTHON) |
Victor Stinner | bcfbbd7 | 2019-05-17 22:44:16 +0200 | [diff] [blame] | 890 | |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 891 | def test_init_read_set(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 892 | config = { |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 893 | 'program_name': './init_read_set', |
| 894 | 'executable': 'my_executable', |
| 895 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 896 | self.check_all_configs("test_init_read_set", config, |
| 897 | api=API_PYTHON, |
| 898 | add_path="init_read_set_path") |
Victor Stinner | 91c9987 | 2019-05-14 22:01:51 +0200 | [diff] [blame] | 899 | |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 900 | def test_init_run_main(self): |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 901 | code = ('import _testinternalcapi, json; ' |
| 902 | 'print(json.dumps(_testinternalcapi.get_configs()))') |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 903 | config = { |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 904 | 'argv': ['-c', 'arg2'], |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 905 | 'program_name': './python3', |
| 906 | 'run_command': code + '\n', |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 907 | 'parse_argv': 1, |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 908 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 909 | self.check_all_configs("test_init_run_main", config, api=API_PYTHON) |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 910 | |
| 911 | def test_init_main(self): |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 912 | code = ('import _testinternalcapi, json; ' |
| 913 | 'print(json.dumps(_testinternalcapi.get_configs()))') |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 914 | config = { |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 915 | 'argv': ['-c', 'arg2'], |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 916 | 'program_name': './python3', |
| 917 | 'run_command': code + '\n', |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 918 | 'parse_argv': 1, |
Victor Stinner | 9ef5dca | 2019-05-16 17:38:16 +0200 | [diff] [blame] | 919 | '_init_main': 0, |
| 920 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 921 | self.check_all_configs("test_init_main", config, |
| 922 | api=API_PYTHON, |
| 923 | stderr="Run Python code before _Py_InitializeMain") |
Victor Stinner | 5eb8b07 | 2019-05-15 02:12:48 +0200 | [diff] [blame] | 924 | |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 925 | def test_init_parse_argv(self): |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 926 | config = { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 927 | 'parse_argv': 1, |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 928 | 'argv': ['-c', 'arg1', '-v', 'arg3'], |
| 929 | 'program_name': './argv0', |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 930 | 'run_command': 'pass\n', |
| 931 | 'use_environment': 0, |
| 932 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 933 | self.check_all_configs("test_init_parse_argv", config, api=API_PYTHON) |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 934 | |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 935 | def test_init_dont_parse_argv(self): |
Victor Stinner | 6d1c467 | 2019-05-20 11:02:00 +0200 | [diff] [blame] | 936 | pre_config = { |
| 937 | 'parse_argv': 0, |
| 938 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 939 | config = { |
Victor Stinner | bab0db6 | 2019-05-18 03:21:27 +0200 | [diff] [blame] | 940 | 'parse_argv': 0, |
Victor Stinner | cab5d07 | 2019-05-17 19:01:14 +0200 | [diff] [blame] | 941 | 'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'], |
| 942 | 'program_name': './argv0', |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 943 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 944 | self.check_all_configs("test_init_dont_parse_argv", config, pre_config, |
| 945 | api=API_PYTHON) |
Victor Stinner | ae239f6 | 2019-05-16 17:02:56 +0200 | [diff] [blame] | 946 | |
Victor Stinner | 56b29b6 | 2018-07-26 18:57:56 +0200 | [diff] [blame] | 947 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 948 | class AuditingTests(EmbeddingTestsMixin, unittest.TestCase): |
| 949 | def test_open_code_hook(self): |
| 950 | self.run_embedded_interpreter("test_open_code_hook") |
| 951 | |
| 952 | def test_audit(self): |
| 953 | self.run_embedded_interpreter("test_audit") |
| 954 | |
| 955 | def test_audit_subinterpreter(self): |
| 956 | self.run_embedded_interpreter("test_audit_subinterpreter") |
| 957 | |
Miss Islington (bot) | 746992c | 2019-07-01 16:22:29 -0700 | [diff] [blame^] | 958 | def test_audit_run_command(self): |
| 959 | self.run_embedded_interpreter("test_audit_run_command", timeout=3, returncode=1) |
| 960 | |
| 961 | def test_audit_run_file(self): |
| 962 | self.run_embedded_interpreter("test_audit_run_file", timeout=3, returncode=1) |
| 963 | |
| 964 | def test_audit_run_interactivehook(self): |
| 965 | startup = os.path.join(self.oldcwd, support.TESTFN) + ".py" |
| 966 | with open(startup, "w", encoding="utf-8") as f: |
| 967 | print("import sys", file=f) |
| 968 | print("sys.__interactivehook__ = lambda: None", file=f) |
| 969 | try: |
| 970 | env = {**remove_python_envvars(), "PYTHONSTARTUP": startup} |
| 971 | self.run_embedded_interpreter("test_audit_run_interactivehook", timeout=5, |
| 972 | returncode=10, env=env) |
| 973 | finally: |
| 974 | os.unlink(startup) |
| 975 | |
| 976 | def test_audit_run_startup(self): |
| 977 | startup = os.path.join(self.oldcwd, support.TESTFN) + ".py" |
| 978 | with open(startup, "w", encoding="utf-8") as f: |
| 979 | print("pass", file=f) |
| 980 | try: |
| 981 | env = {**remove_python_envvars(), "PYTHONSTARTUP": startup} |
| 982 | self.run_embedded_interpreter("test_audit_run_startup", timeout=5, |
| 983 | returncode=10, env=env) |
| 984 | finally: |
| 985 | os.unlink(startup) |
| 986 | |
| 987 | def test_audit_run_stdin(self): |
| 988 | self.run_embedded_interpreter("test_audit_run_stdin", timeout=3, returncode=1) |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 989 | |
Nick Coghlan | 39f0bb5 | 2017-11-28 08:11:51 +1000 | [diff] [blame] | 990 | if __name__ == "__main__": |
| 991 | unittest.main() |