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