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