diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h index 456dda2e8490830..aee87145f9762d7 100644 --- a/Include/internal/pycore_pystate.h +++ b/Include/internal/pycore_pystate.h @@ -8,6 +8,7 @@ extern "C" { # error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define" #endif +#include "cpython/coreconfig.h" #include "pystate.h" #include "pythread.h" @@ -220,6 +221,7 @@ typedef struct pyruntimestate { struct _ceval_runtime_state ceval; struct _gilstate_runtime_state gilstate; + _PyPreConfig preconfig; // XXX Consolidate globals found via the check-c-globals script. } _PyRuntimeState; diff --git a/Python/preconfig.c b/Python/preconfig.c index a86ece57cfced93..0520e4b96b4447e 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -839,5 +839,15 @@ _PyPreConfig_Write(_PyPreConfig *config) /* Set LC_CTYPE to the user preferred locale */ _Py_SetLocaleFromEnv(LC_CTYPE); + /* Write the new pre-configuration into _PyRuntime */ + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, config); + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + + if (res < 0) { + return _Py_INIT_NO_MEMORY(); + } + return _Py_INIT_OK(); } diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 0902508429a3e73..15fc55bd7f1f775 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -737,6 +737,11 @@ pyinit_coreconfig(_PyCoreConfig *config, const _PyCoreConfig *src_config, return _Py_INIT_ERR("failed to copy core config"); } + /* Read config written by _PyPreConfig_Write() */ + if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) { + return _Py_INIT_NO_MEMORY(); + } + _PyInitError err = _PyCoreConfig_Read(config, NULL); if (_Py_INIT_FAILED(err)) { return err; diff --git a/Python/pystate.c b/Python/pystate.c index 3978baa7af89d8a..bda075c46be91a5 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -41,6 +41,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) _PyGC_Initialize(&runtime->gc); _PyEval_Initialize(&runtime->ceval); + runtime->preconfig = _PyPreConfig_INIT; runtime->gilstate.check_enabled = 1; @@ -92,6 +93,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) runtime->interpreters.mutex = NULL; } + _PyPreConfig_Clear(&runtime->preconfig); + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); }