blob: 41ef7d28d06676fe66f0b395e0de34ad26904403 [file] [log] [blame]
Victor Stinner31a83932017-12-04 13:39:15 +01001/* Path configuration like module_search_path (sys.path) */
2
3#include "Python.h"
4#include "osdefs.h"
Victor Stinner331a6a52019-05-27 16:39:22 +02005#include "pycore_initconfig.h"
Victor Stinner9fc57a32018-11-07 00:44:03 +01006#include "pycore_fileutils.h"
Victor Stinnera1c249c2018-11-01 03:15:58 +01007#include "pycore_pathconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01008#include "pycore_pymem.h"
9#include "pycore_pystate.h"
Nick Coghland5d9e022018-03-25 23:03:10 +100010#include <wchar.h>
Victor Stinner31a83932017-12-04 13:39:15 +010011
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16
17_PyPathConfig _Py_path_config = _PyPathConfig_INIT;
Victor Stinner9f3dcf82019-09-21 02:13:14 +020018#ifdef MS_WINDOWS
19wchar_t *_Py_dll_path = NULL;
20#endif
Victor Stinner31a83932017-12-04 13:39:15 +010021
22
Victor Stinnerb1147e42018-07-21 02:06:16 +020023static int
24copy_wstr(wchar_t **dst, const wchar_t *src)
25{
Victor Stinner96c84752019-09-26 16:17:34 +020026 assert(*dst == NULL);
Victor Stinnerb1147e42018-07-21 02:06:16 +020027 if (src != NULL) {
28 *dst = _PyMem_RawWcsdup(src);
29 if (*dst == NULL) {
30 return -1;
31 }
32 }
33 else {
34 *dst = NULL;
35 }
36 return 0;
37}
38
39
40static void
Victor Stinner331a6a52019-05-27 16:39:22 +020041pathconfig_clear(_PyPathConfig *config)
Victor Stinner31a83932017-12-04 13:39:15 +010042{
43 /* _PyMem_SetDefaultAllocator() is needed to get a known memory allocator,
44 since Py_SetPath(), Py_SetPythonHome() and Py_SetProgramName() can be
45 called before Py_Initialize() which can changes the memory allocator. */
46 PyMemAllocatorEx old_alloc;
47 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
48
49#define CLEAR(ATTR) \
50 do { \
51 PyMem_RawFree(ATTR); \
52 ATTR = NULL; \
53 } while (0)
54
Victor Stinner31a83932017-12-04 13:39:15 +010055 CLEAR(config->program_full_path);
Victor Stinner3f5409a2019-09-23 19:50:27 +020056 CLEAR(config->prefix);
Steve Dower177a41a2018-11-17 20:41:48 -080057 CLEAR(config->exec_prefix);
Victor Stinner31a83932017-12-04 13:39:15 +010058 CLEAR(config->module_search_path);
Victor Stinner31a83932017-12-04 13:39:15 +010059 CLEAR(config->program_name);
Victor Stinner3f5409a2019-09-23 19:50:27 +020060 CLEAR(config->home);
Victor Stinner96c84752019-09-26 16:17:34 +020061#ifdef MS_WINDOWS
Steve Dower323e7432019-06-29 14:28:59 -070062 CLEAR(config->base_executable);
Victor Stinner96c84752019-09-26 16:17:34 +020063#endif
64
Victor Stinner31a83932017-12-04 13:39:15 +010065#undef CLEAR
66
67 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
68}
69
70
Victor Stinner331a6a52019-05-27 16:39:22 +020071static PyStatus
Victor Stinner3f5409a2019-09-23 19:50:27 +020072pathconfig_copy(_PyPathConfig *config, const _PyPathConfig *config2)
Victor Stinner31a83932017-12-04 13:39:15 +010073{
Victor Stinner3f5409a2019-09-23 19:50:27 +020074 pathconfig_clear(config);
Victor Stinner31a83932017-12-04 13:39:15 +010075
Victor Stinner3f5409a2019-09-23 19:50:27 +020076#define COPY_ATTR(ATTR) \
77 do { \
78 if (copy_wstr(&config->ATTR, config2->ATTR) < 0) { \
79 return _PyStatus_NO_MEMORY(); \
80 } \
81 } while (0)
Victor Stinner31a83932017-12-04 13:39:15 +010082
Victor Stinner3f5409a2019-09-23 19:50:27 +020083 COPY_ATTR(program_full_path);
84 COPY_ATTR(prefix);
85 COPY_ATTR(exec_prefix);
86 COPY_ATTR(module_search_path);
87 COPY_ATTR(program_name);
88 COPY_ATTR(home);
Victor Stinner96c84752019-09-26 16:17:34 +020089#ifdef MS_WINDOWS
Victor Stinner3f5409a2019-09-23 19:50:27 +020090 config->isolated = config2->isolated;
91 config->site_import = config2->site_import;
92 COPY_ATTR(base_executable);
Victor Stinner96c84752019-09-26 16:17:34 +020093#endif
Victor Stinner9f3dcf82019-09-21 02:13:14 +020094
Victor Stinner3f5409a2019-09-23 19:50:27 +020095#undef COPY_ATTR
Victor Stinner31a83932017-12-04 13:39:15 +010096
Victor Stinner3f5409a2019-09-23 19:50:27 +020097 return _PyStatus_OK();
Victor Stinnerb1147e42018-07-21 02:06:16 +020098}
99
100
Victor Stinnerb1147e42018-07-21 02:06:16 +0200101void
102_PyPathConfig_ClearGlobal(void)
103{
Victor Stinnerc1834442019-03-18 22:24:28 +0100104 PyMemAllocatorEx old_alloc;
105 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
106
Victor Stinner331a6a52019-05-27 16:39:22 +0200107 pathconfig_clear(&_Py_path_config);
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200108#ifdef MS_WINDOWS
109 PyMem_RawFree(_Py_dll_path);
110 _Py_dll_path = NULL;
111#endif
Victor Stinnerc1834442019-03-18 22:24:28 +0100112
113 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200114}
115
116
117static wchar_t*
Victor Stinner331a6a52019-05-27 16:39:22 +0200118_PyWideStringList_Join(const PyWideStringList *list, wchar_t sep)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200119{
120 size_t len = 1; /* NUL terminator */
Victor Stinner74f65682019-03-15 15:08:05 +0100121 for (Py_ssize_t i=0; i < list->length; i++) {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200122 if (i != 0) {
123 len++;
124 }
Victor Stinner74f65682019-03-15 15:08:05 +0100125 len += wcslen(list->items[i]);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200126 }
127
128 wchar_t *text = PyMem_RawMalloc(len * sizeof(wchar_t));
129 if (text == NULL) {
130 return NULL;
131 }
132 wchar_t *str = text;
Victor Stinner74f65682019-03-15 15:08:05 +0100133 for (Py_ssize_t i=0; i < list->length; i++) {
134 wchar_t *path = list->items[i];
Victor Stinnerb1147e42018-07-21 02:06:16 +0200135 if (i != 0) {
Victor Stinner96c84752019-09-26 16:17:34 +0200136 *str++ = sep;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200137 }
138 len = wcslen(path);
139 memcpy(str, path, len * sizeof(wchar_t));
140 str += len;
141 }
142 *str = L'\0';
143
144 return text;
145}
146
147
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200148#ifdef MS_WINDOWS
Victor Stinner96c84752019-09-26 16:17:34 +0200149/* Initialize _Py_dll_path on Windows. Do nothing on other platforms. */
150static PyStatus
151_PyPathConfig_InitDLLPath(void)
152{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200153 if (_Py_dll_path == NULL) {
154 /* Already set: nothing to do */
155 return _PyStatus_OK();
156 }
157
Victor Stinnerb1147e42018-07-21 02:06:16 +0200158 PyMemAllocatorEx old_alloc;
159 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
160
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200161 _Py_dll_path = _Py_GetDLLPath();
162
163 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
164
165 if (_Py_dll_path == NULL) {
166 return _PyStatus_NO_MEMORY();
167 }
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200168 return _PyStatus_OK();
169}
Victor Stinner96c84752019-09-26 16:17:34 +0200170#endif
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200171
172
173static PyStatus
Victor Stinner3f5409a2019-09-23 19:50:27 +0200174pathconfig_set_from_config(_PyPathConfig *pathconfig, const PyConfig *config)
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200175{
Victor Stinner331a6a52019-05-27 16:39:22 +0200176 PyStatus status;
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200177 PyMemAllocatorEx old_alloc;
178 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
179
Victor Stinner3f5409a2019-09-23 19:50:27 +0200180 if (config->module_search_paths_set) {
Victor Stinner96c84752019-09-26 16:17:34 +0200181 PyMem_RawFree(pathconfig->module_search_path);
Victor Stinner3f5409a2019-09-23 19:50:27 +0200182 pathconfig->module_search_path = _PyWideStringList_Join(&config->module_search_paths, DELIM);
183 if (pathconfig->module_search_path == NULL) {
184 goto no_memory;
185 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200186 }
187
Victor Stinner3f5409a2019-09-23 19:50:27 +0200188#define COPY_CONFIG(PATH_ATTR, CONFIG_ATTR) \
189 if (config->CONFIG_ATTR) { \
Victor Stinner96c84752019-09-26 16:17:34 +0200190 PyMem_RawFree(pathconfig->PATH_ATTR); \
191 pathconfig->PATH_ATTR = NULL; \
Victor Stinner3f5409a2019-09-23 19:50:27 +0200192 if (copy_wstr(&pathconfig->PATH_ATTR, config->CONFIG_ATTR) < 0) { \
193 goto no_memory; \
194 } \
195 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200196
Victor Stinner3f5409a2019-09-23 19:50:27 +0200197 COPY_CONFIG(program_full_path, executable);
198 COPY_CONFIG(prefix, prefix);
199 COPY_CONFIG(exec_prefix, exec_prefix);
200 COPY_CONFIG(program_name, program_name);
201 COPY_CONFIG(home, home);
Victor Stinner96c84752019-09-26 16:17:34 +0200202#ifdef MS_WINDOWS
203 COPY_CONFIG(base_executable, base_executable);
204#endif
Victor Stinner3f5409a2019-09-23 19:50:27 +0200205
206#undef COPY_CONFIG
Victor Stinnerb1147e42018-07-21 02:06:16 +0200207
Victor Stinner331a6a52019-05-27 16:39:22 +0200208 status = _PyStatus_OK();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200209 goto done;
210
211no_memory:
Victor Stinner331a6a52019-05-27 16:39:22 +0200212 status = _PyStatus_NO_MEMORY();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200213
214done:
Victor Stinnerb1147e42018-07-21 02:06:16 +0200215 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinner331a6a52019-05-27 16:39:22 +0200216 return status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200217}
218
219
Victor Stinner96c84752019-09-26 16:17:34 +0200220PyStatus
221_PyConfig_WritePathConfig(const PyConfig *config)
222{
223#ifdef MS_WINDOWS
224 PyStatus status = _PyPathConfig_InitDLLPath();
225 if (_PyStatus_EXCEPTION(status)) {
226 return status;
227 }
228#endif
229
230 return pathconfig_set_from_config(&_Py_path_config, config);
231}
232
233
Victor Stinner331a6a52019-05-27 16:39:22 +0200234static PyStatus
235config_init_module_search_paths(PyConfig *config, _PyPathConfig *pathconfig)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200236{
Victor Stinner331a6a52019-05-27 16:39:22 +0200237 assert(!config->module_search_paths_set);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200238
Victor Stinner331a6a52019-05-27 16:39:22 +0200239 _PyWideStringList_Clear(&config->module_search_paths);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200240
Victor Stinner331a6a52019-05-27 16:39:22 +0200241 const wchar_t *sys_path = pathconfig->module_search_path;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200242 const wchar_t delim = DELIM;
243 const wchar_t *p = sys_path;
244 while (1) {
245 p = wcschr(sys_path, delim);
246 if (p == NULL) {
247 p = sys_path + wcslen(sys_path); /* End of string */
248 }
249
250 size_t path_len = (p - sys_path);
251 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
252 if (path == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200253 return _PyStatus_NO_MEMORY();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200254 }
255 memcpy(path, sys_path, path_len * sizeof(wchar_t));
256 path[path_len] = L'\0';
257
Victor Stinner331a6a52019-05-27 16:39:22 +0200258 PyStatus status = PyWideStringList_Append(&config->module_search_paths, path);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200259 PyMem_RawFree(path);
Victor Stinner331a6a52019-05-27 16:39:22 +0200260 if (_PyStatus_EXCEPTION(status)) {
261 return status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200262 }
263
264 if (*p == '\0') {
265 break;
266 }
267 sys_path = p + 1;
268 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200269 config->module_search_paths_set = 1;
270 return _PyStatus_OK();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200271}
272
273
Victor Stinner3f5409a2019-09-23 19:50:27 +0200274/* Calculate the path configuration:
275
276 - exec_prefix
277 - module_search_path
278 - prefix
279 - program_full_path
280
281 On Windows, more fields are calculated:
282
283 - base_executable
284 - isolated
285 - site_import
286
287 On other platforms, isolated and site_import are left unchanged, and
288 _PyConfig_InitPathConfig() copies executable to base_executable (if it's not
289 set).
290
291 Priority, highest to lowest:
292
293 - PyConfig
294 - _Py_path_config: set by Py_SetPath(), Py_SetPythonHome()
295 and Py_SetProgramName()
296 - _PyPathConfig_Calculate()
297*/
298static PyStatus
299pathconfig_calculate(_PyPathConfig *pathconfig, const PyConfig *config)
300{
301 PyStatus status;
302
303 PyMemAllocatorEx old_alloc;
304 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
305
306 status = pathconfig_copy(pathconfig, &_Py_path_config);
307 if (_PyStatus_EXCEPTION(status)) {
308 goto done;
309 }
310
311 status = pathconfig_set_from_config(pathconfig, config);
312 if (_PyStatus_EXCEPTION(status)) {
313 goto done;
314 }
315
316 if (_Py_path_config.module_search_path == NULL) {
317 status = _PyPathConfig_Calculate(pathconfig, config);
318 }
319 else {
320 /* Py_SetPath() has been called: avoid _PyPathConfig_Calculate() */
321 }
322
323done:
324 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
325 return status;
326}
327
328
Victor Stinner331a6a52019-05-27 16:39:22 +0200329static PyStatus
330config_calculate_pathconfig(PyConfig *config)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200331{
Victor Stinner331a6a52019-05-27 16:39:22 +0200332 _PyPathConfig pathconfig = _PyPathConfig_INIT;
333 PyStatus status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200334
Victor Stinner331a6a52019-05-27 16:39:22 +0200335 status = pathconfig_calculate(&pathconfig, config);
336 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner3f5409a2019-09-23 19:50:27 +0200337 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200338 }
339
Victor Stinner331a6a52019-05-27 16:39:22 +0200340 if (!config->module_search_paths_set) {
341 status = config_init_module_search_paths(config, &pathconfig);
342 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner3f5409a2019-09-23 19:50:27 +0200343 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200344 }
345 }
346
Victor Stinner3f5409a2019-09-23 19:50:27 +0200347#define COPY_ATTR(PATH_ATTR, CONFIG_ATTR) \
348 if (config->CONFIG_ATTR == NULL) { \
349 if (copy_wstr(&config->CONFIG_ATTR, pathconfig.PATH_ATTR) < 0) { \
350 goto no_memory; \
351 } \
Victor Stinnerb1147e42018-07-21 02:06:16 +0200352 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200353
Victor Stinner96c84752019-09-26 16:17:34 +0200354#ifdef MS_WINDOWS
355 if (config->executable != NULL && config->base_executable == NULL) {
356 /* If executable is set explicitly in the configuration,
357 ignore calculated base_executable: _PyConfig_InitPathConfig()
358 will copy executable to base_executable */
359 }
360 else {
361 COPY_ATTR(base_executable, base_executable);
362 }
363#endif
364
Victor Stinner3f5409a2019-09-23 19:50:27 +0200365 COPY_ATTR(program_full_path, executable);
366 COPY_ATTR(prefix, prefix);
367 COPY_ATTR(exec_prefix, exec_prefix);
Victor Stinner96c84752019-09-26 16:17:34 +0200368
Victor Stinner3f5409a2019-09-23 19:50:27 +0200369#undef COPY_ATTR
Steve Dower323e7432019-06-29 14:28:59 -0700370
Victor Stinner96c84752019-09-26 16:17:34 +0200371#ifdef MS_WINDOWS
372 /* If a ._pth file is found: isolated and site_import are overriden */
Victor Stinner331a6a52019-05-27 16:39:22 +0200373 if (pathconfig.isolated != -1) {
374 config->isolated = pathconfig.isolated;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200375 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200376 if (pathconfig.site_import != -1) {
377 config->site_import = pathconfig.site_import;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200378 }
Victor Stinner96c84752019-09-26 16:17:34 +0200379#endif
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200380
Victor Stinner3f5409a2019-09-23 19:50:27 +0200381 status = _PyStatus_OK();
382 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200383
384no_memory:
Victor Stinner331a6a52019-05-27 16:39:22 +0200385 status = _PyStatus_NO_MEMORY();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200386
Victor Stinner3f5409a2019-09-23 19:50:27 +0200387done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200388 pathconfig_clear(&pathconfig);
389 return status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200390}
391
392
Victor Stinner331a6a52019-05-27 16:39:22 +0200393PyStatus
394_PyConfig_InitPathConfig(PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200395{
396 /* Do we need to calculate the path? */
Victor Stinner331a6a52019-05-27 16:39:22 +0200397 if (!config->module_search_paths_set
Victor Stinner96c84752019-09-26 16:17:34 +0200398 || config->executable == NULL
399 || config->prefix == NULL
400 || config->exec_prefix == NULL)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200401 {
Victor Stinner331a6a52019-05-27 16:39:22 +0200402 PyStatus status = config_calculate_pathconfig(config);
403 if (_PyStatus_EXCEPTION(status)) {
404 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200405 }
406 }
407
408 if (config->base_prefix == NULL) {
409 if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200410 return _PyStatus_NO_MEMORY();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200411 }
412 }
413
414 if (config->base_exec_prefix == NULL) {
Steve Dower177a41a2018-11-17 20:41:48 -0800415 if (copy_wstr(&config->base_exec_prefix,
416 config->exec_prefix) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200417 return _PyStatus_NO_MEMORY();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200418 }
419 }
Steve Dower323e7432019-06-29 14:28:59 -0700420
421 if (config->base_executable == NULL) {
422 if (copy_wstr(&config->base_executable,
423 config->executable) < 0) {
424 return _PyStatus_NO_MEMORY();
425 }
426 }
427
Victor Stinner331a6a52019-05-27 16:39:22 +0200428 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200429}
430
431
Victor Stinner3f5409a2019-09-23 19:50:27 +0200432static PyStatus
433pathconfig_global_read(_PyPathConfig *pathconfig)
434{
435 PyStatus status;
436 PyConfig config;
Victor Stinner6e128382019-09-28 04:50:43 +0200437 config.struct_size = sizeof(PyConfig);
438
439 status = _PyConfig_InitCompatConfig(&config);
440 if (_PyStatus_EXCEPTION(status)) {
441 goto done;
442 }
Victor Stinner3f5409a2019-09-23 19:50:27 +0200443
444 /* Call _PyConfig_InitPathConfig() */
445 status = PyConfig_Read(&config);
446 if (_PyStatus_EXCEPTION(status)) {
447 goto done;
448 }
449
450 status = pathconfig_set_from_config(pathconfig, &config);
451
452done:
453 PyConfig_Clear(&config);
454 return status;
455}
456
457
Victor Stinner31a83932017-12-04 13:39:15 +0100458static void
459pathconfig_global_init(void)
460{
Victor Stinner3f5409a2019-09-23 19:50:27 +0200461 PyStatus status;
462
Victor Stinner96c84752019-09-26 16:17:34 +0200463#ifdef MS_WINDOWS
464 status = _PyPathConfig_InitDLLPath();
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200465 if (_PyStatus_EXCEPTION(status)) {
466 Py_ExitStatusException(status);
467 }
Victor Stinner96c84752019-09-26 16:17:34 +0200468#endif
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200469
Victor Stinner3f5409a2019-09-23 19:50:27 +0200470 if (_Py_path_config.module_search_path == NULL) {
471 status = pathconfig_global_read(&_Py_path_config);
472 if (_PyStatus_EXCEPTION(status)) {
473 Py_ExitStatusException(status);
474 }
475 }
476 else {
477 /* Global configuration already initialized */
Victor Stinner31a83932017-12-04 13:39:15 +0100478 }
479
Victor Stinner3f5409a2019-09-23 19:50:27 +0200480 assert(_Py_path_config.program_full_path != NULL);
481 assert(_Py_path_config.prefix != NULL);
482 assert(_Py_path_config.exec_prefix != NULL);
483 assert(_Py_path_config.module_search_path != NULL);
484 assert(_Py_path_config.program_name != NULL);
485 /* home can be NULL */
Victor Stinner96c84752019-09-26 16:17:34 +0200486#ifdef MS_WINDOWS
Victor Stinner3f5409a2019-09-23 19:50:27 +0200487 assert(_Py_path_config.base_executable != NULL);
Victor Stinner96c84752019-09-26 16:17:34 +0200488#endif
Victor Stinner31a83932017-12-04 13:39:15 +0100489}
490
491
492/* External interface */
493
494void
495Py_SetPath(const wchar_t *path)
496{
497 if (path == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200498 pathconfig_clear(&_Py_path_config);
Victor Stinner31a83932017-12-04 13:39:15 +0100499 return;
500 }
501
502 PyMemAllocatorEx old_alloc;
503 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
504
Victor Stinner96c84752019-09-26 16:17:34 +0200505 /* Getting the program full path calls pathconfig_global_init() */
506 wchar_t *program_full_path = _PyMem_RawWcsdup(Py_GetProgramFullPath());
Victor Stinner31a83932017-12-04 13:39:15 +0100507
Victor Stinner3f5409a2019-09-23 19:50:27 +0200508 PyMem_RawFree(_Py_path_config.program_full_path);
509 PyMem_RawFree(_Py_path_config.prefix);
510 PyMem_RawFree(_Py_path_config.exec_prefix);
511 PyMem_RawFree(_Py_path_config.module_search_path);
Victor Stinner31a83932017-12-04 13:39:15 +0100512
Victor Stinner96c84752019-09-26 16:17:34 +0200513 _Py_path_config.program_full_path = program_full_path;
Victor Stinner3f5409a2019-09-23 19:50:27 +0200514 _Py_path_config.prefix = _PyMem_RawWcsdup(L"");
515 _Py_path_config.exec_prefix = _PyMem_RawWcsdup(L"");
516 _Py_path_config.module_search_path = _PyMem_RawWcsdup(path);
Victor Stinner31a83932017-12-04 13:39:15 +0100517
518 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200519
Victor Stinner3f5409a2019-09-23 19:50:27 +0200520 if (_Py_path_config.program_full_path == NULL
521 || _Py_path_config.prefix == NULL
522 || _Py_path_config.exec_prefix == NULL
523 || _Py_path_config.module_search_path == NULL)
524 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200525 Py_FatalError("Py_SetPath() failed: out of memory");
526 }
Victor Stinner31a83932017-12-04 13:39:15 +0100527}
528
529
530void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200531Py_SetPythonHome(const wchar_t *home)
Victor Stinner31a83932017-12-04 13:39:15 +0100532{
533 if (home == NULL) {
534 return;
535 }
536
537 PyMemAllocatorEx old_alloc;
538 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
539
540 PyMem_RawFree(_Py_path_config.home);
541 _Py_path_config.home = _PyMem_RawWcsdup(home);
542
543 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
544
545 if (_Py_path_config.home == NULL) {
546 Py_FatalError("Py_SetPythonHome() failed: out of memory");
547 }
548}
549
550
551void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200552Py_SetProgramName(const wchar_t *program_name)
Victor Stinner31a83932017-12-04 13:39:15 +0100553{
554 if (program_name == NULL || program_name[0] == L'\0') {
555 return;
556 }
557
558 PyMemAllocatorEx old_alloc;
559 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
560
561 PyMem_RawFree(_Py_path_config.program_name);
562 _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
563
564 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
565
566 if (_Py_path_config.program_name == NULL) {
567 Py_FatalError("Py_SetProgramName() failed: out of memory");
568 }
569}
570
Steve Dower177a41a2018-11-17 20:41:48 -0800571void
572_Py_SetProgramFullPath(const wchar_t *program_full_path)
573{
574 if (program_full_path == NULL || program_full_path[0] == L'\0') {
575 return;
576 }
577
578 PyMemAllocatorEx old_alloc;
579 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
580
581 PyMem_RawFree(_Py_path_config.program_full_path);
582 _Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);
583
584 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
585
586 if (_Py_path_config.program_full_path == NULL) {
587 Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");
588 }
589}
590
Victor Stinner31a83932017-12-04 13:39:15 +0100591
592wchar_t *
593Py_GetPath(void)
594{
595 pathconfig_global_init();
596 return _Py_path_config.module_search_path;
597}
598
599
600wchar_t *
601Py_GetPrefix(void)
602{
603 pathconfig_global_init();
604 return _Py_path_config.prefix;
605}
606
607
608wchar_t *
609Py_GetExecPrefix(void)
610{
Victor Stinner31a83932017-12-04 13:39:15 +0100611 pathconfig_global_init();
612 return _Py_path_config.exec_prefix;
Victor Stinner31a83932017-12-04 13:39:15 +0100613}
614
615
616wchar_t *
617Py_GetProgramFullPath(void)
618{
619 pathconfig_global_init();
620 return _Py_path_config.program_full_path;
621}
622
623
624wchar_t*
625Py_GetPythonHome(void)
626{
627 pathconfig_global_init();
628 return _Py_path_config.home;
629}
630
631
632wchar_t *
633Py_GetProgramName(void)
634{
635 pathconfig_global_init();
636 return _Py_path_config.program_name;
637}
638
Victor Stinnerdcf61712019-03-19 16:09:27 +0100639/* Compute module search path from argv[0] or the current working
640 directory ("-m module" case) which will be prepended to sys.argv:
641 sys.path[0].
642
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200643 Return 1 if the path is correctly resolved and written into *path0_p.
Victor Stinnerdcf61712019-03-19 16:09:27 +0100644
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200645 Return 0 if it fails to resolve the full path. For example, return 0 if the
646 current working directory has been removed (bpo-36236) or if argv is empty.
647
648 Raise an exception and return -1 on error.
Victor Stinnerdcf61712019-03-19 16:09:27 +0100649 */
650int
Victor Stinner331a6a52019-05-27 16:39:22 +0200651_PyPathConfig_ComputeSysPath0(const PyWideStringList *argv, PyObject **path0_p)
Victor Stinner11a247d2017-12-13 21:05:57 +0100652{
Victor Stinner331a6a52019-05-27 16:39:22 +0200653 assert(_PyWideStringList_CheckConsistency(argv));
Victor Stinner74f65682019-03-15 15:08:05 +0100654
Victor Stinnerfc96e542019-03-19 18:22:55 +0100655 if (argv->length == 0) {
656 /* Leave sys.path unchanged if sys.argv is empty */
657 return 0;
658 }
659
660 wchar_t *argv0 = argv->items[0];
661 int have_module_arg = (wcscmp(argv0, L"-m") == 0);
662 int have_script_arg = (!have_module_arg && (wcscmp(argv0, L"-c") != 0));
663
664 wchar_t *path0 = argv0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100665 Py_ssize_t n = 0;
Victor Stinnerfc96e542019-03-19 18:22:55 +0100666
667#ifdef HAVE_REALPATH
Victor Stinner11a247d2017-12-13 21:05:57 +0100668 wchar_t fullpath[MAXPATHLEN];
669#elif defined(MS_WINDOWS)
670 wchar_t fullpath[MAX_PATH];
671#endif
672
Nick Coghland5d9e022018-03-25 23:03:10 +1000673 if (have_module_arg) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100674#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
Victor Stinnerfc96e542019-03-19 18:22:55 +0100675 if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
676 return 0;
677 }
678 path0 = fullpath;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100679#else
Victor Stinnerfc96e542019-03-19 18:22:55 +0100680 path0 = L".";
Victor Stinnerdcf61712019-03-19 16:09:27 +0100681#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100682 n = wcslen(path0);
Nick Coghland5d9e022018-03-25 23:03:10 +1000683 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100684
685#ifdef HAVE_READLINK
Victor Stinnerfc96e542019-03-19 18:22:55 +0100686 wchar_t link[MAXPATHLEN + 1];
687 int nr = 0;
688
689 if (have_script_arg) {
690 nr = _Py_wreadlink(path0, link, Py_ARRAY_LENGTH(link));
691 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100692 if (nr > 0) {
693 /* It's a symlink */
694 link[nr] = '\0';
Victor Stinnerfc96e542019-03-19 18:22:55 +0100695 if (link[0] == SEP) {
696 path0 = link; /* Link to absolute path */
697 }
698 else if (wcschr(link, SEP) == NULL) {
699 /* Link without path */
700 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100701 else {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100702 /* Must join(dirname(path0), link) */
703 wchar_t *q = wcsrchr(path0, SEP);
704 if (q == NULL) {
705 /* path0 without path */
706 path0 = link;
707 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100708 else {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100709 /* Must make a copy, path0copy has room for 2 * MAXPATHLEN */
710 wchar_t path0copy[2 * MAXPATHLEN + 1];
711 wcsncpy(path0copy, path0, MAXPATHLEN);
712 q = wcsrchr(path0copy, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100713 wcsncpy(q+1, link, MAXPATHLEN);
714 q[MAXPATHLEN + 1] = L'\0';
Victor Stinnerfc96e542019-03-19 18:22:55 +0100715 path0 = path0copy;
Victor Stinner11a247d2017-12-13 21:05:57 +0100716 }
717 }
718 }
719#endif /* HAVE_READLINK */
720
Victor Stinnerfc96e542019-03-19 18:22:55 +0100721 wchar_t *p = NULL;
722
Victor Stinner11a247d2017-12-13 21:05:57 +0100723#if SEP == '\\'
724 /* Special case for Microsoft filename syntax */
Nick Coghland5d9e022018-03-25 23:03:10 +1000725 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100726 wchar_t *q;
727#if defined(MS_WINDOWS)
728 /* Replace the first element in argv with the full path. */
729 wchar_t *ptemp;
Victor Stinnerfc96e542019-03-19 18:22:55 +0100730 if (GetFullPathNameW(path0,
Victor Stinner11a247d2017-12-13 21:05:57 +0100731 Py_ARRAY_LENGTH(fullpath),
732 fullpath,
733 &ptemp)) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100734 path0 = fullpath;
Victor Stinner11a247d2017-12-13 21:05:57 +0100735 }
736#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100737 p = wcsrchr(path0, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100738 /* Test for alternate separator */
Victor Stinnerfc96e542019-03-19 18:22:55 +0100739 q = wcsrchr(p ? p : path0, '/');
Victor Stinner11a247d2017-12-13 21:05:57 +0100740 if (q != NULL)
741 p = q;
742 if (p != NULL) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100743 n = p + 1 - path0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100744 if (n > 1 && p[-1] != ':')
745 n--; /* Drop trailing separator */
746 }
747 }
Victor Stinnerfc96e542019-03-19 18:22:55 +0100748#else
749 /* All other filename syntaxes */
Nick Coghland5d9e022018-03-25 23:03:10 +1000750 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100751#if defined(HAVE_REALPATH)
Victor Stinnerfc96e542019-03-19 18:22:55 +0100752 if (_Py_wrealpath(path0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
753 path0 = fullpath;
Victor Stinner11a247d2017-12-13 21:05:57 +0100754 }
755#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100756 p = wcsrchr(path0, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100757 }
758 if (p != NULL) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100759 n = p + 1 - path0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100760#if SEP == '/' /* Special case for Unix filename syntax */
Victor Stinnerfc96e542019-03-19 18:22:55 +0100761 if (n > 1) {
762 /* Drop trailing separator */
763 n--;
764 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100765#endif /* Unix */
766 }
767#endif /* All others */
768
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200769 PyObject *path0_obj = PyUnicode_FromWideChar(path0, n);
770 if (path0_obj == NULL) {
771 return -1;
772 }
773
774 *path0_p = path0_obj;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100775 return 1;
Victor Stinner11a247d2017-12-13 21:05:57 +0100776}
777
Victor Stinner9bee3292017-12-21 16:49:13 +0100778
Minmin Gong8ebc6452019-02-02 20:26:55 -0800779#ifdef MS_WINDOWS
780#define WCSTOK wcstok_s
781#else
782#define WCSTOK wcstok
783#endif
784
Victor Stinner9bee3292017-12-21 16:49:13 +0100785/* Search for a prefix value in an environment file (pyvenv.cfg).
786 If found, copy it into the provided buffer. */
787int
788_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
789 wchar_t *value, size_t value_size)
790{
791 int result = 0; /* meaning not found */
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100792 char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */
793 buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0';
Victor Stinner9bee3292017-12-21 16:49:13 +0100794
795 fseek(env_file, 0, SEEK_SET);
796 while (!feof(env_file)) {
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100797 char * p = fgets(buffer, Py_ARRAY_LENGTH(buffer) - 1, env_file);
Victor Stinner9bee3292017-12-21 16:49:13 +0100798
799 if (p == NULL) {
800 break;
801 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100802
803 size_t n = strlen(p);
Victor Stinner9bee3292017-12-21 16:49:13 +0100804 if (p[n - 1] != '\n') {
805 /* line has overflowed - bail */
806 break;
807 }
808 if (p[0] == '#') {
809 /* Comment - skip */
810 continue;
811 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100812
Victor Stinner5f9cf232019-03-19 01:46:25 +0100813 wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100814 if (tmpbuffer) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100815 wchar_t * state;
Minmin Gong8ebc6452019-02-02 20:26:55 -0800816 wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100817 if ((tok != NULL) && !wcscmp(tok, key)) {
Minmin Gong8ebc6452019-02-02 20:26:55 -0800818 tok = WCSTOK(NULL, L" \t", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100819 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Minmin Gong8ebc6452019-02-02 20:26:55 -0800820 tok = WCSTOK(NULL, L"\r\n", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100821 if (tok != NULL) {
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100822 wcsncpy(value, tok, value_size - 1);
823 value[value_size - 1] = L'\0';
Victor Stinner9bee3292017-12-21 16:49:13 +0100824 result = 1;
825 PyMem_RawFree(tmpbuffer);
826 break;
827 }
828 }
829 }
830 PyMem_RawFree(tmpbuffer);
831 }
832 }
833 return result;
834}
835
Victor Stinner31a83932017-12-04 13:39:15 +0100836#ifdef __cplusplus
837}
838#endif