blob: 6695a70c219c3b47ebc10dfc9093752a7a003af5 [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
Mark Hammond19fdbfb2001-09-07 14:08:01 +000047 the registry is used. Other "application paths" in the registry are
Guido van Rossum88716bb2000-03-30 19:45:39 +000048 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
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000069/* Search in some common locations for the associated Python libraries.
70 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000071 * Py_GetPath() tries to return a sensible Python module search path.
72 *
Guido van Rossum42a97441998-02-19 21:00:45 +000073 * The approach is an adaptation for Windows of the strategy used in
74 * ../Modules/getpath.c; it uses the Windows Registry as one of its
75 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000076 */
77
78#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +000079#define LANDMARK "lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000080#endif
81
82static char prefix[MAXPATHLEN+1];
83static char progpath[MAXPATHLEN+1];
84static char *module_search_path = NULL;
85
Guido van Rossumeea14491997-08-13 21:30:44 +000086
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087static int
Thomas Wouters78890102000-07-22 19:25:51 +000088is_sep(char ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000089{
90#ifdef ALTSEP
91 return ch == SEP || ch == ALTSEP;
92#else
93 return ch == SEP;
94#endif
95}
96
Mark Hammond8bf9e3b2000-10-07 11:10:50 +000097/* assumes 'dir' null terminated in bounds. Never writes
98 beyond existing terminator.
99*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100static void
Thomas Wouters78890102000-07-22 19:25:51 +0000101reduce(char *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000103 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104 while (i > 0 && !is_sep(dir[i]))
105 --i;
106 dir[i] = '\0';
107}
108
109
110static int
Thomas Wouters78890102000-07-22 19:25:51 +0000111exists(char *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000112{
113 struct stat buf;
114 return stat(filename, &buf) == 0;
115}
116
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000117/* Assumes 'filename' MAXPATHLEN+1 bytes long -
118 may extend 'filename' by one character.
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
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000135/* guarantees buffer will never overflow MAXPATHLEN+1 bytes */
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
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000154/* gotlandmark only called by search_for_prefix, which ensures
155 'prefix' is null terminated in bounds. join() ensures
156 'landmark' can not overflow prefix if too long.
157*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158static int
Thomas Wouters78890102000-07-22 19:25:51 +0000159gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000160{
161 int n, ok;
162
163 n = strlen(prefix);
164 join(prefix, landmark);
165 ok = ismodule(prefix);
166 prefix[n] = '\0';
167 return ok;
168}
169
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000170/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
171 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000172static int
Thomas Wouters78890102000-07-22 19:25:51 +0000173search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000174{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000175 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000176 strcpy(prefix, argv0_path);
177 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000178 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000179 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000180 reduce(prefix);
181 } while (prefix[0]);
182 return 0;
183}
184
Guido van Rossumeea14491997-08-13 21:30:44 +0000185#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000186
Guido van Rossum88716bb2000-03-30 19:45:39 +0000187/* a string loaded from the DLL at startup.*/
188extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000189
Guido van Rossumeea14491997-08-13 21:30:44 +0000190
191/* Load a PYTHONPATH value from the registry.
192 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
193
Guido van Rossum88716bb2000-03-30 19:45:39 +0000194 Works in both Unicode and 8bit environments. Only uses the
195 Ex family of functions so it also works with Windows CE.
196
Guido van Rossumeea14491997-08-13 21:30:44 +0000197 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000198
199 XXX - this code is pretty strange, as it used to also
200 work on Win16, where the buffer sizes werent available
201 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000202*/
203
204static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000205getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000206{
207 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000208 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000209 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000210 LONG rc;
211 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000212 TCHAR *dataBuf = NULL;
213 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
214 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000215 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000216 DWORD index;
217 TCHAR *keyBuf = NULL;
218 TCHAR *keyBufPtr;
219 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000220
Guido van Rossum88716bb2000-03-30 19:45:39 +0000221 /* Tried to use sysget("winver") but here is too early :-( */
222 versionLen = _tcslen(PyWin_DLLVersionString);
223 /* Space for all the chars, plus one \0 */
224 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
225 sizeof(TCHAR)*(versionLen-1) +
226 sizeof(keySuffix));
227 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000228
Guido van Rossum88716bb2000-03-30 19:45:39 +0000229 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
230 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
231 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
232 keyBufPtr += versionLen;
233 /* NULL comes with this one! */
234 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
235 /* Open the root Python key */
236 rc=RegOpenKeyEx(keyBase,
237 keyBuf, /* subkey */
238 0, /* reserved */
239 KEY_READ,
240 &newKey);
241 if (rc!=ERROR_SUCCESS) goto done;
242 /* Find out how big our core buffer is, and how many subkeys we have */
243 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
244 NULL, NULL, &dataSize, NULL, NULL);
245 if (rc!=ERROR_SUCCESS) goto done;
246 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
247 /* Allocate a temp array of char buffers, so we only need to loop
248 reading the registry once
249 */
250 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
251 if (ppPaths==NULL) goto done;
252 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
253 /* Loop over all subkeys, allocating a temp sub-buffer. */
254 for(index=0;index<numKeys;index++) {
255 TCHAR keyBuf[MAX_PATH+1];
256 HKEY subKey = 0;
257 DWORD reqdSize = MAX_PATH+1;
258 /* Get the sub-key name */
259 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
260 NULL, NULL, NULL, NULL );
261 if (rc!=ERROR_SUCCESS) goto done;
262 /* Open the sub-key */
263 rc=RegOpenKeyEx(newKey,
264 keyBuf, /* subkey */
265 0, /* reserved */
266 KEY_READ,
267 &subKey);
268 if (rc!=ERROR_SUCCESS) goto done;
269 /* Find the value of the buffer size, malloc, then read it */
270 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
271 if (reqdSize) {
272 ppPaths[index] = malloc(reqdSize);
273 if (ppPaths[index]) {
Mark Hammonde61aca72000-09-10 09:14:53 +0000274 RegQueryValueEx(subKey, NULL, 0, NULL,
275 (LPBYTE)ppPaths[index],
276 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000277 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000278 }
279 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000280 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000281 }
Mark Hammond5edc6272001-02-23 11:38:38 +0000282 /* original datasize from RegQueryInfo doesn't include the \0 */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000283 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
284 if (dataBuf) {
285 TCHAR *szCur = dataBuf;
286 DWORD reqdSize = dataSize;
287 /* Copy our collected strings */
288 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000289 if (index > 0) {
290 *(szCur++) = _T(';');
291 dataSize--;
292 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000293 if (ppPaths[index]) {
294 int len = _tcslen(ppPaths[index]);
295 _tcsncpy(szCur, ppPaths[index], len);
296 szCur += len;
297 dataSize -= len;
298 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000299 }
300 if (skipcore)
301 *szCur = '\0';
302 else {
Mark Hammond5edc6272001-02-23 11:38:38 +0000303 /* If we have no values, we dont need a ';' */
304 if (numKeys) {
305 *(szCur++) = _T(';');
306 dataSize--;
307 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000308 /* Now append the core path entries -
309 this will include the NULL
310 */
311 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
312 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000313 }
314 /* And set the result - caller must free
315 If MBCS, it is fine as is. If Unicode, allocate new
316 buffer and convert.
317 */
318#ifdef UNICODE
319 retval = (char *)malloc(reqdSize+1);
320 if (retval)
321 WideCharToMultiByte(CP_ACP, 0,
322 dataBuf, -1, /* source */
323 retval, dataSize+1, /* dest */
324 NULL, NULL);
325 free(dataBuf);
326#else
327 retval = dataBuf;
328#endif
329 }
330done:
331 /* Loop freeing my temp buffers */
332 if (ppPaths) {
333 for(index=0;index<numKeys;index++)
334 if (ppPaths[index]) free(ppPaths[index]);
335 free(ppPaths);
336 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000337 if (newKey)
338 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000339 if (keyBuf)
340 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000341 return retval;
342}
343#endif /* MS_WIN32 */
344
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000345static void
Thomas Wouters78890102000-07-22 19:25:51 +0000346get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000347{
Thomas Woutersa5345942000-07-22 23:59:33 +0000348 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000349 char *path = getenv("PATH");
350 char *prog = Py_GetProgramName();
351
Guido van Rossumeea14491997-08-13 21:30:44 +0000352#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000353#ifdef UNICODE
354 WCHAR wprogpath[MAXPATHLEN+1];
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000355 /* Windows documents that GetModuleFileName() will "truncate",
356 but makes no mention of the null terminator. Play it safe.
357 PLUS Windows itself defines MAX_PATH as the same, but anyway...
358 */
359 wprogpath[MAXPATHLEN]=_T('\0')';
Guido van Rossum88716bb2000-03-30 19:45:39 +0000360 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000361 WideCharToMultiByte(CP_ACP, 0,
362 wprogpath, -1,
363 progpath, MAXPATHLEN+1,
364 NULL, NULL);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000365 return;
366 }
367#else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000368 /* static init of progpath ensures final char remains \0 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000369 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
370 return;
371#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000372#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000373 if (prog == NULL || *prog == '\0')
374 prog = "python";
375
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000376 /* If there is no slash in the argv0 path, then we have to
377 * assume python is on the user's $PATH, since there's no
378 * other way to find a directory to start the search from. If
379 * $PATH isn't exported, you lose.
380 */
381#ifdef ALTSEP
382 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
383#else
384 if (strchr(prog, SEP))
385#endif
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000386 strncpy(progpath, prog, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000387 else if (path) {
388 while (1) {
389 char *delim = strchr(path, DELIM);
390
391 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000392 size_t len = delim - path;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000393 /* ensure we can't overwrite buffer */
394 len = min(MAXPATHLEN,len);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000395 strncpy(progpath, path, len);
396 *(progpath + len) = '\0';
397 }
398 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000399 strncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000400
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000401 /* join() is safe for MAXPATHLEN+1 size buffer */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000402 join(progpath, prog);
403 if (exists(progpath))
404 break;
405
406 if (!delim) {
407 progpath[0] = '\0';
408 break;
409 }
410 path = delim + 1;
411 }
412 }
413 else
414 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000415}
416
417static void
Thomas Wouters78890102000-07-22 19:25:51 +0000418calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000419{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000420 char argv0_path[MAXPATHLEN+1];
421 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000422 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000423 char *pythonhome = Py_GetPythonHome();
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000424 char *envpath = Py_GETENV("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000425
Guido van Rossum43ff1141998-08-08 23:40:40 +0000426#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000427 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000428 char *machinepath = NULL;
429 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000430#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000431
432 get_progpath();
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000433 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000434 strcpy(argv0_path, progpath);
435 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000436 if (pythonhome == NULL || *pythonhome == '\0') {
437 if (search_for_prefix(argv0_path, LANDMARK))
438 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000439 else
440 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000441 }
442 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000443 strncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000444
Guido van Rossumeea14491997-08-13 21:30:44 +0000445 if (envpath && *envpath == '\0')
446 envpath = NULL;
447
Guido van Rossume02e48b2000-03-29 01:49:47 +0000448
Guido van Rossum43ff1141998-08-08 23:40:40 +0000449#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000450 skiphome = pythonhome==NULL ? 0 : 1;
451 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
452 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
453 /* We only use the default relative PYTHONPATH if we havent
454 anything better to use! */
455 skipdefault = envpath!=NULL || pythonhome!=NULL || \
456 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000457#endif
458
459 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000460 (1) the PYTHONPATH environment variable, if set;
461 (2) for Win32, the machinepath and userpath, if set;
462 (3) the PYTHONPATH config macro, with the leading "."
463 of each component replaced with pythonhome, if set;
464 (4) the directory containing the executable (argv0_path).
465 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000466 Extra rules:
467 - If PYTHONHOME is set (in any way) item (2) is ignored.
468 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000469 */
470
471 /* Calculate size of return buffer */
472 if (pythonhome != NULL) {
473 char *p;
474 bufsz = 1;
475 for (p = PYTHONPATH; *p; p++) {
476 if (*p == DELIM)
477 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000478 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000479 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000480 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000481 else
482 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000483 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000484 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000485#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000486 if (userpath)
487 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000488 if (machinepath)
489 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000490#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000491 if (envpath != NULL)
492 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000493
494 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000495 if (buf == NULL) {
496 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000497 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
498 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000499 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000500 module_search_path = envpath;
501 }
502 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000503 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000504 module_search_path = PYTHONPATH;
505 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000506#ifdef MS_WIN32
507 if (machinepath)
508 free(machinepath);
509 if (userpath)
510 free(userpath);
511#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000512 return;
513 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000514
515 if (envpath) {
516 strcpy(buf, envpath);
517 buf = strchr(buf, '\0');
518 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000519 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000520#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000521 if (userpath) {
522 strcpy(buf, userpath);
523 buf = strchr(buf, '\0');
524 *buf++ = DELIM;
525 free(userpath);
526 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000527 if (machinepath) {
528 strcpy(buf, machinepath);
529 buf = strchr(buf, '\0');
530 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000531 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000532 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000533 if (pythonhome == NULL) {
534 if (!skipdefault) {
535 strcpy(buf, PYTHONPATH);
536 buf = strchr(buf, '\0');
537 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000538 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000539#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000540 if (pythonhome == NULL) {
541 strcpy(buf, PYTHONPATH);
542 buf = strchr(buf, '\0');
543 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000544#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000545 else {
546 char *p = PYTHONPATH;
547 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000548 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000549 for (;;) {
550 q = strchr(p, DELIM);
551 if (q == NULL)
552 n = strlen(p);
553 else
554 n = q-p;
555 if (p[0] == '.' && is_sep(p[1])) {
556 strcpy(buf, pythonhome);
557 buf = strchr(buf, '\0');
558 p++;
559 n--;
560 }
561 strncpy(buf, p, n);
562 buf += n;
563 if (q == NULL)
564 break;
565 *buf++ = DELIM;
566 p = q+1;
567 }
568 }
569 if (argv0_path) {
570 *buf++ = DELIM;
571 strcpy(buf, argv0_path);
572 buf = strchr(buf, '\0');
573 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000574 *buf = '\0';
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000575 /* Now to pull one last hack/trick. If sys.prefix is
576 empty, then try and find it somewhere on the paths
577 we calculated. We scan backwards, as our general policy
578 is that Python core directories are at the *end* of
579 sys.path. We assume that our "lib" directory is
580 on the path, and that our 'prefix' directory is
581 the parent of that.
582 */
583 if (*prefix=='\0') {
584 char lookBuf[MAXPATHLEN+1];
585 char *look = buf - 1; /* 'buf' is at the end of the buffer */
586 while (1) {
587 int nchars;
588 char *lookEnd = look;
589 /* 'look' will end up one character before the
590 start of the path in question - even if this
591 is one character before the start of the buffer
592 */
593 while (*look != DELIM && look >= module_search_path)
594 look--;
595 nchars = lookEnd-look;
596 strncpy(lookBuf, look+1, nchars);
597 lookBuf[nchars] = '\0';
598 /* Up one level to the parent */
599 reduce(lookBuf);
600 if (search_for_prefix(lookBuf, LANDMARK)) {
601 break;
602 }
603 /* If we are out of paths to search - give up */
604 if (look < module_search_path)
605 break;
606 look--;
607 }
608 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000609}
610
611
612/* External interface */
613
614char *
Thomas Wouters78890102000-07-22 19:25:51 +0000615Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000616{
617 if (!module_search_path)
618 calculate_path();
619 return module_search_path;
620}
621
622char *
Thomas Wouters78890102000-07-22 19:25:51 +0000623Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000624{
Guido van Rossumeea14491997-08-13 21:30:44 +0000625 if (!module_search_path)
626 calculate_path();
627 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000628}
629
630char *
Thomas Wouters78890102000-07-22 19:25:51 +0000631Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000632{
Guido van Rossumeea14491997-08-13 21:30:44 +0000633 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000634}
635
636char *
Thomas Wouters78890102000-07-22 19:25:51 +0000637Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000638{
639 if (!module_search_path)
640 calculate_path();
641 return progpath;
642}