Commit 43125224 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36710: Add runtime variable to Py_InitializeEx() (GH-12939)

Py_InitializeEx() now uses a runtime variable passed to subfunctions,
rather than working directly on the global variable _PyRuntime.

Add 'runtime' parameter to _PyCoreConfig_Write(), _PySys_Create(),
_PySys_InitMain(), _PyGILState_Init(),
emit_stderr_warning_for_legacy_locale() and other subfunctions.
parent 8e91c246
...@@ -8,6 +8,8 @@ extern "C" { ...@@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define" # error "this header requires Py_BUILD_CORE define"
#endif #endif
#include "pycore_pystate.h" /* _PyRuntimeState */
/* --- _PyWstrList ------------------------------------------------ */ /* --- _PyWstrList ------------------------------------------------ */
...@@ -108,7 +110,8 @@ PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig( ...@@ -108,7 +110,8 @@ PyAPI_FUNC(_PyInitError) _PyCoreConfig_SetPathConfig(
const _PyCoreConfig *config); const _PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config, PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config,
const _PyArgv *args); const _PyArgv *args);
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config); PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config,
_PyRuntimeState *runtime);
/* --- Function used for testing ---------------------------------- */ /* --- Function used for testing ---------------------------------- */
......
...@@ -34,10 +34,13 @@ extern _PyInitError _PyFaulthandler_Init(int enable); ...@@ -34,10 +34,13 @@ extern _PyInitError _PyFaulthandler_Init(int enable);
extern int _PyTraceMalloc_Init(int enable); extern int _PyTraceMalloc_Init(int enable);
extern PyObject * _PyBuiltin_Init(void); extern PyObject * _PyBuiltin_Init(void);
extern _PyInitError _PySys_Create( extern _PyInitError _PySys_Create(
_PyRuntimeState *runtime,
PyInterpreterState *interp, PyInterpreterState *interp,
PyObject **sysmod_p); PyObject **sysmod_p);
extern _PyInitError _PySys_SetPreliminaryStderr(PyObject *sysdict); extern _PyInitError _PySys_SetPreliminaryStderr(PyObject *sysdict);
extern int _PySys_InitMain(PyInterpreterState *interp); extern int _PySys_InitMain(
_PyRuntimeState *runtime,
PyInterpreterState *interp);
extern _PyInitError _PyImport_Init(PyInterpreterState *interp); extern _PyInitError _PyImport_Init(PyInterpreterState *interp);
extern _PyInitError _PyExc_Init(void); extern _PyInitError _PyExc_Init(void);
extern _PyInitError _PyBuiltins_AddExceptions(PyObject * bltinmod); extern _PyInitError _PyBuiltins_AddExceptions(PyObject * bltinmod);
...@@ -74,7 +77,10 @@ extern void _PyFaulthandler_Fini(void); ...@@ -74,7 +77,10 @@ extern void _PyFaulthandler_Fini(void);
extern void _PyHash_Fini(void); extern void _PyHash_Fini(void);
extern int _PyTraceMalloc_Fini(void); extern int _PyTraceMalloc_Fini(void);
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); extern void _PyGILState_Init(
_PyRuntimeState *runtime,
PyInterpreterState *interp,
PyThreadState *tstate);
extern void _PyGILState_Fini(_PyRuntimeState *runtime); extern void _PyGILState_Fini(_PyRuntimeState *runtime);
PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void); PyAPI_FUNC(void) _PyGC_DumpShutdownStats(void);
......
...@@ -1605,13 +1605,13 @@ config_init_stdio(const _PyCoreConfig *config) ...@@ -1605,13 +1605,13 @@ config_init_stdio(const _PyCoreConfig *config)
- set Py_xxx global configuration variables - set Py_xxx global configuration variables
- initialize C standard streams (stdin, stdout, stderr) */ - initialize C standard streams (stdin, stdout, stderr) */
void void
_PyCoreConfig_Write(const _PyCoreConfig *config) _PyCoreConfig_Write(const _PyCoreConfig *config, _PyRuntimeState *runtime)
{ {
_PyCoreConfig_SetGlobalConfig(config); _PyCoreConfig_SetGlobalConfig(config);
config_init_stdio(config); config_init_stdio(config);
/* Write the new pre-configuration into _PyRuntime */ /* Write the new pre-configuration into _PyRuntime */
_PyPreConfig *preconfig = &_PyRuntime.preconfig; _PyPreConfig *preconfig = &runtime->preconfig;
preconfig->isolated = config->isolated; preconfig->isolated = config->isolated;
preconfig->use_environment = config->use_environment; preconfig->use_environment = config->use_environment;
preconfig->dev_mode = config->dev_mode; preconfig->dev_mode = config->dev_mode;
......
...@@ -286,9 +286,9 @@ static const char *_C_LOCALE_WARNING = ...@@ -286,9 +286,9 @@ static const char *_C_LOCALE_WARNING =
"locales is recommended.\n"; "locales is recommended.\n";
static void static void
_emit_stderr_warning_for_legacy_locale(void) emit_stderr_warning_for_legacy_locale(_PyRuntimeState *runtime)
{ {
const _PyPreConfig *preconfig = &_PyRuntime.preconfig; const _PyPreConfig *preconfig = &runtime->preconfig;
if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected()) { if (preconfig->coerce_c_locale_warn && _Py_LegacyLocaleDetected()) {
PySys_FormatStderr("%s", _C_LOCALE_WARNING); PySys_FormatStderr("%s", _C_LOCALE_WARNING);
} }
...@@ -468,7 +468,8 @@ _Py_SetLocaleFromEnv(int category) ...@@ -468,7 +468,8 @@ _Py_SetLocaleFromEnv(int category)
*/ */
static _PyInitError static _PyInitError
_Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p, _Py_Initialize_ReconfigureCore(_PyRuntimeState *runtime,
PyInterpreterState **interp_p,
const _PyCoreConfig *core_config) const _PyCoreConfig *core_config)
{ {
PyThreadState *tstate = _PyThreadState_GET(); PyThreadState *tstate = _PyThreadState_GET();
...@@ -482,7 +483,7 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p, ...@@ -482,7 +483,7 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p,
} }
*interp_p = interp; *interp_p = interp;
_PyCoreConfig_Write(core_config); _PyCoreConfig_Write(core_config, runtime);
if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) { if (_PyCoreConfig_Copy(&interp->core_config, core_config) < 0) {
return _Py_INIT_NO_MEMORY(); return _Py_INIT_NO_MEMORY();
...@@ -500,18 +501,14 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p, ...@@ -500,18 +501,14 @@ _Py_Initialize_ReconfigureCore(PyInterpreterState **interp_p,
static _PyInitError static _PyInitError
pycore_init_runtime(const _PyCoreConfig *core_config) pycore_init_runtime(_PyRuntimeState *runtime,
const _PyCoreConfig *core_config)
{ {
if (_PyRuntime.initialized) { if (runtime->initialized) {
return _Py_INIT_ERR("main interpreter already initialized"); return _Py_INIT_ERR("main interpreter already initialized");
} }
_PyCoreConfig_Write(core_config); _PyCoreConfig_Write(core_config, runtime);
_PyInitError err = _PyRuntime_Initialize();
if (_Py_INIT_FAILED(err)) {
return err;
}
/* Py_Finalize leaves _Py_Finalizing set in order to help daemon /* Py_Finalize leaves _Py_Finalizing set in order to help daemon
* threads behave a little more gracefully at interpreter shutdown. * threads behave a little more gracefully at interpreter shutdown.
...@@ -522,14 +519,14 @@ pycore_init_runtime(const _PyCoreConfig *core_config) ...@@ -522,14 +519,14 @@ pycore_init_runtime(const _PyCoreConfig *core_config)
* threads still hanging around from a previous Py_Initialize/Finalize * threads still hanging around from a previous Py_Initialize/Finalize
* pair :( * pair :(
*/ */
_PyRuntime.finalizing = NULL; runtime->finalizing = NULL;
err = _Py_HashRandomization_Init(core_config); _PyInitError err = _Py_HashRandomization_Init(core_config);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
err = _PyInterpreterState_Enable(&_PyRuntime); err = _PyInterpreterState_Enable(runtime);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
...@@ -538,7 +535,8 @@ pycore_init_runtime(const _PyCoreConfig *core_config) ...@@ -538,7 +535,8 @@ pycore_init_runtime(const _PyCoreConfig *core_config)
static _PyInitError static _PyInitError
pycore_create_interpreter(const _PyCoreConfig *core_config, pycore_create_interpreter(_PyRuntimeState *runtime,
const _PyCoreConfig *core_config,
PyInterpreterState **interp_p) PyInterpreterState **interp_p)
{ {
PyInterpreterState *interp = PyInterpreterState_New(); PyInterpreterState *interp = PyInterpreterState_New();
...@@ -565,7 +563,7 @@ pycore_create_interpreter(const _PyCoreConfig *core_config, ...@@ -565,7 +563,7 @@ pycore_create_interpreter(const _PyCoreConfig *core_config,
_PyEval_FiniThreads(); _PyEval_FiniThreads();
/* Auto-thread-state API */ /* Auto-thread-state API */
_PyGILState_Init(interp, tstate); _PyGILState_Init(runtime, interp, tstate);
/* Create the GIL */ /* Create the GIL */
PyEval_InitThreads(); PyEval_InitThreads();
...@@ -671,19 +669,20 @@ pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod) ...@@ -671,19 +669,20 @@ pycore_init_import_warnings(PyInterpreterState *interp, PyObject *sysmod)
static _PyInitError static _PyInitError
_Py_InitializeCore_impl(PyInterpreterState **interp_p, _Py_InitializeCore_impl(_PyRuntimeState *runtime,
PyInterpreterState **interp_p,
const _PyCoreConfig *core_config) const _PyCoreConfig *core_config)
{ {
PyInterpreterState *interp; PyInterpreterState *interp;
_PyCoreConfig_Write(core_config); _PyCoreConfig_Write(core_config, runtime);
_PyInitError err = pycore_init_runtime(core_config); _PyInitError err = pycore_init_runtime(runtime, core_config);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
err = pycore_create_interpreter(core_config, &interp); err = pycore_create_interpreter(runtime, core_config, &interp);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
...@@ -696,7 +695,7 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p, ...@@ -696,7 +695,7 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
} }
PyObject *sysmod; PyObject *sysmod;
err = _PySys_Create(interp, &sysmod); err = _PySys_Create(runtime, interp, &sysmod);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
...@@ -712,7 +711,7 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p, ...@@ -712,7 +711,7 @@ _Py_InitializeCore_impl(PyInterpreterState **interp_p,
} }
/* Only when we get here is the runtime core fully initialized */ /* Only when we get here is the runtime core fully initialized */
_PyRuntime.core_initialized = 1; runtime->core_initialized = 1;
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
...@@ -726,8 +725,9 @@ preinit(const _PyPreConfig *src_config, const _PyArgv *args) ...@@ -726,8 +725,9 @@ preinit(const _PyPreConfig *src_config, const _PyArgv *args)
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
_PyRuntimeState *runtime = &_PyRuntime;
if (_PyRuntime.pre_initialized) { if (runtime->pre_initialized) {
/* If it's already configured: ignored the new configuration */ /* If it's already configured: ignored the new configuration */
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
...@@ -751,7 +751,7 @@ preinit(const _PyPreConfig *src_config, const _PyArgv *args) ...@@ -751,7 +751,7 @@ preinit(const _PyPreConfig *src_config, const _PyArgv *args)
goto done; goto done;
} }
_PyRuntime.pre_initialized = 1; runtime->pre_initialized = 1;
err = _Py_INIT_OK(); err = _Py_INIT_OK();
done: done:
...@@ -795,7 +795,8 @@ _Py_PreInitializeFromCoreConfig(const _PyCoreConfig *coreconfig) ...@@ -795,7 +795,8 @@ _Py_PreInitializeFromCoreConfig(const _PyCoreConfig *coreconfig)
static _PyInitError static _PyInitError
pyinit_coreconfig(_PyCoreConfig *config, pyinit_coreconfig(_PyRuntimeState *runtime,
_PyCoreConfig *config,
const _PyCoreConfig *src_config, const _PyCoreConfig *src_config,
const _PyArgv *args, const _PyArgv *args,
PyInterpreterState **interp_p) PyInterpreterState **interp_p)
...@@ -811,11 +812,11 @@ pyinit_coreconfig(_PyCoreConfig *config, ...@@ -811,11 +812,11 @@ pyinit_coreconfig(_PyCoreConfig *config,
return err; return err;
} }
if (!_PyRuntime.core_initialized) { if (!runtime->core_initialized) {
return _Py_InitializeCore_impl(interp_p, config); return _Py_InitializeCore_impl(runtime, interp_p, config);
} }
else { else {
return _Py_Initialize_ReconfigureCore(interp_p, config); return _Py_Initialize_ReconfigureCore(runtime, interp_p, config);
} }
} }
...@@ -838,7 +839,8 @@ pyinit_coreconfig(_PyCoreConfig *config, ...@@ -838,7 +839,8 @@ pyinit_coreconfig(_PyCoreConfig *config,
* safe to call without calling Py_Initialize first) * safe to call without calling Py_Initialize first)
*/ */
static _PyInitError static _PyInitError
_Py_InitializeCore(const _PyCoreConfig *src_config, _Py_InitializeCore(_PyRuntimeState *runtime,
const _PyCoreConfig *src_config,
const _PyArgv *args, const _PyArgv *args,
PyInterpreterState **interp_p) PyInterpreterState **interp_p)
{ {
...@@ -855,7 +857,7 @@ _Py_InitializeCore(const _PyCoreConfig *src_config, ...@@ -855,7 +857,7 @@ _Py_InitializeCore(const _PyCoreConfig *src_config,
} }
_PyCoreConfig local_config = _PyCoreConfig_INIT; _PyCoreConfig local_config = _PyCoreConfig_INIT;
err = pyinit_coreconfig(&local_config, src_config, args, interp_p); err = pyinit_coreconfig(runtime, &local_config, src_config, args, interp_p);
_PyCoreConfig_Clear(&local_config); _PyCoreConfig_Clear(&local_config);
return err; return err;
} }
...@@ -894,16 +896,17 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp) ...@@ -894,16 +896,17 @@ _Py_ReconfigureMainInterpreter(PyInterpreterState *interp)
* non-zero return code. * non-zero return code.
*/ */
static _PyInitError static _PyInitError
_Py_InitializeMainInterpreter(PyInterpreterState *interp) _Py_InitializeMainInterpreter(_PyRuntimeState *runtime,
PyInterpreterState *interp)
{ {
if (!_PyRuntime.core_initialized) { if (!runtime->core_initialized) {
return _Py_INIT_ERR("runtime core not initialized"); return _Py_INIT_ERR("runtime core not initialized");
} }
/* Configure the main interpreter */ /* Configure the main interpreter */
_PyCoreConfig *core_config = &interp->core_config; _PyCoreConfig *core_config = &interp->core_config;
if (_PyRuntime.initialized) { if (runtime->initialized) {
return _Py_ReconfigureMainInterpreter(interp); return _Py_ReconfigureMainInterpreter(interp);
} }
...@@ -913,7 +916,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp) ...@@ -913,7 +916,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp)
* This means anything which needs support from extension modules * This means anything which needs support from extension modules
* or pure Python code in the standard library won't work. * or pure Python code in the standard library won't work.
*/ */
_PyRuntime.initialized = 1; runtime->initialized = 1;
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
...@@ -921,7 +924,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp) ...@@ -921,7 +924,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp)
return _Py_INIT_ERR("can't initialize time"); return _Py_INIT_ERR("can't initialize time");
} }
if (_PySys_InitMain(interp) < 0) { if (_PySys_InitMain(runtime, interp) < 0) {
return _Py_INIT_ERR("can't finish initializing sys"); return _Py_INIT_ERR("can't finish initializing sys");
} }
...@@ -974,7 +977,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp) ...@@ -974,7 +977,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp)
Py_XDECREF(warnings_module); Py_XDECREF(warnings_module);
} }
_PyRuntime.initialized = 1; runtime->initialized = 1;
if (core_config->site_import) { if (core_config->site_import) {
err = initsite(); /* Module site */ err = initsite(); /* Module site */
...@@ -984,7 +987,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp) ...@@ -984,7 +987,7 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp)
} }
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
_emit_stderr_warning_for_legacy_locale(); emit_stderr_warning_for_legacy_locale(runtime);
#endif #endif
return _Py_INIT_OK(); return _Py_INIT_OK();
...@@ -995,16 +998,23 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp) ...@@ -995,16 +998,23 @@ _Py_InitializeMainInterpreter(PyInterpreterState *interp)
static _PyInitError static _PyInitError
init_python(const _PyCoreConfig *config, const _PyArgv *args) init_python(const _PyCoreConfig *config, const _PyArgv *args)
{ {
PyInterpreterState *interp = NULL;
_PyInitError err; _PyInitError err;
err = _Py_InitializeCore(config, args, &interp);
err = _PyRuntime_Initialize();
if (_Py_INIT_FAILED(err)) {
return err;
}
_PyRuntimeState *runtime = &_PyRuntime;
PyInterpreterState *interp = NULL;
err = _Py_InitializeCore(runtime, config, args, &interp);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
config = &interp->core_config; config = &interp->core_config;
if (config->_init_main) { if (config->_init_main) {
err = _Py_InitializeMainInterpreter(interp); err = _Py_InitializeMainInterpreter(runtime, interp);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
...@@ -1040,7 +1050,15 @@ _Py_InitializeFromConfig(const _PyCoreConfig *config) ...@@ -1040,7 +1050,15 @@ _Py_InitializeFromConfig(const _PyCoreConfig *config)
void void
Py_InitializeEx(int install_sigs) Py_InitializeEx(int install_sigs)
{ {
if (_PyRuntime.initialized) { _PyInitError err;
err = _PyRuntime_Initialize();
if (_Py_INIT_FAILED(err)) {
_Py_ExitInitError(err);
}
_PyRuntimeState *runtime = &_PyRuntime;
if (runtime->initialized) {
/* bpo-33932: Calling Py_Initialize() twice does nothing. */ /* bpo-33932: Calling Py_Initialize() twice does nothing. */
return; return;
} }
...@@ -1048,7 +1066,7 @@ Py_InitializeEx(int install_sigs) ...@@ -1048,7 +1066,7 @@ Py_InitializeEx(int install_sigs)
_PyCoreConfig config = _PyCoreConfig_INIT; _PyCoreConfig config = _PyCoreConfig_INIT;
config.install_signal_handlers = install_sigs; config.install_signal_handlers = install_sigs;
_PyInitError err = _Py_InitializeFromConfig(&config); err = _Py_InitializeFromConfig(&config);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
_Py_ExitInitError(err); _Py_ExitInitError(err);
} }
...@@ -1364,12 +1382,15 @@ Py_Finalize(void) ...@@ -1364,12 +1382,15 @@ Py_Finalize(void)
static _PyInitError static _PyInitError
new_interpreter(PyThreadState **tstate_p) new_interpreter(PyThreadState **tstate_p)
{ {
PyInterpreterState *interp;
PyThreadState *tstate, *save_tstate;
PyObject *bimod, *sysmod;
_PyInitError err; _PyInitError err;
if (!_PyRuntime.initialized) { err = _PyRuntime_Initialize();
if (_Py_INIT_FAILED(err)) {
return err;
}
_PyRuntimeState *runtime = &_PyRuntime;
if (!runtime->initialized) {
return _Py_INIT_ERR("Py_Initialize must be called first"); return _Py_INIT_ERR("Py_Initialize must be called first");
} }
...@@ -1377,20 +1398,20 @@ new_interpreter(PyThreadState **tstate_p) ...@@ -1377,20 +1398,20 @@ new_interpreter(PyThreadState **tstate_p)
interpreters: disable PyGILState_Check(). */ interpreters: disable PyGILState_Check(). */
_PyGILState_check_enabled = 0; _PyGILState_check_enabled = 0;
interp = PyInterpreterState_New(); PyInterpreterState *interp = PyInterpreterState_New();
if (interp == NULL) { if (interp == NULL) {
*tstate_p = NULL; *tstate_p = NULL;
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
tstate = PyThreadState_New(interp); PyThreadState *tstate = PyThreadState_New(interp);
if (tstate == NULL) { if (tstate == NULL) {
PyInterpreterState_Delete(interp); PyInterpreterState_Delete(interp);
*tstate_p = NULL; *tstate_p = NULL;
return _Py_INIT_OK(); return _Py_INIT_OK();
} }
save_tstate = PyThreadState_Swap(tstate); PyThreadState *save_tstate = PyThreadState_Swap(tstate);
/* Copy the current interpreter config into the new interpreter */ /* Copy the current interpreter config into the new interpreter */
_PyCoreConfig *core_config; _PyCoreConfig *core_config;
...@@ -1419,14 +1440,15 @@ new_interpreter(PyThreadState **tstate_p) ...@@ -1419,14 +1440,15 @@ new_interpreter(PyThreadState **tstate_p)
} }
interp->modules = modules; interp->modules = modules;
sysmod = _PyImport_FindBuiltin("sys", modules); PyObject *sysmod = _PyImport_FindBuiltin("sys", modules);
if (sysmod != NULL) { if (sysmod != NULL) {
interp->sysdict = PyModule_GetDict(sysmod); interp->sysdict = PyModule_GetDict(sysmod);
if (interp->sysdict == NULL) if (interp->sysdict == NULL) {
goto handle_error; goto handle_error;
}
Py_INCREF(interp->sysdict); Py_INCREF(interp->sysdict);
PyDict_SetItemString(interp->sysdict, "modules", modules); PyDict_SetItemString(interp->sysdict, "modules", modules);
if (_PySys_InitMain(interp) < 0) { if (_PySys_InitMain(runtime, interp) < 0) {
return _Py_INIT_ERR("can't finish initializing sys"); return _Py_INIT_ERR("can't finish initializing sys");
} }
} }
...@@ -1434,7 +1456,7 @@ new_interpreter(PyThreadState **tstate_p) ...@@ -1434,7 +1456,7 @@ new_interpreter(PyThreadState **tstate_p)
goto handle_error; goto handle_error;
} }
bimod = _PyImport_FindBuiltin("builtins", modules); PyObject *bimod = _PyImport_FindBuiltin("builtins", modules);
if (bimod != NULL) { if (bimod != NULL) {
interp->builtins = PyModule_GetDict(bimod); interp->builtins = PyModule_GetDict(bimod);
if (interp->builtins == NULL) if (interp->builtins == NULL)
......
...@@ -1052,13 +1052,13 @@ PyThreadState_IsCurrent(PyThreadState *tstate) ...@@ -1052,13 +1052,13 @@ PyThreadState_IsCurrent(PyThreadState *tstate)
Py_Initialize/Py_FinalizeEx Py_Initialize/Py_FinalizeEx
*/ */
void void
_PyGILState_Init(PyInterpreterState *interp, PyThreadState *tstate) _PyGILState_Init(_PyRuntimeState *runtime,
PyInterpreterState *interp, PyThreadState *tstate)
{ {
/* must init with valid states */ /* must init with valid states */
assert(interp != NULL); assert(interp != NULL);
assert(tstate != NULL); assert(tstate != NULL);
_PyRuntimeState *runtime = &_PyRuntime;
struct _gilstate_runtime_state *gilstate = &runtime->gilstate; struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) { if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
......
...@@ -2155,12 +2155,12 @@ static PyStructSequence_Desc flags_desc = { ...@@ -2155,12 +2155,12 @@ static PyStructSequence_Desc flags_desc = {
}; };
static PyObject* static PyObject*
make_flags(void) make_flags(_PyRuntimeState *runtime, PyInterpreterState *interp)
{ {
int pos = 0; int pos = 0;
PyObject *seq; PyObject *seq;
const _PyPreConfig *preconfig = &_PyRuntime.preconfig; const _PyPreConfig *preconfig = &runtime->preconfig;
const _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; const _PyCoreConfig *config = &interp->core_config;
seq = PyStructSequence_New(&FlagsType); seq = PyStructSequence_New(&FlagsType);
if (seq == NULL) if (seq == NULL)
...@@ -2375,7 +2375,8 @@ static struct PyModuleDef sysmodule = { ...@@ -2375,7 +2375,8 @@ static struct PyModuleDef sysmodule = {
} while (0) } while (0)
static _PyInitError static _PyInitError
_PySys_InitCore(PyObject *sysdict) _PySys_InitCore(_PyRuntimeState *runtime, PyInterpreterState *interp,
PyObject *sysdict)
{ {
PyObject *version_info; PyObject *version_info;
int res; int res;
...@@ -2465,8 +2466,8 @@ _PySys_InitCore(PyObject *sysdict) ...@@ -2465,8 +2466,8 @@ _PySys_InitCore(PyObject *sysdict)
goto type_init_failed; goto type_init_failed;
} }
} }
/* Set flags to their default values */ /* Set flags to their default values (updated by _PySys_InitMain()) */
SET_SYS_FROM_STRING("flags", make_flags()); SET_SYS_FROM_STRING("flags", make_flags(runtime, interp));
#if defined(MS_WINDOWS) #if defined(MS_WINDOWS)
/* getwindowsversion */ /* getwindowsversion */
...@@ -2587,7 +2588,7 @@ sys_create_xoptions_dict(const _PyCoreConfig *config) ...@@ -2587,7 +2588,7 @@ sys_create_xoptions_dict(const _PyCoreConfig *config)
int int
_PySys_InitMain(PyInterpreterState *interp) _PySys_InitMain(_PyRuntimeState *runtime, PyInterpreterState *interp)
{ {
PyObject *sysdict = interp->sysdict; PyObject *sysdict = interp->sysdict;
const _PyCoreConfig *config = &interp->core_config; const _PyCoreConfig *config = &interp->core_config;
...@@ -2641,7 +2642,7 @@ _PySys_InitMain(PyInterpreterState *interp) ...@@ -2641,7 +2642,7 @@ _PySys_InitMain(PyInterpreterState *interp)
#undef SET_SYS_FROM_WSTR #undef SET_SYS_FROM_WSTR
/* Set flags to their final values */ /* Set flags to their final values */
SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags()); SET_SYS_FROM_STRING_INT_RESULT("flags", make_flags(runtime, interp));
/* prevent user from creating new instances */ /* prevent user from creating new instances */
FlagsType.tp_init = NULL; FlagsType.tp_init = NULL;
FlagsType.tp_new = NULL; FlagsType.tp_new = NULL;
...@@ -2708,7 +2709,8 @@ error: ...@@ -2708,7 +2709,8 @@ error:
/* Create sys module without all attributes: _PySys_InitMain() should be called /* Create sys module without all attributes: _PySys_InitMain() should be called
later to add remaining attributes. */ later to add remaining attributes. */
_PyInitError _PyInitError
_PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p) _PySys_Create(_PyRuntimeState *runtime, PyInterpreterState *interp,
PyObject **sysmod_p)
{ {
PyObject *modules = PyDict_New(); PyObject *modules = PyDict_New();
if (modules == NULL) { if (modules == NULL) {
...@@ -2737,7 +2739,7 @@ _PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p) ...@@ -2737,7 +2739,7 @@ _PySys_Create(PyInterpreterState *interp, PyObject **sysmod_p)
return err; return err;
} }
err = _PySys_InitCore(sysdict); err = _PySys_InitCore(runtime, interp, sysdict);
if (_Py_INIT_FAILED(err)) { if (_Py_INIT_FAILED(err)) {
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment