bpo-36301: Add _PyRuntimeState.preconfig (GH-12506) · py-user/cpython@6d5ee97 · GitHub
Skip to content

Commit 6d5ee97

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

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

Include/internal/pycore_pystate.h

Lines changed: 2 additions & 0 deletions

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

0 commit comments

Comments
 (0)