Commit b5fd9ad0 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

bpo-32030: Rewrite _PyMainInterpreterConfig (#4854)

_PyMainInterpreterConfig now contains Python objects, whereas
_PyCoreConfig contains wchar_t* strings.

Core config:

* Rename _PyMainInterpreterConfig_ReadEnv() to _PyCoreConfig_ReadEnv()
* Move 3 strings from _PyMainInterpreterConfig to _PyCoreConfig:
  module_search_path_env, home, program_name.
* Add _PyCoreConfig_Clear()
* _PyPathConfig_Calculate() now takes core config rather than main
  config
* _PyMainInterpreterConfig_Read() now requires also a core config

Main config:

* Add _PyMainInterpreterConfig.module_search_path: sys.path list
* Add _PyMainInterpreterConfig.argv: sys.argv list
* _PyMainInterpreterConfig_Read() now computes module_search_path
parent 176baa32
......@@ -61,7 +61,7 @@ PyAPI_DATA(_PyPathConfig) _Py_path_config;
PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate(
_PyPathConfig *config,
const _PyMainInterpreterConfig *main_config);
const _PyCoreConfig *core_config);
PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);
......
......@@ -54,8 +54,11 @@ PyAPI_FUNC(int) Py_SetStandardStreamEncoding(const char *encoding,
PyAPI_FUNC(_PyInitError) _Py_InitializeCore(const _PyCoreConfig *);
PyAPI_FUNC(int) _Py_IsCoreInitialized(void);
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *);
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_ReadEnv(_PyCoreConfig *);
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *);
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);
PyAPI_FUNC(_PyInitError) _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *, _PyCoreConfig *);
PyAPI_FUNC(void) _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *);
PyAPI_FUNC(_PyInitError) _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *);
......@@ -103,8 +106,7 @@ PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
#ifdef Py_BUILD_CORE
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(
const _PyMainInterpreterConfig *main_config);
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
#endif
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
......
......@@ -39,6 +39,11 @@ typedef struct {
int dump_refs; /* PYTHONDUMPREFS */
int malloc_stats; /* PYTHONMALLOCSTATS */
int utf8_mode; /* -X utf8 or PYTHONUTF8 environment variable */
wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
wchar_t *home; /* PYTHONHOME environment variable,
see also Py_SetPythonHome(). */
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
} _PyCoreConfig;
#define _PyCoreConfig_INIT (_PyCoreConfig){.use_hash_seed = -1}
......@@ -52,12 +57,8 @@ typedef struct {
*/
typedef struct {
int install_signal_handlers;
/* PYTHONPATH environment variable */
wchar_t *module_search_path_env;
/* PYTHONHOME environment variable, see also Py_SetPythonHome(). */
wchar_t *home;
/* Program name, see also Py_GetProgramName() */
wchar_t *program_name;
PyObject *argv; /* sys.argv list, can be NULL */
PyObject *module_search_path; /* sys.path list */
} _PyMainInterpreterConfig;
#define _PyMainInterpreterConfig_INIT \
......
......@@ -356,15 +356,15 @@ find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
bytes long.
*/
static int
search_for_prefix(const _PyMainInterpreterConfig *main_config,
search_for_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *prefix)
{
size_t n;
wchar_t *vpath;
/* If PYTHONHOME is set, we believe it unconditionally */
if (main_config->home) {
wcsncpy(prefix, main_config->home, MAXPATHLEN);
if (core_config->home) {
wcsncpy(prefix, core_config->home, MAXPATHLEN);
prefix[MAXPATHLEN] = L'\0';
wchar_t *delim = wcschr(prefix, DELIM);
if (delim) {
......@@ -423,10 +423,10 @@ search_for_prefix(const _PyMainInterpreterConfig *main_config,
static void
calculate_prefix(const _PyMainInterpreterConfig *main_config,
calculate_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *prefix)
{
calculate->prefix_found = search_for_prefix(main_config, calculate, prefix);
calculate->prefix_found = search_for_prefix(core_config, calculate, prefix);
if (!calculate->prefix_found) {
if (!Py_FrozenFlag) {
fprintf(stderr,
......@@ -468,19 +468,19 @@ calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix)
MAXPATHLEN bytes long.
*/
static int
search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
search_for_exec_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *exec_prefix)
{
size_t n;
/* If PYTHONHOME is set, we believe it unconditionally */
if (main_config->home) {
wchar_t *delim = wcschr(main_config->home, DELIM);
if (core_config->home) {
wchar_t *delim = wcschr(core_config->home, DELIM);
if (delim) {
wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
}
else {
wcsncpy(exec_prefix, main_config->home, MAXPATHLEN);
wcsncpy(exec_prefix, core_config->home, MAXPATHLEN);
}
exec_prefix[MAXPATHLEN] = L'\0';
joinpath(exec_prefix, calculate->lib_python);
......@@ -551,10 +551,10 @@ search_for_exec_prefix(const _PyMainInterpreterConfig *main_config,
static void
calculate_exec_prefix(const _PyMainInterpreterConfig *main_config,
calculate_exec_prefix(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, wchar_t *exec_prefix)
{
calculate->exec_prefix_found = search_for_exec_prefix(main_config,
calculate->exec_prefix_found = search_for_exec_prefix(core_config,
calculate,
exec_prefix);
if (!calculate->exec_prefix_found) {
......@@ -587,7 +587,7 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
static _PyInitError
calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
calculate_program_full_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
wchar_t program_full_path[MAXPATHLEN+1];
......@@ -607,8 +607,8 @@ calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
* other way to find a directory to start the search from. If
* $PATH isn't exported, you lose.
*/
if (wcschr(main_config->program_name, SEP)) {
wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
if (wcschr(core_config->program_name, SEP)) {
wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
}
#ifdef __APPLE__
/* On Mac OS X, if a script uses an interpreter of the form
......@@ -650,7 +650,7 @@ calculate_program_full_path(const _PyMainInterpreterConfig *main_config,
wcsncpy(program_full_path, path, MAXPATHLEN);
}
joinpath(program_full_path, main_config->program_name);
joinpath(program_full_path, core_config->program_name);
if (isxfile(program_full_path)) {
break;
}
......@@ -815,15 +815,15 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
static _PyInitError
calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
calculate_module_search_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate,
const wchar_t *prefix, const wchar_t *exec_prefix,
_PyPathConfig *config)
{
/* Calculate size of return buffer */
size_t bufsz = 0;
if (main_config->module_search_path_env != NULL) {
bufsz += wcslen(main_config->module_search_path_env) + 1;
if (core_config->module_search_path_env != NULL) {
bufsz += wcslen(core_config->module_search_path_env) + 1;
}
wchar_t *defpath = calculate->pythonpath;
......@@ -857,8 +857,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
buf[0] = '\0';
/* Run-time value of $PYTHONPATH goes first */
if (main_config->module_search_path_env) {
wcscpy(buf, main_config->module_search_path_env);
if (core_config->module_search_path_env) {
wcscpy(buf, core_config->module_search_path_env);
wcscat(buf, delimiter);
}
......@@ -907,7 +907,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
static _PyInitError
calculate_init(PyCalculatePath *calculate,
const _PyMainInterpreterConfig *main_config)
const _PyCoreConfig *core_config)
{
size_t len;
const char *path = getenv("PATH");
......@@ -950,12 +950,12 @@ calculate_free(PyCalculatePath *calculate)
static _PyInitError
calculate_path_impl(const _PyMainInterpreterConfig *main_config,
calculate_path_impl(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
_PyInitError err;
err = calculate_program_full_path(main_config, calculate, config);
err = calculate_program_full_path(core_config, calculate, config);
if (_Py_INIT_FAILED(err)) {
return err;
}
......@@ -969,13 +969,13 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
wchar_t prefix[MAXPATHLEN+1];
memset(prefix, 0, sizeof(prefix));
calculate_prefix(main_config, calculate, prefix);
calculate_prefix(core_config, calculate, prefix);
calculate_zip_path(calculate, prefix);
wchar_t exec_prefix[MAXPATHLEN+1];
memset(exec_prefix, 0, sizeof(exec_prefix));
calculate_exec_prefix(main_config, calculate, exec_prefix);
calculate_exec_prefix(core_config, calculate, exec_prefix);
if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
!Py_FrozenFlag)
......@@ -984,7 +984,7 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
"Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
}
err = calculate_module_search_path(main_config, calculate,
err = calculate_module_search_path(core_config, calculate,
prefix, exec_prefix, config);
if (_Py_INIT_FAILED(err)) {
return err;
......@@ -1009,18 +1009,17 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
_PyInitError
_PyPathConfig_Calculate(_PyPathConfig *config,
const _PyMainInterpreterConfig *main_config)
_PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config)
{
PyCalculatePath calculate;
memset(&calculate, 0, sizeof(calculate));
_PyInitError err = calculate_init(&calculate, main_config);
_PyInitError err = calculate_init(&calculate, core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
err = calculate_path_impl(main_config, &calculate, config);
err = calculate_path_impl(core_config, &calculate, config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
......
This diff is collapsed.
......@@ -498,7 +498,7 @@ get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config)
static _PyInitError
get_program_full_path(const _PyMainInterpreterConfig *main_config,
get_program_full_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
wchar_t program_full_path[MAXPATHLEN+1];
......@@ -514,13 +514,13 @@ get_program_full_path(const _PyMainInterpreterConfig *main_config,
* $PATH isn't exported, you lose.
*/
#ifdef ALTSEP
if (wcschr(main_config->program_name, SEP) ||
wcschr(main_config->program_name, ALTSEP))
if (wcschr(core_config->program_name, SEP) ||
wcschr(core_config->program_name, ALTSEP))
#else
if (wcschr(main_config->program_name, SEP))
if (wcschr(core_config->program_name, SEP))
#endif
{
wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
}
else if (calculate->path_env) {
const wchar_t *path = calculate->path_env;
......@@ -539,7 +539,7 @@ get_program_full_path(const _PyMainInterpreterConfig *main_config,
}
/* join() is safe for MAXPATHLEN+1 size buffer */
join(program_full_path, main_config->program_name);
join(program_full_path, core_config->program_name);
if (exists(program_full_path)) {
break;
}
......@@ -707,9 +707,9 @@ error:
static void
calculate_init(PyCalculatePath *calculate,
const _PyMainInterpreterConfig *main_config)
const _PyCoreConfig *core_config)
{
calculate->home = main_config->home;
calculate->home = core_config->home;
calculate->path_env = _wgetenv(L"PATH");
}
......@@ -813,7 +813,7 @@ calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
static _PyInitError
calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
calculate_module_search_path(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config,
wchar_t *prefix)
{
......@@ -824,7 +824,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
#endif
/* We only use the default relative PYTHONPATH if we haven't
anything better to use! */
int skipdefault = (main_config->module_search_path_env != NULL ||
int skipdefault = (core_config->module_search_path_env != NULL ||
calculate->home != NULL ||
calculate->machine_path != NULL ||
calculate->user_path != NULL);
......@@ -863,8 +863,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
bufsz += wcslen(calculate->machine_path) + 1;
}
bufsz += wcslen(calculate->zip_path) + 1;
if (main_config->module_search_path_env != NULL) {
bufsz += wcslen(main_config->module_search_path_env) + 1;
if (core_config->module_search_path_env != NULL) {
bufsz += wcslen(core_config->module_search_path_env) + 1;
}
wchar_t *buf, *start_buf;
......@@ -872,9 +872,9 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
if (buf == NULL) {
/* We can't exit, so print a warning and limp along */
fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
if (main_config->module_search_path_env) {
if (core_config->module_search_path_env) {
fprintf(stderr, "Using environment $PYTHONPATH.\n");
config->module_search_path = main_config->module_search_path_env;
config->module_search_path = core_config->module_search_path_env;
}
else {
fprintf(stderr, "Using default static path.\n");
......@@ -884,9 +884,9 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
}
start_buf = buf;
if (main_config->module_search_path_env) {
if (core_config->module_search_path_env) {
if (wcscpy_s(buf, bufsz - (buf - start_buf),
main_config->module_search_path_env)) {
core_config->module_search_path_env)) {
return INIT_ERR_BUFFER_OVERFLOW();
}
buf = wcschr(buf, L'\0');
......@@ -999,7 +999,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
static _PyInitError
calculate_path_impl(const _PyMainInterpreterConfig *main_config,
calculate_path_impl(const _PyCoreConfig *core_config,
PyCalculatePath *calculate, _PyPathConfig *config)
{
_PyInitError err;
......@@ -1009,7 +1009,7 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
return err;
}
err = get_program_full_path(main_config, calculate, config);
err = get_program_full_path(core_config, calculate, config);
if (_Py_INIT_FAILED(err)) {
return err;
}
......@@ -1035,7 +1035,7 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config,
calculate_home_prefix(calculate, prefix);
err = calculate_module_search_path(main_config, calculate, config, prefix);
err = calculate_module_search_path(core_config, calculate, config, prefix);
if (_Py_INIT_FAILED(err)) {
return err;
}
......@@ -1059,15 +1059,14 @@ calculate_free(PyCalculatePath *calculate)
_PyInitError
_PyPathConfig_Calculate(_PyPathConfig *config,
const _PyMainInterpreterConfig *main_config)
_PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config)
{
PyCalculatePath calculate;
memset(&calculate, 0, sizeof(calculate));
calculate_init(&calculate, main_config);
calculate_init(&calculate, core_config);
_PyInitError err = calculate_path_impl(main_config, &calculate, config);
_PyInitError err = calculate_path_impl(core_config, &calculate, config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
......
......@@ -46,7 +46,7 @@ _PyPathConfig_Clear(_PyPathConfig *config)
/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
and Py_GetProgramFullPath() */
_PyInitError
_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
_PyPathConfig_Init(const _PyCoreConfig *core_config)
{
if (_Py_path_config.module_search_path) {
/* Already initialized */
......@@ -61,15 +61,15 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
/* Calculate program_full_path, prefix, exec_prefix (Unix)
or dll_path (Windows), and module_search_path */
err = _PyPathConfig_Calculate(&new_config, main_config);
err = _PyPathConfig_Calculate(&new_config, core_config);
if (_Py_INIT_FAILED(err)) {
_PyPathConfig_Clear(&new_config);
goto done;
}
/* Copy home and program_name from main_config */
if (main_config->home != NULL) {
new_config.home = _PyMem_RawWcsdup(main_config->home);
/* Copy home and program_name from core_config */
if (core_config->home != NULL) {
new_config.home = _PyMem_RawWcsdup(core_config->home);
if (new_config.home == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
......@@ -79,7 +79,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
new_config.home = NULL;
}
new_config.program_name = _PyMem_RawWcsdup(main_config->program_name);
new_config.program_name = _PyMem_RawWcsdup(core_config->program_name);
if (new_config.program_name == NULL) {
err = _Py_INIT_NO_MEMORY();
goto done;
......@@ -105,14 +105,14 @@ pathconfig_global_init(void)
}
_PyInitError err;
_PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
_PyCoreConfig config = _PyCoreConfig_INIT;
err = _PyMainInterpreterConfig_ReadEnv(&config);
err = _PyCoreConfig_ReadEnv(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}
err = _PyMainInterpreterConfig_Read(&config);
err = _PyCoreConfig_Read(&config);
if (_Py_INIT_FAILED(err)) {
goto error;
}
......@@ -122,11 +122,11 @@ pathconfig_global_init(void)
goto error;
}
_PyMainInterpreterConfig_Clear(&config);
_PyCoreConfig_Clear(&config);
return;
error:
_PyMainInterpreterConfig_Clear(&config);
_PyCoreConfig_Clear(&config);
_Py_FatalInitError(err);
}
......
......@@ -792,13 +792,8 @@ _Py_InitializeCore(const _PyCoreConfig *config)
*/
_PyInitError
_PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config)
_PyCoreConfig_Read(_PyCoreConfig *config)
{
/* Signal handlers are installed by default */
if (config->install_signal_handlers < 0) {
config->install_signal_handlers = 1;
}
if (config->program_name == NULL) {
#ifdef MS_WINDOWS
const wchar_t *program_name = L"python";
......@@ -814,9 +809,8 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config)
return _Py_INIT_OK();
}
void
_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
_PyCoreConfig_Clear(_PyCoreConfig *config)
{
#define CLEAR(ATTR) \
do { \
......@@ -831,6 +825,14 @@ _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
}
void
_PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
{
Py_CLEAR(config->argv);
Py_CLEAR(config->module_search_path);
}
/* Update interpreter state based on supplied configuration settings
*
* After calling this function, most of the restrictions on the interpreter
......@@ -881,16 +883,11 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
return _Py_INIT_ERR("can't initialize time");
}
/* GetPath may initialize state that _PySys_EndInit locks
in, and so has to be called first. */
err = _PyPathConfig_Init(&interp->config);
if (_Py_INIT_FAILED(err)) {
return err;
assert(interp->config.module_search_path != NULL);
if (PySys_SetObject("path", interp->config.module_search_path) != 0) {
return _Py_INIT_ERR("can't assign sys.path");
}
wchar_t *sys_path = Py_GetPath();
/* Finish setting up the sys module and import system */
PySys_SetPath(sys_path);
if (_PySys_EndInit(interp->sysdict) < 0)
return _Py_INIT_ERR("can't finish initializing sys");
......@@ -949,6 +946,12 @@ _Py_InitializeMainInterpreter(const _PyMainInterpreterConfig *config)
}
}
if (interp->config.argv != NULL) {
if (PySys_SetObject("argv", interp->config.argv) != 0) {
return _Py_INIT_ERR("can't assign sys.argv");
}
}
return _Py_INIT_OK();
}
......@@ -970,12 +973,12 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
goto done;
}
err = _PyMainInterpreterConfig_ReadEnv(&config);
err = _PyCoreConfig_ReadEnv(&core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
err = _PyMainInterpreterConfig_Read(&config);
err = _PyMainInterpreterConfig_Read(&config, &core_config);
if (_Py_INIT_FAILED(err)) {
goto done;
}
......@@ -988,6 +991,7 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
err = _Py_INIT_OK();
done:
_PyCoreConfig_Clear(&core_config);
_PyMainInterpreterConfig_Clear(&config);
return err;
}
......@@ -1342,7 +1346,7 @@ new_interpreter(PyThreadState **tstate_p)
interp->config = main_interp->config;
}
err = _PyPathConfig_Init(&interp->config);
err = _PyPathConfig_Init(&interp->core_config);
if (_Py_INIT_FAILED(err)) {
return err;
}
......
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