Skip to content
Merged
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
7 changes: 6 additions & 1 deletion Include/cpython/coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ typedef struct {
set to !Py_IgnoreEnvironmentFlag. */
int use_environment;

int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
/* PYTHONCOERCECLOCALE, -1 means unknown.

If it is equal to 1, LC_CTYPE locale is read to decide it it should be
coerced or not (ex: PYTHONCOERCECLOCALE=1). Internally, it is set to 2
if the LC_CTYPE locale must be coerced. */
int coerce_c_locale;
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */

#ifdef MS_WINDOWS
Expand Down
15 changes: 7 additions & 8 deletions Include/internal/pycore_coreconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ typedef struct {
/* Note: _PyPreCmdline_INIT sets other fields to 0/NULL */

PyAPI_FUNC(void) _PyPreCmdline_Clear(_PyPreCmdline *cmdline);
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Init(_PyPreCmdline *cmdline,
PyAPI_FUNC(_PyInitError) _PyPreCmdline_SetArgv(_PyPreCmdline *cmdline,
const _PyArgv *args);
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline);
PyAPI_FUNC(void) _PyPreCmdline_SetPreConfig(
const _PyPreCmdline *cmdline,
_PyPreConfig *config);
PyAPI_FUNC(_PyInitError) _PyPreCmdline_Read(_PyPreCmdline *cmdline);


/* --- _PyWstrList ------------------------------------------------ */
Expand Down Expand Up @@ -76,7 +76,8 @@ PyAPI_FUNC(const char*) _PyPreConfig_GetEnv(const _PyPreConfig *config,
PyAPI_FUNC(void) _Py_get_env_flag(_PyPreConfig *config,
int *flag,
const char *name);
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config);
PyAPI_FUNC(_PyInitError) _PyPreConfig_Read(_PyPreConfig *config,
const _PyArgv *args);
PyAPI_FUNC(int) _PyPreConfig_AsDict(const _PyPreConfig *config,
PyObject *dict);
PyAPI_FUNC(_PyInitError) _PyPreConfig_ReadFromArgv(_PyPreConfig *config,
Expand All @@ -103,12 +104,10 @@ 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);
PyAPI_FUNC(void) _PyCoreConfig_Write(const _PyCoreConfig *config);
const _PyArgv *args);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Write(const _PyCoreConfig *config);

#ifdef __cplusplus
}
Expand Down
41 changes: 23 additions & 18 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,28 +283,38 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
/* --- pymain_init() ---------------------------------------------- */

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

_PyPreConfig config = _PyPreConfig_INIT;

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

return _Py_PreInitializeFromPreConfig(config);
err = _Py_PreInitializeFromPreConfig(&config);

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


static _PyInitError
pymain_init_coreconfig(_PyCoreConfig *config, const _PyArgv *args,
const _PyPreConfig *preconfig,
PyInterpreterState **interp_p)
{
_PyInitError err = _PyCoreConfig_ReadFromArgv(config, args, preconfig);
_PyInitError err = _PyCoreConfig_ReadFromArgv(config, args);
if (_Py_INIT_FAILED(err)) {
return err;
}

_PyCoreConfig_Write(config);
err = _PyCoreConfig_Write(config);
if (_Py_INIT_FAILED(err)) {
return err;
}

return _Py_InitializeCore(interp_p, config);
}
Expand Down Expand Up @@ -348,18 +358,14 @@ 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 = pymain_init_preconfig(preconfig, args);
err = pymain_init_preconfig(args);
if (_Py_INIT_FAILED(err)) {
goto done;
return err;
}

err = pymain_init_coreconfig(config, args, preconfig, interp_p);
_PyCoreConfig config = _PyCoreConfig_INIT;

err = pymain_init_coreconfig(&config, args, interp_p);
if (_Py_INIT_FAILED(err)) {
goto done;
}
Expand All @@ -372,8 +378,7 @@ pymain_init(const _PyArgv *args, PyInterpreterState **interp_p)
err = _Py_INIT_OK();

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

Expand Down
86 changes: 21 additions & 65 deletions Python/coreconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -1330,54 +1330,14 @@ 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;
}


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:

* 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;

Expand All @@ -1386,25 +1346,12 @@ _PyCoreConfig_Read(_PyCoreConfig *config, const _PyPreConfig *preconfig)
return err;
}

err = _PyCoreConfig_GetPreConfig(config);
if (_Py_INIT_FAILED(err)) {
return err;
if (_PyPreConfig_Copy(&config->preconfig, &_PyRuntime.preconfig) < 0) {
return _Py_INIT_NO_MEMORY();
}

_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;
}
}

assert(config->preconfig.use_environment >= 0);

if (config->preconfig.isolated > 0) {
Expand Down Expand Up @@ -1548,11 +1495,22 @@ config_init_stdio(const _PyCoreConfig *config)

- set Py_xxx global configuration variables
- initialize C standard streams (stdin, stdout, stderr) */
void
_PyInitError
_PyCoreConfig_Write(const _PyCoreConfig *config)
{
_PyCoreConfig_SetGlobalConfig(config);
config_init_stdio(config);

/* Write the new pre-configuration into _PyRuntime */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
int res = _PyPreConfig_Copy(&_PyRuntime.preconfig, &config->preconfig);
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
if (res < 0) {
return _Py_INIT_NO_MEMORY();
}

return _Py_INIT_OK();
}


Expand Down Expand Up @@ -2047,8 +2005,7 @@ 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;
Expand All @@ -2067,7 +2024,7 @@ config_from_cmdline(_PyCoreConfig *config, _PyCmdline *cmdline,
return err;
}

_PyPreCmdline_SetPreConfig(&cmdline->precmdline, &config->preconfig);
_PyPreCmdline_SetPreConfig(&cmdline->precmdline, &_PyRuntime.preconfig);
if (_PyWstrList_Extend(&config->xoptions, &cmdline->precmdline.xoptions) < 0) {
return _Py_INIT_NO_MEMORY();
}
Expand Down Expand Up @@ -2098,7 +2055,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 @@ -2129,8 +2086,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 @@ -2141,12 +2097,12 @@ _PyCoreConfig_ReadFromArgv(_PyCoreConfig *config, const _PyArgv *args,

_PyCmdline cmdline = {.precmdline = _PyPreCmdline_INIT};

err = _PyPreCmdline_Init(&cmdline.precmdline, args);
err = _PyPreCmdline_SetArgv(&cmdline.precmdline, args);
if (_Py_INIT_FAILED(err)) {
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 @@ -394,7 +394,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
Loading