diff options
author | Victor Stinner <vstinner@python.org> | 2020-07-07 22:20:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 22:20:37 (GMT) |
commit | 8f42748ded5e978fe8a924115179d45a74a6363b (patch) | |
tree | 73ccdf0457e669e8584856dd1cd087fb6b542379 /Python | |
parent | 6ae2780be0667a8dc52c4fb583171ec86067d700 (diff) | |
download | cpython-8f42748ded5e978fe8a924115179d45a74a6363b.zip cpython-8f42748ded5e978fe8a924115179d45a74a6363b.tar.gz cpython-8f42748ded5e978fe8a924115179d45a74a6363b.tar.bz2 |
bpo-29778: test_embed tests the path configuration (GH-21306)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/initconfig.c | 14 | ||||
-rw-r--r-- | Python/pathconfig.c | 74 |
2 files changed, 85 insertions, 3 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c index 86285c7..6428676 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -868,9 +868,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2) static PyObject * config_as_dict(const PyConfig *config) { - PyObject *dict; - - dict = PyDict_New(); + PyObject *dict = PyDict_New(); if (dict == NULL) { return NULL; } @@ -2643,6 +2641,16 @@ _Py_GetConfigsAsDict(void) } Py_CLEAR(dict); + /* path config */ + dict = _PyPathConfig_AsDict(); + if (dict == NULL) { + goto error; + } + if (PyDict_SetItemString(result, "path_config", dict) < 0) { + goto error; + } + Py_CLEAR(dict); + return result; error: diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 9a30221..12a684a 100644 --- a/Python/pathconfig.c +++ b/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) |