blob: 76ee5e6d4ddc397b90502117484f3f096e173040 [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Return the initial module search path. */
Guido van Rossumc4995721998-08-08 20:05:31 +000033/* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000034
Guido van Rossum88716bb2000-03-30 19:45:39 +000035/* ----------------------------------------------------------------
36 PATH RULES FOR WINDOWS:
37 This describes how sys.path is formed on Windows. It describes the
38 functionality, not the implementation (ie, the order in which these
39 are actually fetched is different)
40
41 * Python always adds an empty entry at the start, which corresponds
42 to the current directory.
43
44 * If the PYTHONPATH env. var. exists, it's entries are added next.
45
46 * We look in the registry for "application paths" - that is, sub-keys
47 under the main PythonPath registry key. These are added next (the
48 order of sub-key processing is undefined).
49 HKEY_CURRENT_USER is searched and added first.
50 HKEY_LOCAL_MACHINE is searched and added next.
51 (Note that all known installers only use HKLM, so HKCU is typically
52 empty)
53
54 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
55 is set, we believe it. Otherwise, we use the path of our host .EXE's
Jeremy Hylton847a9962000-05-26 21:49:07 +000056 to try and locate our "landmark" (lib\\os.py) and deduce our home.
Guido van Rossum88716bb2000-03-30 19:45:39 +000057 - If we DO have a Python Home: The relevant sub-directories (Lib,
58 plat-win, lib-tk, etc) are based on the Python Home
59 - If we DO NOT have a Python Home, the core Python Path is
60 loaded from the registry. This is the main PythonPath key,
61 and both HKLM and HKCU are combined to form the path)
62
63 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
64 specified, and can't locate any Registry entries (ie, we have _nothing_
65 we can assume is a good path), a default path with relative entries is
66 used (eg. .\Lib;.\plat-win, etc)
67
68
69 The end result of all this is:
70 * When running python.exe, or any other .exe in the main Python directory
71 (either an installed version, or directly from the PCbuild directory),
72 the core path is deduced, and the core paths in the registry are
73 ignored. Other "application paths" in the registry are always read.
74
75 * When Python is hosted in another exe (different directory, embedded via
76 COM, etc), the Python Home will not be deduced, so the core path from
77 the registry is used. Other "application paths "in the registry are
78 always read.
79
80 * If Python can't find its home and there is no registry (eg, frozen
81 exe, some very strange installation setup) you get a path with
82 some default, but relative, paths.
83
84 ---------------------------------------------------------------- */
85
86
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087#include "Python.h"
88#include "osdefs.h"
89
Guido van Rossum8f1b6511997-08-13 19:55:43 +000090#ifdef MS_WIN32
91#include <windows.h>
Guido van Rossum88716bb2000-03-30 19:45:39 +000092#include <tchar.h>
Guido van Rossum8f1b6511997-08-13 19:55:43 +000093#endif
94
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000095#include <sys/types.h>
96#include <sys/stat.h>
97#include <string.h>
98
99#if HAVE_UNISTD_H
100#include <unistd.h>
101#endif /* HAVE_UNISTD_H */
102
103/* Search in some common locations for the associated Python libraries.
104 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 * Py_GetPath() tries to return a sensible Python module search path.
106 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000107 * The approach is an adaptation for Windows of the strategy used in
108 * ../Modules/getpath.c; it uses the Windows Registry as one of its
109 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000110 */
111
112#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +0000113#define LANDMARK "lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000114#endif
115
116static char prefix[MAXPATHLEN+1];
117static char progpath[MAXPATHLEN+1];
118static char *module_search_path = NULL;
119
Guido van Rossumeea14491997-08-13 21:30:44 +0000120
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000121static int
122is_sep(ch) /* determine if "ch" is a separator character */
123 char ch;
124{
125#ifdef ALTSEP
126 return ch == SEP || ch == ALTSEP;
127#else
128 return ch == SEP;
129#endif
130}
131
Guido van Rossumeea14491997-08-13 21:30:44 +0000132
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000133static void
134reduce(dir)
135 char *dir;
136{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000137 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138 while (i > 0 && !is_sep(dir[i]))
139 --i;
140 dir[i] = '\0';
141}
142
143
144static int
145exists(filename)
146 char *filename;
147{
148 struct stat buf;
149 return stat(filename, &buf) == 0;
150}
151
152
Guido van Rossum43ff1141998-08-08 23:40:40 +0000153static int
154ismodule(filename) /* Is module -- check for .pyc/.pyo too */
155 char *filename;
156{
157 if (exists(filename))
158 return 1;
159
160 /* Check for the compiled version of prefix. */
161 if (strlen(filename) < MAXPATHLEN) {
162 strcat(filename, Py_OptimizeFlag ? "o" : "c");
163 if (exists(filename))
164 return 1;
165 }
166 return 0;
167}
168
169
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000170static void
171join(buffer, stuff)
172 char *buffer;
173 char *stuff;
174{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000175 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000176 if (is_sep(stuff[0]))
177 n = 0;
178 else {
179 n = strlen(buffer);
180 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
181 buffer[n++] = SEP;
182 }
183 k = strlen(stuff);
184 if (n + k > MAXPATHLEN)
185 k = MAXPATHLEN - n;
186 strncpy(buffer+n, stuff, k);
187 buffer[n+k] = '\0';
188}
189
190
191static int
Guido van Rossume02e48b2000-03-29 01:49:47 +0000192gotlandmark(landmark)
193 char *landmark;
194{
195 int n, ok;
196
197 n = strlen(prefix);
198 join(prefix, landmark);
199 ok = ismodule(prefix);
200 prefix[n] = '\0';
201 return ok;
202}
203
204
205static int
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206search_for_prefix(argv0_path, landmark)
207 char *argv0_path;
208 char *landmark;
209{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000210 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000211 strcpy(prefix, argv0_path);
212 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000213 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000214 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000215 reduce(prefix);
216 } while (prefix[0]);
217 return 0;
218}
219
Guido van Rossumeea14491997-08-13 21:30:44 +0000220#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000221
Guido van Rossum88716bb2000-03-30 19:45:39 +0000222/* a string loaded from the DLL at startup.*/
223extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000224
Guido van Rossumeea14491997-08-13 21:30:44 +0000225
226/* Load a PYTHONPATH value from the registry.
227 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
228
Guido van Rossum88716bb2000-03-30 19:45:39 +0000229 Works in both Unicode and 8bit environments. Only uses the
230 Ex family of functions so it also works with Windows CE.
231
Guido van Rossumeea14491997-08-13 21:30:44 +0000232 Returns NULL, or a pointer that should be freed.
233*/
234
235static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000236getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000237{
238 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000239 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000240 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000241 LONG rc;
242 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000243 TCHAR *dataBuf = NULL;
244 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
245 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000246 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000247 DWORD index;
248 TCHAR *keyBuf = NULL;
249 TCHAR *keyBufPtr;
250 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000251
Guido van Rossum88716bb2000-03-30 19:45:39 +0000252 /* Tried to use sysget("winver") but here is too early :-( */
253 versionLen = _tcslen(PyWin_DLLVersionString);
254 /* Space for all the chars, plus one \0 */
255 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
256 sizeof(TCHAR)*(versionLen-1) +
257 sizeof(keySuffix));
258 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000259
Guido van Rossum88716bb2000-03-30 19:45:39 +0000260 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
261 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
262 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
263 keyBufPtr += versionLen;
264 /* NULL comes with this one! */
265 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
266 /* Open the root Python key */
267 rc=RegOpenKeyEx(keyBase,
268 keyBuf, /* subkey */
269 0, /* reserved */
270 KEY_READ,
271 &newKey);
272 if (rc!=ERROR_SUCCESS) goto done;
273 /* Find out how big our core buffer is, and how many subkeys we have */
274 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
275 NULL, NULL, &dataSize, NULL, NULL);
276 if (rc!=ERROR_SUCCESS) goto done;
277 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
278 /* Allocate a temp array of char buffers, so we only need to loop
279 reading the registry once
280 */
281 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
282 if (ppPaths==NULL) goto done;
283 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
284 /* Loop over all subkeys, allocating a temp sub-buffer. */
285 for(index=0;index<numKeys;index++) {
286 TCHAR keyBuf[MAX_PATH+1];
287 HKEY subKey = 0;
288 DWORD reqdSize = MAX_PATH+1;
289 /* Get the sub-key name */
290 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
291 NULL, NULL, NULL, NULL );
292 if (rc!=ERROR_SUCCESS) goto done;
293 /* Open the sub-key */
294 rc=RegOpenKeyEx(newKey,
295 keyBuf, /* subkey */
296 0, /* reserved */
297 KEY_READ,
298 &subKey);
299 if (rc!=ERROR_SUCCESS) goto done;
300 /* Find the value of the buffer size, malloc, then read it */
301 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
302 if (reqdSize) {
303 ppPaths[index] = malloc(reqdSize);
304 if (ppPaths[index]) {
305 RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize);
306 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000307 }
308 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000309 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000310 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000311 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
312 if (dataBuf) {
313 TCHAR *szCur = dataBuf;
314 DWORD reqdSize = dataSize;
315 /* Copy our collected strings */
316 for (index=0;index<numKeys;index++) {
317 int len;
318 if (index > 0) {
319 *(szCur++) = _T(';');
320 dataSize--;
321 }
322 len = _tcslen(ppPaths[index]);
323 _tcsncpy(szCur, ppPaths[index], len);
324 szCur += len;
325 dataSize -= len;
326 }
327 if (skipcore)
328 *szCur = '\0';
329 else {
330 *(szCur++) = _T(';');
331 dataSize--;
332 /* Now append the core path entries - this will include the NULL */
333 rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize);
334 }
335 /* And set the result - caller must free
336 If MBCS, it is fine as is. If Unicode, allocate new
337 buffer and convert.
338 */
339#ifdef UNICODE
340 retval = (char *)malloc(reqdSize+1);
341 if (retval)
342 WideCharToMultiByte(CP_ACP, 0,
343 dataBuf, -1, /* source */
344 retval, dataSize+1, /* dest */
345 NULL, NULL);
346 free(dataBuf);
347#else
348 retval = dataBuf;
349#endif
350 }
351done:
352 /* Loop freeing my temp buffers */
353 if (ppPaths) {
354 for(index=0;index<numKeys;index++)
355 if (ppPaths[index]) free(ppPaths[index]);
356 free(ppPaths);
357 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000358 if (newKey)
359 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000360 if (keyBuf)
361 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000362 return retval;
363}
364#endif /* MS_WIN32 */
365
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000366static void
367get_progpath()
368{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000369 extern char *Py_GetProgramName();
370 char *path = getenv("PATH");
371 char *prog = Py_GetProgramName();
372
Guido van Rossumeea14491997-08-13 21:30:44 +0000373#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000374#ifdef UNICODE
375 WCHAR wprogpath[MAXPATHLEN+1];
376 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
377 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
378 return;
379 }
380#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000381 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
382 return;
383#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000384#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000385 if (prog == NULL || *prog == '\0')
386 prog = "python";
387
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000388 /* If there is no slash in the argv0 path, then we have to
389 * assume python is on the user's $PATH, since there's no
390 * other way to find a directory to start the search from. If
391 * $PATH isn't exported, you lose.
392 */
393#ifdef ALTSEP
394 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
395#else
396 if (strchr(prog, SEP))
397#endif
398 strcpy(progpath, prog);
399 else if (path) {
400 while (1) {
401 char *delim = strchr(path, DELIM);
402
403 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000404 size_t len = delim - path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000405 strncpy(progpath, path, len);
406 *(progpath + len) = '\0';
407 }
408 else
409 strcpy(progpath, path);
410
411 join(progpath, prog);
412 if (exists(progpath))
413 break;
414
415 if (!delim) {
416 progpath[0] = '\0';
417 break;
418 }
419 path = delim + 1;
420 }
421 }
422 else
423 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000424}
425
426static void
427calculate_path()
428{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000429 char argv0_path[MAXPATHLEN+1];
430 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000431 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000432 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000433 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000434
Guido van Rossum43ff1141998-08-08 23:40:40 +0000435#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000436 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000437 char *machinepath = NULL;
438 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000439#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000440
441 get_progpath();
442 strcpy(argv0_path, progpath);
443 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000444 if (pythonhome == NULL || *pythonhome == '\0') {
445 if (search_for_prefix(argv0_path, LANDMARK))
446 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000447 else
448 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000449 }
450 else
451 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000452
Guido van Rossumeea14491997-08-13 21:30:44 +0000453 if (envpath && *envpath == '\0')
454 envpath = NULL;
455
Guido van Rossume02e48b2000-03-29 01:49:47 +0000456
Guido van Rossum43ff1141998-08-08 23:40:40 +0000457#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000458 skiphome = pythonhome==NULL ? 0 : 1;
459 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
460 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
461 /* We only use the default relative PYTHONPATH if we havent
462 anything better to use! */
463 skipdefault = envpath!=NULL || pythonhome!=NULL || \
464 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000465#endif
466
467 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000468 (1) the PYTHONPATH environment variable, if set;
469 (2) for Win32, the machinepath and userpath, if set;
470 (3) the PYTHONPATH config macro, with the leading "."
471 of each component replaced with pythonhome, if set;
472 (4) the directory containing the executable (argv0_path).
473 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000474 Extra rules:
475 - If PYTHONHOME is set (in any way) item (2) is ignored.
476 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000477 */
478
479 /* Calculate size of return buffer */
480 if (pythonhome != NULL) {
481 char *p;
482 bufsz = 1;
483 for (p = PYTHONPATH; *p; p++) {
484 if (*p == DELIM)
485 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000486 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000487 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000489 else
490 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000491 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000492 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000493#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000494 if (userpath)
495 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000496 if (machinepath)
497 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000498#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000499 if (envpath != NULL)
500 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000501
502 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000503 if (buf == NULL) {
504 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000505 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
506 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000507 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000508 module_search_path = envpath;
509 }
510 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000511 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000512 module_search_path = PYTHONPATH;
513 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000514#ifdef MS_WIN32
515 if (machinepath)
516 free(machinepath);
517 if (userpath)
518 free(userpath);
519#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000520 return;
521 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000522
523 if (envpath) {
524 strcpy(buf, envpath);
525 buf = strchr(buf, '\0');
526 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000527 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000528#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000529 if (userpath) {
530 strcpy(buf, userpath);
531 buf = strchr(buf, '\0');
532 *buf++ = DELIM;
533 free(userpath);
534 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000535 if (machinepath) {
536 strcpy(buf, machinepath);
537 buf = strchr(buf, '\0');
538 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000539 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000540 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000541 if (pythonhome == NULL) {
542 if (!skipdefault) {
543 strcpy(buf, PYTHONPATH);
544 buf = strchr(buf, '\0');
545 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000546 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000547#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000548 if (pythonhome == NULL) {
549 strcpy(buf, PYTHONPATH);
550 buf = strchr(buf, '\0');
551 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000552#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000553 else {
554 char *p = PYTHONPATH;
555 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000556 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000557 for (;;) {
558 q = strchr(p, DELIM);
559 if (q == NULL)
560 n = strlen(p);
561 else
562 n = q-p;
563 if (p[0] == '.' && is_sep(p[1])) {
564 strcpy(buf, pythonhome);
565 buf = strchr(buf, '\0');
566 p++;
567 n--;
568 }
569 strncpy(buf, p, n);
570 buf += n;
571 if (q == NULL)
572 break;
573 *buf++ = DELIM;
574 p = q+1;
575 }
576 }
577 if (argv0_path) {
578 *buf++ = DELIM;
579 strcpy(buf, argv0_path);
580 buf = strchr(buf, '\0');
581 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000582 *buf = '\0';
583}
584
585
586/* External interface */
587
588char *
589Py_GetPath()
590{
591 if (!module_search_path)
592 calculate_path();
593 return module_search_path;
594}
595
596char *
597Py_GetPrefix()
598{
Guido van Rossumeea14491997-08-13 21:30:44 +0000599 if (!module_search_path)
600 calculate_path();
601 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000602}
603
604char *
605Py_GetExecPrefix()
606{
Guido van Rossumeea14491997-08-13 21:30:44 +0000607 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000608}
609
610char *
Guido van Rossumeea14491997-08-13 21:30:44 +0000611Py_GetProgramFullPath()
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000612{
613 if (!module_search_path)
614 calculate_path();
615 return progpath;
616}