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