diff --git a/Include/cpython/coreconfig.h b/Include/cpython/coreconfig.h index f05eddad912..ca71c15b67d 100644 --- a/Include/cpython/coreconfig.h +++ b/Include/cpython/coreconfig.h @@ -115,27 +115,8 @@ typedef struct { PyMemAllocatorName allocator; } _PyPreConfig; -#ifdef MS_WINDOWS -# define _PyPreConfig_WINDOWS_INIT \ - .legacy_windows_fs_encoding = -1, -#else -# define _PyPreConfig_WINDOWS_INIT -#endif - -#define _PyPreConfig_INIT \ - (_PyPreConfig){ \ - _PyPreConfig_WINDOWS_INIT \ - ._config_version = _Py_CONFIG_VERSION, \ - .isolated = -1, \ - .use_environment = -1, \ - .configure_locale = 1, \ - .utf8_mode = -2, \ - .dev_mode = -1, \ - .allocator = PYMEM_ALLOCATOR_NOT_SET} - -PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config); PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_InitIsolateConfig(_PyPreConfig *config); +PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config); /* --- _PyCoreConfig ---------------------------------------------- */ @@ -419,47 +400,23 @@ typedef struct { } _PyCoreConfig; -#ifdef MS_WINDOWS -# define _PyCoreConfig_WINDOWS_INIT \ - .legacy_windows_stdio = -1, -#else -# define _PyCoreConfig_WINDOWS_INIT -#endif - -#define _PyCoreConfig_INIT \ - (_PyCoreConfig){ \ - _PyCoreConfig_WINDOWS_INIT \ - ._config_version = _Py_CONFIG_VERSION, \ - .isolated = -1, \ - .use_environment = -1, \ - .dev_mode = -1, \ - .install_signal_handlers = 1, \ - .use_hash_seed = -1, \ - .faulthandler = -1, \ - .tracemalloc = -1, \ - .use_module_search_paths = 0, \ - .parse_argv = 0, \ - .site_import = -1, \ - .bytes_warning = -1, \ - .inspect = -1, \ - .interactive = -1, \ - .optimization_level = -1, \ - .parser_debug= -1, \ - .write_bytecode = -1, \ - .verbose = -1, \ - .quiet = -1, \ - .user_site_directory = -1, \ - .configure_c_stdio = 0, \ - .buffered_stdio = -1, \ - ._install_importlib = 1, \ - .check_hash_pycs_mode = NULL, \ - .pathconfig_warnings = -1, \ - ._init_main = 1} -/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */ - -PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config); PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolateConfig(_PyCoreConfig *config); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config); +PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString( + wchar_t **config_str, + const wchar_t *str); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale( + wchar_t **config_str, + const char *str); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetArgv( + _PyCoreConfig *config, + Py_ssize_t argc, + char **argv); +PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config, + Py_ssize_t argc, + wchar_t **argv); #ifdef __cplusplus } diff --git a/Include/internal/pycore_coreconfig.h b/Include/internal/pycore_coreconfig.h index ea4418f5061..edde7b1d8d8 100644 --- a/Include/internal/pycore_coreconfig.h +++ b/Include/internal/pycore_coreconfig.h @@ -121,8 +121,6 @@ PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline, /* --- _PyPreConfig ----------------------------------------------- */ PyAPI_FUNC(void) _PyPreConfig_Init(_PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_InitPythonConfig(_PyPreConfig *config); -PyAPI_FUNC(void) _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config); PyAPI_FUNC(void) _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2); PyAPI_FUNC(PyObject*) _PyPreConfig_AsDict(const _PyPreConfig *config); @@ -135,34 +133,18 @@ PyAPI_FUNC(_PyInitError) _PyPreConfig_Write(const _PyPreConfig *config); /* --- _PyCoreConfig ---------------------------------------------- */ -PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config); +PyAPI_FUNC(void) _PyCoreConfig_Init(_PyCoreConfig *config); PyAPI_FUNC(_PyInitError) _PyCoreConfig_Copy( _PyCoreConfig *config, const _PyCoreConfig *config2); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetString( - wchar_t **config_str, - const wchar_t *str); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_DecodeLocale( - wchar_t **config_str, - const char *str); PyAPI_FUNC(_PyInitError) _PyCoreConfig_InitPathConfig(_PyCoreConfig *config); PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig( const _PyCoreConfig *config); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config); PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config, _PyRuntimeState *runtime); PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPyArgv( _PyCoreConfig *config, const _PyArgv *args); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetArgv( - _PyCoreConfig *config, - Py_ssize_t argc, - char **argv); -PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetWideArgv(_PyCoreConfig *config, - Py_ssize_t argc, - wchar_t **argv); /* --- Function used for testing ---------------------------------- */ diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index c389df85fb6..6b77a2d1fd8 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -285,6 +285,16 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'coerce_c_locale_warn': 0, 'utf8_mode': 0, } + ISOLATED_PRE_CONFIG = dict(DEFAULT_PRE_CONFIG, + configure_locale=0, + isolated=1, + use_environment=0, + utf8_mode=0, + dev_mode=0, + ) + if MS_WINDOWS: + ISOLATED_PRE_CONFIG['legacy_windows_fs_encoding'] = 0 + COPY_PRE_CONFIG = [ 'dev_mode', 'isolated', @@ -363,6 +373,24 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'legacy_windows_stdio': 0, }) + PYTHON_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG, + configure_c_stdio=1, + parse_argv=1, + ) + ISOLATED_CORE_CONFIG = dict(DEFAULT_CORE_CONFIG, + isolated=1, + use_environment=0, + user_site_directory=0, + dev_mode=0, + install_signal_handlers=0, + use_hash_seed=0, + faulthandler=0, + tracemalloc=0, + pathconfig_warnings=0, + ) + if MS_WINDOWS: + ISOLATED_CORE_CONFIG['legacy_windows_stdio'] = 0 + # global config DEFAULT_GLOBAL_CONFIG = { 'Py_HasFileSystemDefaultEncoding': 0, @@ -410,8 +438,15 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): xoptions[opt] = True return xoptions - def get_expected_config(self, expected_preconfig, expected, env, add_path=None): - expected = dict(self.DEFAULT_CORE_CONFIG, **expected) + def get_expected_config(self, expected_preconfig, expected, env, api, + add_path=None): + if api == "python": + default_config = self.PYTHON_CORE_CONFIG + elif api == "isolated": + default_config = self.ISOLATED_CORE_CONFIG + else: + default_config = self.DEFAULT_CORE_CONFIG + expected = dict(default_config, **expected) code = textwrap.dedent(''' import json @@ -521,8 +556,8 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): self.assertEqual(config['global_config'], expected) - def check_config(self, testname, expected_config, expected_preconfig, - add_path=None, stderr=None): + def check_config(self, testname, expected_config=None, expected_preconfig=None, + add_path=None, stderr=None, api="default"): env = dict(os.environ) # Remove PYTHON* environment variables to get deterministic environment for key in list(env): @@ -533,8 +568,18 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): env['PYTHONCOERCECLOCALE'] = '0' env['PYTHONUTF8'] = '0' - expected_preconfig = dict(self.DEFAULT_PRE_CONFIG, **expected_preconfig) - expected_config = self.get_expected_config(expected_preconfig, expected_config, env, add_path) + if api == "isolated": + default_preconfig = self.ISOLATED_PRE_CONFIG + else: + default_preconfig = self.DEFAULT_PRE_CONFIG + if expected_preconfig is None: + expected_preconfig = {} + expected_preconfig = dict(default_preconfig, **expected_preconfig) + if expected_config is None: + expected_config = {} + expected_config = self.get_expected_config(expected_preconfig, + expected_config, env, + api, add_path) for key in self.COPY_PRE_CONFIG: if key not in expected_preconfig: expected_preconfig[key] = expected_config[key] @@ -677,76 +722,56 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'dev_mode': 1, 'warnoptions': ['default'], } - self.check_config("init_dev_mode", config, preconfig) + self.check_config("init_dev_mode", config, preconfig, api="python") def test_init_isolated_flag(self): - preconfig = {} config = { 'isolated': 1, 'use_environment': 0, 'user_site_directory': 0, } - self.check_config("init_isolated_flag", config, preconfig) + self.check_config("init_isolated_flag", config, api="python") def test_preinit_isolated1(self): # _PyPreConfig.isolated=1, _PyCoreConfig.isolated not set - preconfig = {} config = { 'isolated': 1, 'use_environment': 0, 'user_site_directory': 0, } - self.check_config("preinit_isolated1", config, preconfig) + self.check_config("preinit_isolated1", config) def test_preinit_isolated2(self): # _PyPreConfig.isolated=0, _PyCoreConfig.isolated=1 - preconfig = {} config = { 'isolated': 1, 'use_environment': 0, 'user_site_directory': 0, } - self.check_config("preinit_isolated2", config, preconfig) + self.check_config("preinit_isolated2", config) def test_init_isolated_config(self): - preconfig = { - 'configure_locale': 0, - } - config = { - 'isolated': 1, - 'use_environment': 0, - 'user_site_directory': 0, - 'install_signal_handlers': 0, - 'pathconfig_warnings': 0, - } - self.check_config("init_isolated_config", config, preconfig) + self.check_config("init_isolated_config", api="isolated") def test_init_python_config(self): - preconfig = {} - config = { - 'configure_c_stdio': 1, - 'parse_argv': 1, - } - self.check_config("init_python_config", config, preconfig) + self.check_config("init_python_config", api="python") def test_init_dont_configure_locale(self): # _PyPreConfig.configure_locale=0 preconfig = { 'configure_locale': 0, } - self.check_config("init_dont_configure_locale", {}, preconfig) + self.check_config("init_dont_configure_locale", {}, preconfig, api="python") def test_init_read_set(self): - preconfig = {} core_config = { 'program_name': './init_read_set', 'executable': 'my_executable', } - self.check_config("init_read_set", core_config, preconfig, + self.check_config("init_read_set", core_config, api="python", add_path="init_read_set_path") def test_init_run_main(self): - preconfig = {} code = ('import _testinternalcapi, json; ' 'print(json.dumps(_testinternalcapi.get_configs()))') core_config = { @@ -755,10 +780,9 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'run_command': code + '\n', 'parse_argv': 1, } - self.check_config("init_run_main", core_config, preconfig) + self.check_config("init_run_main", core_config, api="python") def test_init_main(self): - preconfig = {} code = ('import _testinternalcapi, json; ' 'print(json.dumps(_testinternalcapi.get_configs()))') core_config = { @@ -768,25 +792,26 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): 'parse_argv': 1, '_init_main': 0, } - self.check_config("init_main", core_config, preconfig, + self.check_config("init_main", core_config, api="python", stderr="Run Python code before _Py_InitializeMain") def test_init_parse_argv(self): core_config = { + 'parse_argv': 1, 'argv': ['-c', 'arg1', '-v', 'arg3'], 'program_name': './argv0', - 'parse_argv': 1, 'run_command': 'pass\n', 'use_environment': 0, } - self.check_config("init_parse_argv", core_config, {}) + self.check_config("init_parse_argv", core_config, api="python") def test_init_dont_parse_argv(self): core_config = { + 'parse_argv': 0, 'argv': ['./argv0', '-E', '-c', 'pass', 'arg1', '-v', 'arg3'], 'program_name': './argv0', } - self.check_config("init_dont_parse_argv", core_config, {}) + self.check_config("init_dont_parse_argv", core_config, api="python") if __name__ == "__main__": diff --git a/Programs/_freeze_importlib.c b/Programs/_freeze_importlib.c index d89d66abee7..1a719e2f967 100644 --- a/Programs/_freeze_importlib.c +++ b/Programs/_freeze_importlib.c @@ -77,14 +77,12 @@ main(int argc, char *argv[]) text[text_size] = '\0'; _PyCoreConfig config; - _PyCoreConfig_Init(&config); - config.use_environment = 0; - config.user_site_directory = 0; + _PyCoreConfig_InitIsolatedConfig(&config); + config.site_import = 0; config.program_name = L"./_freeze_importlib"; /* Don't install importlib, since it could execute outdated bytecode. */ config._install_importlib = 0; - config.pathconfig_warnings = 0; config._init_main = 0; _PyInitError err = _Py_InitializeFromConfig(&config); diff --git a/Programs/_testembed.c b/Programs/_testembed.c index 2352179cc6f..f1bb731dcba 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -5,6 +5,7 @@ #include #include "pycore_coreconfig.h" /* FIXME: PEP 587 makes these functions public */ +#include #include "pythread.h" #include #include @@ -404,7 +405,7 @@ static int test_init_from_config(void) config.use_hash_seed = 1; config.hash_seed = 123; - /* dev_mode=1 is tested in init_dev_mode() */ + /* dev_mode=1 is tested in test_init_dev_mode() */ putenv("PYTHONFAULTHANDLER="); config.faulthandler = 1; @@ -521,12 +522,12 @@ static int test_init_from_config(void) } -static int test_init_parse_argv(int parse_argv) +static int check_init_parse_argv(int parse_argv) { _PyInitError err; _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); static wchar_t* argv[] = { L"./argv0", @@ -552,15 +553,15 @@ static int test_init_parse_argv(int parse_argv) } -static int init_parse_argv(void) +static int test_init_parse_argv(void) { - return test_init_parse_argv(1); + return check_init_parse_argv(1); } -static int init_dont_parse_argv(void) +static int test_init_dont_parse_argv(void) { - return test_init_parse_argv(0); + return check_init_parse_argv(0); } @@ -603,7 +604,7 @@ static int test_init_env(void) } -static void test_init_env_dev_mode_putenvs(void) +static void set_all_env_vars(void) { test_init_env_putenvs(); putenv("PYTHONMALLOC="); @@ -616,7 +617,7 @@ static int test_init_env_dev_mode(void) { /* Test initialization from environment variables */ Py_IgnoreEnvironmentFlag = 0; - test_init_env_dev_mode_putenvs(); + set_all_env_vars(); _testembed_Py_Initialize(); dump_config(); Py_Finalize(); @@ -628,7 +629,7 @@ static int test_init_env_dev_mode_alloc(void) { /* Test initialization from environment variables */ Py_IgnoreEnvironmentFlag = 0; - test_init_env_dev_mode_putenvs(); + set_all_env_vars(); putenv("PYTHONMALLOC=malloc"); _testembed_Py_Initialize(); dump_config(); @@ -637,13 +638,13 @@ static int test_init_env_dev_mode_alloc(void) } -static int init_isolated_flag(void) +static int test_init_isolated_flag(void) { _PyInitError err; /* Test _PyCoreConfig.isolated=1 */ _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); Py_IsolatedFlag = 0; config.isolated = 1; @@ -651,7 +652,7 @@ static int init_isolated_flag(void) /* Use path starting with "./" avoids a search along the PATH */ config.program_name = L"./_testembed"; - test_init_env_dev_mode_putenvs(); + set_all_env_vars(); err = _Py_InitializeFromConfig(&config); if (_PyInitError_Failed(err)) { _Py_ExitInitError(err); @@ -680,7 +681,7 @@ static int test_preinit_isolated1(void) _PyCoreConfig_Init(&config); config.program_name = L"./_testembed"; - test_init_env_dev_mode_putenvs(); + set_all_env_vars(); err = _Py_InitializeFromConfig(&config); if (_PyInitError_Failed(err)) { _Py_ExitInitError(err); @@ -715,7 +716,7 @@ static int test_preinit_isolated2(void) /* Use path starting with "./" avoids a search along the PATH */ config.program_name = L"./_testembed"; - test_init_env_dev_mode_putenvs(); + set_all_env_vars(); err = _Py_InitializeFromConfig(&config); if (_PyInitError_Failed(err)) { _Py_ExitInitError(err); @@ -726,10 +727,38 @@ static int test_preinit_isolated2(void) } -static int init_isolated_config(void) +static void set_all_global_config_variables(void) +{ + Py_IsolatedFlag = 0; + Py_IgnoreEnvironmentFlag = 0; + Py_BytesWarningFlag = 2; + Py_InspectFlag = 1; + Py_InteractiveFlag = 1; + Py_OptimizeFlag = 1; + Py_DebugFlag = 1; + Py_VerboseFlag = 1; + Py_QuietFlag = 1; + Py_FrozenFlag = 0; + Py_UnbufferedStdioFlag = 1; + Py_NoSiteFlag = 1; + Py_DontWriteBytecodeFlag = 1; + Py_NoUserSiteDirectory = 1; +#ifdef MS_WINDOWS + Py_LegacyWindowsStdioFlag = 1; +#endif +} + + +static int test_init_isolated_config(void) { _PyInitError err; + /* environment variables must be ignored */ + set_all_env_vars(); + + /* global configuration variables must be ignored */ + set_all_global_config_variables(); + _PyPreConfig preconfig; _PyPreConfig_InitIsolatedConfig(&preconfig); @@ -759,10 +788,23 @@ static int init_isolated_config(void) } -static int init_python_config(void) +static int test_init_python_config(void) { _PyInitError err; + /* global configuration variables must be ignored */ + set_all_global_config_variables(); + Py_IsolatedFlag = 1; + Py_IgnoreEnvironmentFlag = 1; + Py_FrozenFlag = 1; + Py_UnbufferedStdioFlag = 1; + Py_NoSiteFlag = 1; + Py_DontWriteBytecodeFlag = 1; + Py_NoUserSiteDirectory = 1; +#ifdef MS_WINDOWS + Py_LegacyWindowsStdioFlag = 1; +#endif + _PyPreConfig preconfig; _PyPreConfig_InitPythonConfig(&preconfig); @@ -788,11 +830,12 @@ static int init_python_config(void) } -static int init_dont_configure_locale(void) +static int test_init_dont_configure_locale(void) { _PyInitError err; - _PyPreConfig preconfig = _PyPreConfig_INIT; + _PyPreConfig preconfig; + _PyPreConfig_InitPythonConfig(&preconfig); preconfig.configure_locale = 0; preconfig.coerce_c_locale = 1; preconfig.coerce_c_locale_warn = 1; @@ -802,7 +845,8 @@ static int init_dont_configure_locale(void) _Py_ExitInitError(err); } - _PyCoreConfig config = _PyCoreConfig_INIT; + _PyCoreConfig config; + _PyCoreConfig_InitPythonConfig(&config); config.program_name = L"./_testembed"; err = _Py_InitializeFromConfig(&config); if (_PyInitError_Failed(err)) { @@ -815,10 +859,10 @@ static int init_dont_configure_locale(void) } -static int init_dev_mode(void) +static int test_init_dev_mode(void) { _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); putenv("PYTHONFAULTHANDLER="); putenv("PYTHONMALLOC="); config.dev_mode = 1; @@ -837,7 +881,7 @@ static int test_init_read_set(void) { _PyInitError err; _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); err = _PyCoreConfig_DecodeLocale(&config.program_name, "./init_read_set"); if (_PyInitError_Failed(err)) { @@ -894,7 +938,7 @@ static void configure_init_main(_PyCoreConfig *config) static int test_init_run_main(void) { _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); configure_init_main(&config); _PyInitError err = _Py_InitializeFromConfig(&config); @@ -909,7 +953,7 @@ static int test_init_run_main(void) static int test_init_main(void) { _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); configure_init_main(&config); config._init_main = 0; @@ -939,7 +983,7 @@ static int test_init_main(void) static int test_run_main(void) { _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); wchar_t *argv[] = {L"python3", L"-c", (L"import sys; " @@ -947,7 +991,6 @@ static int test_run_main(void) L"arg2"}; config.argv.length = Py_ARRAY_LENGTH(argv); config.argv.items = argv; - config.parse_argv = 1; config.program_name = L"./python3"; _PyInitError err = _Py_InitializeFromConfig(&config); @@ -988,16 +1031,16 @@ static struct TestCase TestCases[] = { { "init_default_config", test_init_default_config }, { "init_global_config", test_init_global_config }, { "init_from_config", test_init_from_config }, - { "init_parse_argv", init_parse_argv }, - { "init_dont_parse_argv", init_dont_parse_argv }, + { "init_parse_argv", test_init_parse_argv }, + { "init_dont_parse_argv", test_init_dont_parse_argv }, { "init_env", test_init_env }, { "init_env_dev_mode", test_init_env_dev_mode }, { "init_env_dev_mode_alloc", test_init_env_dev_mode_alloc }, - { "init_dont_configure_locale", init_dont_configure_locale }, - { "init_dev_mode", init_dev_mode }, - { "init_isolated_flag", init_isolated_flag }, - { "init_isolated_config", init_isolated_config }, - { "init_python_config", init_python_config }, + { "init_dont_configure_locale", test_init_dont_configure_locale }, + { "init_dev_mode", test_init_dev_mode }, + { "init_isolated_flag", test_init_isolated_flag }, + { "init_isolated_config", test_init_isolated_config }, + { "init_python_config", test_init_python_config }, { "preinit_isolated1", test_preinit_isolated1 }, { "preinit_isolated2", test_preinit_isolated2 }, { "init_read_set", test_init_read_set }, diff --git a/Python/coreconfig.c b/Python/coreconfig.c index fd457262a82..3678d124067 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -551,14 +551,69 @@ _PyCoreConfig_Clear(_PyCoreConfig *config) void _PyCoreConfig_Init(_PyCoreConfig *config) { - *config = _PyCoreConfig_INIT; + memset(config, 0, sizeof(*config)); + + config->_config_version = _Py_CONFIG_VERSION; + config->isolated = -1; + config->use_environment = -1; + config->dev_mode = -1; + config->install_signal_handlers = 1; + config->use_hash_seed = -1; + config->faulthandler = -1; + config->tracemalloc = -1; + config->use_module_search_paths = 0; + config->parse_argv = 0; + config->site_import = -1; + config->bytes_warning = -1; + config->inspect = -1; + config->interactive = -1; + config->optimization_level = -1; + config->parser_debug= -1; + config->write_bytecode = -1; + config->verbose = -1; + config->quiet = -1; + config->user_site_directory = -1; + config->configure_c_stdio = 0; + config->buffered_stdio = -1; + config->_install_importlib = 1; + config->check_hash_pycs_mode = NULL; + config->pathconfig_warnings = -1; + config->_init_main = 1; +#ifdef MS_WINDOWS + config->legacy_windows_stdio = -1; +#endif +} + + +static void +_PyCoreConfig_InitDefaults(_PyCoreConfig *config) +{ + _PyCoreConfig_Init(config); + + config->isolated = 0; + config->use_environment = 1; + config->site_import = 1; + config->bytes_warning = 0; + config->inspect = 0; + config->interactive = 0; + config->optimization_level = 0; + config->parser_debug= 0; + config->write_bytecode = 1; + config->verbose = 0; + config->quiet = 0; + config->user_site_directory = 1; + config->buffered_stdio = 1; + config->pathconfig_warnings = 1; +#ifdef MS_WINDOWS + config->legacy_windows_stdio = 0; +#endif } _PyInitError _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config) { - _PyCoreConfig_Init(config); + _PyCoreConfig_InitDefaults(config); config->configure_c_stdio = 1; config->parse_argv = 1; @@ -570,30 +625,16 @@ _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config) _PyInitError _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config) { - _PyCoreConfig_Init(config); + _PyCoreConfig_InitDefaults(config); - /* set to 1 */ config->isolated = 1; - config->site_import = 1; - config->write_bytecode = 1; - config->buffered_stdio = 1; - - /* set to 0 */ config->use_environment = 0; + config->user_site_directory = 0; config->dev_mode = 0; config->install_signal_handlers = 0; config->use_hash_seed = 0; config->faulthandler = 0; config->tracemalloc = 0; - config->bytes_warning = 0; - config->inspect = 0; - config->interactive = 0; - config->optimization_level = 0; - config->parser_debug = 0; - config->verbose = 0; - config->quiet = 0; - config->user_site_directory = 0; - config->configure_c_stdio = 0; config->pathconfig_warnings = 0; #ifdef MS_WINDOWS config->legacy_windows_stdio = 0; diff --git a/Python/frozenmain.c b/Python/frozenmain.c index 7b232bb8023..a51fb580012 100644 --- a/Python/frozenmain.c +++ b/Python/frozenmain.c @@ -40,7 +40,7 @@ Py_FrozenMain(int argc, char **argv) } _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitPythonConfig(&config); config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */ if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') diff --git a/Python/preconfig.c b/Python/preconfig.c index b7bcfeb9b2f..0f4bd8ece53 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -241,8 +241,9 @@ _PyPreCmdline_Read(_PyPreCmdline *cmdline, } /* dev_mode */ - if ((cmdline && _Py_get_xoption(&cmdline->xoptions, L"dev")) - || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE")) + if ((cmdline->dev_mode < 0) + && (_Py_get_xoption(&cmdline->xoptions, L"dev") + || _Py_GetEnv(cmdline->use_environment, "PYTHONDEVMODE"))) { cmdline->dev_mode = 1; } @@ -260,10 +261,22 @@ _PyPreCmdline_Read(_PyPreCmdline *cmdline, /* --- _PyPreConfig ----------------------------------------------- */ + void _PyPreConfig_Init(_PyPreConfig *config) { - *config = _PyPreConfig_INIT; + memset(config, 0, sizeof(*config)); + + config->_config_version = _Py_CONFIG_VERSION; + config->isolated = -1; + config->use_environment = -1; + config->configure_locale = 1; + config->utf8_mode = -2; + config->dev_mode = -1; + config->allocator = PYMEM_ALLOCATOR_NOT_SET; +#ifdef MS_WINDOWS + config->legacy_windows_fs_encoding = -1; +#endif } @@ -289,11 +302,11 @@ _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config) config->configure_locale = 0; config->isolated = 1; config->use_environment = 0; + config->utf8_mode = 0; + config->dev_mode = 0; #ifdef MS_WINDOWS config->legacy_windows_fs_encoding = 0; #endif - config->utf8_mode = 0; - config->dev_mode = 0; }