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