diff options
| author | shadchin <[email protected]> | 2022-04-18 12:39:32 +0300 |
|---|---|---|
| committer | shadchin <[email protected]> | 2022-04-18 12:39:32 +0300 |
| commit | d4be68e361f4258cf0848fc70018dfe37a2acc24 (patch) | |
| tree | 153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Python/pathconfig.c | |
| parent | 260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff) | |
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Python/pathconfig.c')
| -rw-r--r-- | contrib/tools/python3/src/Python/pathconfig.c | 163 |
1 files changed, 89 insertions, 74 deletions
diff --git a/contrib/tools/python3/src/Python/pathconfig.c b/contrib/tools/python3/src/Python/pathconfig.c index b2ce86e64ce..1017a571f2b 100644 --- a/contrib/tools/python3/src/Python/pathconfig.c +++ b/contrib/tools/python3/src/Python/pathconfig.c @@ -186,6 +186,80 @@ done: return status; } +PyObject * +_PyPathConfig_AsDict(void) +{ + PyObject *dict = PyDict_New(); + if (dict == NULL) { + return NULL; + } + +#define SET_ITEM(KEY, EXPR) \ + do { \ + PyObject *obj = (EXPR); \ + if (obj == NULL) { \ + goto fail; \ + } \ + int res = PyDict_SetItemString(dict, KEY, obj); \ + Py_DECREF(obj); \ + if (res < 0) { \ + goto fail; \ + } \ + } while (0) +#define SET_ITEM_STR(KEY) \ + SET_ITEM(#KEY, \ + (_Py_path_config.KEY \ + ? PyUnicode_FromWideChar(_Py_path_config.KEY, -1) \ + : (Py_INCREF(Py_None), Py_None))) +#define SET_ITEM_INT(KEY) \ + SET_ITEM(#KEY, PyLong_FromLong(_Py_path_config.KEY)) + + SET_ITEM_STR(program_full_path); + SET_ITEM_STR(prefix); + SET_ITEM_STR(exec_prefix); + SET_ITEM_STR(module_search_path); + SET_ITEM_STR(program_name); + SET_ITEM_STR(home); +#ifdef MS_WINDOWS + SET_ITEM_INT(isolated); + SET_ITEM_INT(site_import); + SET_ITEM_STR(base_executable); + + { + wchar_t py3path[MAX_PATH]; + HMODULE hPython3 = GetModuleHandleW(PY3_DLLNAME); + PyObject *obj; + if (hPython3 + && GetModuleFileNameW(hPython3, py3path, Py_ARRAY_LENGTH(py3path))) + { + obj = PyUnicode_FromWideChar(py3path, -1); + if (obj == NULL) { + goto fail; + } + } + else { + obj = Py_None; + Py_INCREF(obj); + } + if (PyDict_SetItemString(dict, "python3_dll", obj) < 0) { + Py_DECREF(obj); + goto fail; + } + Py_DECREF(obj); + } +#endif + +#undef SET_ITEM +#undef SET_ITEM_STR +#undef SET_ITEM_INT + + return dict; + +fail: + Py_DECREF(dict); + return NULL; +} + PyStatus _PyConfig_WritePathConfig(const PyConfig *config) @@ -258,7 +332,8 @@ config_init_module_search_paths(PyConfig *config, _PyPathConfig *pathconfig) - _PyPathConfig_Calculate() */ static PyStatus -pathconfig_calculate(_PyPathConfig *pathconfig, const PyConfig *config) +pathconfig_init(_PyPathConfig *pathconfig, const PyConfig *config, + int compute_path_config) { PyStatus status; @@ -275,12 +350,9 @@ pathconfig_calculate(_PyPathConfig *pathconfig, const PyConfig *config) goto done; } - if (_Py_path_config.module_search_path == NULL) { + if (compute_path_config) { status = _PyPathConfig_Calculate(pathconfig, config); } - else { - /* Py_SetPath() has been called: avoid _PyPathConfig_Calculate() */ - } done: PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); @@ -289,17 +361,19 @@ done: static PyStatus -config_calculate_pathconfig(PyConfig *config) +config_init_pathconfig(PyConfig *config, int compute_path_config) { _PyPathConfig pathconfig = _PyPathConfig_INIT; PyStatus status; - status = pathconfig_calculate(&pathconfig, config); + status = pathconfig_init(&pathconfig, config, compute_path_config); if (_PyStatus_EXCEPTION(status)) { goto done; } - if (!config->module_search_paths_set) { + if (!config->module_search_paths_set + && pathconfig.module_search_path != NULL) + { status = config_init_module_search_paths(config, &pathconfig); if (_PyStatus_EXCEPTION(status)) { goto done; @@ -307,7 +381,7 @@ config_calculate_pathconfig(PyConfig *config) } #define COPY_ATTR(PATH_ATTR, CONFIG_ATTR) \ - if (config->CONFIG_ATTR == NULL) { \ + if (config->CONFIG_ATTR == NULL && pathconfig.PATH_ATTR != NULL) { \ if (copy_wstr(&config->CONFIG_ATTR, pathconfig.PATH_ATTR) < 0) { \ goto no_memory; \ } \ @@ -353,7 +427,7 @@ done: PyStatus -_PyConfig_InitPathConfig(PyConfig *config) +_PyConfig_InitPathConfig(PyConfig *config, int compute_path_config) { /* Do we need to calculate the path? */ if (!config->module_search_paths_set @@ -361,26 +435,26 @@ _PyConfig_InitPathConfig(PyConfig *config) || config->prefix == NULL || config->exec_prefix == NULL) { - PyStatus status = config_calculate_pathconfig(config); + PyStatus status = config_init_pathconfig(config, compute_path_config); if (_PyStatus_EXCEPTION(status)) { return status; } } - if (config->base_prefix == NULL) { + if (config->base_prefix == NULL && config->prefix != NULL) { if (copy_wstr(&config->base_prefix, config->prefix) < 0) { return _PyStatus_NO_MEMORY(); } } - if (config->base_exec_prefix == NULL) { + if (config->base_exec_prefix == NULL && config->exec_prefix != NULL) { if (copy_wstr(&config->base_exec_prefix, config->exec_prefix) < 0) { return _PyStatus_NO_MEMORY(); } } - if (config->base_executable == NULL) { + if (config->base_executable == NULL && config->executable != NULL) { if (copy_wstr(&config->base_executable, config->executable) < 0) { return _PyStatus_NO_MEMORY(); @@ -391,53 +465,6 @@ _PyConfig_InitPathConfig(PyConfig *config) } -static PyStatus -pathconfig_global_read(_PyPathConfig *pathconfig) -{ - PyConfig config; - _PyConfig_InitCompatConfig(&config); - - /* Call _PyConfig_InitPathConfig() */ - PyStatus status = PyConfig_Read(&config); - if (_PyStatus_EXCEPTION(status)) { - goto done; - } - - status = pathconfig_set_from_config(pathconfig, &config); - -done: - PyConfig_Clear(&config); - return status; -} - - -static void -pathconfig_global_init(void) -{ - PyStatus status; - - if (_Py_path_config.module_search_path == NULL) { - status = pathconfig_global_read(&_Py_path_config); - if (_PyStatus_EXCEPTION(status)) { - Py_ExitStatusException(status); - } - } - else { - /* Global configuration already initialized */ - } - - assert(_Py_path_config.program_full_path != NULL); - assert(_Py_path_config.prefix != NULL); - assert(_Py_path_config.exec_prefix != NULL); - assert(_Py_path_config.module_search_path != NULL); - assert(_Py_path_config.program_name != NULL); - /* home can be NULL */ -#ifdef MS_WINDOWS - assert(_Py_path_config.base_executable != NULL); -#endif -} - - /* External interface */ static void _Py_NO_RETURN @@ -457,23 +484,17 @@ Py_SetPath(const wchar_t *path) PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - /* Getting the program full path calls pathconfig_global_init() */ - wchar_t *program_full_path = _PyMem_RawWcsdup(Py_GetProgramFullPath()); - - PyMem_RawFree(_Py_path_config.program_full_path); PyMem_RawFree(_Py_path_config.prefix); PyMem_RawFree(_Py_path_config.exec_prefix); PyMem_RawFree(_Py_path_config.module_search_path); - _Py_path_config.program_full_path = program_full_path; _Py_path_config.prefix = _PyMem_RawWcsdup(L""); _Py_path_config.exec_prefix = _PyMem_RawWcsdup(L""); _Py_path_config.module_search_path = _PyMem_RawWcsdup(path); PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); - if (_Py_path_config.program_full_path == NULL - || _Py_path_config.prefix == NULL + if (_Py_path_config.prefix == NULL || _Py_path_config.exec_prefix == NULL || _Py_path_config.module_search_path == NULL) { @@ -547,7 +568,6 @@ _Py_SetProgramFullPath(const wchar_t *program_full_path) wchar_t * Py_GetPath(void) { - pathconfig_global_init(); return _Py_path_config.module_search_path; } @@ -555,7 +575,6 @@ Py_GetPath(void) wchar_t * Py_GetPrefix(void) { - pathconfig_global_init(); return _Py_path_config.prefix; } @@ -563,7 +582,6 @@ Py_GetPrefix(void) wchar_t * Py_GetExecPrefix(void) { - pathconfig_global_init(); return _Py_path_config.exec_prefix; } @@ -571,7 +589,6 @@ Py_GetExecPrefix(void) wchar_t * Py_GetProgramFullPath(void) { - pathconfig_global_init(); return _Py_path_config.program_full_path; } @@ -579,7 +596,6 @@ Py_GetProgramFullPath(void) wchar_t* Py_GetPythonHome(void) { - pathconfig_global_init(); return _Py_path_config.home; } @@ -587,7 +603,6 @@ Py_GetPythonHome(void) wchar_t * Py_GetProgramName(void) { - pathconfig_global_init(); return _Py_path_config.program_name; } |
