diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-12-01 19:50:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-01 19:50:58 (GMT) |
commit | 0ea395ae964c9cd0f499e2ef0d0030c971201220 (patch) | |
tree | 92e86e1b1754d179c8dc4b397f4fa33e020a3aee /PC/getpathp.c | |
parent | ebac19dad6263141d5db0a2c923efe049dba99d2 (diff) | |
download | cpython-0ea395ae964c9cd0f499e2ef0d0030c971201220.zip cpython-0ea395ae964c9cd0f499e2ef0d0030c971201220.tar.gz cpython-0ea395ae964c9cd0f499e2ef0d0030c971201220.tar.bz2 |
bpo-32030: Add Python/pathconfig.c (#4668)
* Factorize code from PC/getpathp.c and Modules/getpath.c to remove
duplicated code
* rename pathconfig_clear() to _PyPathConfig_Clear()
* Inline _PyPathConfig_Fini() in pymain_impl() and then remove it,
since it's a oneliner
Diffstat (limited to 'PC/getpathp.c')
-rw-r--r-- | PC/getpathp.c | 119 |
1 files changed, 1 insertions, 118 deletions
diff --git a/PC/getpathp.c b/PC/getpathp.c index f8cfd7e..3a0ebc1 100644 --- a/PC/getpathp.c +++ b/PC/getpathp.c @@ -130,9 +130,6 @@ typedef struct { } PyCalculatePath; -static _PyPathConfig _Py_path_config = _PyPathConfig_INIT; - - /* determine if "ch" is a separator character */ static int is_sep(wchar_t ch) @@ -1061,23 +1058,6 @@ 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 @@ -1108,110 +1088,13 @@ _PyPathConfig_Init(const _PyMainInterpreterConfig *main_config) done: if (_Py_INIT_FAILED(err)) { - pathconfig_clear(&new_path_config); + _PyPathConfig_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 |