Commit 6d5ee973 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-36301: Add _PyRuntimeState.preconfig (GH-12506)

_PyPreConfig_Write() now writes the applied pre-configuration into
_PyRuntimeState.preconfig.
parent 2b751555
...@@ -8,6 +8,7 @@ extern "C" { ...@@ -8,6 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define" # error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
#endif #endif
#include "cpython/coreconfig.h"
#include "pystate.h" #include "pystate.h"
#include "pythread.h" #include "pythread.h"
...@@ -176,6 +177,7 @@ typedef struct pyruntimestate { ...@@ -176,6 +177,7 @@ typedef struct pyruntimestate {
struct _ceval_runtime_state ceval; struct _ceval_runtime_state ceval;
struct _gilstate_runtime_state gilstate; struct _gilstate_runtime_state gilstate;
_PyPreConfig preconfig;
// XXX Consolidate globals found via the check-c-globals script. // XXX Consolidate globals found via the check-c-globals script.
} _PyRuntimeState; } _PyRuntimeState;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "pycore_getopt.h" #include "pycore_getopt.h"
#include "pycore_pylifecycle.h" #include "pycore_pylifecycle.h"
#include "pycore_pymem.h" #include "pycore_pymem.h"
#include "pycore_pystate.h" /* _PyRuntime */
#include "pycore_pathconfig.h" #include "pycore_pathconfig.h"
#include <locale.h> /* setlocale() */ #include <locale.h> /* setlocale() */
#ifdef HAVE_LANGINFO_H #ifdef HAVE_LANGINFO_H
...@@ -1358,6 +1359,17 @@ done: ...@@ -1358,6 +1359,17 @@ done:
} }
static _PyInitError
_PyCoreConfig_GetPreConfig(_PyCoreConfig *config)
{
/* Read config written by _PyPreConfig_Write() */
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
return _Py_INIT_NO_MEMORY();
}
return _Py_INIT_OK();
}
/* Read the configuration into _PyCoreConfig from: /* Read the configuration into _PyCoreConfig from:
* Environment variables * Environment variables
...@@ -1374,6 +1386,11 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig) ...@@ -1374,6 +1386,11 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
return err; return err;
} }
err = _PyCoreConfig_GetPreConfig(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
_PyCoreConfig_GetGlobalConfig(config); _PyCoreConfig_GetGlobalConfig(config);
if (preconfig != NULL) { if (preconfig != NULL) {
...@@ -2117,6 +2134,11 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args, ...@@ -2117,6 +2134,11 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
{ {
_PyInitError err; _PyInitError err;
err = _Py_PreInitialize();
if (_Py_INIT_FAILED(err)) {
return err;
}
_PyCmdline cmdline = {.precmdline = _PyPreCmdline_INIT}; _PyCmdline cmdline = {.precmdline = _PyPreCmdline_INIT};
err = _PyPreCmdline_Init(&cmdline.precmdline, args); err = _PyPreCmdline_Init(&cmdline.precmdline, args);
......
...@@ -862,5 +862,14 @@ _PyPreConfig_Write(_PyPreConfig *config) ...@@ -862,5 +862,14 @@ _PyPreConfig_Write(_PyPreConfig *config)
/* Set LC_CTYPE to the user preferred locale */ /* Set LC_CTYPE to the user preferred locale */
_Py_SetLocaleFromEnv(LC_CTYPE); _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(); return _Py_INIT_OK();
} }
...@@ -41,6 +41,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime) ...@@ -41,6 +41,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
_PyGC_Initialize(&runtime->gc); _PyGC_Initialize(&runtime->gc);
_PyEval_Initialize(&runtime->ceval); _PyEval_Initialize(&runtime->ceval);
runtime->preconfig = _PyPreConfig_INIT;
runtime->gilstate.check_enabled = 1; runtime->gilstate.check_enabled = 1;
...@@ -97,6 +98,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime) ...@@ -97,6 +98,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
runtime->xidregistry.mutex = NULL; runtime->xidregistry.mutex = NULL;
} }
_PyPreConfig_Clear(&runtime->preconfig);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
} }
......
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