diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-05-22 21:58:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-22 21:58:50 (GMT) |
commit | 022be02dcfdfd9011415804bb4553a33fa7ec8f3 (patch) | |
tree | 6c4f3b4afed8475e6504786cb176044eade9ef02 /Python | |
parent | e4d300e07c33a9a77549c62d8687d8fe130c53d5 (diff) | |
download | cpython-022be02dcfdfd9011415804bb4553a33fa7ec8f3.zip cpython-022be02dcfdfd9011415804bb4553a33fa7ec8f3.tar.gz cpython-022be02dcfdfd9011415804bb4553a33fa7ec8f3.tar.bz2 |
bpo-36763: Add _PyPreConfig._config_init (GH-13481)
* _PyPreConfig_GetGlobalConfig() and _PyCoreConfig_GetGlobalConfig()
now do nothing if the configuration was not initialized with
_PyPreConfig_InitCompatConfig() and _PyCoreConfig_InitCompatConfig()
* Remove utf8_mode=-2 special case: use utf8_mode=-1 instead.
* Fix _PyPreConfig_InitPythonConfig():
* isolated = 0 instead of -1
* use_environment = 1 instead of -1
* Rename _PyConfig_INIT to _PyConfig_INIT_COMPAT
* Rename _PyPreConfig_Init() to _PyPreConfig_InitCompatConfig()
* Rename _PyCoreConfig_Init() to _PyCoreConfig_InitCompatConfig()
* PyInterpreterState_New() now uses _PyCoreConfig_InitPythonConfig()
as default configuration, but it's very quickly overriden anyway.
* _freeze_importlib.c uses _PyCoreConfig_SetString() to set
program_name.
* Cleanup preconfig_init_utf8_mode(): cmdline is always non-NULL.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/coreconfig.c | 17 | ||||
-rw-r--r-- | Python/frozenmain.c | 9 | ||||
-rw-r--r-- | Python/pathconfig.c | 2 | ||||
-rw-r--r-- | Python/preconfig.c | 44 | ||||
-rw-r--r-- | Python/pylifecycle.c | 4 | ||||
-rw-r--r-- | Python/pystate.c | 10 |
6 files changed, 53 insertions, 33 deletions
diff --git a/Python/coreconfig.c b/Python/coreconfig.c index 958845e..40dba4e 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -109,7 +109,7 @@ static const char usage_6[] = /* UTF-8 mode (PEP 540): if equals to 1, use the UTF-8 encoding, and change stdin and stdout error handler to "surrogateescape". It is equal to -1 by default: unknown, will be set by Py_Main() */ -int Py_UTF8Mode = 0; +int Py_UTF8Mode = -1; int Py_DebugFlag = 0; /* Needed by parser.c */ int Py_VerboseFlag = 0; /* Needed by import.c */ int Py_QuietFlag = 0; /* Needed by sysmodule.c */ @@ -546,12 +546,12 @@ _PyCoreConfig_Clear(_PyCoreConfig *config) void -_PyCoreConfig_Init(_PyCoreConfig *config) +_PyCoreConfig_InitCompatConfig(_PyCoreConfig *config) { memset(config, 0, sizeof(*config)); config->_config_version = _Py_CONFIG_VERSION; - config->_config_init = (int)_PyCoreConfig_INIT; + config->_config_init = (int)_PyConfig_INIT_COMPAT; config->isolated = -1; config->use_environment = -1; config->dev_mode = -1; @@ -586,7 +586,7 @@ _PyCoreConfig_Init(_PyCoreConfig *config) static void _PyCoreConfig_InitDefaults(_PyCoreConfig *config) { - _PyCoreConfig_Init(config); + _PyCoreConfig_InitCompatConfig(config); config->isolated = 0; config->use_environment = 1; @@ -613,7 +613,7 @@ _PyCoreConfig_InitPythonConfig(_PyCoreConfig *config) { _PyCoreConfig_InitDefaults(config); - config->_config_init = (int)_PyCoreConfig_INIT_PYTHON; + config->_config_init = (int)_PyConfig_INIT_PYTHON; config->configure_c_stdio = 1; config->parse_argv = 1; @@ -626,7 +626,7 @@ _PyCoreConfig_InitIsolatedConfig(_PyCoreConfig *config) { _PyCoreConfig_InitDefaults(config); - config->_config_init = (int)_PyCoreConfig_INIT_ISOLATED; + config->_config_init = (int)_PyConfig_INIT_ISOLATED; config->isolated = 1; config->use_environment = 0; config->user_site_directory = 0; @@ -962,6 +962,11 @@ _PyCoreConfig_GetEnvDup(_PyCoreConfig *config, static void _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config) { + if (config->_config_init != _PyConfig_INIT_COMPAT) { + /* Python and Isolated configuration ignore global variables */ + return; + } + #define COPY_FLAG(ATTR, VALUE) \ if (config->ATTR == -1) { \ config->ATTR = VALUE; \ diff --git a/Python/frozenmain.c b/Python/frozenmain.c index a51fb58..c3af080 100644 --- a/Python/frozenmain.c +++ b/Python/frozenmain.c @@ -40,7 +40,11 @@ Py_FrozenMain(int argc, char **argv) } _PyCoreConfig config; - _PyCoreConfig_InitPythonConfig(&config); + err = _PyCoreConfig_InitPythonConfig(&config); + if (_PyInitError_Failed(err)) { + _PyCoreConfig_Clear(&config); + _Py_ExitInitError(err); + } config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */ if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') @@ -82,8 +86,7 @@ Py_FrozenMain(int argc, char **argv) Py_SetProgramName(argv_copy[0]); err = _Py_InitializeFromConfig(&config); - /* No need to call _PyCoreConfig_Clear() since we didn't allocate any - memory: program_name is a constant string. */ + _PyCoreConfig_Clear(&config); if (_PyInitError_Failed(err)) { _Py_ExitInitError(err); } diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 3d9d3b1..bbf29b2 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -383,7 +383,7 @@ pathconfig_global_init(void) _PyInitError err; _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitCompatConfig(&config); err = _PyCoreConfig_Read(&config); if (_Py_INIT_FAILED(err)) { diff --git a/Python/preconfig.c b/Python/preconfig.c index 4df6208..392324f 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -262,16 +262,17 @@ _PyPreCmdline_Read(_PyPreCmdline *cmdline, const _PyPreConfig *preconfig) void -_PyPreConfig_Init(_PyPreConfig *config) +_PyPreConfig_InitCompatConfig(_PyPreConfig *config) { memset(config, 0, sizeof(*config)); config->_config_version = _Py_CONFIG_VERSION; + config->_config_init = (int)_PyConfig_INIT_COMPAT; config->parse_argv = 0; config->isolated = -1; config->use_environment = -1; config->configure_locale = 1; - config->utf8_mode = -2; + config->utf8_mode = -1; config->dev_mode = -1; config->allocator = PYMEM_ALLOCATOR_NOT_SET; #ifdef MS_WINDOWS @@ -283,23 +284,30 @@ _PyPreConfig_Init(_PyPreConfig *config) void _PyPreConfig_InitPythonConfig(_PyPreConfig *config) { - _PyPreConfig_Init(config); + _PyPreConfig_InitCompatConfig(config); + config->_config_init = (int)_PyConfig_INIT_PYTHON; + config->isolated = 0; config->parse_argv = 1; + config->use_environment = 1; /* Set to -1 to enable C locale coercion (PEP 538) and UTF-8 Mode (PEP 540) depending on the LC_CTYPE locale, PYTHONUTF8 and PYTHONCOERCECLOCALE environment variables. */ config->coerce_c_locale = -1; config->coerce_c_locale_warn = -1; config->utf8_mode = -1; +#ifdef MS_WINDOWS + config->legacy_windows_fs_encoding = 0; +#endif } void _PyPreConfig_InitIsolatedConfig(_PyPreConfig *config) { - _PyPreConfig_Init(config); + _PyPreConfig_InitCompatConfig(config); + config->_config_init = (int)_PyConfig_INIT_ISOLATED; config->configure_locale = 0; config->isolated = 1; config->use_environment = 0; @@ -315,7 +323,7 @@ void _PyPreConfig_InitFromPreConfig(_PyPreConfig *config, const _PyPreConfig *config2) { - _PyPreConfig_Init(config); + _PyPreConfig_InitCompatConfig(config); _PyPreConfig_Copy(config, config2); } @@ -324,17 +332,17 @@ void _PyPreConfig_InitFromCoreConfig(_PyPreConfig *config, const _PyCoreConfig *coreconfig) { - _PyCoreConfigInitEnum config_init = (_PyCoreConfigInitEnum)coreconfig->_config_init; + _PyConfigInitEnum config_init = (_PyConfigInitEnum)coreconfig->_config_init; switch (config_init) { - case _PyCoreConfig_INIT_PYTHON: + case _PyConfig_INIT_PYTHON: _PyPreConfig_InitPythonConfig(config); break; - case _PyCoreConfig_INIT_ISOLATED: + case _PyConfig_INIT_ISOLATED: _PyPreConfig_InitIsolatedConfig(config); break; - case _PyCoreConfig_INIT: + case _PyConfig_INIT_COMPAT: default: - _PyPreConfig_Init(config); + _PyPreConfig_InitCompatConfig(config); } _PyPreConfig_GetCoreConfig(config, coreconfig); } @@ -428,6 +436,11 @@ _PyPreConfig_GetCoreConfig(_PyPreConfig *config, static void _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) { + if (config->_config_init != _PyConfig_INIT_COMPAT) { + /* Python and Isolated configuration ignore global variables */ + return; + } + #define COPY_FLAG(ATTR, VALUE) \ if (config->ATTR < 0) { \ config->ATTR = VALUE; \ @@ -439,12 +452,10 @@ _PyPreConfig_GetGlobalConfig(_PyPreConfig *config) COPY_FLAG(isolated, Py_IsolatedFlag); COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); + COPY_FLAG(utf8_mode, Py_UTF8Mode); #ifdef MS_WINDOWS COPY_FLAG(legacy_windows_fs_encoding, Py_LegacyWindowsFSEncodingFlag); #endif - if (config->utf8_mode == -2) { - config->utf8_mode = Py_UTF8Mode; - } #undef COPY_FLAG #undef COPY_NOT_FLAG @@ -565,12 +576,7 @@ preconfig_init_utf8_mode(_PyPreConfig *config, const _PyPreCmdline *cmdline) } const wchar_t *xopt; - if (cmdline) { - xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8"); - } - else { - xopt = NULL; - } + xopt = _Py_get_xoption(&cmdline->xoptions, L"utf8"); if (xopt) { wchar_t *sep = wcschr(xopt, L'='); if (sep) { diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 6dc684b..21c386b 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -867,7 +867,7 @@ _Py_InitializeCore(_PyRuntimeState *runtime, } _PyCoreConfig local_config; - _PyCoreConfig_Init(&local_config); + _PyCoreConfig_InitCompatConfig(&local_config); err = pyinit_coreconfig(runtime, &local_config, src_config, args, interp_p); _PyCoreConfig_Clear(&local_config); return err; @@ -1096,7 +1096,7 @@ Py_InitializeEx(int install_sigs) } _PyCoreConfig config; - _PyCoreConfig_Init(&config); + _PyCoreConfig_InitCompatConfig(&config); config.install_signal_handlers = install_sigs; err = _Py_InitializeFromConfig(&config); diff --git a/Python/pystate.c b/Python/pystate.c index 2f80aa2..879a5a9 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -49,7 +49,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) _PyGC_Initialize(&runtime->gc); _PyEval_Initialize(&runtime->ceval); - _PyPreConfig_Init(&runtime->preconfig); + _PyPreConfig_InitPythonConfig(&runtime->preconfig); runtime->gilstate.check_enabled = 1; @@ -189,7 +189,13 @@ PyInterpreterState_New(void) memset(interp, 0, sizeof(*interp)); interp->id_refcount = -1; interp->check_interval = 100; - _PyCoreConfig_Init(&interp->core_config); + + _PyInitError err = _PyCoreConfig_InitPythonConfig(&interp->core_config); + if (_Py_INIT_FAILED(err)) { + PyMem_RawFree(interp); + return NULL; + } + interp->eval_frame = _PyEval_EvalFrameDefault; #ifdef HAVE_DLOPEN #if HAVE_DECL_RTLD_NOW |