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;
......
This diff is collapsed.
...@@ -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