Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Include/cpython/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(

/* Initialization and finalization */

PyAPI_FUNC(_PyInitError) _Py_PreInitialize(void);
PyAPI_FUNC(_PyInitError) _Py_PreInitializeFromPreConfig(
const _PyPreConfig *config);
PyAPI_FUNC(_PyInitError) _Py_InitializeFromConfig(
const _PyCoreConfig *config);
PyAPI_FUNC(void) _Py_NO_RETURN _Py_ExitInitError(_PyInitError err);
Expand Down
6 changes: 2 additions & 4 deletions Include/internal/pycore_coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ PyAPI_FUNC(int) _PyCoreConfig_GetEnvDup(
wchar_t **dest,
wchar_t *wname,
char *name);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config,
const _PyPreConfig *preconfig);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config,
const _PyArgv *args,
const _PyPreConfig *preconfig);
const _PyArgv *args);
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config);

#ifdef __cplusplus
Expand Down
10 changes: 8 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -185,8 +186,9 @@ struct _gilstate_runtime_state {
/* Full Python runtime state */

typedef struct pyruntimestate {
int initialized;
int pre_initialized;
int core_initialized;
int initialized;
PyThreadState *finalizing;

struct pyinterpreters {
Expand Down Expand Up @@ -220,10 +222,14 @@ 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;

#define _PyRuntimeState_INIT {.initialized = 0, .core_initialized = 0}
#define _PyRuntimeState_INIT \
{.pre_initialized = 0, \
.core_initialized = 0, \
.initialized = 0}
/* Note: _PyRuntimeState_INIT sets other fields to 0/NULL */

PyAPI_DATA(_PyRuntimeState) _PyRuntime;
Expand Down
74 changes: 37 additions & 37 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,32 +283,50 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
/* --- pymain_init() ---------------------------------------------- */

static _PyInitError
preconfig_read_write(_PyPreConfig *config, const _PyArgv *args)
pymain_init_preconfig(const _PyArgv *args)
{
_PyPreConfig_GetGlobalConfig(config);
_PyInitError err;
_PyPreConfig config = _PyPreConfig_INIT;

_PyInitError err = _PyPreConfig_ReadFromArgv(config, args);
err = _PyPreConfig_ReadFromArgv(&config, args);
if (_Py_INIT_FAILED(err)) {
return err;
goto done;
}

err = _Py_PreInitializeFromPreConfig(&config);
if (_Py_INIT_FAILED(err)) {
goto done;
}

return _PyPreConfig_Write(config);
done:
_PyPreConfig_Clear(&config);
return err;
}


static _PyInitError
config_read_write(_PyCoreConfig *config, const _PyArgv *args,
const _PyPreConfig *preconfig)
pymain_init_coreconfig(const _PyArgv *args, PyInterpreterState **interp_p)
{
_PyCoreConfig_GetGlobalConfig(config);
_PyInitError err;
_PyCoreConfig config = _PyCoreConfig_INIT;

_PyInitError err = _PyCoreConfig_ReadFromArgv(config, args, preconfig);
err = _PyCoreConfig_ReadFromArgv(&config, args);
if (_Py_INIT_FAILED(err)) {
return err;
goto done;
}

_PyCoreConfig_Write(config);
return _Py_INIT_OK();
_PyCoreConfig_Write(&config);

err = _Py_InitializeCore(interp_p, &config);
if (_Py_INIT_FAILED(err)) {
goto done;
}

err = _Py_INIT_OK();

done:
_PyCoreConfig_Clear(&config);
return err;
}


Expand Down Expand Up @@ -350,40 +368,22 @@ pymain_init(const _PyArgv *args, PyInterpreterState **interp_p)
fedisableexcept(FE_OVERFLOW);
#endif

_PyPreConfig local_preconfig = _PyPreConfig_INIT;
_PyPreConfig *preconfig = &local_preconfig;

_PyCoreConfig local_config = _PyCoreConfig_INIT;
_PyCoreConfig *config = &local_config;

err = preconfig_read_write(preconfig, args);
if (_Py_INIT_FAILED(err)) {
goto done;
}

err = config_read_write(config, args, preconfig);
err = pymain_init_preconfig(args);
if (_Py_INIT_FAILED(err)) {
goto done;
return err;
}

PyInterpreterState *interp;
err = _Py_InitializeCore(&interp, config);
err = pymain_init_coreconfig(args, interp_p);
if (_Py_INIT_FAILED(err)) {
goto done;
return err;
}
*interp_p = interp;

err = pymain_init_python_main(interp);
err = pymain_init_python_main(*interp_p);
if (_Py_INIT_FAILED(err)) {
goto done;
return err;
}

err = _Py_INIT_OK();

done:
_PyPreConfig_Clear(preconfig);
_PyCoreConfig_Clear(config);
return err;
return _Py_INIT_OK();
}


Expand Down
64 changes: 17 additions & 47 deletions Python/coreconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include "pycore_coreconfig.h"
#include "pycore_fileutils.h"
#include "pycore_getopt.h"
#include "pycore_pathconfig.h"
#include "pycore_pylifecycle.h"
#include "pycore_pymem.h"
#include "pycore_pathconfig.h"
#include "pycore_pystate.h" /* _PyRuntime */
#include <locale.h> /* setlocale() */
#ifdef HAVE_LANGINFO_H
# include <langinfo.h> /* nl_langinfo(CODESET) */
Expand Down Expand Up @@ -1327,58 +1328,27 @@ config_init_fs_encoding(_PyCoreConfig *config)
}


static _PyInitError
_PyCoreConfig_ReadPreConfig(_PyCoreConfig *config)
{
_PyInitError err;
_PyPreConfig local_preconfig = _PyPreConfig_INIT;
_PyPreConfig_GetGlobalConfig(&local_preconfig);

if (_PyPreConfig_Copy(&local_preconfig, &config->preconfig) < 0) {
err = _Py_INIT_NO_MEMORY();
goto done;
}

err = _PyPreConfig_Read(&local_preconfig);
if (_Py_INIT_FAILED(err)) {
goto done;
}

if (_PyPreConfig_Copy(&config->preconfig, &local_preconfig) < 0) {
err = _Py_INIT_NO_MEMORY();
goto done;
}
err = _Py_INIT_OK();

done:
_PyPreConfig_Clear(&local_preconfig);
return err;
}


/* Read the configuration into _PyCoreConfig from:

* Environment variables
* Py_xxx global configuration variables

See _PyCoreConfig_ReadFromArgv() to parse also command line arguments. */
_PyInitError
_PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
_PyCoreConfig_Read(_PyCoreConfig *config)
{
_PyInitError err;

err = _Py_PreInitialize();
if (_Py_INIT_FAILED(err)) {
return err;
}

_PyCoreConfig_GetGlobalConfig(config);

if (preconfig != NULL) {
if (_PyPreConfig_Copy(&config->preconfig, preconfig) < 0) {
return _Py_INIT_NO_MEMORY();
}
}
else {
err = _PyCoreConfig_ReadPreConfig(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
/* Read pre-configuration written by _PyPreConfig_Write() */
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
return _Py_INIT_NO_MEMORY();
}

assert(config->preconfig.use_environment >= 0);
Expand Down Expand Up @@ -2019,12 +1989,13 @@ config_usage(int error, const wchar_t* program)

/* Parse command line options and environment variables. */
static _PyInitError
config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
const _PyPreConfig *preconfig)
config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline)
{
int need_usage = 0;
_PyInitError err;

_PyCoreConfig_GetGlobalConfig(config);

err = config_init_program(config, cmdline);
if (_Py_INIT_FAILED(err)) {
return err;
Expand Down Expand Up @@ -2056,7 +2027,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
return err;
}

err = _PyCoreConfig_Read(config, preconfig);
err = _PyCoreConfig_Read(config);
if (_Py_INIT_FAILED(err)) {
return err;
}
Expand Down Expand Up @@ -2086,8 +2057,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
* Environment variables
* Py_xxx global configuration variables */
_PyInitError
_PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
const _PyPreConfig *preconfig)
_PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args)
{
_PyInitError err;

Expand All @@ -2099,7 +2069,7 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,
goto done;
}

err = config_from_cmdline(config, &cmdline, preconfig);
err = config_from_cmdline(config, &cmdline);
if (_Py_INIT_FAILED(err)) {
goto done;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/pathconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ pathconfig_global_init(void)
_PyInitError err;
_PyCoreConfig config = _PyCoreConfig_INIT;

err = _PyCoreConfig_Read(&config, NULL);
err = _PyCoreConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}
Expand Down
12 changes: 12 additions & 0 deletions Python/preconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ _PyPreConfig_ReadFromArgv(_PyPreConfig *config, const _PyArgv *args)
int locale_coerced = 0;
int loops = 0;

_PyPreConfig_GetGlobalConfig(config);

err = get_ctype_locale(&init_ctype_locale);
if (_Py_INIT_FAILED(err)) {
goto done;
Expand Down Expand Up @@ -839,5 +841,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();
}
Loading