diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-12-02 09:11:32 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-02 09:11:32 (GMT) |
commit | af5a895073c24637c094772b27526b94a12ec897 (patch) | |
tree | 2960347bdeb6d0ba50746d5d36630d13630f1bb9 /Python | |
parent | e23c06e2b03452c9aaf0dae52296c85e572f9bcd (diff) | |
download | cpython-af5a895073c24637c094772b27526b94a12ec897.zip cpython-af5a895073c24637c094772b27526b94a12ec897.tar.gz cpython-af5a895073c24637c094772b27526b94a12ec897.tar.bz2 |
bpo-32030: _PyPathConfig_Init() sets home and program_name (#4673)
_PyPathConfig_Init() now also initialize home and program_name:
* Rename existing _PyPathConfig_Init() to _PyPathConfig_Calculate().
Add a new _PyPathConfig_Init() function in pathconfig.c which
handles the _Py_path_config variable and call
_PyPathConfig_Calculate().
* Add home and program_name fields to _PyPathConfig.home
* _PyPathConfig_Init() now initialize home and program_name
from main_config
* Py_SetProgramName(), Py_SetPythonHome() and Py_GetPythonHome() now
calls Py_FatalError() on failure, instead of silently ignoring
failures.
* config_init_home() now gets directly _Py_path_config.home to only
get the value set by Py_SetPythonHome(), or NULL if
Py_SetPythonHome() was not called.
* config_get_program_name() now gets directly
_Py_path_config.program_name to only get the value set by
Py_SetProgramName(), or NULL if Py_SetProgramName() was not called.
* pymain_init_python() doesn't call Py_SetProgramName() anymore,
_PyPathConfig_Init() now always sets the program name
* Call _PyMainInterpreterConfig_Read() in
pymain_parse_cmdline_envvars_impl() to control the memory allocator
* C API documentation: it's no more safe to call Py_GetProgramName()
before Py_Initialize().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pathconfig.c | 161 | ||||
-rw-r--r-- | Python/pylifecycle.c | 7 |
2 files changed, 125 insertions, 43 deletions
diff --git a/Python/pathconfig.c b/Python/pathconfig.c index daa2227..6a03f7d 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,57 +35,64 @@ _PyPathConfig_Clear(_PyPathConfig *config) CLEAR(config->exec_prefix); #endif CLEAR(config->module_search_path); + CLEAR(config->home); + CLEAR(config->program_name); #undef CLEAR -} - -void -Py_SetProgramName(wchar_t *pn) -{ - if (pn && *pn) - progname = pn; + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); } -wchar_t * -Py_GetProgramName(void) +/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix() + and Py_GetProgramFullPath() */ +_PyInitError +_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) { - return progname; -} - - -void -Py_SetPythonHome(wchar_t *home) -{ - default_home = home; -} + if (_Py_path_config.module_search_path) { + /* Already initialized */ + return _Py_INIT_OK(); + } + _PyInitError err; + _PyPathConfig new_config = _PyPathConfig_INIT; -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]; + PyMemAllocatorEx old_alloc; + _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - if (default_home) { - return default_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; } - char *home = Py_GETENV("PYTHONHOME"); - if (!home) { - return NULL; + /* 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; } - 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; + new_config.program_name = _PyMem_RawWcsdup(main_config->program_name); + if (new_config.program_name == NULL) { + err = _Py_INIT_NO_MEMORY(); + goto done; } - return buffer; + _PyPathConfig_Clear(&_Py_path_config); + _Py_path_config = new_config; + + err = _Py_INIT_OK(); + +done: + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); + return err; } @@ -134,6 +141,9 @@ 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""); @@ -144,8 +154,58 @@ 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"); + } } @@ -184,6 +244,23 @@ 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 8d71154..523397f 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -804,7 +804,12 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *config) } if (config->program_name == NULL) { - config->program_name = _PyMem_RawWcsdup(Py_GetProgramName()); +#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); if (config->program_name == NULL) { return _Py_INIT_NO_MEMORY(); } |