diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-05-18 03:21:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-18 03:21:27 +0200 |
commit | bab0db6076900cd828588be8595b3cdfade7e7e9 (patch) | |
tree | 4506d3bcfa77235fd79a66563098ef2066962819 /Programs | |
parent | Fix couple of dead code paths (GH-7418) (diff) | |
download | cpython-bab0db6076900cd828588be8595b3cdfade7e7e9.tar.gz cpython-bab0db6076900cd828588be8595b3cdfade7e7e9.tar.bz2 cpython-bab0db6076900cd828588be8595b3cdfade7e7e9.zip |
bpo-36763: Use _PyCoreConfig_InitPythonConfig() (GH-13398)
_PyPreConfig_InitPythonConfig() and _PyCoreConfig_InitPythonConfig()
no longer inherit their values from global configuration variables.
Changes:
* _PyPreCmdline_Read() now ignores -X dev and PYTHONDEVMODE
if dev_mode is already set.
* Inline _PyPreConfig_INIT macro into _PyPreConfig_Init() function.
* Inline _PyCoreConfig_INIT macro into _PyCoreConfig_Init() function.
* Replace _PyCoreConfig_Init() with _PyCoreConfig_InitPythonConfig()
in most tests of _testembed.c.
* Replace _PyCoreConfig_Init() with _PyCoreConfig_InitIsolatedConfig()
in _freeze_importlib.c.
* Move some initialization functions from the internal
to the private API.
Diffstat (limited to 'Programs')
-rw-r--r-- | Programs/_freeze_importlib.c | 6 | ||||
-rw-r--r-- | Programs/_testembed.c | 111 |
2 files changed, 79 insertions, 38 deletions
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 <Python.h> #include "pycore_coreconfig.h" /* FIXME: PEP 587 makes these functions public */ +#include <Python.h> #include "pythread.h" #include <inttypes.h> #include <stdio.h> @@ -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 }, |