bpo-36763: Add _Py_InitializeMain() (GH-13362)

* Add a private _Py_InitializeMain() function.
* Add again _PyCoreConfig._init_main.
* _Py_InitializeFromConfig() now uses _init_main to decide
  if _Py_InitializeMainInterpreter() should be called.
* _PyCoreConfig: rename _frozen to pathconfig_warnings, its value is
  now the opposite of Py_FrozenFlag.
* Add an unit test for _init_main=0 and _Py_InitializeMain().
This commit is contained in:
Victor Stinner 2019-05-16 17:38:16 +02:00 committed by GitHub
parent ae239f6b06
commit 9ef5dcaa0b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 130 additions and 40 deletions

View file

@ -343,7 +343,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'_install_importlib': 1,
'check_hash_pycs_mode': 'default',
'_frozen': 0,
'pathconfig_warnings': 1,
'_init_main': 1,
}
if MS_WINDOWS:
DEFAULT_PRE_CONFIG.update({
@ -371,7 +372,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
('Py_DontWriteBytecodeFlag', 'write_bytecode', True),
('Py_FileSystemDefaultEncodeErrors', 'filesystem_errors'),
('Py_FileSystemDefaultEncoding', 'filesystem_encoding'),
('Py_FrozenFlag', '_frozen'),
('Py_FrozenFlag', 'pathconfig_warnings', True),
('Py_IgnoreEnvironmentFlag', 'use_environment', True),
('Py_InspectFlag', 'inspect'),
('Py_InteractiveFlag', 'interactive'),
@ -500,7 +501,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
self.assertEqual(config['global_config'], expected)
def check_config(self, testname, expected_config, expected_preconfig, add_path=None):
def check_config(self, testname, expected_config, expected_preconfig,
add_path=None, stderr=None):
env = dict(os.environ)
# Remove PYTHON* environment variables to get deterministic environment
for key in list(env):
@ -511,19 +513,22 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
env['PYTHONCOERCECLOCALE'] = '0'
env['PYTHONUTF8'] = '0'
out, err = self.run_embedded_interpreter(testname, env=env)
# Ignore err
try:
config = json.loads(out)
except json.JSONDecodeError:
self.fail(f"fail to decode stdout: {out!r}")
expected_preconfig = dict(self.DEFAULT_PRE_CONFIG, **expected_preconfig)
expected_config = self.get_expected_config(expected_config, env, add_path)
for key in self.COPY_PRE_CONFIG:
if key not in expected_preconfig:
expected_preconfig[key] = expected_config[key]
out, err = self.run_embedded_interpreter(testname, env=env)
if stderr is None and not expected_config['verbose']:
stderr = ""
if stderr is not None:
self.assertEqual(err.rstrip(), stderr)
try:
config = json.loads(out)
except json.JSONDecodeError:
self.fail(f"fail to decode stdout: {out!r}")
self.check_pre_config(config, expected_preconfig)
self.check_core_config(config, expected_config)
self.check_global_config(config)
@ -689,7 +694,7 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
self.check_config("init_read_set", core_config, preconfig,
add_path="init_read_set_path")
def test_run_main_config(self):
def test_init_run_main(self):
preconfig = {}
code = ('import _testinternalcapi, json; '
'print(json.dumps(_testinternalcapi.get_configs()))')
@ -699,7 +704,21 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
'program_name': './python3',
'run_command': code + '\n',
}
self.check_config("run_main_config", core_config, preconfig)
self.check_config("init_run_main", core_config, preconfig)
def test_init_main(self):
preconfig = {}
code = ('import _testinternalcapi, json; '
'print(json.dumps(_testinternalcapi.get_configs()))')
core_config = {
'argv': ['-c', 'arg2'],
'program': 'python3',
'program_name': './python3',
'run_command': code + '\n',
'_init_main': 0,
}
self.check_config("init_main", core_config, preconfig,
stderr="Run Python code before _Py_InitializeMain")
def test_init_dont_parse_argv(self):
core_config = {