8000 bpo-36301: Add _PyRuntimeState.preconfig (GH-12506) · python/cpython@6d5ee97 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d5ee97

Browse files
authored
bpo-36301: Add _PyRuntimeState.preconfig (GH-12506)
_PyPreConfig_Write() now writes the applied pre-configuration into _PyRuntimeState.preconfig.
1 parent 2b75155 commit 6d5ee97

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

11+
#include "cpython/coreconfig.h"
1112
#include "pystate.h"
1213
#include "pythread.h"
1314

@@ -176,6 +177,7 @@ typedef struct pyruntimestate {
176177
struct _ceval_runtime_state ceval;
177178
struct _gilstate_runtime_state gilstate;
178179

180+
_PyPreConfig preconfig;
179181
// XXX Consolidate globals found via the check-c-globals script.
180182
} _PyRuntimeState;
181183

Python/coreconfig.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "pycore_getopt.h"
66
#include "pycore_pylifecycle.h"
77
#include "pycore_pymem.h"
8+
#include "pycore_pystate.h" /* _PyRuntime */
89
#include "pycore_pathconfig.h"
910
#include <locale.h> /* setlocale() */
1011
#ifdef HAVE_LANGINFO_H
@@ -1358,6 +1359,17 @@ _PyCoreConfig_ReadPreConfig(_PyCoreConfig *config)
13581359
}
13591360

13601361

1362+
static _PyInitError
1363+
_PyCoreConfig_GetPreConfig(_PyCoreConfig *config)
1364+
{
1365+
/* Read config written by _PyPreConfig_Write() */
1366+
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
1367+
return _Py_INIT_NO_MEMORY();
1368+
}
1369+
return _Py_INIT_OK();
1370+
}
1371+
1372+
13611373
/* Read the configuration into _PyCoreConfig from:
13621374
13631375
* Environment variables
@@ -1374,6 +1386,11 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
13741386
return err;
13751387
}
13761388

1389+
err = _PyCoreConfig_GetPreConfig(config);
1390+
if (_Py_INIT_FAILED(err)) {
1391+
return err;
1392+
}
1393+
13771394
_PyCoreConfig_GetGlobalConfig(config);
13781395

13791396
if (preconfig != NULL) {
@@ -2117,6 +2134,11 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
21172134
{
21182135
_PyInitError err;
21192136

2137+
err = _Py_PreInitialize();
2138+
if (_Py_INIT_FAILED(err)) {
2139+
return err;
2140+
}
2141+
21202142
_PyCmdline cmdline = {.precmdline = _PyPreCmdline_INIT};
21212143

21222144
err = _PyPreCmdline_Init(&cmdline.precmdline, args);

Python/preconfig.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,5 +862,14 @@ _PyPreConfig_Write(_PyPreConfig *config)
862862
/* Set LC_CTYPE to the user preferred locale */
863863
_Py_SetLocaleFromEnv(LC_CTYPE);
864864

865+
/* Write the new pre-configuration into _PyRuntime */
866+
PyMemAllocatorEx old_alloc;
867+
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
868+
int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, config);
869+
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
870+
if (res < 0) {
871+
return _Py_INIT_NO_MEMORY();
872+
}
873+
865874
return _Py_INIT_OK();
866875
}

Python/pystate.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ _PyRuntimeState_Init_impl(_PyRuntimeState *runtime)
4141

4242
_PyGC_Initialize(&runtime->gc);
4343
_PyEval_Initialize(&runtime->ceval);
44+
runtime->preconfig = _PyPreConfig_INIT;
4445

4546
runtime->gilstate.check_enabled = 1;
4647

@@ -97,6 +98,8 @@ _PyRuntimeState_Fini(_PyRuntimeState *runtime)
9798
runtime->xidregistry.mutex = NULL;
9899
}
99100

101+
_PyPreConfig_Clear(&runtime->preconfig);
102+
100103
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
101104
}
102105

0 commit comments

Comments
 (0)
0