diff options
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 6 | ||||
-rw-r--r-- | Python/coreconfig.c | 174 | ||||
-rw-r--r-- | Python/import.c | 19 | ||||
-rw-r--r-- | Python/pathconfig.c | 2 | ||||
-rw-r--r-- | Python/preconfig.c | 80 | ||||
-rw-r--r-- | Python/pylifecycle.c | 60 | ||||
-rw-r--r-- | Python/sysmodule.c | 13 |
7 files changed, 221 insertions, 133 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 40320bf357..28e923219d 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -4948,7 +4948,7 @@ import_from(PyObject *v, PyObject *name) } x = PyImport_GetModule(fullmodname); Py_DECREF(fullmodname); - if (x == NULL) { + if (x == NULL && !PyErr_Occurred()) { goto error; } Py_DECREF(pkgname); @@ -4971,7 +4971,7 @@ import_from(PyObject *v, PyObject *name) "cannot import name %R from %R (unknown location)", name, pkgname_or_unknown ); - /* NULL check for errmsg done by PyErr_SetImportError. */ + /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ PyErr_SetImportError(errmsg, pkgname, NULL); } else { @@ -4979,7 +4979,7 @@ import_from(PyObject *v, PyObject *name) "cannot import name %R from %R (%S)", name, pkgname_or_unknown, pkgpath ); - /* NULL check for errmsg done by PyErr_SetImportError. */ + /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */ PyErr_SetImportError(errmsg, pkgname, pkgpath); } diff --git a/Python/coreconfig.c b/Python/coreconfig.c index ba5abb6ca4..2e6eb40298 100644 --- a/Python/coreconfig.c +++ b/Python/coreconfig.c @@ -131,7 +131,7 @@ int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */ #endif -PyObject * +static PyObject * _Py_GetGlobalVariablesAsDict(void) { PyObject *dict, *obj; @@ -469,8 +469,6 @@ Py_GetArgcArgv(int *argc, wchar_t ***argv) void _PyCoreConfig_Clear(_PyCoreConfig *config) { - _PyPreConfig_Clear(&config->preconfig); - #define CLEAR(ATTR) \ do { \ PyMem_RawFree(ATTR); \ @@ -514,10 +512,6 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2) { _PyCoreConfig_Clear(config); - if (_PyPreConfig_Copy(&config->preconfig, &config2->preconfig) < 0) { - return -1; - } - #define COPY_ATTR(ATTR) config->ATTR = config2->ATTR #define COPY_STR_ATTR(ATTR) \ do { \ @@ -544,6 +538,9 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2) } \ } while (0) + COPY_ATTR(isolated); + COPY_ATTR(use_environment); + COPY_ATTR(dev_mode); COPY_ATTR(install_signal_handlers); COPY_ATTR(use_hash_seed); COPY_ATTR(hash_seed); @@ -610,21 +607,21 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2) } -const char* +static const char* _PyCoreConfig_GetEnv(const _PyCoreConfig *config, const char *name) { - return _PyPreConfig_GetEnv(&config->preconfig, name); + return _Py_GetEnv(config->use_environment, name); } -int +static int _PyCoreConfig_GetEnvDup(const _PyCoreConfig *config, wchar_t **dest, wchar_t *wname, char *name) { - assert(config->preconfig.use_environment >= 0); + assert(config->use_environment >= 0); - if (!config->preconfig.use_environment) { + if (!config->use_environment) { *dest = NULL; return 0; } @@ -668,8 +665,6 @@ _PyCoreConfig_GetEnvDup(const _PyCoreConfig *config, void _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config) { - _PyPreConfig_GetGlobalConfig(&config->preconfig); - #define COPY_FLAG(ATTR, VALUE) \ if (config->ATTR == -1) { \ config->ATTR = VALUE; \ @@ -679,6 +674,8 @@ _PyCoreConfig_GetGlobalConfig(_PyCoreConfig *config) config->ATTR = !(VALUE); \ } + COPY_FLAG(isolated, Py_IsolatedFlag); + COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); COPY_FLAG(bytes_warning, Py_BytesWarningFlag); COPY_FLAG(inspect, Py_InspectFlag); COPY_FLAG(interactive, Py_InteractiveFlag); @@ -714,6 +711,8 @@ _PyCoreConfig_SetGlobalConfig(const _PyCoreConfig *config) VAR = !config->ATTR; \ } + COPY_FLAG(isolated, Py_IsolatedFlag); + COPY_NOT_FLAG(use_environment, Py_IgnoreEnvironmentFlag); COPY_FLAG(bytes_warning, Py_BytesWarningFlag); COPY_FLAG(inspect, Py_InspectFlag); COPY_FLAG(interactive, Py_InteractiveFlag); @@ -924,34 +923,34 @@ config_wstr_to_int(const wchar_t *wstr, int *result) static _PyInitError config_read_env_vars(_PyCoreConfig *config) { - _PyPreConfig *preconfig = &config->preconfig; + int use_env = config->use_environment; /* Get environment variables */ - _Py_get_env_flag(preconfig, &config->parser_debug, "PYTHONDEBUG"); - _Py_get_env_flag(preconfig, &config->verbose, "PYTHONVERBOSE"); - _Py_get_env_flag(preconfig, &config->optimization_level, "PYTHONOPTIMIZE"); - _Py_get_env_flag(preconfig, &config->inspect, "PYTHONINSPECT"); + _Py_get_env_flag(use_env, &config->parser_debug, "PYTHONDEBUG"); + _Py_get_env_flag(use_env, &config->verbose, "PYTHONVERBOSE"); + _Py_get_env_flag(use_env, &config->optimization_level, "PYTHONOPTIMIZE"); + _Py_get_env_flag(use_env, &config->inspect, "PYTHONINSPECT"); int dont_write_bytecode = 0; - _Py_get_env_flag(preconfig, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE"); + _Py_get_env_flag(use_env, &dont_write_bytecode, "PYTHONDONTWRITEBYTECODE"); if (dont_write_bytecode) { config->write_bytecode = 0; } int no_user_site_directory = 0; - _Py_get_env_flag(preconfig, &no_user_site_directory, "PYTHONNOUSERSITE"); + _Py_get_env_flag(use_env, &no_user_site_directory, "PYTHONNOUSERSITE"); if (no_user_site_directory) { config->user_site_directory = 0; } int unbuffered_stdio = 0; - _Py_get_env_flag(preconfig, &unbuffered_stdio, "PYTHONUNBUFFERED"); + _Py_get_env_flag(use_env, &unbuffered_stdio, "PYTHONUNBUFFERED"); if (unbuffered_stdio) { config->buffered_stdio = 0; } #ifdef MS_WINDOWS - _Py_get_env_flag(preconfig, &config->legacy_windows_stdio, + _Py_get_env_flag(use_env, &config->legacy_windows_stdio, "PYTHONLEGACYWINDOWSSTDIO"); #endif @@ -1149,7 +1148,8 @@ get_locale_encoding(char **locale_encoding) static _PyInitError -config_init_stdio_encoding(_PyCoreConfig *config) +config_init_stdio_encoding(_PyCoreConfig *config, + const _PyPreConfig *preconfig) { /* If Py_SetStandardStreamEncoding() have been called, use these parameters. */ @@ -1219,7 +1219,7 @@ config_init_stdio_encoding(_PyCoreConfig *config) } /* UTF-8 Mode uses UTF-8/surrogateescape */ - if (config->preconfig.utf8_mode) { + if (preconfig->utf8_mode) { if (config->stdio_encoding == NULL) { config->stdio_encoding = _PyMem_RawStrdup("utf-8"); if (config->stdio_encoding == NULL) { @@ -1254,10 +1254,10 @@ config_init_stdio_encoding(_PyCoreConfig *config) static _PyInitError -config_init_fs_encoding(_PyCoreConfig *config) +config_init_fs_encoding(_PyCoreConfig *config, const _PyPreConfig *preconfig) { #ifdef MS_WINDOWS - if (config->preconfig.legacy_windows_fs_encoding) { + if (preconfig->legacy_windows_fs_encoding) { /* Legacy Windows filesystem encoding: mbcs/replace */ if (config->filesystem_encoding == NULL) { config->filesystem_encoding = _PyMem_RawStrdup("mbcs"); @@ -1292,7 +1292,7 @@ config_init_fs_encoding(_PyCoreConfig *config) } #else if (config->filesystem_encoding == NULL) { - if (config->preconfig.utf8_mode) { + if (preconfig->utf8_mode) { /* UTF-8 Mode use: utf-8/surrogateescape */ config->filesystem_encoding = _PyMem_RawStrdup("utf-8"); /* errors defaults to surrogateescape above */ @@ -1341,12 +1341,8 @@ config_read_impl(_PyCoreConfig *config, _PyPreCmdline *cmdline) { _PyInitError err; - err = _Py_PreInitializeFromConfig(config); - if (_Py_INIT_FAILED(err)) { - return err; - } - - _PyPreCmdline_GetPreConfig(cmdline, &_PyRuntime.preconfig); + const _PyPreConfig *preconfig = &_PyRuntime.preconfig; + _PyPreCmdline_GetPreConfig(cmdline, preconfig); _PyPreCmdline_GetCoreConfig(cmdline, config); err = _PyPreCmdline_Read(cmdline); @@ -1360,19 +1356,16 @@ config_read_impl(_PyCoreConfig *config, _PyPreCmdline *cmdline) return _Py_INIT_NO_MEMORY(); } - if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) { - return _Py_INIT_NO_MEMORY(); - } - _PyCoreConfig_GetGlobalConfig(config); - assert(config->preconfig.use_environment >= 0); + assert(config->use_environment >= 0); - if (config->preconfig.isolated > 0) { + if (config->isolated > 0) { + config->use_environment = 0; config->user_site_directory = 0; } - if (config->preconfig.use_environment) { + if (config->use_environment) { err = config_read_env_vars(config); if (_Py_INIT_FAILED(err)) { return err; @@ -1421,7 +1414,7 @@ config_read_impl(_PyCoreConfig *config, _PyPreCmdline *cmdline) } /* default values */ - if (config->preconfig.dev_mode) { + if (config->dev_mode) { if (config->faulthandler < 0) { config->faulthandler = 1; } @@ -1438,13 +1431,13 @@ config_read_impl(_PyCoreConfig *config, _PyPreCmdline *cmdline) } if (config->filesystem_encoding == NULL || config->filesystem_errors == NULL) { - err = config_init_fs_encoding(config); + err = config_init_fs_encoding(config, preconfig); if (_Py_INIT_FAILED(err)) { return err; } } - err = config_init_stdio_encoding(config); + err = config_init_stdio_encoding(config, preconfig); if (_Py_INIT_FAILED(err)) { return err; } @@ -1456,7 +1449,7 @@ config_read_impl(_PyCoreConfig *config, _PyPreCmdline *cmdline) } } - assert(config->preconfig.use_environment >= 0); + assert(config->use_environment >= 0); assert(config->filesystem_encoding != NULL); assert(config->filesystem_errors != NULL); assert(config->stdio_encoding != NULL); @@ -1544,26 +1537,15 @@ config_init_stdio(const _PyCoreConfig *config) - set Py_xxx global configuration variables - initialize C standard streams (stdin, stdout, stderr) */ -_PyInitError +void _PyCoreConfig_Write(const _PyCoreConfig *config) { _PyCoreConfig_SetGlobalConfig(config); config_init_stdio(config); - - /* Write the new pre-configuration into _PyRuntime */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, &config->preconfig); - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - if (res < 0) { - return _Py_INIT_NO_MEMORY(); - } - - return _Py_INIT_OK(); } -PyObject * +static PyObject * _PyCoreConfig_AsDict(const _PyCoreConfig *config) { PyObject *dict; @@ -1573,11 +1555,6 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) return NULL; } - if (_PyPreConfig_AsDict(&config->preconfig, dict) < 0) { - Py_DECREF(dict); - return NULL; - } - #define SET_ITEM(KEY, EXPR) \ do { \ PyObject *obj = (EXPR); \ @@ -1609,6 +1586,9 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config) #define SET_ITEM_WSTRLIST(LIST) \ SET_ITEM(#LIST, _PyWstrList_AsList(&config->LIST)) + SET_ITEM_INT(isolated); + SET_ITEM_INT(use_environment); + SET_ITEM_INT(dev_mode); SET_ITEM_INT(install_signal_handlers); SET_ITEM_INT(use_hash_seed); SET_ITEM_UINT(hash_seed); @@ -1950,7 +1930,7 @@ config_init_warnoptions(_PyCoreConfig *config, const _PyCmdline *cmdline) * the lowest precedence entries first so that later entries override them. */ - if (config->preconfig.dev_mode) { + if (config->dev_mode) { if (_PyWstrList_Append(&config->warnoptions, L"default")) { return _Py_INIT_NO_MEMORY(); } @@ -2106,7 +2086,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline) return err; } - if (config->preconfig.use_environment) { + if (config->use_environment) { err = cmdline_init_env_warnoptions(cmdline, config); if (_Py_INIT_FAILED(err)) { return err; @@ -2158,3 +2138,67 @@ done: cmdline_clear(&cmdline); return err; } + + +PyObject* +_Py_GetConfigsAsDict(void) +{ + PyObject *config = NULL; + PyObject *dict = NULL; + + config = PyDict_New(); + if (config == NULL) { + goto error; + } + + /* global config */ + dict = _Py_GetGlobalVariablesAsDict(); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "global_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + /* pre config */ + PyInterpreterState *interp = _PyInterpreterState_Get(); + const _PyPreConfig *pre_config = &_PyRuntime.preconfig; + dict = _PyPreConfig_AsDict(pre_config); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "pre_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + /* core config */ + const _PyCoreConfig *core_config = _PyInterpreterState_GetCoreConfig(interp); + dict = _PyCoreConfig_AsDict(core_config); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "core_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + /* main config */ + const _PyMainInterpreterConfig *main_config = _PyInterpreterState_GetMainConfig(interp); + dict = _PyMainInterpreterConfig_AsDict(main_config); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(config, "main_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + + return config; + +error: + Py_XDECREF(config); + Py_XDECREF(dict); + return NULL; +} diff --git a/Python/import.c b/Python/import.c index bf3a99414f..c00c3aa640 100644 --- a/Python/import.c +++ b/Python/import.c @@ -966,11 +966,10 @@ exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object Py_DECREF(v); m = PyImport_GetModule(name); - if (m == NULL) { + if (m == NULL && !PyErr_Occurred()) { PyErr_Format(PyExc_ImportError, "Loaded module %R not found in sys.modules", name); - return NULL; } return m; @@ -1735,6 +1734,10 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, } mod = PyImport_GetModule(abs_name); + if (mod == NULL && PyErr_Occurred()) { + goto error; + } + if (mod != NULL && mod != Py_None) { _Py_IDENTIFIER(__spec__); _Py_IDENTIFIER(_lock_unlock_module); @@ -1810,9 +1813,11 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, final_mod = PyImport_GetModule(to_return); Py_DECREF(to_return); if (final_mod == NULL) { - PyErr_Format(PyExc_KeyError, - "%R not in sys.modules as expected", - to_return); + if (!PyErr_Occurred()) { + PyErr_Format(PyExc_KeyError, + "%R not in sys.modules as expected", + to_return); + } goto error; } } @@ -1875,6 +1880,10 @@ PyImport_ReloadModule(PyObject *m) PyObject *reloaded_module = NULL; PyObject *imp = _PyImport_GetModuleId(&PyId_imp); if (imp == NULL) { + if (PyErr_Occurred()) { + return NULL; + } + imp = PyImport_ImportModule("imp"); if (imp == NULL) { return NULL; diff --git a/Python/pathconfig.c b/Python/pathconfig.c index f0b13fd1b0..7fea7c3667 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -331,7 +331,7 @@ _PyCoreConfig_CalculatePathConfig(_PyCoreConfig *config) #endif if (path_config.isolated != -1) { - config->preconfig.isolated = path_config.isolated; + config->isolated = path_config.isolated; } if (path_config.site_import != -1) { config->site_import = path_config.site_import; diff --git a/Python/preconfig.c b/Python/preconfig.c index ac87a7a3c7..d336352d93 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -143,6 +143,23 @@ _PyPreCmdline_GetPreConfig(_PyPreCmdline *cmdline, const _PyPreConfig *config) COPY_ATTR(use_environment); COPY_ATTR(isolated); + COPY_ATTR(dev_mode); + +#undef COPY_ATTR +} + + +void +_PyPreCmdline_SetPreConfig(const _PyPreCmdline *cmdline, _PyPreConfig *config) +{ +#define COPY_ATTR(ATTR) \ + if (cmdline->ATTR != -1) { \ + config->ATTR = cmdline->ATTR; \ + } + + COPY_ATTR(use_environment); + COPY_ATTR(isolated); + COPY_ATTR(dev_mode); #undef COPY_ATTR } @@ -152,12 +169,13 @@ void _PyPreCmdline_GetCoreConfig(_PyPreCmdline *cmdline, const _PyCoreConfig *config) { #define COPY_ATTR(ATTR) \ - if (config->preconfig.ATTR != -1) { \ - cmdline->ATTR = config->preconfig.ATTR; \ + if (config->ATTR != -1) { \ + cmdline->ATTR = config->ATTR; \ } COPY_ATTR(use_environment); COPY_ATTR(isolated); + COPY_ATTR(dev_mode); #undef COPY_ATTR } @@ -167,12 +185,13 @@ void _PyPreCmdline_SetCoreConfig(const _PyPreCmdline *cmdline, _PyCoreConfig *config) { #define COPY_ATTR(ATTR) \ - if (config->preconfig.ATTR == -1 && cmdline->ATTR != -1) { \ - config->preconfig.ATTR = cmdline->ATTR; \ + if (config->ATTR == -1 && cmdline->ATTR != -1) { \ + config->ATTR = cmdline->ATTR; \ } COPY_ATTR(use_environment); COPY_ATTR(isolated); + COPY_ATTR(dev_mode); #undef COPY_ATTR } @@ -206,13 +225,13 @@ _PyPreConfig_Copy(_PyPreConfig *config, const _PyPreConfig *config2) COPY_ATTR(isolated); COPY_ATTR(use_environment); + COPY_ATTR(dev_mode); COPY_ATTR(coerce_c_locale); COPY_ATTR(coerce_c_locale_warn); #ifdef MS_WINDOWS COPY_ATTR(legacy_windows_fs_encoding); #endif COPY_ATTR(utf8_mode); - COPY_ATTR(dev_mode); COPY_STR_ATTR(allocator); #undef COPY_ATTR @@ -270,11 +289,11 @@ _PyPreConfig_SetGlobalConfig(const _PyPreConfig *config) const char* -_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name) +_Py_GetEnv(int use_environment, const char *name) { - assert(config->use_environment >= 0); + assert(use_environment >= 0); - if (!config->use_environment) { + if (!use_environment) { return NULL; } @@ -288,6 +307,13 @@ _PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name) } +static const char* +_PyPreConfig_GetEnv(const _PyPreConfig *config, const char *name) +{ + return _Py_GetEnv(config->use_environment, name); +} + + int _Py_str_to_int(const char *str, int *result) { @@ -307,9 +333,9 @@ _Py_str_to_int(const char *str, int *result) void -_Py_get_env_flag(_PyPreConfig *config, int *flag, const char *name) +_Py_get_env_flag(int use_environment, int *flag, const char *name) { - const char *var = _PyPreConfig_GetEnv(config, name); + const char *var = _Py_GetEnv(use_environment, name); if (!var) { return; } @@ -434,8 +460,9 @@ preconfig_read(_PyPreConfig *config, _PyPreCmdline *cmdline) /* legacy_windows_fs_encoding, utf8_mode, coerce_c_locale */ if (config->use_environment) { #ifdef MS_WINDOWS - _Py_get_env_flag(config, &config->legacy_windows_fs_encoding, - "PYTHONLEGACYWINDOWSFSENCODING"); + _Py_get_env_flag(config->use_environment, + &config->legacy_windows_fs_encoding, + "PYTHONLEGACYWINDOWSFSENCODING"); #endif const char *env = _PyPreConfig_GetEnv(config, "PYTHONCOERCECLOCALE"); @@ -559,24 +586,16 @@ get_ctype_locale(char **locale_p) } -void -_PyPreCmdline_SetPreConfig(const _PyPreCmdline *cmdline, _PyPreConfig *config) +PyObject* +_PyPreConfig_AsDict(const _PyPreConfig *config) { -#define COPY_ATTR(ATTR) \ - if (cmdline->ATTR != -1) { \ - config->ATTR = cmdline->ATTR; \ - } - - COPY_ATTR(use_environment); - COPY_ATTR(isolated); - -#undef COPY_ATTR -} + PyObject *dict; + dict = PyDict_New(); + if (dict == NULL) { + return NULL; + } -int -_PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) -{ #define SET_ITEM(KEY, EXPR) \ do { \ PyObject *obj = (EXPR); \ @@ -608,10 +627,11 @@ _PyPreConfig_AsDict(const _PyPreConfig *config, PyObject *dict) #endif SET_ITEM_INT(dev_mode); SET_ITEM_STR(allocator); - return 0; + return dict; fail: - return -1; + Py_DECREF(dict); + return NULL; #undef FROM_STRING #undef SET_ITEM @@ -696,7 +716,7 @@ _PyPreConfig_Read(_PyPreConfig *config, const _PyArgv *args, if (coreconfig) { _PyPreCmdline_GetCoreConfig(&cmdline, coreconfig); if (config->dev_mode == -1) { - config->dev_mode = coreconfig->preconfig.dev_mode; + config->dev_mode = coreconfig->dev_mode; } } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 66cadc99c7..b12fa820e9 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -286,9 +286,10 @@ static const char *_C_LOCALE_WARNING = "locales is recommended.\n"; static void -_emit_stderr_warning_for_legacy_locale(const _PyCoreConfig *core_config) +_emit_stderr_warning_for_legacy_locale(void) { - if (core_config->preconfig.coerce_c_locale_warn && _Py_LegacyLocaleDetected()) { + const _PyPreConfig *preconfig = &_PyRuntime.preconfig; + if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected()) { PySys_FormatStderr("%s", _C_LOCALE_WARNING); } } @@ -675,6 +676,8 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p, { PyInterpreterState *interp; + _PyCoreConfig_Write(core_config); + _PyInitError err = pycore_init_runtime(core_config); if (_Py_INIT_FAILED(err)) { return err; @@ -720,54 +723,64 @@ pyinit_preinit(_PyPreConfig *config, const _PyCoreConfig *coreconfig) { _PyInitError err; + _PyPreConfig local_config = _PyPreConfig_INIT; + if (!config) { + config = &local_config; + } err = _PyRuntime_Initialize(); if (_Py_INIT_FAILED(err)) { - return err; + goto done; } if (_PyRuntime.pre_initialized) { /* If it's already configured: ignored the new configuration */ - return _Py_INIT_OK(); - } - - if (!src_config && coreconfig) { - src_config = &coreconfig->preconfig; + err = _Py_INIT_OK(); + goto done; } if (src_config) { if (_PyPreConfig_Copy(config, src_config) < 0) { - return _Py_INIT_ERR("failed to copy pre config"); + err = _Py_INIT_ERR("failed to copy pre config"); + goto done; } } err = _PyPreConfig_Read(config, NULL, coreconfig); if (_Py_INIT_FAILED(err)) { - return err; + goto done; } err = _PyPreConfig_Write(config); if (_Py_INIT_FAILED(err)) { - return err; + goto done; } _PyRuntime.pre_initialized = 1; - return _Py_INIT_OK(); + err = _Py_INIT_OK(); + +done: + _PyPreConfig_Clear(&local_config); + return err; } _PyInitError _Py_PreInitialize(void) { - _PyPreConfig config = _PyPreConfig_INIT; - _PyInitError err = pyinit_preinit(&config, NULL, NULL); - _PyPreConfig_Clear(&config); - return err; + return pyinit_preinit(NULL, NULL, NULL); +} + + +_PyInitError +_Py_PreInitializeFromPreConfig(const _PyPreConfig *src_config) +{ + return pyinit_preinit(NULL, src_config, NULL); } _PyInitError -_Py_PreInitializeFromPreConfig(_PyPreConfig *config) +_Py_PreInitializeInPlace(_PyPreConfig *config) { return pyinit_preinit(config, NULL, NULL); } @@ -776,10 +789,7 @@ _Py_PreInitializeFromPreConfig(_PyPreConfig *config) _PyInitError _Py_PreInitializeFromConfig(const _PyCoreConfig *coreconfig) { - _PyPreConfig config = _PyPreConfig_INIT; - _PyInitError err = pyinit_preinit(&config, NULL, coreconfig); - _PyPreConfig_Clear(&config); - return err; + return pyinit_preinit(NULL, NULL, coreconfig); } @@ -964,7 +974,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp, } #ifndef MS_WINDOWS - _emit_stderr_warning_for_legacy_locale(core_config); + _emit_stderr_warning_for_legacy_locale(); #endif return _Py_INIT_OK(); @@ -2157,8 +2167,10 @@ wait_for_thread_shutdown(void) PyObject *result; PyObject *threading = _PyImport_GetModuleId(&PyId_threading); if (threading == NULL) { - /* threading not imported */ - PyErr_Clear(); + if (PyErr_Occurred()) { + PyErr_WriteUnraisable(NULL); + } + /* else: threading not imported */ return; } result = _PyObject_CallMethodId(threading, &PyId__shutdown, NULL); diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 4351a7fb37..12ec7d5918 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -283,7 +283,9 @@ sys_displayhook(PyObject *module, PyObject *o) builtins = _PyImport_GetModuleId(&PyId_builtins); if (builtins == NULL) { - PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_RuntimeError, "lost builtins module"); + } return NULL; } Py_DECREF(builtins); @@ -2156,6 +2158,7 @@ make_flags(void) { int pos = 0; PyObject *seq; + const _PyPreConfig *preconfig = &_PyRuntime.preconfig; const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; seq = PyStructSequence_New(&FlagsType); @@ -2172,16 +2175,16 @@ make_flags(void) SetFlag(!config->write_bytecode); SetFlag(!config->user_site_directory); SetFlag(!config->site_import); - SetFlag(!config->preconfig.use_environment); + SetFlag(!config->use_environment); SetFlag(config->verbose); /* SetFlag(saw_unbuffered_flag); */ /* SetFlag(skipfirstline); */ SetFlag(config->bytes_warning); SetFlag(config->quiet); SetFlag(config->use_hash_seed == 0 || config->hash_seed != 0); - SetFlag(config->preconfig.isolated); - PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->preconfig.dev_mode)); - SetFlag(config->preconfig.utf8_mode); + SetFlag(config->isolated); + PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(config->dev_mode)); + SetFlag(preconfig->utf8_mode); #undef SetFlag if (PyErr_Occurred()) { |