blob: 3ed912464711186ac2ed401781be709999c9d5c8 [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]) {
266 RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize);
267 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000268 }
269 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000270 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000271 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000272 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
273 if (dataBuf) {
274 TCHAR *szCur = dataBuf;
275 DWORD reqdSize = dataSize;
276 /* Copy our collected strings */
277 for (index=0;index<numKeys;index++) {
278 int len;
279 if (index > 0) {
280 *(szCur++) = _T(';');
281 dataSize--;
282 }
283 len = _tcslen(ppPaths[index]);
284 _tcsncpy(szCur, ppPaths[index], len);
285 szCur += len;
286 dataSize -= len;
287 }
288 if (skipcore)
289 *szCur = '\0';
290 else {
291 *(szCur++) = _T(';');
292 dataSize--;
293 /* Now append the core path entries - this will include the NULL */
294 rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize);
295 }
296 /* And set the result - caller must free
297 If MBCS, it is fine as is. If Unicode, allocate new
298 buffer and convert.
299 */
300#ifdef UNICODE
301 retval = (char *)malloc(reqdSize+1);
302 if (retval)
303 WideCharToMultiByte(CP_ACP, 0,
304 dataBuf, -1, /* source */
305 retval, dataSize+1, /* dest */
306 NULL, NULL);
307 free(dataBuf);
308#else
309 retval = dataBuf;
310#endif
311 }
312done:
313 /* Loop freeing my temp buffers */
314 if (ppPaths) {
315 for(index=0;index<numKeys;index++)
316 if (ppPaths[index]) free(ppPaths[index]);
317 free(ppPaths);
318 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000319 if (newKey)
320 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000321 if (keyBuf)
322 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000323 return retval;
324}
325#endif /* MS_WIN32 */
326
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000327static void
Thomas Wouters78890102000-07-22 19:25:51 +0000328get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000329{
Thomas Woutersa5345942000-07-22 23:59:33 +0000330 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000331 char *path = getenv("PATH");
332 char *prog = Py_GetProgramName();
333
Guido van Rossumeea14491997-08-13 21:30:44 +0000334#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000335#ifdef UNICODE
336 WCHAR wprogpath[MAXPATHLEN+1];
337 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
338 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
339 return;
340 }
341#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000342 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
343 return;
344#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000345#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000346 if (prog == NULL || *prog == '\0')
347 prog = "python";
348
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000349 /* If there is no slash in the argv0 path, then we have to
350 * assume python is on the user's $PATH, since there's no
351 * other way to find a directory to start the search from. If
352 * $PATH isn't exported, you lose.
353 */
354#ifdef ALTSEP
355 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
356#else
357 if (strchr(prog, SEP))
358#endif
359 strcpy(progpath, prog);
360 else if (path) {
361 while (1) {
362 char *delim = strchr(path, DELIM);
363
364 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000365 size_t len = delim - path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000366 strncpy(progpath, path, len);
367 *(progpath + len) = '\0';
368 }
369 else
370 strcpy(progpath, path);
371
372 join(progpath, prog);
373 if (exists(progpath))
374 break;
375
376 if (!delim) {
377 progpath[0] = '\0';
378 break;
379 }
380 path = delim + 1;
381 }
382 }
383 else
384 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000385}
386
387static void
Thomas Wouters78890102000-07-22 19:25:51 +0000388calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000389{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000390 char argv0_path[MAXPATHLEN+1];
391 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000392 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000393 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000394 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000395
Guido van Rossum43ff1141998-08-08 23:40:40 +0000396#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000397 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000398 char *machinepath = NULL;
399 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000400#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000401
402 get_progpath();
403 strcpy(argv0_path, progpath);
404 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000405 if (pythonhome == NULL || *pythonhome == '\0') {
406 if (search_for_prefix(argv0_path, LANDMARK))
407 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000408 else
409 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000410 }
411 else
412 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000413
Guido van Rossumeea14491997-08-13 21:30:44 +0000414 if (envpath && *envpath == '\0')
415 envpath = NULL;
416
Guido van Rossume02e48b2000-03-29 01:49:47 +0000417
Guido van Rossum43ff1141998-08-08 23:40:40 +0000418#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000419 skiphome = pythonhome==NULL ? 0 : 1;
420 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
421 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
422 /* We only use the default relative PYTHONPATH if we havent
423 anything better to use! */
424 skipdefault = envpath!=NULL || pythonhome!=NULL || \
425 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000426#endif
427
428 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000429 (1) the PYTHONPATH environment variable, if set;
430 (2) for Win32, the machinepath and userpath, if set;
431 (3) the PYTHONPATH config macro, with the leading "."
432 of each component replaced with pythonhome, if set;
433 (4) the directory containing the executable (argv0_path).
434 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000435 Extra rules:
436 - If PYTHONHOME is set (in any way) item (2) is ignored.
437 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000438 */
439
440 /* Calculate size of return buffer */
441 if (pythonhome != NULL) {
442 char *p;
443 bufsz = 1;
444 for (p = PYTHONPATH; *p; p++) {
445 if (*p == DELIM)
446 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000447 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000448 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000449 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000450 else
451 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000452 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000453 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000454#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000455 if (userpath)
456 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000457 if (machinepath)
458 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000459#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000460 if (envpath != NULL)
461 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000462
463 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000464 if (buf == NULL) {
465 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000466 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
467 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000468 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000469 module_search_path = envpath;
470 }
471 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000472 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000473 module_search_path = PYTHONPATH;
474 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000475#ifdef MS_WIN32
476 if (machinepath)
477 free(machinepath);
478 if (userpath)
479 free(userpath);
480#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000481 return;
482 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000483
484 if (envpath) {
485 strcpy(buf, envpath);
486 buf = strchr(buf, '\0');
487 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000489#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000490 if (userpath) {
491 strcpy(buf, userpath);
492 buf = strchr(buf, '\0');
493 *buf++ = DELIM;
494 free(userpath);
495 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000496 if (machinepath) {
497 strcpy(buf, machinepath);
498 buf = strchr(buf, '\0');
499 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000500 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000501 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000502 if (pythonhome == NULL) {
503 if (!skipdefault) {
504 strcpy(buf, PYTHONPATH);
505 buf = strchr(buf, '\0');
506 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000507 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000508#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000509 if (pythonhome == NULL) {
510 strcpy(buf, PYTHONPATH);
511 buf = strchr(buf, '\0');
512 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000513#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000514 else {
515 char *p = PYTHONPATH;
516 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000517 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000518 for (;;) {
519 q = strchr(p, DELIM);
520 if (q == NULL)
521 n = strlen(p);
522 else
523 n = q-p;
524 if (p[0] == '.' && is_sep(p[1])) {
525 strcpy(buf, pythonhome);
526 buf = strchr(buf, '\0');
527 p++;
528 n--;
529 }
530 strncpy(buf, p, n);
531 buf += n;
532 if (q == NULL)
533 break;
534 *buf++ = DELIM;
535 p = q+1;
536 }
537 }
538 if (argv0_path) {
539 *buf++ = DELIM;
540 strcpy(buf, argv0_path);
541 buf = strchr(buf, '\0');
542 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000543 *buf = '\0';
544}
545
546
547/* External interface */
548
549char *
Thomas Wouters78890102000-07-22 19:25:51 +0000550Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000551{
552 if (!module_search_path)
553 calculate_path();
554 return module_search_path;
555}
556
557char *
Thomas Wouters78890102000-07-22 19:25:51 +0000558Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000559{
Guido van Rossumeea14491997-08-13 21:30:44 +0000560 if (!module_search_path)
561 calculate_path();
562 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000563}
564
565char *
Thomas Wouters78890102000-07-22 19:25:51 +0000566Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000567{
Guido van Rossumeea14491997-08-13 21:30:44 +0000568 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000569}
570
571char *
Thomas Wouters78890102000-07-22 19:25:51 +0000572Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000573{
574 if (!module_search_path)
575 calculate_path();
576 return progpath;
577}