blob: e4ff136ea7013bcf2cbde3d49351beb15d06610e [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00009******************************************************************/
10
11/* Return the initial module search path. */
Guido van Rossumc4995721998-08-08 20:05:31 +000012/* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000013
Guido van Rossum88716bb2000-03-30 19:45:39 +000014/* ----------------------------------------------------------------
15 PATH RULES FOR WINDOWS:
16 This describes how sys.path is formed on Windows. It describes the
17 functionality, not the implementation (ie, the order in which these
18 are actually fetched is different)
19
20 * Python always adds an empty entry at the start, which corresponds
21 to the current directory.
22
23 * If the PYTHONPATH env. var. exists, it's entries are added next.
24
25 * We look in the registry for "application paths" - that is, sub-keys
26 under the main PythonPath registry key. These are added next (the
27 order of sub-key processing is undefined).
28 HKEY_CURRENT_USER is searched and added first.
29 HKEY_LOCAL_MACHINE is searched and added next.
30 (Note that all known installers only use HKLM, so HKCU is typically
31 empty)
32
33 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
34 is set, we believe it. Otherwise, we use the path of our host .EXE's
Jeremy Hylton847a9962000-05-26 21:49:07 +000035 to try and locate our "landmark" (lib\\os.py) and deduce our home.
Guido van Rossum88716bb2000-03-30 19:45:39 +000036 - If we DO have a Python Home: The relevant sub-directories (Lib,
37 plat-win, lib-tk, etc) are based on the Python Home
38 - If we DO NOT have a Python Home, the core Python Path is
39 loaded from the registry. This is the main PythonPath key,
40 and both HKLM and HKCU are combined to form the path)
41
42 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
43 specified, and can't locate any Registry entries (ie, we have _nothing_
44 we can assume is a good path), a default path with relative entries is
45 used (eg. .\Lib;.\plat-win, etc)
46
47
48 The end result of all this is:
49 * When running python.exe, or any other .exe in the main Python directory
50 (either an installed version, or directly from the PCbuild directory),
51 the core path is deduced, and the core paths in the registry are
52 ignored. Other "application paths" in the registry are always read.
53
54 * When Python is hosted in another exe (different directory, embedded via
55 COM, etc), the Python Home will not be deduced, so the core path from
56 the registry is used. Other "application paths "in the registry are
57 always read.
58
59 * If Python can't find its home and there is no registry (eg, frozen
60 exe, some very strange installation setup) you get a path with
61 some default, but relative, paths.
62
63 ---------------------------------------------------------------- */
64
65
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000066#include "Python.h"
67#include "osdefs.h"
68
Guido van Rossum8f1b6511997-08-13 19:55:43 +000069#ifdef MS_WIN32
70#include <windows.h>
Guido van Rossum88716bb2000-03-30 19:45:39 +000071#include <tchar.h>
Guido van Rossum8f1b6511997-08-13 19:55:43 +000072#endif
73
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000074#include <sys/types.h>
75#include <sys/stat.h>
76#include <string.h>
77
78#if HAVE_UNISTD_H
79#include <unistd.h>
80#endif /* HAVE_UNISTD_H */
81
82/* Search in some common locations for the associated Python libraries.
83 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000084 * Py_GetPath() tries to return a sensible Python module search path.
85 *
Guido van Rossum42a97441998-02-19 21:00:45 +000086 * The approach is an adaptation for Windows of the strategy used in
87 * ../Modules/getpath.c; it uses the Windows Registry as one of its
88 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000089 */
90
91#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +000092#define LANDMARK "lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000093#endif
94
95static char prefix[MAXPATHLEN+1];
96static char progpath[MAXPATHLEN+1];
97static char *module_search_path = NULL;
98
Guido van Rossumeea14491997-08-13 21:30:44 +000099
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100static int
Thomas Wouters78890102000-07-22 19:25:51 +0000101is_sep(char ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102{
103#ifdef ALTSEP
104 return ch == SEP || ch == ALTSEP;
105#else
106 return ch == SEP;
107#endif
108}
109
Guido van Rossumeea14491997-08-13 21:30:44 +0000110
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000111static void
Thomas Wouters78890102000-07-22 19:25:51 +0000112reduce(char *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000114 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115 while (i > 0 && !is_sep(dir[i]))
116 --i;
117 dir[i] = '\0';
118}
119
120
121static int
Thomas Wouters78890102000-07-22 19:25:51 +0000122exists(char *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000123{
124 struct stat buf;
125 return stat(filename, &buf) == 0;
126}
127
128
Guido van Rossum43ff1141998-08-08 23:40:40 +0000129static int
Thomas Wouters78890102000-07-22 19:25:51 +0000130ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000131{
132 if (exists(filename))
133 return 1;
134
135 /* Check for the compiled version of prefix. */
136 if (strlen(filename) < MAXPATHLEN) {
137 strcat(filename, Py_OptimizeFlag ? "o" : "c");
138 if (exists(filename))
139 return 1;
140 }
141 return 0;
142}
143
144
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000145static void
Thomas Wouters78890102000-07-22 19:25:51 +0000146join(char *buffer, char *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000148 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000149 if (is_sep(stuff[0]))
150 n = 0;
151 else {
152 n = strlen(buffer);
153 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
154 buffer[n++] = SEP;
155 }
156 k = strlen(stuff);
157 if (n + k > MAXPATHLEN)
158 k = MAXPATHLEN - n;
159 strncpy(buffer+n, stuff, k);
160 buffer[n+k] = '\0';
161}
162
163
164static int
Thomas Wouters78890102000-07-22 19:25:51 +0000165gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000166{
167 int n, ok;
168
169 n = strlen(prefix);
170 join(prefix, landmark);
171 ok = ismodule(prefix);
172 prefix[n] = '\0';
173 return ok;
174}
175
176
177static int
Thomas Wouters78890102000-07-22 19:25:51 +0000178search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000179{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000180 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000181 strcpy(prefix, argv0_path);
182 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000183 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000184 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000185 reduce(prefix);
186 } while (prefix[0]);
187 return 0;
188}
189
Guido van Rossumeea14491997-08-13 21:30:44 +0000190#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000191
Guido van Rossum88716bb2000-03-30 19:45:39 +0000192/* a string loaded from the DLL at startup.*/
193extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000194
Guido van Rossumeea14491997-08-13 21:30:44 +0000195
196/* Load a PYTHONPATH value from the registry.
197 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
198
Guido van Rossum88716bb2000-03-30 19:45:39 +0000199 Works in both Unicode and 8bit environments. Only uses the
200 Ex family of functions so it also works with Windows CE.
201
Guido van Rossumeea14491997-08-13 21:30:44 +0000202 Returns NULL, or a pointer that should be freed.
203*/
204
205static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000206getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000207{
208 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000209 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000210 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000211 LONG rc;
212 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000213 TCHAR *dataBuf = NULL;
214 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
215 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000216 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000217 DWORD index;
218 TCHAR *keyBuf = NULL;
219 TCHAR *keyBufPtr;
220 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000221
Guido van Rossum88716bb2000-03-30 19:45:39 +0000222 /* Tried to use sysget("winver") but here is too early :-( */
223 versionLen = _tcslen(PyWin_DLLVersionString);
224 /* Space for all the chars, plus one \0 */
225 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
226 sizeof(TCHAR)*(versionLen-1) +
227 sizeof(keySuffix));
228 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000229
Guido van Rossum88716bb2000-03-30 19:45:39 +0000230 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
231 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
232 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
233 keyBufPtr += versionLen;
234 /* NULL comes with this one! */
235 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
236 /* Open the root Python key */
237 rc=RegOpenKeyEx(keyBase,
238 keyBuf, /* subkey */
239 0, /* reserved */
240 KEY_READ,
241 &newKey);
242 if (rc!=ERROR_SUCCESS) goto done;
243 /* Find out how big our core buffer is, and how many subkeys we have */
244 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
245 NULL, NULL, &dataSize, NULL, NULL);
246 if (rc!=ERROR_SUCCESS) goto done;
247 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
248 /* Allocate a temp array of char buffers, so we only need to loop
249 reading the registry once
250 */
251 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
252 if (ppPaths==NULL) goto done;
253 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
254 /* Loop over all subkeys, allocating a temp sub-buffer. */
255 for(index=0;index<numKeys;index++) {
256 TCHAR keyBuf[MAX_PATH+1];
257 HKEY subKey = 0;
258 DWORD reqdSize = MAX_PATH+1;
259 /* Get the sub-key name */
260 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
261 NULL, NULL, NULL, NULL );
262 if (rc!=ERROR_SUCCESS) goto done;
263 /* Open the sub-key */
264 rc=RegOpenKeyEx(newKey,
265 keyBuf, /* subkey */
266 0, /* reserved */
267 KEY_READ,
268 &subKey);
269 if (rc!=ERROR_SUCCESS) goto done;
270 /* Find the value of the buffer size, malloc, then read it */
271 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
272 if (reqdSize) {
273 ppPaths[index] = malloc(reqdSize);
274 if (ppPaths[index]) {
275 RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize);
276 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000277 }
278 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000279 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000280 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000281 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
282 if (dataBuf) {
283 TCHAR *szCur = dataBuf;
284 DWORD reqdSize = dataSize;
285 /* Copy our collected strings */
286 for (index=0;index<numKeys;index++) {
287 int len;
288 if (index > 0) {
289 *(szCur++) = _T(';');
290 dataSize--;
291 }
292 len = _tcslen(ppPaths[index]);
293 _tcsncpy(szCur, ppPaths[index], len);
294 szCur += len;
295 dataSize -= len;
296 }
297 if (skipcore)
298 *szCur = '\0';
299 else {
300 *(szCur++) = _T(';');
301 dataSize--;
302 /* Now append the core path entries - this will include the NULL */
303 rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize);
304 }
305 /* And set the result - caller must free
306 If MBCS, it is fine as is. If Unicode, allocate new
307 buffer and convert.
308 */
309#ifdef UNICODE
310 retval = (char *)malloc(reqdSize+1);
311 if (retval)
312 WideCharToMultiByte(CP_ACP, 0,
313 dataBuf, -1, /* source */
314 retval, dataSize+1, /* dest */
315 NULL, NULL);
316 free(dataBuf);
317#else
318 retval = dataBuf;
319#endif
320 }
321done:
322 /* Loop freeing my temp buffers */
323 if (ppPaths) {
324 for(index=0;index<numKeys;index++)
325 if (ppPaths[index]) free(ppPaths[index]);
326 free(ppPaths);
327 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000328 if (newKey)
329 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000330 if (keyBuf)
331 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000332 return retval;
333}
334#endif /* MS_WIN32 */
335
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000336static void
Thomas Wouters78890102000-07-22 19:25:51 +0000337get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000338{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000339 extern char *Py_GetProgramName();
340 char *path = getenv("PATH");
341 char *prog = Py_GetProgramName();
342
Guido van Rossumeea14491997-08-13 21:30:44 +0000343#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000344#ifdef UNICODE
345 WCHAR wprogpath[MAXPATHLEN+1];
346 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
347 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
348 return;
349 }
350#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000351 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
352 return;
353#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000354#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000355 if (prog == NULL || *prog == '\0')
356 prog = "python";
357
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000358 /* If there is no slash in the argv0 path, then we have to
359 * assume python is on the user's $PATH, since there's no
360 * other way to find a directory to start the search from. If
361 * $PATH isn't exported, you lose.
362 */
363#ifdef ALTSEP
364 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
365#else
366 if (strchr(prog, SEP))
367#endif
368 strcpy(progpath, prog);
369 else if (path) {
370 while (1) {
371 char *delim = strchr(path, DELIM);
372
373 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000374 size_t len = delim - path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000375 strncpy(progpath, path, len);
376 *(progpath + len) = '\0';
377 }
378 else
379 strcpy(progpath, path);
380
381 join(progpath, prog);
382 if (exists(progpath))
383 break;
384
385 if (!delim) {
386 progpath[0] = '\0';
387 break;
388 }
389 path = delim + 1;
390 }
391 }
392 else
393 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000394}
395
396static void
Thomas Wouters78890102000-07-22 19:25:51 +0000397calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000398{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000399 char argv0_path[MAXPATHLEN+1];
400 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000401 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000402 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000403 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000404
Guido van Rossum43ff1141998-08-08 23:40:40 +0000405#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000406 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000407 char *machinepath = NULL;
408 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000409#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000410
411 get_progpath();
412 strcpy(argv0_path, progpath);
413 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000414 if (pythonhome == NULL || *pythonhome == '\0') {
415 if (search_for_prefix(argv0_path, LANDMARK))
416 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000417 else
418 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000419 }
420 else
421 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000422
Guido van Rossumeea14491997-08-13 21:30:44 +0000423 if (envpath && *envpath == '\0')
424 envpath = NULL;
425
Guido van Rossume02e48b2000-03-29 01:49:47 +0000426
Guido van Rossum43ff1141998-08-08 23:40:40 +0000427#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000428 skiphome = pythonhome==NULL ? 0 : 1;
429 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
430 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
431 /* We only use the default relative PYTHONPATH if we havent
432 anything better to use! */
433 skipdefault = envpath!=NULL || pythonhome!=NULL || \
434 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000435#endif
436
437 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000438 (1) the PYTHONPATH environment variable, if set;
439 (2) for Win32, the machinepath and userpath, if set;
440 (3) the PYTHONPATH config macro, with the leading "."
441 of each component replaced with pythonhome, if set;
442 (4) the directory containing the executable (argv0_path).
443 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000444 Extra rules:
445 - If PYTHONHOME is set (in any way) item (2) is ignored.
446 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000447 */
448
449 /* Calculate size of return buffer */
450 if (pythonhome != NULL) {
451 char *p;
452 bufsz = 1;
453 for (p = PYTHONPATH; *p; p++) {
454 if (*p == DELIM)
455 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000456 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000457 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000458 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000459 else
460 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000461 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000462 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000463#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000464 if (userpath)
465 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000466 if (machinepath)
467 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000468#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000469 if (envpath != NULL)
470 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000471
472 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000473 if (buf == NULL) {
474 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000475 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
476 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000477 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000478 module_search_path = envpath;
479 }
480 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000481 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000482 module_search_path = PYTHONPATH;
483 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000484#ifdef MS_WIN32
485 if (machinepath)
486 free(machinepath);
487 if (userpath)
488 free(userpath);
489#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000490 return;
491 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000492
493 if (envpath) {
494 strcpy(buf, envpath);
495 buf = strchr(buf, '\0');
496 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000497 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000498#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000499 if (userpath) {
500 strcpy(buf, userpath);
501 buf = strchr(buf, '\0');
502 *buf++ = DELIM;
503 free(userpath);
504 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000505 if (machinepath) {
506 strcpy(buf, machinepath);
507 buf = strchr(buf, '\0');
508 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000509 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000510 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000511 if (pythonhome == NULL) {
512 if (!skipdefault) {
513 strcpy(buf, PYTHONPATH);
514 buf = strchr(buf, '\0');
515 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000516 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000517#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000518 if (pythonhome == NULL) {
519 strcpy(buf, PYTHONPATH);
520 buf = strchr(buf, '\0');
521 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000522#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000523 else {
524 char *p = PYTHONPATH;
525 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000526 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000527 for (;;) {
528 q = strchr(p, DELIM);
529 if (q == NULL)
530 n = strlen(p);
531 else
532 n = q-p;
533 if (p[0] == '.' && is_sep(p[1])) {
534 strcpy(buf, pythonhome);
535 buf = strchr(buf, '\0');
536 p++;
537 n--;
538 }
539 strncpy(buf, p, n);
540 buf += n;
541 if (q == NULL)
542 break;
543 *buf++ = DELIM;
544 p = q+1;
545 }
546 }
547 if (argv0_path) {
548 *buf++ = DELIM;
549 strcpy(buf, argv0_path);
550 buf = strchr(buf, '\0');
551 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000552 *buf = '\0';
553}
554
555
556/* External interface */
557
558char *
Thomas Wouters78890102000-07-22 19:25:51 +0000559Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000560{
561 if (!module_search_path)
562 calculate_path();
563 return module_search_path;
564}
565
566char *
Thomas Wouters78890102000-07-22 19:25:51 +0000567Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000568{
Guido van Rossumeea14491997-08-13 21:30:44 +0000569 if (!module_search_path)
570 calculate_path();
571 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000572}
573
574char *
Thomas Wouters78890102000-07-22 19:25:51 +0000575Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000576{
Guido van Rossumeea14491997-08-13 21:30:44 +0000577 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000578}
579
580char *
Thomas Wouters78890102000-07-22 19:25:51 +0000581Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000582{
583 if (!module_search_path)
584 calculate_path();
585 return progpath;
586}