Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1 | /* Path configuration like module_search_path (sys.path) */ |
| 2 | |
| 3 | #include "Python.h" |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 4 | #include "osdefs.h" // DELIM |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5 | #include "pycore_initconfig.h" |
Victor Stinner | 9fc57a3 | 2018-11-07 00:44:03 +0100 | [diff] [blame] | 6 | #include "pycore_fileutils.h" |
Victor Stinner | a1c249c | 2018-11-01 03:15:58 +0100 | [diff] [blame] | 7 | #include "pycore_pathconfig.h" |
Victor Stinner | d9ea5ca | 2020-04-15 02:57:50 +0200 | [diff] [blame] | 8 | #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() |
Nick Coghlan | d5d9e02 | 2018-03-25 23:03:10 +1000 | [diff] [blame] | 9 | #include <wchar.h> |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 10 | #ifdef MS_WINDOWS |
| 11 | # include <windows.h> // GetFullPathNameW(), MAX_PATH |
| 12 | #endif |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 13 | |
| 14 | #ifdef __cplusplus |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | |
| 19 | _PyPathConfig _Py_path_config = _PyPathConfig_INIT; |
| 20 | |
| 21 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 22 | static int |
| 23 | copy_wstr(wchar_t **dst, const wchar_t *src) |
| 24 | { |
Victor Stinner | 1ce152a | 2019-09-24 17:44:15 +0200 | [diff] [blame] | 25 | assert(*dst == NULL); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 26 | if (src != NULL) { |
| 27 | *dst = _PyMem_RawWcsdup(src); |
| 28 | if (*dst == NULL) { |
| 29 | return -1; |
| 30 | } |
| 31 | } |
| 32 | else { |
| 33 | *dst = NULL; |
| 34 | } |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | static void |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 40 | pathconfig_clear(_PyPathConfig *config) |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 41 | { |
| 42 | /* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator, |
| 43 | since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be |
| 44 | called before Py_Initialize() which can changes the memory allocator. */ |
| 45 | PyMemAllocatorEx old_alloc; |
| 46 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 47 | |
| 48 | #define CLEAR(ATTR) \ |
| 49 | do { \ |
| 50 | PyMem_RawFree(ATTR); \ |
| 51 | ATTR = NULL; \ |
| 52 | } while (0) |
| 53 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 54 | CLEAR(config->program_full_path); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 55 | CLEAR(config->prefix); |
Steve Dower | 177a41a | 2018-11-17 20:41:48 -0800 | [diff] [blame] | 56 | CLEAR(config->exec_prefix); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 57 | CLEAR(config->module_search_path); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 58 | CLEAR(config->program_name); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 59 | CLEAR(config->home); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 60 | #ifdef MS_WINDOWS |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 61 | CLEAR(config->base_executable); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 62 | #endif |
| 63 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 64 | #undef CLEAR |
| 65 | |
| 66 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 67 | } |
| 68 | |
| 69 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 70 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 71 | pathconfig_copy(_PyPathConfig *config, const _PyPathConfig *config2) |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 72 | { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 73 | pathconfig_clear(config); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 74 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 75 | #define COPY_ATTR(ATTR) \ |
| 76 | do { \ |
| 77 | if (copy_wstr(&config->ATTR, config2->ATTR) < 0) { \ |
| 78 | return _PyStatus_NO_MEMORY(); \ |
| 79 | } \ |
| 80 | } while (0) |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 81 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 82 | COPY_ATTR(program_full_path); |
| 83 | COPY_ATTR(prefix); |
| 84 | COPY_ATTR(exec_prefix); |
| 85 | COPY_ATTR(module_search_path); |
| 86 | COPY_ATTR(program_name); |
| 87 | COPY_ATTR(home); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 88 | #ifdef MS_WINDOWS |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 89 | config->isolated = config2->isolated; |
| 90 | config->site_import = config2->site_import; |
| 91 | COPY_ATTR(base_executable); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 92 | #endif |
Victor Stinner | e267793 | 2019-09-21 01:50:16 +0200 | [diff] [blame] | 93 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 94 | #undef COPY_ATTR |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 95 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 96 | return _PyStatus_OK(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 100 | void |
| 101 | _PyPathConfig_ClearGlobal(void) |
| 102 | { |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 103 | PyMemAllocatorEx old_alloc; |
| 104 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 105 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 106 | pathconfig_clear(&_Py_path_config); |
Victor Stinner | c183444 | 2019-03-18 22:24:28 +0100 | [diff] [blame] | 107 | |
| 108 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | static wchar_t* |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 113 | _PyWideStringList_Join(const PyWideStringList *list, wchar_t sep) |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 114 | { |
| 115 | size_t len = 1; /* NUL terminator */ |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 116 | for (Py_ssize_t i=0; i < list->length; i++) { |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 117 | if (i != 0) { |
| 118 | len++; |
| 119 | } |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 120 | len += wcslen(list->items[i]); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | wchar_t *text = PyMem_RawMalloc(len * sizeof(wchar_t)); |
| 124 | if (text == NULL) { |
| 125 | return NULL; |
| 126 | } |
| 127 | wchar_t *str = text; |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 128 | for (Py_ssize_t i=0; i < list->length; i++) { |
| 129 | wchar_t *path = list->items[i]; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 130 | if (i != 0) { |
Victor Stinner | 12f2f17 | 2019-09-26 15:51:50 +0200 | [diff] [blame] | 131 | *str++ = sep; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 132 | } |
| 133 | len = wcslen(path); |
| 134 | memcpy(str, path, len * sizeof(wchar_t)); |
| 135 | str += len; |
| 136 | } |
| 137 | *str = L'\0'; |
| 138 | |
| 139 | return text; |
| 140 | } |
| 141 | |
| 142 | |
Victor Stinner | c422167 | 2019-09-21 01:02:56 +0200 | [diff] [blame] | 143 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 144 | pathconfig_set_from_config(_PyPathConfig *pathconfig, const PyConfig *config) |
Victor Stinner | c422167 | 2019-09-21 01:02:56 +0200 | [diff] [blame] | 145 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 146 | PyStatus status; |
Victor Stinner | c422167 | 2019-09-21 01:02:56 +0200 | [diff] [blame] | 147 | PyMemAllocatorEx old_alloc; |
| 148 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 149 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 150 | if (config->module_search_paths_set) { |
Victor Stinner | 1ce152a | 2019-09-24 17:44:15 +0200 | [diff] [blame] | 151 | PyMem_RawFree(pathconfig->module_search_path); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 152 | pathconfig->module_search_path = _PyWideStringList_Join(&config->module_search_paths, DELIM); |
| 153 | if (pathconfig->module_search_path == NULL) { |
| 154 | goto no_memory; |
| 155 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 158 | #define COPY_CONFIG(PATH_ATTR, CONFIG_ATTR) \ |
| 159 | if (config->CONFIG_ATTR) { \ |
Victor Stinner | 1ce152a | 2019-09-24 17:44:15 +0200 | [diff] [blame] | 160 | PyMem_RawFree(pathconfig->PATH_ATTR); \ |
| 161 | pathconfig->PATH_ATTR = NULL; \ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 162 | if (copy_wstr(&pathconfig->PATH_ATTR, config->CONFIG_ATTR) < 0) { \ |
| 163 | goto no_memory; \ |
| 164 | } \ |
| 165 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 166 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 167 | COPY_CONFIG(program_full_path, executable); |
| 168 | COPY_CONFIG(prefix, prefix); |
| 169 | COPY_CONFIG(exec_prefix, exec_prefix); |
| 170 | COPY_CONFIG(program_name, program_name); |
| 171 | COPY_CONFIG(home, home); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 172 | #ifdef MS_WINDOWS |
| 173 | COPY_CONFIG(base_executable, base_executable); |
| 174 | #endif |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 175 | |
| 176 | #undef COPY_CONFIG |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 177 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 178 | status = _PyStatus_OK(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 179 | goto done; |
| 180 | |
| 181 | no_memory: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 182 | status = _PyStatus_NO_MEMORY(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 183 | |
| 184 | done: |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 185 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 186 | return status; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Victor Stinner | 8f42748 | 2020-07-08 00:20:37 +0200 | [diff] [blame] | 189 | PyObject * |
| 190 | _PyPathConfig_AsDict(void) |
| 191 | { |
| 192 | PyObject *dict = PyDict_New(); |
| 193 | if (dict == NULL) { |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | #define SET_ITEM(KEY, EXPR) \ |
| 198 | do { \ |
| 199 | PyObject *obj = (EXPR); \ |
| 200 | if (obj == NULL) { \ |
| 201 | goto fail; \ |
| 202 | } \ |
| 203 | int res = PyDict_SetItemString(dict, KEY, obj); \ |
| 204 | Py_DECREF(obj); \ |
| 205 | if (res < 0) { \ |
| 206 | goto fail; \ |
| 207 | } \ |
| 208 | } while (0) |
| 209 | #define SET_ITEM_STR(KEY) \ |
| 210 | SET_ITEM(#KEY, \ |
| 211 | (_Py_path_config.KEY \ |
| 212 | ? PyUnicode_FromWideChar(_Py_path_config.KEY, -1) \ |
| 213 | : (Py_INCREF(Py_None), Py_None))) |
| 214 | #define SET_ITEM_INT(KEY) \ |
| 215 | SET_ITEM(#KEY, PyLong_FromLong(_Py_path_config.KEY)) |
| 216 | |
| 217 | SET_ITEM_STR(program_full_path); |
| 218 | SET_ITEM_STR(prefix); |
| 219 | SET_ITEM_STR(exec_prefix); |
| 220 | SET_ITEM_STR(module_search_path); |
| 221 | SET_ITEM_STR(program_name); |
| 222 | SET_ITEM_STR(home); |
| 223 | #ifdef MS_WINDOWS |
| 224 | SET_ITEM_INT(isolated); |
| 225 | SET_ITEM_INT(site_import); |
| 226 | SET_ITEM_STR(base_executable); |
| 227 | |
| 228 | { |
| 229 | wchar_t py3path[MAX_PATH]; |
| 230 | HMODULE hPython3 = GetModuleHandleW(PY3_DLLNAME); |
| 231 | PyObject *obj; |
| 232 | if (hPython3 |
| 233 | && GetModuleFileNameW(hPython3, py3path, Py_ARRAY_LENGTH(py3path))) |
| 234 | { |
| 235 | obj = PyUnicode_FromWideChar(py3path, -1); |
| 236 | if (obj == NULL) { |
| 237 | goto fail; |
| 238 | } |
| 239 | } |
| 240 | else { |
| 241 | obj = Py_None; |
| 242 | Py_INCREF(obj); |
| 243 | } |
| 244 | if (PyDict_SetItemString(dict, "python3_dll", obj) < 0) { |
| 245 | Py_DECREF(obj); |
| 246 | goto fail; |
| 247 | } |
| 248 | Py_DECREF(obj); |
| 249 | } |
| 250 | #endif |
| 251 | |
| 252 | #undef SET_ITEM |
| 253 | #undef SET_ITEM_STR |
| 254 | #undef SET_ITEM_INT |
| 255 | |
| 256 | return dict; |
| 257 | |
| 258 | fail: |
| 259 | Py_DECREF(dict); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 263 | |
Victor Stinner | 12f2f17 | 2019-09-26 15:51:50 +0200 | [diff] [blame] | 264 | PyStatus |
| 265 | _PyConfig_WritePathConfig(const PyConfig *config) |
| 266 | { |
Victor Stinner | 12f2f17 | 2019-09-26 15:51:50 +0200 | [diff] [blame] | 267 | return pathconfig_set_from_config(&_Py_path_config, config); |
| 268 | } |
| 269 | |
| 270 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 271 | static PyStatus |
| 272 | config_init_module_search_paths(PyConfig *config, _PyPathConfig *pathconfig) |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 273 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 274 | assert(!config->module_search_paths_set); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 275 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 276 | _PyWideStringList_Clear(&config->module_search_paths); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 277 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 278 | const wchar_t *sys_path = pathconfig->module_search_path; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 279 | const wchar_t delim = DELIM; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 280 | while (1) { |
Alex Henrie | f3e5e95 | 2020-01-09 09:14:11 +0000 | [diff] [blame] | 281 | const wchar_t *p = wcschr(sys_path, delim); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 282 | if (p == NULL) { |
| 283 | p = sys_path + wcslen(sys_path); /* End of string */ |
| 284 | } |
| 285 | |
| 286 | size_t path_len = (p - sys_path); |
| 287 | wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t)); |
| 288 | if (path == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 289 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 290 | } |
| 291 | memcpy(path, sys_path, path_len * sizeof(wchar_t)); |
| 292 | path[path_len] = L'\0'; |
| 293 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 294 | PyStatus status = PyWideStringList_Append(&config->module_search_paths, path); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 295 | PyMem_RawFree(path); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 296 | if (_PyStatus_EXCEPTION(status)) { |
| 297 | return status; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (*p == '\0') { |
| 301 | break; |
| 302 | } |
| 303 | sys_path = p + 1; |
| 304 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 305 | config->module_search_paths_set = 1; |
| 306 | return _PyStatus_OK(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 310 | /* Calculate the path configuration: |
| 311 | |
| 312 | - exec_prefix |
| 313 | - module_search_path |
| 314 | - prefix |
| 315 | - program_full_path |
| 316 | |
| 317 | On Windows, more fields are calculated: |
| 318 | |
| 319 | - base_executable |
| 320 | - isolated |
| 321 | - site_import |
| 322 | |
| 323 | On other platforms, isolated and site_import are left unchanged, and |
| 324 | _PyConfig_InitPathConfig() copies executable to base_executable (if it's not |
| 325 | set). |
| 326 | |
| 327 | Priority, highest to lowest: |
| 328 | |
| 329 | - PyConfig |
| 330 | - _Py_path_config: set by Py_SetPath(), Py_SetPythonHome() |
| 331 | and Py_SetProgramName() |
| 332 | - _PyPathConfig_Calculate() |
| 333 | */ |
| 334 | static PyStatus |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 335 | pathconfig_init(_PyPathConfig *pathconfig, const PyConfig *config, |
| 336 | int compute_path_config) |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 337 | { |
| 338 | PyStatus status; |
| 339 | |
| 340 | PyMemAllocatorEx old_alloc; |
| 341 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 342 | |
| 343 | status = pathconfig_copy(pathconfig, &_Py_path_config); |
| 344 | if (_PyStatus_EXCEPTION(status)) { |
| 345 | goto done; |
| 346 | } |
| 347 | |
| 348 | status = pathconfig_set_from_config(pathconfig, config); |
| 349 | if (_PyStatus_EXCEPTION(status)) { |
| 350 | goto done; |
| 351 | } |
| 352 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 353 | if (compute_path_config) { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 354 | status = _PyPathConfig_Calculate(pathconfig, config); |
| 355 | } |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 356 | |
| 357 | done: |
| 358 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 359 | return status; |
| 360 | } |
| 361 | |
| 362 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 363 | static PyStatus |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 364 | config_init_pathconfig(PyConfig *config, int compute_path_config) |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 365 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 366 | _PyPathConfig pathconfig = _PyPathConfig_INIT; |
| 367 | PyStatus status; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 368 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 369 | status = pathconfig_init(&pathconfig, config, compute_path_config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 370 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 371 | goto done; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 374 | if (!config->module_search_paths_set |
| 375 | && pathconfig.module_search_path != NULL) |
| 376 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 377 | status = config_init_module_search_paths(config, &pathconfig); |
| 378 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 379 | goto done; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 383 | #define COPY_ATTR(PATH_ATTR, CONFIG_ATTR) \ |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 384 | if (config->CONFIG_ATTR == NULL && pathconfig.PATH_ATTR != NULL) { \ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 385 | if (copy_wstr(&config->CONFIG_ATTR, pathconfig.PATH_ATTR) < 0) { \ |
| 386 | goto no_memory; \ |
| 387 | } \ |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 388 | } |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 389 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 390 | #ifdef MS_WINDOWS |
| 391 | if (config->executable != NULL && config->base_executable == NULL) { |
| 392 | /* If executable is set explicitly in the configuration, |
| 393 | ignore calculated base_executable: _PyConfig_InitPathConfig() |
| 394 | will copy executable to base_executable */ |
| 395 | } |
| 396 | else { |
| 397 | COPY_ATTR(base_executable, base_executable); |
| 398 | } |
| 399 | #endif |
| 400 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 401 | COPY_ATTR(program_full_path, executable); |
| 402 | COPY_ATTR(prefix, prefix); |
| 403 | COPY_ATTR(exec_prefix, exec_prefix); |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 404 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 405 | #undef COPY_ATTR |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 406 | |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 407 | #ifdef MS_WINDOWS |
| 408 | /* If a ._pth file is found: isolated and site_import are overriden */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 409 | if (pathconfig.isolated != -1) { |
| 410 | config->isolated = pathconfig.isolated; |
Victor Stinner | f2626ce | 2018-07-21 03:54:20 +0200 | [diff] [blame] | 411 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 412 | if (pathconfig.site_import != -1) { |
| 413 | config->site_import = pathconfig.site_import; |
Victor Stinner | f2626ce | 2018-07-21 03:54:20 +0200 | [diff] [blame] | 414 | } |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 415 | #endif |
Victor Stinner | f2626ce | 2018-07-21 03:54:20 +0200 | [diff] [blame] | 416 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 417 | status = _PyStatus_OK(); |
| 418 | goto done; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 419 | |
| 420 | no_memory: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 421 | status = _PyStatus_NO_MEMORY(); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 422 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 423 | done: |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 424 | pathconfig_clear(&pathconfig); |
| 425 | return status; |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 429 | PyStatus |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 430 | _PyConfig_InitPathConfig(PyConfig *config, int compute_path_config) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 431 | { |
| 432 | /* Do we need to calculate the path? */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 433 | if (!config->module_search_paths_set |
Victor Stinner | 8bf39b6 | 2019-09-26 02:22:35 +0200 | [diff] [blame] | 434 | || config->executable == NULL |
| 435 | || config->prefix == NULL |
| 436 | || config->exec_prefix == NULL) |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 437 | { |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 438 | PyStatus status = config_init_pathconfig(config, compute_path_config); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 439 | if (_PyStatus_EXCEPTION(status)) { |
| 440 | return status; |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 444 | if (config->base_prefix == NULL && config->prefix != NULL) { |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 445 | if (copy_wstr(&config->base_prefix, config->prefix) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 446 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 450 | if (config->base_exec_prefix == NULL && config->exec_prefix != NULL) { |
Steve Dower | 177a41a | 2018-11-17 20:41:48 -0800 | [diff] [blame] | 451 | if (copy_wstr(&config->base_exec_prefix, |
| 452 | config->exec_prefix) < 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 453 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 454 | } |
| 455 | } |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 456 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 457 | if (config->base_executable == NULL && config->executable != NULL) { |
Steve Dower | 9048c49 | 2019-06-29 10:34:11 -0700 | [diff] [blame] | 458 | if (copy_wstr(&config->base_executable, |
| 459 | config->executable) < 0) { |
| 460 | return _PyStatus_NO_MEMORY(); |
| 461 | } |
| 462 | } |
| 463 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 464 | return _PyStatus_OK(); |
Victor Stinner | 1dc6e39 | 2018-07-25 02:49:17 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 468 | /* External interface */ |
| 469 | |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 470 | static void _Py_NO_RETURN |
| 471 | path_out_of_memory(const char *func) |
| 472 | { |
| 473 | _Py_FatalErrorFunc(func, "out of memory"); |
| 474 | } |
| 475 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 476 | void |
| 477 | Py_SetPath(const wchar_t *path) |
| 478 | { |
| 479 | if (path == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 480 | pathconfig_clear(&_Py_path_config); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
| 484 | PyMemAllocatorEx old_alloc; |
| 485 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 486 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 487 | PyMem_RawFree(_Py_path_config.prefix); |
| 488 | PyMem_RawFree(_Py_path_config.exec_prefix); |
| 489 | PyMem_RawFree(_Py_path_config.module_search_path); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 490 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 491 | _Py_path_config.prefix = _PyMem_RawWcsdup(L""); |
| 492 | _Py_path_config.exec_prefix = _PyMem_RawWcsdup(L""); |
| 493 | _Py_path_config.module_search_path = _PyMem_RawWcsdup(path); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 494 | |
| 495 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 496 | |
Victor Stinner | ace3f9a | 2020-11-10 21:10:22 +0100 | [diff] [blame] | 497 | if (_Py_path_config.prefix == NULL |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 498 | || _Py_path_config.exec_prefix == NULL |
| 499 | || _Py_path_config.module_search_path == NULL) |
| 500 | { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 501 | path_out_of_memory(__func__); |
Victor Stinner | b1147e4 | 2018-07-21 02:06:16 +0200 | [diff] [blame] | 502 | } |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | |
| 506 | void |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 507 | Py_SetPythonHome(const wchar_t *home) |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 508 | { |
| 509 | if (home == NULL) { |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | PyMemAllocatorEx old_alloc; |
| 514 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 515 | |
| 516 | PyMem_RawFree(_Py_path_config.home); |
| 517 | _Py_path_config.home = _PyMem_RawWcsdup(home); |
| 518 | |
| 519 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 520 | |
| 521 | if (_Py_path_config.home == NULL) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 522 | path_out_of_memory(__func__); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | |
| 527 | void |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 528 | Py_SetProgramName(const wchar_t *program_name) |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 529 | { |
| 530 | if (program_name == NULL || program_name[0] == L'\0') { |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | PyMemAllocatorEx old_alloc; |
| 535 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 536 | |
| 537 | PyMem_RawFree(_Py_path_config.program_name); |
| 538 | _Py_path_config.program_name = _PyMem_RawWcsdup(program_name); |
| 539 | |
| 540 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 541 | |
| 542 | if (_Py_path_config.program_name == NULL) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 543 | path_out_of_memory(__func__); |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Steve Dower | 177a41a | 2018-11-17 20:41:48 -0800 | [diff] [blame] | 547 | void |
| 548 | _Py_SetProgramFullPath(const wchar_t *program_full_path) |
| 549 | { |
| 550 | if (program_full_path == NULL || program_full_path[0] == L'\0') { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | PyMemAllocatorEx old_alloc; |
| 555 | _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 556 | |
| 557 | PyMem_RawFree(_Py_path_config.program_full_path); |
| 558 | _Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path); |
| 559 | |
| 560 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); |
| 561 | |
| 562 | if (_Py_path_config.program_full_path == NULL) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 563 | path_out_of_memory(__func__); |
Steve Dower | 177a41a | 2018-11-17 20:41:48 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 567 | |
| 568 | wchar_t * |
| 569 | Py_GetPath(void) |
| 570 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 571 | return _Py_path_config.module_search_path; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | wchar_t * |
| 576 | Py_GetPrefix(void) |
| 577 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 578 | return _Py_path_config.prefix; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | wchar_t * |
| 583 | Py_GetExecPrefix(void) |
| 584 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 585 | return _Py_path_config.exec_prefix; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
| 589 | wchar_t * |
| 590 | Py_GetProgramFullPath(void) |
| 591 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 592 | return _Py_path_config.program_full_path; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | wchar_t* |
| 597 | Py_GetPythonHome(void) |
| 598 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 599 | return _Py_path_config.home; |
| 600 | } |
| 601 | |
| 602 | |
| 603 | wchar_t * |
| 604 | Py_GetProgramName(void) |
| 605 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 606 | return _Py_path_config.program_name; |
| 607 | } |
| 608 | |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 609 | /* Compute module search path from argv[0] or the current working |
| 610 | directory ("-m module" case) which will be prepended to sys.argv: |
| 611 | sys.path[0]. |
| 612 | |
Victor Stinner | dbacfc2 | 2019-05-16 16:39:26 +0200 | [diff] [blame] | 613 | Return 1 if the path is correctly resolved and written into *path0_p. |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 614 | |
Victor Stinner | dbacfc2 | 2019-05-16 16:39:26 +0200 | [diff] [blame] | 615 | Return 0 if it fails to resolve the full path. For example, return 0 if the |
| 616 | current working directory has been removed (bpo-36236) or if argv is empty. |
| 617 | |
| 618 | Raise an exception and return -1 on error. |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 619 | */ |
| 620 | int |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 621 | _PyPathConfig_ComputeSysPath0(const PyWideStringList *argv, PyObject **path0_p) |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 622 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 623 | assert(_PyWideStringList_CheckConsistency(argv)); |
Victor Stinner | 74f6568 | 2019-03-15 15:08:05 +0100 | [diff] [blame] | 624 | |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 625 | if (argv->length == 0) { |
| 626 | /* Leave sys.path unchanged if sys.argv is empty */ |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | wchar_t *argv0 = argv->items[0]; |
| 631 | int have_module_arg = (wcscmp(argv0, L"-m") == 0); |
| 632 | int have_script_arg = (!have_module_arg && (wcscmp(argv0, L"-c") != 0)); |
| 633 | |
| 634 | wchar_t *path0 = argv0; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 635 | Py_ssize_t n = 0; |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 636 | |
| 637 | #ifdef HAVE_REALPATH |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 638 | wchar_t fullpath[MAXPATHLEN]; |
| 639 | #elif defined(MS_WINDOWS) |
| 640 | wchar_t fullpath[MAX_PATH]; |
| 641 | #endif |
| 642 | |
Nick Coghlan | d5d9e02 | 2018-03-25 23:03:10 +1000 | [diff] [blame] | 643 | if (have_module_arg) { |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 644 | #if defined(HAVE_REALPATH) || defined(MS_WINDOWS) |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 645 | if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) { |
| 646 | return 0; |
| 647 | } |
| 648 | path0 = fullpath; |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 649 | #else |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 650 | path0 = L"."; |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 651 | #endif |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 652 | n = wcslen(path0); |
Nick Coghlan | d5d9e02 | 2018-03-25 23:03:10 +1000 | [diff] [blame] | 653 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 654 | |
| 655 | #ifdef HAVE_READLINK |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 656 | wchar_t link[MAXPATHLEN + 1]; |
| 657 | int nr = 0; |
Gregory P. Smith | 81328f3 | 2020-06-22 00:27:20 -0700 | [diff] [blame] | 658 | wchar_t path0copy[2 * MAXPATHLEN + 1]; |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 659 | |
| 660 | if (have_script_arg) { |
| 661 | nr = _Py_wreadlink(path0, link, Py_ARRAY_LENGTH(link)); |
| 662 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 663 | if (nr > 0) { |
| 664 | /* It's a symlink */ |
| 665 | link[nr] = '\0'; |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 666 | if (link[0] == SEP) { |
| 667 | path0 = link; /* Link to absolute path */ |
| 668 | } |
| 669 | else if (wcschr(link, SEP) == NULL) { |
| 670 | /* Link without path */ |
| 671 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 672 | else { |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 673 | /* Must join(dirname(path0), link) */ |
| 674 | wchar_t *q = wcsrchr(path0, SEP); |
| 675 | if (q == NULL) { |
| 676 | /* path0 without path */ |
| 677 | path0 = link; |
| 678 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 679 | else { |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 680 | /* Must make a copy, path0copy has room for 2 * MAXPATHLEN */ |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 681 | wcsncpy(path0copy, path0, MAXPATHLEN); |
| 682 | q = wcsrchr(path0copy, SEP); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 683 | wcsncpy(q+1, link, MAXPATHLEN); |
| 684 | q[MAXPATHLEN + 1] = L'\0'; |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 685 | path0 = path0copy; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | } |
| 689 | #endif /* HAVE_READLINK */ |
| 690 | |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 691 | wchar_t *p = NULL; |
| 692 | |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 693 | #if SEP == '\\' |
| 694 | /* Special case for Microsoft filename syntax */ |
Nick Coghlan | d5d9e02 | 2018-03-25 23:03:10 +1000 | [diff] [blame] | 695 | if (have_script_arg) { |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 696 | wchar_t *q; |
| 697 | #if defined(MS_WINDOWS) |
| 698 | /* Replace the first element in argv with the full path. */ |
| 699 | wchar_t *ptemp; |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 700 | if (GetFullPathNameW(path0, |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 701 | Py_ARRAY_LENGTH(fullpath), |
| 702 | fullpath, |
| 703 | &ptemp)) { |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 704 | path0 = fullpath; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 705 | } |
| 706 | #endif |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 707 | p = wcsrchr(path0, SEP); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 708 | /* Test for alternate separator */ |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 709 | q = wcsrchr(p ? p : path0, '/'); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 710 | if (q != NULL) |
| 711 | p = q; |
| 712 | if (p != NULL) { |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 713 | n = p + 1 - path0; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 714 | if (n > 1 && p[-1] != ':') |
| 715 | n--; /* Drop trailing separator */ |
| 716 | } |
| 717 | } |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 718 | #else |
| 719 | /* All other filename syntaxes */ |
Nick Coghlan | d5d9e02 | 2018-03-25 23:03:10 +1000 | [diff] [blame] | 720 | if (have_script_arg) { |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 721 | #if defined(HAVE_REALPATH) |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 722 | if (_Py_wrealpath(path0, fullpath, Py_ARRAY_LENGTH(fullpath))) { |
| 723 | path0 = fullpath; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 724 | } |
| 725 | #endif |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 726 | p = wcsrchr(path0, SEP); |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 727 | } |
| 728 | if (p != NULL) { |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 729 | n = p + 1 - path0; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 730 | #if SEP == '/' /* Special case for Unix filename syntax */ |
Victor Stinner | fc96e54 | 2019-03-19 18:22:55 +0100 | [diff] [blame] | 731 | if (n > 1) { |
| 732 | /* Drop trailing separator */ |
| 733 | n--; |
| 734 | } |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 735 | #endif /* Unix */ |
| 736 | } |
| 737 | #endif /* All others */ |
| 738 | |
Victor Stinner | dbacfc2 | 2019-05-16 16:39:26 +0200 | [diff] [blame] | 739 | PyObject *path0_obj = PyUnicode_FromWideChar(path0, n); |
| 740 | if (path0_obj == NULL) { |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | *path0_p = path0_obj; |
Victor Stinner | dcf6171 | 2019-03-19 16:09:27 +0100 | [diff] [blame] | 745 | return 1; |
Victor Stinner | 11a247d | 2017-12-13 21:05:57 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 748 | |
Minmin Gong | 8ebc645 | 2019-02-02 20:26:55 -0800 | [diff] [blame] | 749 | #ifdef MS_WINDOWS |
| 750 | #define WCSTOK wcstok_s |
| 751 | #else |
| 752 | #define WCSTOK wcstok |
| 753 | #endif |
| 754 | |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 755 | /* Search for a prefix value in an environment file (pyvenv.cfg). |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 756 | |
| 757 | - If found, copy it into *value_p: string which must be freed by |
| 758 | PyMem_RawFree(). |
| 759 | - If not found, *value_p is set to NULL. |
| 760 | */ |
| 761 | PyStatus |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 762 | _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 763 | wchar_t **value_p) |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 764 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 765 | *value_p = NULL; |
| 766 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 767 | char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */ |
| 768 | buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0'; |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 769 | |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 770 | while (!feof(env_file)) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 771 | char * p = fgets(buffer, Py_ARRAY_LENGTH(buffer) - 1, env_file); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 772 | |
| 773 | if (p == NULL) { |
| 774 | break; |
| 775 | } |
Victor Stinner | 05d68a8 | 2018-01-18 11:15:25 +0100 | [diff] [blame] | 776 | |
| 777 | size_t n = strlen(p); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 778 | if (p[n - 1] != '\n') { |
| 779 | /* line has overflowed - bail */ |
| 780 | break; |
| 781 | } |
| 782 | if (p[0] == '#') { |
| 783 | /* Comment - skip */ |
| 784 | continue; |
| 785 | } |
Victor Stinner | 05d68a8 | 2018-01-18 11:15:25 +0100 | [diff] [blame] | 786 | |
Victor Stinner | 5f9cf23 | 2019-03-19 01:46:25 +0100 | [diff] [blame] | 787 | wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL); |
Victor Stinner | 7ed7aea | 2018-01-15 10:45:49 +0100 | [diff] [blame] | 788 | if (tmpbuffer) { |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 789 | wchar_t * state; |
Minmin Gong | 8ebc645 | 2019-02-02 20:26:55 -0800 | [diff] [blame] | 790 | wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 791 | if ((tok != NULL) && !wcscmp(tok, key)) { |
Minmin Gong | 8ebc645 | 2019-02-02 20:26:55 -0800 | [diff] [blame] | 792 | tok = WCSTOK(NULL, L" \t", &state); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 793 | if ((tok != NULL) && !wcscmp(tok, L"=")) { |
Minmin Gong | 8ebc645 | 2019-02-02 20:26:55 -0800 | [diff] [blame] | 794 | tok = WCSTOK(NULL, L"\r\n", &state); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 795 | if (tok != NULL) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 796 | *value_p = _PyMem_RawWcsdup(tok); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 797 | PyMem_RawFree(tmpbuffer); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 798 | |
| 799 | if (*value_p == NULL) { |
| 800 | return _PyStatus_NO_MEMORY(); |
| 801 | } |
| 802 | |
| 803 | /* found */ |
| 804 | return _PyStatus_OK(); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | PyMem_RawFree(tmpbuffer); |
| 809 | } |
| 810 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 811 | |
| 812 | /* not found */ |
| 813 | return _PyStatus_OK(); |
Victor Stinner | 9bee329 | 2017-12-21 16:49:13 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 816 | #ifdef __cplusplus |
| 817 | } |
| 818 | #endif |