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