From 842bd4e25afcccdfe8761f9a9f78e030178e836f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 20:47:19 +0200 Subject: [PATCH 1/5] Revert "bpo-32030: _PyPathConfig_Init() sets home and program_name (#4673)" This reverts commit af5a895073c24637c094772b27526b94a12ec897. --- Doc/c-api/init.rst | 5 +- Include/internal/pystate.h | 25 +----- Include/pystate.h | 3 +- Modules/getpath.c | 17 +++- Modules/main.c | 38 ++++----- PC/getpathp.c | 21 ++++- Python/pathconfig.c | 161 ++++++++++--------------------------- Python/pylifecycle.c | 7 +- 8 files changed, 97 insertions(+), 180 deletions(-) diff --git a/Doc/c-api/init.rst b/Doc/c-api/init.rst index a3113a390fdb8d..a9927aba5e13a6 100644 --- a/Doc/c-api/init.rst +++ b/Doc/c-api/init.rst @@ -40,6 +40,7 @@ The following functions can be safely called before Python is initialized: * :c:func:`Py_GetCompiler` * :c:func:`Py_GetCopyright` * :c:func:`Py_GetPlatform` + * :c:func:`Py_GetProgramName` * :c:func:`Py_GetVersion` * Utilities: @@ -58,8 +59,8 @@ The following functions can be safely called before Python is initialized: The following functions **should not be called** before :c:func:`Py_Initialize`: :c:func:`Py_EncodeLocale`, :c:func:`Py_GetPath`, :c:func:`Py_GetPrefix`, :c:func:`Py_GetExecPrefix`, - :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome`, - :c:func:`Py_GetProgramName` and :c:func:`PyEval_InitThreads`. + :c:func:`Py_GetProgramFullPath`, :c:func:`Py_GetPythonHome` and + :c:func:`PyEval_InitThreads`. .. _global-conf-vars: diff --git a/Include/internal/pystate.h b/Include/internal/pystate.h index b93342120477f3..9d8531965f183d 100644 --- a/Include/internal/pystate.h +++ b/Include/internal/pystate.h @@ -48,35 +48,12 @@ typedef struct { #endif /* Set by Py_SetPath(), or computed by _PyPathConfig_Init() */ wchar_t *module_search_path; - /* Python program name */ - wchar_t *program_name; - /* Set by Py_SetPythonHome() or PYTHONHOME environment variable */ - wchar_t *home; } _PyPathConfig; -#ifdef MS_WINDOWS -#define _PyPathConfig_INIT \ - {.program_full_path = NULL, \ - .prefix = NULL, \ - .dll_path = NULL, \ - .module_search_path = NULL, \ - .program_name = NULL, \ - .home = NULL} -#else -#define _PyPathConfig_INIT \ - {.program_full_path = NULL, \ - .prefix = NULL, \ - .exec_prefix = NULL, \ - .module_search_path = NULL, \ - .program_name = NULL, \ - .home = NULL} -#endif +#define _PyPathConfig_INIT {.module_search_path = NULL} PyAPI_DATA(_PyPathConfig) _Py_path_config; -PyAPI_FUNC(_PyInitError) _PyPathConfig_Calculate( - _PyPathConfig *config, - const _PyMainInterpreterConfig *main_config); PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config); diff --git a/Include/pystate.h b/Include/pystate.h index 1d8aab6d83f742..60d001c4926c20 100644 --- a/Include/pystate.h +++ b/Include/pystate.h @@ -72,8 +72,7 @@ typedef struct { (_PyMainInterpreterConfig){\ .install_signal_handlers = -1, \ .module_search_path_env = NULL, \ - .home = NULL, \ - .program_name = NULL} + .home = NULL} typedef struct _is { diff --git a/Modules/getpath.c b/Modules/getpath.c index fc2b5442ce26b8..8554dbe2a2c71b 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -1008,10 +1008,16 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config, } +/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() + and Py_GetProgramFullPath() */ _PyInitError -_PyPathConfig_Calculate(_PyPathConfig *config, - const _PyMainInterpreterConfig *main_config) +_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) { + if (_Py_path_config.module_search_path) { + /* Already initialized */ + return _Py_INIT_OK(); + } + PyCalculatePath calculate; memset(&calculate, 0, sizeof(calculate)); @@ -1020,11 +1026,16 @@ _PyPathConfig_Calculate(_PyPathConfig *config, goto done; } - err = calculate_path_impl(main_config, &calculate, config); + _PyPathConfig new_path_config; + memset(&new_path_config, 0, sizeof(new_path_config)); + + err = calculate_path_impl(main_config, &calculate, &new_path_config); if (_Py_INIT_FAILED(err)) { + _PyPathConfig_Clear(&new_path_config); goto done; } + _Py_path_config = new_path_config; err = _Py_INIT_OK(); done: diff --git a/Modules/main.c b/Modules/main.c index 84706e1e29063a..6c6c8018fd4bd7 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -876,16 +876,6 @@ static _PyInitError config_get_program_name(_PyMainInterpreterConfig *config) { assert(config->program_name == NULL); - - /* If Py_SetProgramName() was called, use its value */ - wchar_t *program_name = _Py_path_config.program_name; - if (program_name != NULL) { - config->program_name = _PyMem_RawWcsdup(program_name); - if (config->program_name == NULL) { - return _Py_INIT_NO_MEMORY(); - } - } - #ifdef __APPLE__ char *p; /* On MacOS X, when the Python interpreter is embedded in an @@ -924,7 +914,6 @@ config_get_program_name(_PyMainInterpreterConfig *config) } #endif /* WITH_NEXT_FRAMEWORK */ #endif /* __APPLE__ */ - return _Py_INIT_OK(); } @@ -959,6 +948,13 @@ pymain_init_main_interpreter(_PyMain *pymain) { _PyInitError err; + /* TODO: Print any exceptions raised by these operations */ + err = _PyMainInterpreterConfig_Read(&pymain->config); + if (_Py_INIT_FAILED(err)) { + pymain->err = err; + return -1; + } + err = _Py_InitializeMainInterpreter(&pymain->config); if (_Py_INIT_FAILED(err)) { pymain->err = err; @@ -1416,13 +1412,14 @@ config_init_pythonpath(_PyMainInterpreterConfig *config) static _PyInitError -config_init_home(_PyMainInterpreterConfig *config) +config_init_pythonhome(_PyMainInterpreterConfig *config) { wchar_t *home; - /* If Py_SetPythonHome() was called, use its value */ - home = _Py_path_config.home; + home = Py_GetPythonHome(); if (home) { + /* Py_SetPythonHome() has been called before Py_Main(), + use its value */ config->home = _PyMem_RawWcsdup(home); if (config->home == NULL) { return _Py_INIT_NO_MEMORY(); @@ -1442,7 +1439,7 @@ config_init_home(_PyMainInterpreterConfig *config) _PyInitError _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config) { - _PyInitError err = config_init_home(config); + _PyInitError err = config_init_pythonhome(config); if (_Py_INIT_FAILED(err)) { return err; } @@ -1546,12 +1543,6 @@ pymain_parse_cmdline_envvars_impl(_PyMain *pymain) return -1; } - _PyInitError err = _PyMainInterpreterConfig_Read(&pymain->config); - if (_Py_INIT_FAILED(err)) { - pymain->err = err; - return -1; - } - return 0; } @@ -1575,6 +1566,11 @@ pymain_init_python(_PyMain *pymain) { pymain_init_stdio(pymain); + Py_SetProgramName(pymain->config.program_name); + /* Don't free program_name here: the argument to Py_SetProgramName + must remain valid until Py_FinalizeEx is called. The string is freed + by pymain_free(). */ + pymain->err = _Py_InitializeCore(&pymain->core_config); if (_Py_INIT_FAILED(pymain->err)) { return -1; diff --git a/PC/getpathp.c b/PC/getpathp.c index 08ed8ccc83f913..3a0ebc10636d7d 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -1058,23 +1058,38 @@ calculate_free(PyCalculatePath *calculate) } +/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() + and Py_GetProgramFullPath() */ _PyInitError -_PyPathConfig_Calculate(_PyPathConfig *config, - const _PyMainInterpreterConfig *main_config) +_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) { + if (_Py_path_config.module_search_path) { + /* Already initialized */ + return _Py_INIT_OK(); + } + + _PyInitError err; + PyCalculatePath calculate; memset(&calculate, 0, sizeof(calculate)); calculate_init(&calculate, main_config); - _PyInitError err = calculate_path_impl(main_config, &calculate, config); + _PyPathConfig new_path_config; + memset(&new_path_config, 0, sizeof(new_path_config)); + + err = calculate_path_impl(main_config, &calculate, &new_path_config); if (_Py_INIT_FAILED(err)) { goto done; } + _Py_path_config = new_path_config; err = _Py_INIT_OK(); done: + if (_Py_INIT_FAILED(err)) { + _PyPathConfig_Clear(&new_path_config); + } calculate_free(&calculate); return err; } diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 6a03f7dca1ba7a..daa222703697df 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -10,17 +10,17 @@ extern "C" { _PyPathConfig _Py_path_config = _PyPathConfig_INIT; +#ifdef MS_WINDOWS +static wchar_t *progname = L"python"; +#else +static wchar_t *progname = L"python3"; +#endif +static wchar_t *default_home = NULL; void _PyPathConfig_Clear(_PyPathConfig *config) { - /* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator, - since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be - called before Py_Initialize() which can changes the memory allocator. */ - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - #define CLEAR(ATTR) \ do { \ PyMem_RawFree(ATTR); \ @@ -35,64 +35,57 @@ _PyPathConfig_Clear(_PyPathConfig *config) CLEAR(config->exec_prefix); #endif CLEAR(config->module_search_path); - CLEAR(config->home); - CLEAR(config->program_name); #undef CLEAR +} - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + +void +Py_SetProgramName(wchar_t *pn) +{ + if (pn && *pn) + progname = pn; } -/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() - and Py_GetProgramFullPath() */ -_PyInitError -_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) +wchar_t * +Py_GetProgramName(void) { - if (_Py_path_config.module_search_path) { - /* Already initialized */ - return _Py_INIT_OK(); - } + return progname; +} - _PyInitError err; - _PyPathConfig new_config = _PyPathConfig_INIT; - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); +void +Py_SetPythonHome(wchar_t *home) +{ + default_home = home; +} - /* Calculate program_full_path, prefix, exec_prefix (Unix) - or dll_path (Windows), and module_search_path */ - err = _PyPathConfig_Calculate(&new_config, main_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); - if (new_config.home == NULL) { - err = _Py_INIT_NO_MEMORY(); - goto done; - } - } - else { - new_config.home = NULL; - } +wchar_t* +Py_GetPythonHome(void) +{ + /* Use a static buffer to avoid heap memory allocation failure. + Py_GetPythonHome() doesn't allow to report error, and the caller + doesn't release memory. */ + static wchar_t buffer[MAXPATHLEN+1]; - new_config.program_name = _PyMem_RawWcsdup(main_config->program_name); - if (new_config.program_name == NULL) { - err = _Py_INIT_NO_MEMORY(); - goto done; + if (default_home) { + return default_home; } - _PyPathConfig_Clear(&_Py_path_config); - _Py_path_config = new_config; + char *home = Py_GETENV("PYTHONHOME"); + if (!home) { + return NULL; + } - err = _Py_INIT_OK(); + size_t size = Py_ARRAY_LENGTH(buffer); + size_t r = mbstowcs(buffer, home, size); + if (r == (size_t)-1 || r >= size) { + /* conversion failed or the static buffer is too small */ + return NULL; + } -done: - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - return err; + return buffer; } @@ -141,9 +134,6 @@ Py_SetPath(const wchar_t *path) return; } - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - _PyPathConfig new_config; new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName()); new_config.prefix = _PyMem_RawWcsdup(L""); @@ -154,58 +144,8 @@ Py_SetPath(const wchar_t *path) #endif new_config.module_search_path = _PyMem_RawWcsdup(path); - /* steal the home and program_name values (to leave them unchanged) */ - new_config.home = _Py_path_config.home; - _Py_path_config.home = NULL; - new_config.program_name = _Py_path_config.program_name; - _Py_path_config.program_name = NULL; - _PyPathConfig_Clear(&_Py_path_config); _Py_path_config = new_config; - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); -} - - -void -Py_SetPythonHome(wchar_t *home) -{ - if (home == NULL) { - return; - } - - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - PyMem_RawFree(_Py_path_config.home); - _Py_path_config.home = _PyMem_RawWcsdup(home); - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - if (_Py_path_config.home == NULL) { - Py_FatalError("Py_SetPythonHome() failed: out of memory"); - } -} - - -void -Py_SetProgramName(wchar_t *program_name) -{ - if (program_name == NULL || program_name[0] == L'\0') { - return; - } - - PyMemAllocatorEx old_alloc; - _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - PyMem_RawFree(_Py_path_config.program_name); - _Py_path_config.program_name = _PyMem_RawWcsdup(program_name); - - PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - - if (_Py_path_config.program_name == NULL) { - Py_FatalError("Py_SetProgramName() failed: out of memory"); - } } @@ -244,23 +184,6 @@ Py_GetProgramFullPath(void) return _Py_path_config.program_full_path; } - -wchar_t* -Py_GetPythonHome(void) -{ - pathconfig_global_init(); - return _Py_path_config.home; -} - - -wchar_t * -Py_GetProgramName(void) -{ - pathconfig_global_init(); - return _Py_path_config.program_name; -} - - #ifdef __cplusplus } #endif diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 523397f1269f74..8d71154c563729 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -804,12 +804,7 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config) } if (config->program_name == NULL) { -#ifdef MS_WINDOWS - const wchar_t *program_name = L"python"; -#else - const wchar_t *program_name = L"python3"; -#endif - config->program_name = _PyMem_RawWcsdup(program_name); + config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); if (config->program_name == NULL) { return _Py_INIT_NO_MEMORY(); } From bcb47e143f2d23a59ba0597c9e0893ef435c40c5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 20:47:26 +0200 Subject: [PATCH 2/5] Revert "bpo-32030: Fix config_get_program_name() on macOS (#4669)" This reverts commit e23c06e2b03452c9aaf0dae52296c85e572f9bcd. --- Modules/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/main.c b/Modules/main.c index 6c6c8018fd4bd7..6c1cf0d306a5c4 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -894,7 +894,7 @@ config_get_program_name(_PyMainInterpreterConfig *config) return SET_DECODE_ERROR("PYTHONEXECUTABLE environment " "variable", len); } - config->program_name = program_name; + pymain->config.program_name = buffer; } #ifdef WITH_NEXT_FRAMEWORK else { @@ -909,7 +909,7 @@ config_get_program_name(_PyMainInterpreterConfig *config) return SET_DECODE_ERROR("__PYVENV_LAUNCHER__ environment " "variable", len); } - config->program_name = program_name; + pymain->config.program_name = program_name; } } #endif /* WITH_NEXT_FRAMEWORK */ From dd54bf3205826a671dd9afedfa52e4cca208893b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 20:47:27 +0200 Subject: [PATCH 3/5] Revert "bpo-32030: Add Python/pathconfig.c (#4668)" This reverts commit 0ea395ae964c9cd0f499e2ef0d0030c971201220. --- Include/internal/pystate.h | 4 - Include/pylifecycle.h | 1 + Makefile.pre.in | 3 +- Modules/getpath.c | 114 ++++++++++++++++- Modules/main.c | 6 +- PC/getpathp.c | 119 +++++++++++++++++- PCbuild/pythoncore.vcxproj | 1 - PCbuild/pythoncore.vcxproj.filters | 3 - Python/pathconfig.c | 189 ----------------------------- Python/pylifecycle.c | 55 +++++++++ 10 files changed, 291 insertions(+), 204 deletions(-) delete mode 100644 Python/pathconfig.c diff --git a/Include/internal/pystate.h b/Include/internal/pystate.h index 9d8531965f183d..50ad2fc83a361c 100644 --- a/Include/internal/pystate.h +++ b/Include/internal/pystate.h @@ -52,10 +52,6 @@ typedef struct { #define _PyPathConfig_INIT {.module_search_path = NULL} -PyAPI_DATA(_PyPathConfig) _Py_path_config; - -PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config); - /* Full Python runtime state */ diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h index fa751692a6670a..3db88326aeeca6 100644 --- a/Include/pylifecycle.h +++ b/Include/pylifecycle.h @@ -105,6 +105,7 @@ PyAPI_FUNC(wchar_t *) Py_GetPath(void); #ifdef Py_BUILD_CORE PyAPI_FUNC(_PyInitError) _PyPathConfig_Init( const _PyMainInterpreterConfig *main_config); +PyAPI_FUNC(void) _PyPathConfig_Fini(void); #endif PyAPI_FUNC(void) Py_SetPath(const wchar_t *); #ifdef MS_WINDOWS diff --git a/Makefile.pre.in b/Makefile.pre.in index 14f6f8abc54e8a..f425a89173ae14 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -337,9 +337,8 @@ PYTHON_OBJS= \ Python/importdl.o \ Python/marshal.o \ Python/modsupport.o \ - Python/mysnprintf.o \ Python/mystrtoul.o \ - Python/pathconfig.o \ + Python/mysnprintf.o \ Python/peephole.o \ Python/pyarena.o \ Python/pyctype.o \ diff --git a/Modules/getpath.c b/Modules/getpath.c index 8554dbe2a2c71b..235badab230bd1 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -132,6 +132,7 @@ typedef struct { static const wchar_t delimiter[2] = {DELIM, '\0'}; static const wchar_t separator[2] = {SEP, '\0'}; +static _PyPathConfig _Py_path_config = _PyPathConfig_INIT; /* Get file status. Encode the path to the locale encoding. */ @@ -1008,6 +1009,23 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config, } +static void +pathconfig_clear(_PyPathConfig *config) +{ +#define CLEAR(ATTR) \ + do { \ + PyMem_RawFree(ATTR); \ + ATTR = NULL; \ + } while (0) + + CLEAR(config->prefix); + CLEAR(config->exec_prefix); + CLEAR(config->program_full_path); + CLEAR(config->module_search_path); +#undef CLEAR +} + + /* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() and Py_GetProgramFullPath() */ _PyInitError @@ -1031,7 +1049,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) err = calculate_path_impl(main_config, &calculate, &new_path_config); if (_Py_INIT_FAILED(err)) { - _PyPathConfig_Clear(&new_path_config); + pathconfig_clear(&new_path_config); goto done; } @@ -1043,6 +1061,100 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) return err; } + +static void +pathconfig_global_init(void) +{ + if (_Py_path_config.module_search_path) { + /* Already initialized */ + return; + } + + _PyInitError err; + _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; + + err = _PyMainInterpreterConfig_ReadEnv(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + err = _PyMainInterpreterConfig_Read(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + err = _PyPathConfig_Init(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + _PyMainInterpreterConfig_Clear(&config); + return; + +error: + _PyMainInterpreterConfig_Clear(&config); + _Py_FatalInitError(err); +} + + +void +_PyPathConfig_Fini(void) +{ + pathconfig_clear(&_Py_path_config); +} + + +/* External interface */ +void +Py_SetPath(const wchar_t *path) +{ + if (path == NULL) { + pathconfig_clear(&_Py_path_config); + return; + } + + _PyPathConfig new_config; + new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName()); + new_config.exec_prefix = _PyMem_RawWcsdup(L""); + new_config.prefix = _PyMem_RawWcsdup(L""); + new_config.module_search_path = _PyMem_RawWcsdup(path); + + pathconfig_clear(&_Py_path_config); + _Py_path_config = new_config; +} + + +wchar_t * +Py_GetPath(void) +{ + pathconfig_global_init(); + return _Py_path_config.module_search_path; +} + + +wchar_t * +Py_GetPrefix(void) +{ + pathconfig_global_init(); + return _Py_path_config.prefix; +} + + +wchar_t * +Py_GetExecPrefix(void) +{ + pathconfig_global_init(); + return _Py_path_config.exec_prefix; +} + + +wchar_t * +Py_GetProgramFullPath(void) +{ + pathconfig_global_init(); + return _Py_path_config.program_full_path; +} + #ifdef __cplusplus } #endif diff --git a/Modules/main.c b/Modules/main.c index 6c1cf0d306a5c4..4659c5d670b78a 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -1665,12 +1665,12 @@ pymain_impl(_PyMain *pymain) pymain->status = 120; } - /* _PyPathConfig_Clear() cannot be called in Py_FinalizeEx(). + /* _PyPathConfig_Fini() cannot be called in Py_FinalizeEx(). Py_Initialize() and Py_Finalize() can be called multiple times, but it must not "forget" parameters set by Py_SetProgramName(), Py_SetPath() or - Py_SetPythonHome(), whereas _PyPathConfig_Clear() clear all these + Py_SetPythonHome(), whereas _PyPathConfig_Fini() clear all these parameters. */ - _PyPathConfig_Clear(&_Py_path_config); + _PyPathConfig_Fini(); return 0; } diff --git a/PC/getpathp.c b/PC/getpathp.c index 3a0ebc10636d7d..f8cfd7e6978386 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -130,6 +130,9 @@ typedef struct { } PyCalculatePath; +static _PyPathConfig _Py_path_config = _PyPathConfig_INIT; + + /* determine if "ch" is a separator character */ static int is_sep(wchar_t ch) @@ -1058,6 +1061,23 @@ calculate_free(PyCalculatePath *calculate) } +static void +pathconfig_clear(_PyPathConfig *config) +{ +#define CLEAR(ATTR) \ + do { \ + PyMem_RawFree(ATTR); \ + ATTR = NULL; \ + } while (0) + + CLEAR(config->prefix); + CLEAR(config->program_full_path); + CLEAR(config->dll_path); + CLEAR(config->module_search_path); +#undef CLEAR +} + + /* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() and Py_GetProgramFullPath() */ _PyInitError @@ -1088,13 +1108,110 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) done: if (_Py_INIT_FAILED(err)) { - _PyPathConfig_Clear(&new_path_config); + pathconfig_clear(&new_path_config); } calculate_free(&calculate); return err; } +static void +pathconfig_global_init(void) +{ + if (_Py_path_config.module_search_path) { + /* Already initialized */ + return; + } + + _PyInitError err; + _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; + + err = _PyMainInterpreterConfig_ReadEnv(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + err = _PyMainInterpreterConfig_Read(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + err = _PyPathConfig_Init(&config); + if (_Py_INIT_FAILED(err)) { + goto error; + } + + _PyMainInterpreterConfig_Clear(&config); + return; + +error: + _PyMainInterpreterConfig_Clear(&config); + _Py_FatalInitError(err); +} + + +void +_PyPathConfig_Fini(void) +{ + pathconfig_clear(&_Py_path_config); +} + + +/* External interface */ + +void +Py_SetPath(const wchar_t *path) +{ + if (_Py_path_config.module_search_path != NULL) { + pathconfig_clear(&_Py_path_config); + } + + if (path == NULL) { + return; + } + + _PyPathConfig new_config; + new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName()); + new_config.prefix = _PyMem_RawWcsdup(L""); + new_config.dll_path = _PyMem_RawWcsdup(L""); + new_config.module_search_path = _PyMem_RawWcsdup(path); + + pathconfig_clear(&_Py_path_config); + _Py_path_config = new_config; +} + + +wchar_t * +Py_GetPath(void) +{ + pathconfig_global_init(); + return _Py_path_config.module_search_path; +} + + +wchar_t * +Py_GetPrefix(void) +{ + pathconfig_global_init(); + return _Py_path_config.prefix; +} + + +wchar_t * +Py_GetExecPrefix(void) +{ + return Py_GetPrefix(); +} + + +wchar_t * +Py_GetProgramFullPath(void) +{ + pathconfig_global_init(); + return _Py_path_config.program_full_path; +} + + /* Load python3.dll before loading any extension module that might refer to it. That way, we can be sure that always the python3.dll corresponding to this python DLL is loaded, not a python3.dll that might be on the path diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj index b430e05b629b1c..3793cbda8829d8 100644 --- a/PCbuild/pythoncore.vcxproj +++ b/PCbuild/pythoncore.vcxproj @@ -381,7 +381,6 @@ - diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters index c9aa3da355e99f..1d33c6e2cc2802 100644 --- a/PCbuild/pythoncore.vcxproj.filters +++ b/PCbuild/pythoncore.vcxproj.filters @@ -896,9 +896,6 @@ Python - - Python - Python diff --git a/Python/pathconfig.c b/Python/pathconfig.c deleted file mode 100644 index daa222703697df..00000000000000 --- a/Python/pathconfig.c +++ /dev/null @@ -1,189 +0,0 @@ -/* Path configuration like module_search_path (sys.path) */ - -#include "Python.h" -#include "osdefs.h" -#include "internal/pystate.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -_PyPathConfig _Py_path_config = _PyPathConfig_INIT; -#ifdef MS_WINDOWS -static wchar_t *progname = L"python"; -#else -static wchar_t *progname = L"python3"; -#endif -static wchar_t *default_home = NULL; - - -void -_PyPathConfig_Clear(_PyPathConfig *config) -{ -#define CLEAR(ATTR) \ - do { \ - PyMem_RawFree(ATTR); \ - ATTR = NULL; \ - } while (0) - - CLEAR(config->prefix); - CLEAR(config->program_full_path); -#ifdef MS_WINDOWS - CLEAR(config->dll_path); -#else - CLEAR(config->exec_prefix); -#endif - CLEAR(config->module_search_path); -#undef CLEAR -} - - -void -Py_SetProgramName(wchar_t *pn) -{ - if (pn && *pn) - progname = pn; -} - - -wchar_t * -Py_GetProgramName(void) -{ - return progname; -} - - -void -Py_SetPythonHome(wchar_t *home) -{ - default_home = home; -} - - -wchar_t* -Py_GetPythonHome(void) -{ - /* Use a static buffer to avoid heap memory allocation failure. - Py_GetPythonHome() doesn't allow to report error, and the caller - doesn't release memory. */ - static wchar_t buffer[MAXPATHLEN+1]; - - if (default_home) { - return default_home; - } - - char *home = Py_GETENV("PYTHONHOME"); - if (!home) { - return NULL; - } - - size_t size = Py_ARRAY_LENGTH(buffer); - size_t r = mbstowcs(buffer, home, size); - if (r == (size_t)-1 || r >= size) { - /* conversion failed or the static buffer is too small */ - return NULL; - } - - return buffer; -} - - -static void -pathconfig_global_init(void) -{ - if (_Py_path_config.module_search_path) { - /* Already initialized */ - return; - } - - _PyInitError err; - _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; - - err = _PyMainInterpreterConfig_ReadEnv(&config); - if (_Py_INIT_FAILED(err)) { - goto error; - } - - err = _PyMainInterpreterConfig_Read(&config); - if (_Py_INIT_FAILED(err)) { - goto error; - } - - err = _PyPathConfig_Init(&config); - if (_Py_INIT_FAILED(err)) { - goto error; - } - - _PyMainInterpreterConfig_Clear(&config); - return; - -error: - _PyMainInterpreterConfig_Clear(&config); - _Py_FatalInitError(err); -} - - -/* External interface */ - -void -Py_SetPath(const wchar_t *path) -{ - if (path == NULL) { - _PyPathConfig_Clear(&_Py_path_config); - return; - } - - _PyPathConfig new_config; - new_config.program_full_path = _PyMem_RawWcsdup(Py_GetProgramName()); - new_config.prefix = _PyMem_RawWcsdup(L""); -#ifdef MS_WINDOWS - new_config.dll_path = _PyMem_RawWcsdup(L""); -#else - new_config.exec_prefix = _PyMem_RawWcsdup(L""); -#endif - new_config.module_search_path = _PyMem_RawWcsdup(path); - - _PyPathConfig_Clear(&_Py_path_config); - _Py_path_config = new_config; -} - - -wchar_t * -Py_GetPath(void) -{ - pathconfig_global_init(); - return _Py_path_config.module_search_path; -} - - -wchar_t * -Py_GetPrefix(void) -{ - pathconfig_global_init(); - return _Py_path_config.prefix; -} - - -wchar_t * -Py_GetExecPrefix(void) -{ -#ifdef MS_WINDOWS - return Py_GetPrefix(); -#else - pathconfig_global_init(); - return _Py_path_config.exec_prefix; -#endif -} - - -wchar_t * -Py_GetProgramFullPath(void) -{ - pathconfig_global_init(); - return _Py_path_config.program_full_path; -} - -#ifdef __cplusplus -} -#endif diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8d71154c563729..a1b29f2a0eb0fd 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1489,6 +1489,61 @@ Py_EndInterpreter(PyThreadState *tstate) PyInterpreterState_Delete(interp); } +#ifdef MS_WINDOWS +static wchar_t *progname = L"python"; +#else +static wchar_t *progname = L"python3"; +#endif + +void +Py_SetProgramName(wchar_t *pn) +{ + if (pn && *pn) + progname = pn; +} + +wchar_t * +Py_GetProgramName(void) +{ + return progname; +} + +static wchar_t *default_home = NULL; + +void +Py_SetPythonHome(wchar_t *home) +{ + default_home = home; +} + + +wchar_t* +Py_GetPythonHome(void) +{ + /* Use a static buffer to avoid heap memory allocation failure. + Py_GetPythonHome() doesn't allow to report error, and the caller + doesn't release memory. */ + static wchar_t buffer[MAXPATHLEN+1]; + + if (default_home) { + return default_home; + } + + char *home = Py_GETENV("PYTHONHOME"); + if (!home) { + return NULL; + } + + size_t size = Py_ARRAY_LENGTH(buffer); + size_t r = mbstowcs(buffer, home, size); + if (r == (size_t)-1 || r >= size) { + /* conversion failed or the static buffer is too small */ + return NULL; + } + + return buffer; +} + /* Add the __main__ module */ static _PyInitError From 76e9ab212cf25d363a3806ca74ef9f14d4e7f730 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 20:47:28 +0200 Subject: [PATCH 4/5] Revert "bpo-32030: Don't call _PyPathConfig_Fini() in Py_FinalizeEx() (#4667)" This reverts commit ebac19dad6263141d5db0a2c923efe049dba99d2. --- Include/pylifecycle.h | 2 +- Modules/getpath.c | 10 ++++------ Modules/main.c | 30 ++++++++++++------------------ PC/getpathp.c | 19 ++++++------------- Python/pylifecycle.c | 2 ++ 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/Include/pylifecycle.h b/Include/pylifecycle.h index 3db88326aeeca6..d32c98b6985688 100644 --- a/Include/pylifecycle.h +++ b/Include/pylifecycle.h @@ -109,7 +109,7 @@ PyAPI_FUNC(void) _PyPathConfig_Fini(void); #endif PyAPI_FUNC(void) Py_SetPath(const wchar_t *); #ifdef MS_WINDOWS -int _Py_CheckPython3(void); +int _Py_CheckPython3(); #endif /* In their own files */ diff --git a/Modules/getpath.c b/Modules/getpath.c index 235badab230bd1..183717d817173e 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -625,13 +625,11 @@ calculate_program_full_path(const _PyMainInterpreterConfig *main_config, else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) { - size_t len; - wchar_t *path = Py_DecodeLocale(execpath, &len); - if (path == NULL) { - return DECODE_LOCALE_ERR("executable path", len); + size_t r = mbstowcs(program_full_path, execpath, MAXPATHLEN+1); + if (r == (size_t)-1 || r > MAXPATHLEN) { + /* Could not convert execpath, or it's too long. */ + program_full_path[0] = '\0'; } - wcsncpy(program_full_path, path, MAXPATHLEN); - PyMem_RawFree(path); } #endif /* __APPLE__ */ else if (calculate->path_env) { diff --git a/Modules/main.c b/Modules/main.c index 4659c5d670b78a..caec97f8642385 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -888,12 +888,15 @@ config_get_program_name(_PyMainInterpreterConfig *config) See Lib/plat-mac/bundlebuiler.py for details about the bootstrap script. */ if ((p = Py_GETENV("PYTHONEXECUTABLE")) && *p != '\0') { - size_t len; - wchar_t* program_name = Py_DecodeLocale(p, &len); - if (program_name == NULL) { - return SET_DECODE_ERROR("PYTHONEXECUTABLE environment " - "variable", len); + wchar_t* buffer; + size_t len = strlen(p) + 1; + + buffer = PyMem_RawMalloc(len * sizeof(wchar_t)); + if (buffer == NULL) { + return _Py_INIT_NO_MEMORY(); } + + mbstowcs(buffer, p, len); pymain->config.program_name = buffer; } #ifdef WITH_NEXT_FRAMEWORK @@ -904,12 +907,11 @@ config_get_program_name(_PyMainInterpreterConfig *config) * the argv0 of the stub executable */ size_t len; - wchar_t* program_name = Py_DecodeLocale(pyvenv_launcher, &len); - if (program_name == NULL) { - return SET_DECODE_ERROR("__PYVENV_LAUNCHER__ environment " - "variable", len); + wchar_t* wbuf = Py_DecodeLocale(pyvenv_launcher, &len); + if (wbuf == NULL) { + return SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len); } - pymain->config.program_name = program_name; + pymain->config.program_name = wbuf; } } #endif /* WITH_NEXT_FRAMEWORK */ @@ -1664,14 +1666,6 @@ pymain_impl(_PyMain *pymain) other special meaning */ pymain->status = 120; } - - /* _PyPathConfig_Fini() cannot be called in Py_FinalizeEx(). - Py_Initialize() and Py_Finalize() can be called multiple times, but it - must not "forget" parameters set by Py_SetProgramName(), Py_SetPath() or - Py_SetPythonHome(), whereas _PyPathConfig_Fini() clear all these - parameters. */ - _PyPathConfig_Fini(); - return 0; } diff --git a/PC/getpathp.c b/PC/getpathp.c index f8cfd7e6978386..89d37a84133622 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -721,16 +721,12 @@ static int get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config) { if (config->dll_path[0]) { - if (!change_ext(spbuffer, config->dll_path, L"._pth") && - exists(spbuffer)) - { + if (!change_ext(spbuffer, config->dll_path, L"._pth") && exists(spbuffer)) { return 1; } } if (config->program_full_path[0]) { - if (!change_ext(spbuffer, config->program_full_path, L"._pth") && - exists(spbuffer)) - { + if (!change_ext(spbuffer, config->program_full_path, L"._pth") && exists(spbuffer)) { return 1; } } @@ -827,10 +823,8 @@ 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 || - calculate->home != NULL || - calculate->machine_path != NULL || - calculate->user_path != NULL); + int skipdefault = (main_config->module_search_path_env!=NULL || calculate->home!=NULL || \ + calculate->machine_path!=NULL || calculate->user_path!=NULL); /* We need to construct a path from the following parts. (1) the PYTHONPATH environment variable, if set; @@ -888,8 +882,7 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, start_buf = buf; if (main_config->module_search_path_env) { - if (wcscpy_s(buf, bufsz - (buf - start_buf), - main_config->module_search_path_env)) { + if (wcscpy_s(buf, bufsz - (buf - start_buf), main_config->module_search_path_env)) { return INIT_ERR_BUFFER_OVERFLOW(); } buf = wcschr(buf, L'\0'); @@ -1221,7 +1214,7 @@ Py_GetProgramFullPath(void) static int python3_checked = 0; static HANDLE hPython3; int -_Py_CheckPython3(void) +_Py_CheckPython3() { wchar_t py3path[MAXPATHLEN+1]; wchar_t *s; diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index a1b29f2a0eb0fd..f0a49f91fb85bf 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1273,6 +1273,8 @@ Py_FinalizeEx(void) call_ll_exitfuncs(); + _PyPathConfig_Fini(); + _PyRuntime_Finalize(); return status; } From eec7372c8cec0491bc88a77e8f22fb80249a6358 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 2 Dec 2017 20:47:28 +0200 Subject: [PATCH 5/5] Revert "bpo-32030: Fix Py_GetPath(): init program_name (#4665)" This reverts commit 9ac3d8882712c9675c3d2f9f84af6b5729575cde. --- Modules/getpath.c | 92 ++++++++++++++++++++--------------------------- Modules/main.c | 69 ++++++++++++++++++----------------- PC/getpathp.c | 65 ++++++++++++++------------------- 3 files changed, 102 insertions(+), 124 deletions(-) diff --git a/Modules/getpath.c b/Modules/getpath.c index 183717d817173e..9f5e8b3ff5c6b3 100644 --- a/Modules/getpath.c +++ b/Modules/getpath.c @@ -117,7 +117,10 @@ extern "C" { typedef struct { wchar_t *path_env; /* PATH environment variable */ + wchar_t *home; /* PYTHONHOME environment variable */ + wchar_t *module_search_path_env; /* PYTHONPATH environment variable */ + wchar_t *program_name; /* Program name */ wchar_t *pythonpath; /* PYTHONPATH define */ wchar_t *prefix; /* PREFIX define */ wchar_t *exec_prefix; /* EXEC_PREFIX define */ @@ -357,15 +360,14 @@ 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, - PyCalculatePath *calculate, wchar_t *prefix) +search_for_prefix(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 (calculate->home) { + wcsncpy(prefix, calculate->home, MAXPATHLEN); prefix[MAXPATHLEN] = L'\0'; wchar_t *delim = wcschr(prefix, DELIM); if (delim) { @@ -424,10 +426,9 @@ search_for_prefix(const _PyMainInterpreterConfig *main_config, static void -calculate_prefix(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, wchar_t *prefix) +calculate_prefix(PyCalculatePath *calculate, wchar_t *prefix) { - calculate->prefix_found = search_for_prefix(main_config, calculate, prefix); + calculate->prefix_found = search_for_prefix(calculate, prefix); if (!calculate->prefix_found) { if (!Py_FrozenFlag) { fprintf(stderr, @@ -469,19 +470,18 @@ calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix) MAXPATHLEN bytes long. */ static int -search_for_exec_prefix(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, wchar_t *exec_prefix) +search_for_exec_prefix(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 (calculate->home) { + wchar_t *delim = wcschr(calculate->home, DELIM); if (delim) { wcsncpy(exec_prefix, delim+1, MAXPATHLEN); } else { - wcsncpy(exec_prefix, main_config->home, MAXPATHLEN); + wcsncpy(exec_prefix, calculate->home, MAXPATHLEN); } exec_prefix[MAXPATHLEN] = L'\0'; joinpath(exec_prefix, calculate->lib_python); @@ -552,12 +552,9 @@ search_for_exec_prefix(const _PyMainInterpreterConfig *main_config, static void -calculate_exec_prefix(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, wchar_t *exec_prefix) +calculate_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix) { - calculate->exec_prefix_found = search_for_exec_prefix(main_config, - calculate, - exec_prefix); + calculate->exec_prefix_found = search_for_exec_prefix(calculate, exec_prefix); if (!calculate->exec_prefix_found) { if (!Py_FrozenFlag) { fprintf(stderr, @@ -588,8 +585,7 @@ calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix) static _PyInitError -calculate_program_full_path(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, _PyPathConfig *config) +calculate_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config) { wchar_t program_full_path[MAXPATHLEN+1]; memset(program_full_path, 0, sizeof(program_full_path)); @@ -608,8 +604,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(calculate->program_name, SEP)) { + wcsncpy(program_full_path, calculate->program_name, MAXPATHLEN); } #ifdef __APPLE__ /* On Mac OS X, if a script uses an interpreter of the form @@ -649,7 +645,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, calculate->program_name); if (isxfile(program_full_path)) { break; } @@ -814,15 +810,14 @@ calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix) static _PyInitError -calculate_module_search_path(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, +calculate_module_search_path(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 (calculate->module_search_path_env != NULL) { + bufsz += wcslen(calculate->module_search_path_env) + 1; } wchar_t *defpath = calculate->pythonpath; @@ -856,8 +851,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 (calculate->module_search_path_env) { + wcscpy(buf, calculate->module_search_path_env); wcscat(buf, delimiter); } @@ -908,6 +903,10 @@ static _PyInitError calculate_init(PyCalculatePath *calculate, const _PyMainInterpreterConfig *main_config) { + calculate->home = main_config->home; + calculate->module_search_path_env = main_config->module_search_path_env; + calculate->program_name = main_config->program_name; + size_t len; char *path = getenv("PATH"); if (path) { @@ -949,12 +948,9 @@ calculate_free(PyCalculatePath *calculate) static _PyInitError -calculate_path_impl(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, _PyPathConfig *config) +calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config) { - _PyInitError err; - - err = calculate_program_full_path(main_config, calculate, config); + _PyInitError err = calculate_program_full_path(calculate, config); if (_Py_INIT_FAILED(err)) { return err; } @@ -968,13 +964,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(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(calculate, exec_prefix); if ((!calculate->prefix_found || !calculate->exec_prefix_found) && !Py_FrozenFlag) @@ -983,8 +979,8 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config, "Consider setting $PYTHONHOME to [:]\n"); } - err = calculate_module_search_path(main_config, calculate, - prefix, exec_prefix, config); + err = calculate_module_search_path(calculate, prefix, exec_prefix, + config); if (_Py_INIT_FAILED(err)) { return err; } @@ -1045,7 +1041,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) _PyPathConfig new_path_config; memset(&new_path_config, 0, sizeof(new_path_config)); - err = calculate_path_impl(main_config, &calculate, &new_path_config); + err = calculate_path_impl(&calculate, &new_path_config); if (_Py_INIT_FAILED(err)) { pathconfig_clear(&new_path_config); goto done; @@ -1072,26 +1068,14 @@ pathconfig_global_init(void) _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; err = _PyMainInterpreterConfig_ReadEnv(&config); - if (_Py_INIT_FAILED(err)) { - goto error; - } - - err = _PyMainInterpreterConfig_Read(&config); - if (_Py_INIT_FAILED(err)) { - goto error; + if (!_Py_INIT_FAILED(err)) { + err = _PyPathConfig_Init(&config); } + _PyMainInterpreterConfig_Clear(&config); - err = _PyPathConfig_Init(&config); if (_Py_INIT_FAILED(err)) { - goto error; + _Py_FatalInitError(err); } - - _PyMainInterpreterConfig_Clear(&config); - return; - -error: - _PyMainInterpreterConfig_Clear(&config); - _Py_FatalInitError(err); } diff --git a/Modules/main.c b/Modules/main.c index caec97f8642385..e9d524a1463627 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -412,6 +412,7 @@ typedef struct { /* non-zero if filename, command (-c) or module (-m) is set on the command line */ int run_code; + wchar_t *program_name; /* Error message if a function failed */ _PyInitError err; /* PYTHONWARNINGS env var */ @@ -428,6 +429,7 @@ typedef struct { .config = _PyMainInterpreterConfig_INIT, \ .main_importer_path = NULL, \ .run_code = -1, \ + .program_name = NULL, \ .err = _Py_INIT_OK(), \ .env_warning_options = {0, NULL}} @@ -453,6 +455,7 @@ pymain_free_impl(_PyMain *pymain) pymain_optlist_clear(&pymain->env_warning_options); Py_CLEAR(pymain->main_importer_path); + PyMem_RawFree(pymain->program_name); _PyMainInterpreterConfig_Clear(&pymain->config); @@ -871,11 +874,14 @@ pymain_init_stdio(_PyMain *pymain) /* Get the program name: use PYTHONEXECUTABLE and __PYVENV_LAUNCHER__ - environment variables on macOS if available. */ -static _PyInitError -config_get_program_name(_PyMainInterpreterConfig *config) + environment variables on macOS if available, use argv[0] by default. + + Return 0 on success. + Set pymain->err and return -1 on error. */ +static int +pymain_get_program_name(_PyMain *pymain) { - assert(config->program_name == NULL); + assert(pymain->program_name == NULL); #ifdef __APPLE__ char *p; /* On MacOS X, when the Python interpreter is embedded in an @@ -893,11 +899,12 @@ config_get_program_name(_PyMainInterpreterConfig *config) buffer = PyMem_RawMalloc(len * sizeof(wchar_t)); if (buffer == NULL) { - return _Py_INIT_NO_MEMORY(); + pymain->err = _Py_INIT_NO_MEMORY(); + return -1; } mbstowcs(buffer, p, len); - pymain->config.program_name = buffer; + pymain->program_name = buffer; } #ifdef WITH_NEXT_FRAMEWORK else { @@ -909,26 +916,19 @@ config_get_program_name(_PyMainInterpreterConfig *config) size_t len; wchar_t* wbuf = Py_DecodeLocale(pyvenv_launcher, &len); if (wbuf == NULL) { - return SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len); + SET_DECODE_ERROR("__PYVENV_LAUNCHER__", len); + return -1; } - pymain->config.program_name = wbuf; + pymain->program_name = wbuf; } } #endif /* WITH_NEXT_FRAMEWORK */ #endif /* __APPLE__ */ - return _Py_INIT_OK(); -} - -/* If config_get_program_name() found no program name: use argv[0] by default. - Return 0 on success. Set pymain->err and return -1 on error. */ -static int -pymain_get_program_name(_PyMain *pymain) -{ - if (pymain->config.program_name == NULL) { + if (pymain->program_name == NULL) { /* Use argv[0] by default */ - pymain->config.program_name = pymain_wstrdup(pymain, pymain->argv[0]); - if (pymain->config.program_name == NULL) { + pymain->program_name = pymain_wstrdup(pymain, pymain->argv[0]); + if (pymain->program_name == NULL) { return -1; } } @@ -1451,9 +1451,11 @@ _PyMainInterpreterConfig_ReadEnv(_PyMainInterpreterConfig *config) return err; } - err = config_get_program_name(config); - if (_Py_INIT_FAILED(err)) { - return err; + /* FIXME: _PyMainInterpreterConfig_Read() has the same code. Remove it + here? See also pymain_get_program_name() and pymain_parse_envvars(). */ + config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); + if (config->program_name == NULL) { + return _Py_INIT_NO_MEMORY(); } return _Py_INIT_OK(); @@ -1479,17 +1481,25 @@ pymain_parse_envvars(_PyMain *pymain) if (pymain_warnings_envvar(pymain) < 0) { return -1; } + if (pymain_get_program_name(pymain) < 0) { + return -1; + } + core_config->allocator = Py_GETENV("PYTHONMALLOC"); + + /* FIXME: move pymain_get_program_name() code into + _PyMainInterpreterConfig_ReadEnv(). + Problem: _PyMainInterpreterConfig_ReadEnv() doesn't have access + to argv[0]. */ + Py_SetProgramName(pymain->program_name); + /* Don't free program_name here: the argument to Py_SetProgramName + must remain valid until Py_FinalizeEx is called. The string is freed + by pymain_free(). */ _PyInitError err = _PyMainInterpreterConfig_ReadEnv(&pymain->config); if (_Py_INIT_FAILED(pymain->err)) { pymain->err = err; return -1; } - if (pymain_get_program_name(pymain) < 0) { - return -1; - } - - core_config->allocator = Py_GETENV("PYTHONMALLOC"); /* -X options */ if (pymain_get_xoption(pymain, L"showrefcount")) { @@ -1568,11 +1578,6 @@ pymain_init_python(_PyMain *pymain) { pymain_init_stdio(pymain); - Py_SetProgramName(pymain->config.program_name); - /* Don't free program_name here: the argument to Py_SetProgramName - must remain valid until Py_FinalizeEx is called. The string is freed - by pymain_free(). */ - pymain->err = _Py_InitializeCore(&pymain->core_config); if (_Py_INIT_FAILED(pymain->err)) { return -1; diff --git a/PC/getpathp.c b/PC/getpathp.c index 89d37a84133622..ad04b6b6efc926 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -118,6 +118,7 @@ #endif typedef struct { + wchar_t *module_search_path_env; /* PYTHONPATH environment variable */ wchar_t *path_env; /* PATH environment variable */ wchar_t *home; /* PYTHONHOME environment variable */ @@ -125,6 +126,7 @@ typedef struct { wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */ wchar_t *user_path; /* from HKEY_CURRENT_USER */ + wchar_t *program_name; /* Program name */ wchar_t argv0_path[MAXPATHLEN+1]; wchar_t zip_path[MAXPATHLEN+1]; } PyCalculatePath; @@ -501,8 +503,7 @@ get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config) static _PyInitError -get_program_full_path(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, _PyPathConfig *config) +get_program_full_path(PyCalculatePath *calculate, _PyPathConfig *config) { wchar_t program_full_path[MAXPATHLEN+1]; memset(program_full_path, 0, sizeof(program_full_path)); @@ -517,13 +518,12 @@ 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(calculate->program_name, SEP) || wcschr(calculate->program_name, ALTSEP)) #else - if (wcschr(main_config->program_name, SEP)) + if (wcschr(calculate->program_name, SEP)) #endif { - wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN); + wcsncpy(program_full_path, calculate->program_name, MAXPATHLEN); } else if (calculate->path_env) { wchar_t *path = calculate->path_env; @@ -542,7 +542,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, calculate->program_name); if (exists(program_full_path)) { break; } @@ -713,6 +713,9 @@ calculate_init(PyCalculatePath *calculate, const _PyMainInterpreterConfig *main_config) { calculate->home = main_config->home; + calculate->module_search_path_env = main_config->module_search_path_env; + calculate->program_name = main_config->program_name; + calculate->path_env = _wgetenv(L"PATH"); } @@ -812,9 +815,7 @@ calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix) static _PyInitError -calculate_module_search_path(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, _PyPathConfig *config, - wchar_t *prefix) +calculate_module_search_path(PyCalculatePath *calculate, _PyPathConfig *config, wchar_t *prefix) { int skiphome = calculate->home==NULL ? 0 : 1; #ifdef Py_ENABLE_SHARED @@ -823,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 || calculate->home!=NULL || \ + int skipdefault = (calculate->module_search_path_env!=NULL || calculate->home!=NULL || \ calculate->machine_path!=NULL || calculate->user_path!=NULL); /* We need to construct a path from the following parts. @@ -860,8 +861,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 (calculate->module_search_path_env != NULL) { + bufsz += wcslen(calculate->module_search_path_env) + 1; } wchar_t *buf, *start_buf; @@ -869,9 +870,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 (calculate->module_search_path_env) { fprintf(stderr, "Using environment $PYTHONPATH.\n"); - config->module_search_path = main_config->module_search_path_env; + config->module_search_path = calculate->module_search_path_env; } else { fprintf(stderr, "Using default static path.\n"); @@ -881,8 +882,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, } start_buf = buf; - if (main_config->module_search_path_env) { - if (wcscpy_s(buf, bufsz - (buf - start_buf), main_config->module_search_path_env)) { + if (calculate->module_search_path_env) { + if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->module_search_path_env)) { return INIT_ERR_BUFFER_OVERFLOW(); } buf = wcschr(buf, L'\0'); @@ -995,8 +996,8 @@ calculate_module_search_path(const _PyMainInterpreterConfig *main_config, static _PyInitError -calculate_path_impl(const _PyMainInterpreterConfig *main_config, - PyCalculatePath *calculate, _PyPathConfig *config) +calculate_path_impl(PyCalculatePath *calculate, _PyPathConfig *config, + const _PyMainInterpreterConfig *main_config) { _PyInitError err; @@ -1005,7 +1006,7 @@ calculate_path_impl(const _PyMainInterpreterConfig *main_config, return err; } - err = get_program_full_path(main_config, calculate, config); + err = get_program_full_path(calculate, config); if (_Py_INIT_FAILED(err)) { return err; } @@ -1031,7 +1032,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(calculate, config, prefix); if (_Py_INIT_FAILED(err)) { return err; } @@ -1091,7 +1092,7 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) _PyPathConfig new_path_config; memset(&new_path_config, 0, sizeof(new_path_config)); - err = calculate_path_impl(main_config, &calculate, &new_path_config); + err = calculate_path_impl(&calculate, &new_path_config, main_config); if (_Py_INIT_FAILED(err)) { goto done; } @@ -1120,26 +1121,14 @@ pathconfig_global_init(void) _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT; err = _PyMainInterpreterConfig_ReadEnv(&config); - if (_Py_INIT_FAILED(err)) { - goto error; - } - - err = _PyMainInterpreterConfig_Read(&config); - if (_Py_INIT_FAILED(err)) { - goto error; + if (!_Py_INIT_FAILED(err)) { + err = _PyPathConfig_Init(&config); } + _PyMainInterpreterConfig_Clear(&config); - err = _PyPathConfig_Init(&config); if (_Py_INIT_FAILED(err)) { - goto error; + _Py_FatalInitError(err); } - - _PyMainInterpreterConfig_Clear(&config); - return; - -error: - _PyMainInterpreterConfig_Clear(&config); - _Py_FatalInitError(err); }