blob: f4e14986687838df8aed7d2f3398b6dd622c38e6 [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 Stinnerc4221672019-09-21 01:02:56 +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 Stinner1ce152a2019-09-24 17:44:15 +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 Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner9c42f8c2019-09-23 18:47:29 +020060 CLEAR(config->home);
Victor Stinner8bf39b62019-09-26 02:22:35 +020061#ifdef MS_WINDOWS
Steve Dower9048c492019-06-29 10:34:11 -070062 CLEAR(config->base_executable);
Victor Stinner8bf39b62019-09-26 02:22:35 +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 Stinner9c42f8c2019-09-23 18:47:29 +020072pathconfig_copy(_PyPathConfig *config, const _PyPathConfig *config2)
Victor Stinner31a83932017-12-04 13:39:15 +010073{
Victor Stinner9c42f8c2019-09-23 18:47:29 +020074 pathconfig_clear(config);
Victor Stinner31a83932017-12-04 13:39:15 +010075
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner8bf39b62019-09-26 02:22:35 +020089#ifdef MS_WINDOWS
Victor Stinner9c42f8c2019-09-23 18:47:29 +020090 config->isolated = config2->isolated;
91 config->site_import = config2->site_import;
92 COPY_ATTR(base_executable);
Victor Stinner8bf39b62019-09-26 02:22:35 +020093#endif
Victor Stinnere2677932019-09-21 01:50:16 +020094
Victor Stinner9c42f8c2019-09-23 18:47:29 +020095#undef COPY_ATTR
Victor Stinner31a83932017-12-04 13:39:15 +010096
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 Stinnerc4221672019-09-21 01:02:56 +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) {
136 *str++ = SEP;
137 }
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 Stinner9c42f8c2019-09-23 18:47:29 +0200148/* Initialize _Py_dll_path on Windows. Do nothing on other platforms. */
Victor Stinner331a6a52019-05-27 16:39:22 +0200149PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200150_PyPathConfig_Init(void)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200151{
Victor Stinnerc4221672019-09-21 01:02:56 +0200152#ifdef MS_WINDOWS
153 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 Stinnerc4221672019-09-21 01:02:56 +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 }
168#endif
169 return _PyStatus_OK();
170}
171
172
173static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200174pathconfig_set_from_config(_PyPathConfig *pathconfig, const PyConfig *config)
Victor Stinnerc4221672019-09-21 01:02:56 +0200175{
Victor Stinner331a6a52019-05-27 16:39:22 +0200176 PyStatus status;
Victor Stinnerc4221672019-09-21 01:02:56 +0200177 PyMemAllocatorEx old_alloc;
178 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
179
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200180 if (config->module_search_paths_set) {
Victor Stinner1ce152a2019-09-24 17:44:15 +0200181 PyMem_RawFree(pathconfig->module_search_path);
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner9c42f8c2019-09-23 18:47:29 +0200188#define COPY_CONFIG(PATH_ATTR, CONFIG_ATTR) \
189 if (config->CONFIG_ATTR) { \
Victor Stinner1ce152a2019-09-24 17:44:15 +0200190 PyMem_RawFree(pathconfig->PATH_ATTR); \
191 pathconfig->PATH_ATTR = NULL; \
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner8bf39b62019-09-26 02:22:35 +0200202#ifdef MS_WINDOWS
203 COPY_CONFIG(base_executable, base_executable);
204#endif
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 Stinner331a6a52019-05-27 16:39:22 +0200220static PyStatus
221config_init_module_search_paths(PyConfig *config, _PyPathConfig *pathconfig)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200222{
Victor Stinner331a6a52019-05-27 16:39:22 +0200223 assert(!config->module_search_paths_set);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200224
Victor Stinner331a6a52019-05-27 16:39:22 +0200225 _PyWideStringList_Clear(&config->module_search_paths);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200226
Victor Stinner331a6a52019-05-27 16:39:22 +0200227 const wchar_t *sys_path = pathconfig->module_search_path;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200228 const wchar_t delim = DELIM;
229 const wchar_t *p = sys_path;
230 while (1) {
231 p = wcschr(sys_path, delim);
232 if (p == NULL) {
233 p = sys_path + wcslen(sys_path); /* End of string */
234 }
235
236 size_t path_len = (p - sys_path);
237 wchar_t *path = PyMem_RawMalloc((path_len + 1) * sizeof(wchar_t));
238 if (path == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200239 return _PyStatus_NO_MEMORY();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200240 }
241 memcpy(path, sys_path, path_len * sizeof(wchar_t));
242 path[path_len] = L'\0';
243
Victor Stinner331a6a52019-05-27 16:39:22 +0200244 PyStatus status = PyWideStringList_Append(&config->module_search_paths, path);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200245 PyMem_RawFree(path);
Victor Stinner331a6a52019-05-27 16:39:22 +0200246 if (_PyStatus_EXCEPTION(status)) {
247 return status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200248 }
249
250 if (*p == '\0') {
251 break;
252 }
253 sys_path = p + 1;
254 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200255 config->module_search_paths_set = 1;
256 return _PyStatus_OK();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200257}
258
259
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200260/* Calculate the path configuration:
261
262 - exec_prefix
263 - module_search_path
264 - prefix
265 - program_full_path
266
267 On Windows, more fields are calculated:
268
269 - base_executable
270 - isolated
271 - site_import
272
273 On other platforms, isolated and site_import are left unchanged, and
274 _PyConfig_InitPathConfig() copies executable to base_executable (if it's not
275 set).
276
277 Priority, highest to lowest:
278
279 - PyConfig
280 - _Py_path_config: set by Py_SetPath(), Py_SetPythonHome()
281 and Py_SetProgramName()
282 - _PyPathConfig_Calculate()
283*/
284static PyStatus
285pathconfig_calculate(_PyPathConfig *pathconfig, const PyConfig *config)
286{
287 PyStatus status;
288
289 PyMemAllocatorEx old_alloc;
290 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
291
292 status = pathconfig_copy(pathconfig, &_Py_path_config);
293 if (_PyStatus_EXCEPTION(status)) {
294 goto done;
295 }
296
297 status = pathconfig_set_from_config(pathconfig, config);
298 if (_PyStatus_EXCEPTION(status)) {
299 goto done;
300 }
301
302 if (_Py_path_config.module_search_path == NULL) {
303 status = _PyPathConfig_Calculate(pathconfig, config);
304 }
305 else {
306 /* Py_SetPath() has been called: avoid _PyPathConfig_Calculate() */
307 }
308
309done:
310 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
311 return status;
312}
313
314
Victor Stinner331a6a52019-05-27 16:39:22 +0200315static PyStatus
316config_calculate_pathconfig(PyConfig *config)
Victor Stinnerb1147e42018-07-21 02:06:16 +0200317{
Victor Stinner331a6a52019-05-27 16:39:22 +0200318 _PyPathConfig pathconfig = _PyPathConfig_INIT;
319 PyStatus status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200320
Victor Stinner331a6a52019-05-27 16:39:22 +0200321 status = pathconfig_calculate(&pathconfig, config);
322 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200323 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200324 }
325
Victor Stinner331a6a52019-05-27 16:39:22 +0200326 if (!config->module_search_paths_set) {
327 status = config_init_module_search_paths(config, &pathconfig);
328 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200329 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200330 }
331 }
332
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200333#define COPY_ATTR(PATH_ATTR, CONFIG_ATTR) \
334 if (config->CONFIG_ATTR == NULL) { \
335 if (copy_wstr(&config->CONFIG_ATTR, pathconfig.PATH_ATTR) < 0) { \
336 goto no_memory; \
337 } \
Victor Stinnerb1147e42018-07-21 02:06:16 +0200338 }
Victor Stinnerb1147e42018-07-21 02:06:16 +0200339
Victor Stinner8bf39b62019-09-26 02:22:35 +0200340#ifdef MS_WINDOWS
341 if (config->executable != NULL && config->base_executable == NULL) {
342 /* If executable is set explicitly in the configuration,
343 ignore calculated base_executable: _PyConfig_InitPathConfig()
344 will copy executable to base_executable */
345 }
346 else {
347 COPY_ATTR(base_executable, base_executable);
348 }
349#endif
350
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200351 COPY_ATTR(program_full_path, executable);
352 COPY_ATTR(prefix, prefix);
353 COPY_ATTR(exec_prefix, exec_prefix);
Victor Stinner8bf39b62019-09-26 02:22:35 +0200354
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200355#undef COPY_ATTR
Steve Dower9048c492019-06-29 10:34:11 -0700356
Victor Stinner8bf39b62019-09-26 02:22:35 +0200357#ifdef MS_WINDOWS
358 /* If a ._pth file is found: isolated and site_import are overriden */
Victor Stinner331a6a52019-05-27 16:39:22 +0200359 if (pathconfig.isolated != -1) {
360 config->isolated = pathconfig.isolated;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200361 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200362 if (pathconfig.site_import != -1) {
363 config->site_import = pathconfig.site_import;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200364 }
Victor Stinner8bf39b62019-09-26 02:22:35 +0200365#endif
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200366
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200367 status = _PyStatus_OK();
368 goto done;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200369
370no_memory:
Victor Stinner331a6a52019-05-27 16:39:22 +0200371 status = _PyStatus_NO_MEMORY();
Victor Stinnerb1147e42018-07-21 02:06:16 +0200372
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200373done:
Victor Stinner331a6a52019-05-27 16:39:22 +0200374 pathconfig_clear(&pathconfig);
375 return status;
Victor Stinnerb1147e42018-07-21 02:06:16 +0200376}
377
378
Victor Stinner331a6a52019-05-27 16:39:22 +0200379PyStatus
380_PyConfig_InitPathConfig(PyConfig *config)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200381{
382 /* Do we need to calculate the path? */
Victor Stinner331a6a52019-05-27 16:39:22 +0200383 if (!config->module_search_paths_set
Victor Stinner8bf39b62019-09-26 02:22:35 +0200384 || config->executable == NULL
385 || config->prefix == NULL
386 || config->exec_prefix == NULL)
Victor Stinner1dc6e392018-07-25 02:49:17 +0200387 {
Victor Stinner331a6a52019-05-27 16:39:22 +0200388 PyStatus status = config_calculate_pathconfig(config);
389 if (_PyStatus_EXCEPTION(status)) {
390 return status;
Victor Stinner1dc6e392018-07-25 02:49:17 +0200391 }
392 }
393
394 if (config->base_prefix == NULL) {
395 if (copy_wstr(&config->base_prefix, config->prefix) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200396 return _PyStatus_NO_MEMORY();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200397 }
398 }
399
400 if (config->base_exec_prefix == NULL) {
Steve Dower177a41a2018-11-17 20:41:48 -0800401 if (copy_wstr(&config->base_exec_prefix,
402 config->exec_prefix) < 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200403 return _PyStatus_NO_MEMORY();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200404 }
405 }
Steve Dower9048c492019-06-29 10:34:11 -0700406
407 if (config->base_executable == NULL) {
408 if (copy_wstr(&config->base_executable,
409 config->executable) < 0) {
410 return _PyStatus_NO_MEMORY();
411 }
412 }
413
Victor Stinner331a6a52019-05-27 16:39:22 +0200414 return _PyStatus_OK();
Victor Stinner1dc6e392018-07-25 02:49:17 +0200415}
416
417
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200418static PyStatus
419pathconfig_global_read(_PyPathConfig *pathconfig)
420{
421 PyStatus status;
422 PyConfig config;
423 _PyConfig_InitCompatConfig(&config);
424
425 /* Call _PyConfig_InitPathConfig() */
426 status = PyConfig_Read(&config);
427 if (_PyStatus_EXCEPTION(status)) {
428 goto done;
429 }
430
431 status = pathconfig_set_from_config(pathconfig, &config);
432
433done:
434 PyConfig_Clear(&config);
435 return status;
436}
437
438
Victor Stinner31a83932017-12-04 13:39:15 +0100439static void
440pathconfig_global_init(void)
441{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200442 PyStatus status;
443
Victor Stinnerc4221672019-09-21 01:02:56 +0200444 /* Initialize _Py_dll_path if needed */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200445 status = _PyPathConfig_Init();
Victor Stinnerc4221672019-09-21 01:02:56 +0200446 if (_PyStatus_EXCEPTION(status)) {
447 Py_ExitStatusException(status);
448 }
449
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200450 if (_Py_path_config.module_search_path == NULL) {
451 status = pathconfig_global_read(&_Py_path_config);
452 if (_PyStatus_EXCEPTION(status)) {
453 Py_ExitStatusException(status);
454 }
455 }
456 else {
457 /* Global configuration already initialized */
Victor Stinner31a83932017-12-04 13:39:15 +0100458 }
459
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200460 assert(_Py_path_config.program_full_path != NULL);
461 assert(_Py_path_config.prefix != NULL);
462 assert(_Py_path_config.exec_prefix != NULL);
463 assert(_Py_path_config.module_search_path != NULL);
464 assert(_Py_path_config.program_name != NULL);
465 /* home can be NULL */
Victor Stinner8bf39b62019-09-26 02:22:35 +0200466#ifdef MS_WINDOWS
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200467 assert(_Py_path_config.base_executable != NULL);
Victor Stinner8bf39b62019-09-26 02:22:35 +0200468#endif
Victor Stinner31a83932017-12-04 13:39:15 +0100469}
470
471
472/* External interface */
473
474void
475Py_SetPath(const wchar_t *path)
476{
477 if (path == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200478 pathconfig_clear(&_Py_path_config);
Victor Stinner31a83932017-12-04 13:39:15 +0100479 return;
480 }
481
482 PyMemAllocatorEx old_alloc;
483 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
484
Victor Stinner1ce152a2019-09-24 17:44:15 +0200485 /* Getting the program full path calls pathconfig_global_init() */
486 wchar_t *program_full_path = _PyMem_RawWcsdup(Py_GetProgramFullPath());
Victor Stinner31a83932017-12-04 13:39:15 +0100487
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200488 PyMem_RawFree(_Py_path_config.program_full_path);
489 PyMem_RawFree(_Py_path_config.prefix);
490 PyMem_RawFree(_Py_path_config.exec_prefix);
491 PyMem_RawFree(_Py_path_config.module_search_path);
Victor Stinner31a83932017-12-04 13:39:15 +0100492
Victor Stinner1ce152a2019-09-24 17:44:15 +0200493 _Py_path_config.program_full_path = program_full_path;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200494 _Py_path_config.prefix = _PyMem_RawWcsdup(L"");
495 _Py_path_config.exec_prefix = _PyMem_RawWcsdup(L"");
496 _Py_path_config.module_search_path = _PyMem_RawWcsdup(path);
Victor Stinner31a83932017-12-04 13:39:15 +0100497
498 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
Victor Stinnerb1147e42018-07-21 02:06:16 +0200499
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200500 if (_Py_path_config.program_full_path == NULL
501 || _Py_path_config.prefix == NULL
502 || _Py_path_config.exec_prefix == NULL
503 || _Py_path_config.module_search_path == NULL)
504 {
Victor Stinnerb1147e42018-07-21 02:06:16 +0200505 Py_FatalError("Py_SetPath() failed: out of memory");
506 }
Victor Stinner31a83932017-12-04 13:39:15 +0100507}
508
509
510void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200511Py_SetPythonHome(const wchar_t *home)
Victor Stinner31a83932017-12-04 13:39:15 +0100512{
513 if (home == NULL) {
514 return;
515 }
516
517 PyMemAllocatorEx old_alloc;
518 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
519
520 PyMem_RawFree(_Py_path_config.home);
521 _Py_path_config.home = _PyMem_RawWcsdup(home);
522
523 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
524
525 if (_Py_path_config.home == NULL) {
526 Py_FatalError("Py_SetPythonHome() failed: out of memory");
527 }
528}
529
530
531void
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200532Py_SetProgramName(const wchar_t *program_name)
Victor Stinner31a83932017-12-04 13:39:15 +0100533{
534 if (program_name == NULL || program_name[0] == L'\0') {
535 return;
536 }
537
538 PyMemAllocatorEx old_alloc;
539 _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
540
541 PyMem_RawFree(_Py_path_config.program_name);
542 _Py_path_config.program_name = _PyMem_RawWcsdup(program_name);
543
544 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
545
546 if (_Py_path_config.program_name == NULL) {
547 Py_FatalError("Py_SetProgramName() failed: out of memory");
548 }
549}
550
Steve Dower177a41a2018-11-17 20:41:48 -0800551void
552_Py_SetProgramFullPath(const wchar_t *program_full_path)
553{
554 if (program_full_path == NULL || program_full_path[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_full_path);
562 _Py_path_config.program_full_path = _PyMem_RawWcsdup(program_full_path);
563
564 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
565
566 if (_Py_path_config.program_full_path == NULL) {
567 Py_FatalError("_Py_SetProgramFullPath() failed: out of memory");
568 }
569}
570
Victor Stinner31a83932017-12-04 13:39:15 +0100571
572wchar_t *
573Py_GetPath(void)
574{
575 pathconfig_global_init();
576 return _Py_path_config.module_search_path;
577}
578
579
580wchar_t *
581Py_GetPrefix(void)
582{
583 pathconfig_global_init();
584 return _Py_path_config.prefix;
585}
586
587
588wchar_t *
589Py_GetExecPrefix(void)
590{
Victor Stinner31a83932017-12-04 13:39:15 +0100591 pathconfig_global_init();
592 return _Py_path_config.exec_prefix;
Victor Stinner31a83932017-12-04 13:39:15 +0100593}
594
595
596wchar_t *
597Py_GetProgramFullPath(void)
598{
599 pathconfig_global_init();
600 return _Py_path_config.program_full_path;
601}
602
603
604wchar_t*
605Py_GetPythonHome(void)
606{
607 pathconfig_global_init();
608 return _Py_path_config.home;
609}
610
611
612wchar_t *
613Py_GetProgramName(void)
614{
615 pathconfig_global_init();
616 return _Py_path_config.program_name;
617}
618
Victor Stinnerdcf61712019-03-19 16:09:27 +0100619/* Compute module search path from argv[0] or the current working
620 directory ("-m module" case) which will be prepended to sys.argv:
621 sys.path[0].
622
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200623 Return 1 if the path is correctly resolved and written into *path0_p.
Victor Stinnerdcf61712019-03-19 16:09:27 +0100624
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200625 Return 0 if it fails to resolve the full path. For example, return 0 if the
626 current working directory has been removed (bpo-36236) or if argv is empty.
627
628 Raise an exception and return -1 on error.
Victor Stinnerdcf61712019-03-19 16:09:27 +0100629 */
630int
Victor Stinner331a6a52019-05-27 16:39:22 +0200631_PyPathConfig_ComputeSysPath0(const PyWideStringList *argv, PyObject **path0_p)
Victor Stinner11a247d2017-12-13 21:05:57 +0100632{
Victor Stinner331a6a52019-05-27 16:39:22 +0200633 assert(_PyWideStringList_CheckConsistency(argv));
Victor Stinner74f65682019-03-15 15:08:05 +0100634
Victor Stinnerfc96e542019-03-19 18:22:55 +0100635 if (argv->length == 0) {
636 /* Leave sys.path unchanged if sys.argv is empty */
637 return 0;
638 }
639
640 wchar_t *argv0 = argv->items[0];
641 int have_module_arg = (wcscmp(argv0, L"-m") == 0);
642 int have_script_arg = (!have_module_arg && (wcscmp(argv0, L"-c") != 0));
643
644 wchar_t *path0 = argv0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100645 Py_ssize_t n = 0;
Victor Stinnerfc96e542019-03-19 18:22:55 +0100646
647#ifdef HAVE_REALPATH
Victor Stinner11a247d2017-12-13 21:05:57 +0100648 wchar_t fullpath[MAXPATHLEN];
649#elif defined(MS_WINDOWS)
650 wchar_t fullpath[MAX_PATH];
651#endif
652
Nick Coghland5d9e022018-03-25 23:03:10 +1000653 if (have_module_arg) {
Victor Stinnerdcf61712019-03-19 16:09:27 +0100654#if defined(HAVE_REALPATH) || defined(MS_WINDOWS)
Victor Stinnerfc96e542019-03-19 18:22:55 +0100655 if (!_Py_wgetcwd(fullpath, Py_ARRAY_LENGTH(fullpath))) {
656 return 0;
657 }
658 path0 = fullpath;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100659#else
Victor Stinnerfc96e542019-03-19 18:22:55 +0100660 path0 = L".";
Victor Stinnerdcf61712019-03-19 16:09:27 +0100661#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100662 n = wcslen(path0);
Nick Coghland5d9e022018-03-25 23:03:10 +1000663 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100664
665#ifdef HAVE_READLINK
Victor Stinnerfc96e542019-03-19 18:22:55 +0100666 wchar_t link[MAXPATHLEN + 1];
667 int nr = 0;
668
669 if (have_script_arg) {
670 nr = _Py_wreadlink(path0, link, Py_ARRAY_LENGTH(link));
671 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100672 if (nr > 0) {
673 /* It's a symlink */
674 link[nr] = '\0';
Victor Stinnerfc96e542019-03-19 18:22:55 +0100675 if (link[0] == SEP) {
676 path0 = link; /* Link to absolute path */
677 }
678 else if (wcschr(link, SEP) == NULL) {
679 /* Link without path */
680 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100681 else {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100682 /* Must join(dirname(path0), link) */
683 wchar_t *q = wcsrchr(path0, SEP);
684 if (q == NULL) {
685 /* path0 without path */
686 path0 = link;
687 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100688 else {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100689 /* Must make a copy, path0copy has room for 2 * MAXPATHLEN */
690 wchar_t path0copy[2 * MAXPATHLEN + 1];
691 wcsncpy(path0copy, path0, MAXPATHLEN);
692 q = wcsrchr(path0copy, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100693 wcsncpy(q+1, link, MAXPATHLEN);
694 q[MAXPATHLEN + 1] = L'\0';
Victor Stinnerfc96e542019-03-19 18:22:55 +0100695 path0 = path0copy;
Victor Stinner11a247d2017-12-13 21:05:57 +0100696 }
697 }
698 }
699#endif /* HAVE_READLINK */
700
Victor Stinnerfc96e542019-03-19 18:22:55 +0100701 wchar_t *p = NULL;
702
Victor Stinner11a247d2017-12-13 21:05:57 +0100703#if SEP == '\\'
704 /* Special case for Microsoft filename syntax */
Nick Coghland5d9e022018-03-25 23:03:10 +1000705 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100706 wchar_t *q;
707#if defined(MS_WINDOWS)
708 /* Replace the first element in argv with the full path. */
709 wchar_t *ptemp;
Victor Stinnerfc96e542019-03-19 18:22:55 +0100710 if (GetFullPathNameW(path0,
Victor Stinner11a247d2017-12-13 21:05:57 +0100711 Py_ARRAY_LENGTH(fullpath),
712 fullpath,
713 &ptemp)) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100714 path0 = fullpath;
Victor Stinner11a247d2017-12-13 21:05:57 +0100715 }
716#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100717 p = wcsrchr(path0, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100718 /* Test for alternate separator */
Victor Stinnerfc96e542019-03-19 18:22:55 +0100719 q = wcsrchr(p ? p : path0, '/');
Victor Stinner11a247d2017-12-13 21:05:57 +0100720 if (q != NULL)
721 p = q;
722 if (p != NULL) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100723 n = p + 1 - path0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100724 if (n > 1 && p[-1] != ':')
725 n--; /* Drop trailing separator */
726 }
727 }
Victor Stinnerfc96e542019-03-19 18:22:55 +0100728#else
729 /* All other filename syntaxes */
Nick Coghland5d9e022018-03-25 23:03:10 +1000730 if (have_script_arg) {
Victor Stinner11a247d2017-12-13 21:05:57 +0100731#if defined(HAVE_REALPATH)
Victor Stinnerfc96e542019-03-19 18:22:55 +0100732 if (_Py_wrealpath(path0, fullpath, Py_ARRAY_LENGTH(fullpath))) {
733 path0 = fullpath;
Victor Stinner11a247d2017-12-13 21:05:57 +0100734 }
735#endif
Victor Stinnerfc96e542019-03-19 18:22:55 +0100736 p = wcsrchr(path0, SEP);
Victor Stinner11a247d2017-12-13 21:05:57 +0100737 }
738 if (p != NULL) {
Victor Stinnerfc96e542019-03-19 18:22:55 +0100739 n = p + 1 - path0;
Victor Stinner11a247d2017-12-13 21:05:57 +0100740#if SEP == '/' /* Special case for Unix filename syntax */
Victor Stinnerfc96e542019-03-19 18:22:55 +0100741 if (n > 1) {
742 /* Drop trailing separator */
743 n--;
744 }
Victor Stinner11a247d2017-12-13 21:05:57 +0100745#endif /* Unix */
746 }
747#endif /* All others */
748
Victor Stinnerdbacfc22019-05-16 16:39:26 +0200749 PyObject *path0_obj = PyUnicode_FromWideChar(path0, n);
750 if (path0_obj == NULL) {
751 return -1;
752 }
753
754 *path0_p = path0_obj;
Victor Stinnerdcf61712019-03-19 16:09:27 +0100755 return 1;
Victor Stinner11a247d2017-12-13 21:05:57 +0100756}
757
Victor Stinner9bee3292017-12-21 16:49:13 +0100758
Minmin Gong8ebc6452019-02-02 20:26:55 -0800759#ifdef MS_WINDOWS
760#define WCSTOK wcstok_s
761#else
762#define WCSTOK wcstok
763#endif
764
Victor Stinner9bee3292017-12-21 16:49:13 +0100765/* Search for a prefix value in an environment file (pyvenv.cfg).
766 If found, copy it into the provided buffer. */
767int
768_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
769 wchar_t *value, size_t value_size)
770{
771 int result = 0; /* meaning not found */
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100772 char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */
773 buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0';
Victor Stinner9bee3292017-12-21 16:49:13 +0100774
Victor Stinner9bee3292017-12-21 16:49:13 +0100775 while (!feof(env_file)) {
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100776 char * p = fgets(buffer, Py_ARRAY_LENGTH(buffer) - 1, env_file);
Victor Stinner9bee3292017-12-21 16:49:13 +0100777
778 if (p == NULL) {
779 break;
780 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100781
782 size_t n = strlen(p);
Victor Stinner9bee3292017-12-21 16:49:13 +0100783 if (p[n - 1] != '\n') {
784 /* line has overflowed - bail */
785 break;
786 }
787 if (p[0] == '#') {
788 /* Comment - skip */
789 continue;
790 }
Victor Stinner05d68a82018-01-18 11:15:25 +0100791
Victor Stinner5f9cf232019-03-19 01:46:25 +0100792 wchar_t *tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100793 if (tmpbuffer) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100794 wchar_t * state;
Minmin Gong8ebc6452019-02-02 20:26:55 -0800795 wchar_t * tok = WCSTOK(tmpbuffer, L" \t\r\n", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100796 if ((tok != NULL) && !wcscmp(tok, key)) {
Minmin Gong8ebc6452019-02-02 20:26:55 -0800797 tok = WCSTOK(NULL, L" \t", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100798 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Minmin Gong8ebc6452019-02-02 20:26:55 -0800799 tok = WCSTOK(NULL, L"\r\n", &state);
Victor Stinner9bee3292017-12-21 16:49:13 +0100800 if (tok != NULL) {
Victor Stinnerfaddaed2019-03-19 02:58:14 +0100801 wcsncpy(value, tok, value_size - 1);
802 value[value_size - 1] = L'\0';
Victor Stinner9bee3292017-12-21 16:49:13 +0100803 result = 1;
804 PyMem_RawFree(tmpbuffer);
805 break;
806 }
807 }
808 }
809 PyMem_RawFree(tmpbuffer);
810 }
811 }
812 return result;
813}
814
Victor Stinner31a83932017-12-04 13:39:15 +0100815#ifdef __cplusplus
816}
817#endif