blob: a3c2bf00ffa94062f83f495a678f0c1bdbc10104 [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001
2/* Return the initial module search path. */
Guido van Rossumc4995721998-08-08 20:05:31 +00003/* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00004
Guido van Rossum88716bb2000-03-30 19:45:39 +00005/* ----------------------------------------------------------------
6 PATH RULES FOR WINDOWS:
7 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these
9 are actually fetched is different)
10
11 * Python always adds an empty entry at the start, which corresponds
12 to the current directory.
13
14 * If the PYTHONPATH env. var. exists, it's entries are added next.
15
16 * We look in the registry for "application paths" - that is, sub-keys
17 under the main PythonPath registry key. These are added next (the
18 order of sub-key processing is undefined).
19 HKEY_CURRENT_USER is searched and added first.
20 HKEY_LOCAL_MACHINE is searched and added next.
21 (Note that all known installers only use HKLM, so HKCU is typically
22 empty)
23
24 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
25 is set, we believe it. Otherwise, we use the path of our host .EXE's
Jeremy Hylton847a9962000-05-26 21:49:07 +000026 to try and locate our "landmark" (lib\\os.py) and deduce our home.
Guido van Rossum88716bb2000-03-30 19:45:39 +000027 - If we DO have a Python Home: The relevant sub-directories (Lib,
28 plat-win, lib-tk, etc) are based on the Python Home
29 - If we DO NOT have a Python Home, the core Python Path is
30 loaded from the registry. This is the main PythonPath key,
31 and both HKLM and HKCU are combined to form the path)
32
33 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
34 specified, and can't locate any Registry entries (ie, we have _nothing_
35 we can assume is a good path), a default path with relative entries is
36 used (eg. .\Lib;.\plat-win, etc)
37
38
39 The end result of all this is:
40 * When running python.exe, or any other .exe in the main Python directory
41 (either an installed version, or directly from the PCbuild directory),
42 the core path is deduced, and the core paths in the registry are
43 ignored. Other "application paths" in the registry are always read.
44
45 * When Python is hosted in another exe (different directory, embedded via
46 COM, etc), the Python Home will not be deduced, so the core path from
47 the registry is used. Other "application paths "in the registry are
48 always read.
49
50 * If Python can't find its home and there is no registry (eg, frozen
51 exe, some very strange installation setup) you get a path with
52 some default, but relative, paths.
53
54 ---------------------------------------------------------------- */
55
56
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000057#include "Python.h"
58#include "osdefs.h"
59
Guido van Rossum8f1b6511997-08-13 19:55:43 +000060#ifdef MS_WIN32
61#include <windows.h>
Guido van Rossum88716bb2000-03-30 19:45:39 +000062#include <tchar.h>
Guido van Rossum8f1b6511997-08-13 19:55:43 +000063#endif
64
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000065#include <sys/types.h>
66#include <sys/stat.h>
67#include <string.h>
68
69#if HAVE_UNISTD_H
70#include <unistd.h>
71#endif /* HAVE_UNISTD_H */
72
73/* Search in some common locations for the associated Python libraries.
74 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000075 * Py_GetPath() tries to return a sensible Python module search path.
76 *
Guido van Rossum42a97441998-02-19 21:00:45 +000077 * The approach is an adaptation for Windows of the strategy used in
78 * ../Modules/getpath.c; it uses the Windows Registry as one of its
79 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000080 */
81
82#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +000083#define LANDMARK "lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000084#endif
85
86static char prefix[MAXPATHLEN+1];
87static char progpath[MAXPATHLEN+1];
88static char *module_search_path = NULL;
89
Guido van Rossumeea14491997-08-13 21:30:44 +000090
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000091static int
Thomas Wouters78890102000-07-22 19:25:51 +000092is_sep(char ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000093{
94#ifdef ALTSEP
95 return ch == SEP || ch == ALTSEP;
96#else
97 return ch == SEP;
98#endif
99}
100
Guido van Rossumeea14491997-08-13 21:30:44 +0000101
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102static void
Thomas Wouters78890102000-07-22 19:25:51 +0000103reduce(char *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000105 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106 while (i > 0 && !is_sep(dir[i]))
107 --i;
108 dir[i] = '\0';
109}
110
111
112static int
Thomas Wouters78890102000-07-22 19:25:51 +0000113exists(char *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000114{
115 struct stat buf;
116 return stat(filename, &buf) == 0;
117}
118
119
Guido van Rossum43ff1141998-08-08 23:40:40 +0000120static int
Thomas Wouters78890102000-07-22 19:25:51 +0000121ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000122{
123 if (exists(filename))
124 return 1;
125
126 /* Check for the compiled version of prefix. */
127 if (strlen(filename) < MAXPATHLEN) {
128 strcat(filename, Py_OptimizeFlag ? "o" : "c");
129 if (exists(filename))
130 return 1;
131 }
132 return 0;
133}
134
135
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000136static void
Thomas Wouters78890102000-07-22 19:25:51 +0000137join(char *buffer, char *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000139 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140 if (is_sep(stuff[0]))
141 n = 0;
142 else {
143 n = strlen(buffer);
144 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
145 buffer[n++] = SEP;
146 }
147 k = strlen(stuff);
148 if (n + k > MAXPATHLEN)
149 k = MAXPATHLEN - n;
150 strncpy(buffer+n, stuff, k);
151 buffer[n+k] = '\0';
152}
153
154
155static int
Thomas Wouters78890102000-07-22 19:25:51 +0000156gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000157{
158 int n, ok;
159
160 n = strlen(prefix);
161 join(prefix, landmark);
162 ok = ismodule(prefix);
163 prefix[n] = '\0';
164 return ok;
165}
166
167
168static int
Thomas Wouters78890102000-07-22 19:25:51 +0000169search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000170{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000171 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000172 strcpy(prefix, argv0_path);
173 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000174 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000175 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000176 reduce(prefix);
177 } while (prefix[0]);
178 return 0;
179}
180
Guido van Rossumeea14491997-08-13 21:30:44 +0000181#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000182
Guido van Rossum88716bb2000-03-30 19:45:39 +0000183/* a string loaded from the DLL at startup.*/
184extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000185
Guido van Rossumeea14491997-08-13 21:30:44 +0000186
187/* Load a PYTHONPATH value from the registry.
188 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
189
Guido van Rossum88716bb2000-03-30 19:45:39 +0000190 Works in both Unicode and 8bit environments. Only uses the
191 Ex family of functions so it also works with Windows CE.
192
Guido van Rossumeea14491997-08-13 21:30:44 +0000193 Returns NULL, or a pointer that should be freed.
194*/
195
196static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000197getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000198{
199 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000200 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000201 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000202 LONG rc;
203 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000204 TCHAR *dataBuf = NULL;
205 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
206 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000207 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000208 DWORD index;
209 TCHAR *keyBuf = NULL;
210 TCHAR *keyBufPtr;
211 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000212
Guido van Rossum88716bb2000-03-30 19:45:39 +0000213 /* Tried to use sysget("winver") but here is too early :-( */
214 versionLen = _tcslen(PyWin_DLLVersionString);
215 /* Space for all the chars, plus one \0 */
216 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
217 sizeof(TCHAR)*(versionLen-1) +
218 sizeof(keySuffix));
219 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000220
Guido van Rossum88716bb2000-03-30 19:45:39 +0000221 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
222 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
223 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
224 keyBufPtr += versionLen;
225 /* NULL comes with this one! */
226 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
227 /* Open the root Python key */
228 rc=RegOpenKeyEx(keyBase,
229 keyBuf, /* subkey */
230 0, /* reserved */
231 KEY_READ,
232 &newKey);
233 if (rc!=ERROR_SUCCESS) goto done;
234 /* Find out how big our core buffer is, and how many subkeys we have */
235 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
236 NULL, NULL, &dataSize, NULL, NULL);
237 if (rc!=ERROR_SUCCESS) goto done;
238 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
239 /* Allocate a temp array of char buffers, so we only need to loop
240 reading the registry once
241 */
242 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
243 if (ppPaths==NULL) goto done;
244 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
245 /* Loop over all subkeys, allocating a temp sub-buffer. */
246 for(index=0;index<numKeys;index++) {
247 TCHAR keyBuf[MAX_PATH+1];
248 HKEY subKey = 0;
249 DWORD reqdSize = MAX_PATH+1;
250 /* Get the sub-key name */
251 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
252 NULL, NULL, NULL, NULL );
253 if (rc!=ERROR_SUCCESS) goto done;
254 /* Open the sub-key */
255 rc=RegOpenKeyEx(newKey,
256 keyBuf, /* subkey */
257 0, /* reserved */
258 KEY_READ,
259 &subKey);
260 if (rc!=ERROR_SUCCESS) goto done;
261 /* Find the value of the buffer size, malloc, then read it */
262 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
263 if (reqdSize) {
264 ppPaths[index] = malloc(reqdSize);
265 if (ppPaths[index]) {
Mark Hammonde61aca72000-09-10 09:14:53 +0000266 RegQueryValueEx(subKey, NULL, 0, NULL,
267 (LPBYTE)ppPaths[index],
268 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000269 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000270 }
271 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000272 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000273 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000274 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
275 if (dataBuf) {
276 TCHAR *szCur = dataBuf;
277 DWORD reqdSize = dataSize;
278 /* Copy our collected strings */
279 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000280 if (index > 0) {
281 *(szCur++) = _T(';');
282 dataSize--;
283 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000284 if (ppPaths[index]) {
285 int len = _tcslen(ppPaths[index]);
286 _tcsncpy(szCur, ppPaths[index], len);
287 szCur += len;
288 dataSize -= len;
289 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000290 }
291 if (skipcore)
292 *szCur = '\0';
293 else {
294 *(szCur++) = _T(';');
295 dataSize--;
Mark Hammonde61aca72000-09-10 09:14:53 +0000296 /* Now append the core path entries -
297 this will include the NULL
298 */
299 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
300 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000301 }
302 /* And set the result - caller must free
303 If MBCS, it is fine as is. If Unicode, allocate new
304 buffer and convert.
305 */
306#ifdef UNICODE
307 retval = (char *)malloc(reqdSize+1);
308 if (retval)
309 WideCharToMultiByte(CP_ACP, 0,
310 dataBuf, -1, /* source */
311 retval, dataSize+1, /* dest */
312 NULL, NULL);
313 free(dataBuf);
314#else
315 retval = dataBuf;
316#endif
317 }
318done:
319 /* Loop freeing my temp buffers */
320 if (ppPaths) {
321 for(index=0;index<numKeys;index++)
322 if (ppPaths[index]) free(ppPaths[index]);
323 free(ppPaths);
324 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000325 if (newKey)
326 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000327 if (keyBuf)
328 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000329 return retval;
330}
331#endif /* MS_WIN32 */
332
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000333static void
Thomas Wouters78890102000-07-22 19:25:51 +0000334get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000335{
Thomas Woutersa5345942000-07-22 23:59:33 +0000336 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000337 char *path = getenv("PATH");
338 char *prog = Py_GetProgramName();
339
Guido van Rossumeea14491997-08-13 21:30:44 +0000340#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000341#ifdef UNICODE
342 WCHAR wprogpath[MAXPATHLEN+1];
343 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
344 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
345 return;
346 }
347#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000348 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
349 return;
350#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000351#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000352 if (prog == NULL || *prog == '\0')
353 prog = "python";
354
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000355 /* If there is no slash in the argv0 path, then we have to
356 * assume python is on the user's $PATH, since there's no
357 * other way to find a directory to start the search from. If
358 * $PATH isn't exported, you lose.
359 */
360#ifdef ALTSEP
361 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
362#else
363 if (strchr(prog, SEP))
364#endif
365 strcpy(progpath, prog);
366 else if (path) {
367 while (1) {
368 char *delim = strchr(path, DELIM);
369
370 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000371 size_t len = delim - path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000372 strncpy(progpath, path, len);
373 *(progpath + len) = '\0';
374 }
375 else
376 strcpy(progpath, path);
377
378 join(progpath, prog);
379 if (exists(progpath))
380 break;
381
382 if (!delim) {
383 progpath[0] = '\0';
384 break;
385 }
386 path = delim + 1;
387 }
388 }
389 else
390 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000391}
392
393static void
Thomas Wouters78890102000-07-22 19:25:51 +0000394calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000395{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000396 char argv0_path[MAXPATHLEN+1];
397 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000398 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000399 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000400 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000401
Guido van Rossum43ff1141998-08-08 23:40:40 +0000402#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000403 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000404 char *machinepath = NULL;
405 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000406#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000407
408 get_progpath();
409 strcpy(argv0_path, progpath);
410 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000411 if (pythonhome == NULL || *pythonhome == '\0') {
412 if (search_for_prefix(argv0_path, LANDMARK))
413 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000414 else
415 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000416 }
417 else
418 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000419
Guido van Rossumeea14491997-08-13 21:30:44 +0000420 if (envpath && *envpath == '\0')
421 envpath = NULL;
422
Guido van Rossume02e48b2000-03-29 01:49:47 +0000423
Guido van Rossum43ff1141998-08-08 23:40:40 +0000424#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000425 skiphome = pythonhome==NULL ? 0 : 1;
426 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
427 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
428 /* We only use the default relative PYTHONPATH if we havent
429 anything better to use! */
430 skipdefault = envpath!=NULL || pythonhome!=NULL || \
431 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000432#endif
433
434 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000435 (1) the PYTHONPATH environment variable, if set;
436 (2) for Win32, the machinepath and userpath, if set;
437 (3) the PYTHONPATH config macro, with the leading "."
438 of each component replaced with pythonhome, if set;
439 (4) the directory containing the executable (argv0_path).
440 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000441 Extra rules:
442 - If PYTHONHOME is set (in any way) item (2) is ignored.
443 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000444 */
445
446 /* Calculate size of return buffer */
447 if (pythonhome != NULL) {
448 char *p;
449 bufsz = 1;
450 for (p = PYTHONPATH; *p; p++) {
451 if (*p == DELIM)
452 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000453 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000454 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000455 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000456 else
457 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000458 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000459 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000460#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000461 if (userpath)
462 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000463 if (machinepath)
464 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000465#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000466 if (envpath != NULL)
467 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000468
469 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000470 if (buf == NULL) {
471 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000472 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
473 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000474 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000475 module_search_path = envpath;
476 }
477 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000478 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000479 module_search_path = PYTHONPATH;
480 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000481#ifdef MS_WIN32
482 if (machinepath)
483 free(machinepath);
484 if (userpath)
485 free(userpath);
486#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000487 return;
488 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000489
490 if (envpath) {
491 strcpy(buf, envpath);
492 buf = strchr(buf, '\0');
493 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000494 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000495#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000496 if (userpath) {
497 strcpy(buf, userpath);
498 buf = strchr(buf, '\0');
499 *buf++ = DELIM;
500 free(userpath);
501 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000502 if (machinepath) {
503 strcpy(buf, machinepath);
504 buf = strchr(buf, '\0');
505 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000506 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000507 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000508 if (pythonhome == NULL) {
509 if (!skipdefault) {
510 strcpy(buf, PYTHONPATH);
511 buf = strchr(buf, '\0');
512 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000513 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000514#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000515 if (pythonhome == NULL) {
516 strcpy(buf, PYTHONPATH);
517 buf = strchr(buf, '\0');
518 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000519#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000520 else {
521 char *p = PYTHONPATH;
522 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000523 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000524 for (;;) {
525 q = strchr(p, DELIM);
526 if (q == NULL)
527 n = strlen(p);
528 else
529 n = q-p;
530 if (p[0] == '.' && is_sep(p[1])) {
531 strcpy(buf, pythonhome);
532 buf = strchr(buf, '\0');
533 p++;
534 n--;
535 }
536 strncpy(buf, p, n);
537 buf += n;
538 if (q == NULL)
539 break;
540 *buf++ = DELIM;
541 p = q+1;
542 }
543 }
544 if (argv0_path) {
545 *buf++ = DELIM;
546 strcpy(buf, argv0_path);
547 buf = strchr(buf, '\0');
548 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000549 *buf = '\0';
550}
551
552
553/* External interface */
554
555char *
Thomas Wouters78890102000-07-22 19:25:51 +0000556Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000557{
558 if (!module_search_path)
559 calculate_path();
560 return module_search_path;
561}
562
563char *
Thomas Wouters78890102000-07-22 19:25:51 +0000564Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000565{
Guido van Rossumeea14491997-08-13 21:30:44 +0000566 if (!module_search_path)
567 calculate_path();
568 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000569}
570
571char *
Thomas Wouters78890102000-07-22 19:25:51 +0000572Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000573{
Guido van Rossumeea14491997-08-13 21:30:44 +0000574 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000575}
576
577char *
Thomas Wouters78890102000-07-22 19:25:51 +0000578Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000579{
580 if (!module_search_path)
581 calculate_path();
582 return progpath;
583}