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