blob: f32c9d3159bfa7f42662fa1a957e35fb476e4042 [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
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000101/* assumes 'dir' null terminated in bounds. Never writes
102 beyond existing terminator.
103*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104static void
Thomas Wouters78890102000-07-22 19:25:51 +0000105reduce(char *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000107 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000108 while (i > 0 && !is_sep(dir[i]))
109 --i;
110 dir[i] = '\0';
111}
112
113
114static int
Thomas Wouters78890102000-07-22 19:25:51 +0000115exists(char *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000116{
117 struct stat buf;
118 return stat(filename, &buf) == 0;
119}
120
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000121/* Assumes 'filename' MAXPATHLEN+1 bytes long -
122 may extend 'filename' by one character.
123*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000124static int
Thomas Wouters78890102000-07-22 19:25:51 +0000125ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000126{
127 if (exists(filename))
128 return 1;
129
130 /* Check for the compiled version of prefix. */
131 if (strlen(filename) < MAXPATHLEN) {
132 strcat(filename, Py_OptimizeFlag ? "o" : "c");
133 if (exists(filename))
134 return 1;
135 }
136 return 0;
137}
138
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000139/* guarantees buffer will never overflow MAXPATHLEN+1 bytes */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140static void
Thomas Wouters78890102000-07-22 19:25:51 +0000141join(char *buffer, char *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000143 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000144 if (is_sep(stuff[0]))
145 n = 0;
146 else {
147 n = strlen(buffer);
148 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
149 buffer[n++] = SEP;
150 }
151 k = strlen(stuff);
152 if (n + k > MAXPATHLEN)
153 k = MAXPATHLEN - n;
154 strncpy(buffer+n, stuff, k);
155 buffer[n+k] = '\0';
156}
157
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000158/* gotlandmark only called by search_for_prefix, which ensures
159 'prefix' is null terminated in bounds. join() ensures
160 'landmark' can not overflow prefix if too long.
161*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000162static int
Thomas Wouters78890102000-07-22 19:25:51 +0000163gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000164{
165 int n, ok;
166
167 n = strlen(prefix);
168 join(prefix, landmark);
169 ok = ismodule(prefix);
170 prefix[n] = '\0';
171 return ok;
172}
173
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000174/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
175 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000176static int
Thomas Wouters78890102000-07-22 19:25:51 +0000177search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000178{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000179 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000180 strcpy(prefix, argv0_path);
181 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000182 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000183 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000184 reduce(prefix);
185 } while (prefix[0]);
186 return 0;
187}
188
Guido van Rossumeea14491997-08-13 21:30:44 +0000189#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000190
Guido van Rossum88716bb2000-03-30 19:45:39 +0000191/* a string loaded from the DLL at startup.*/
192extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000193
Guido van Rossumeea14491997-08-13 21:30:44 +0000194
195/* Load a PYTHONPATH value from the registry.
196 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
197
Guido van Rossum88716bb2000-03-30 19:45:39 +0000198 Works in both Unicode and 8bit environments. Only uses the
199 Ex family of functions so it also works with Windows CE.
200
Guido van Rossumeea14491997-08-13 21:30:44 +0000201 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000202
203 XXX - this code is pretty strange, as it used to also
204 work on Win16, where the buffer sizes werent available
205 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000206*/
207
208static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000209getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000210{
211 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000212 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000213 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000214 LONG rc;
215 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000216 TCHAR *dataBuf = NULL;
217 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
218 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000219 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000220 DWORD index;
221 TCHAR *keyBuf = NULL;
222 TCHAR *keyBufPtr;
223 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000224
Guido van Rossum88716bb2000-03-30 19:45:39 +0000225 /* Tried to use sysget("winver") but here is too early :-( */
226 versionLen = _tcslen(PyWin_DLLVersionString);
227 /* Space for all the chars, plus one \0 */
228 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
229 sizeof(TCHAR)*(versionLen-1) +
230 sizeof(keySuffix));
231 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000232
Guido van Rossum88716bb2000-03-30 19:45:39 +0000233 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
234 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
235 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
236 keyBufPtr += versionLen;
237 /* NULL comes with this one! */
238 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
239 /* Open the root Python key */
240 rc=RegOpenKeyEx(keyBase,
241 keyBuf, /* subkey */
242 0, /* reserved */
243 KEY_READ,
244 &newKey);
245 if (rc!=ERROR_SUCCESS) goto done;
246 /* Find out how big our core buffer is, and how many subkeys we have */
247 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
248 NULL, NULL, &dataSize, NULL, NULL);
249 if (rc!=ERROR_SUCCESS) goto done;
250 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
251 /* Allocate a temp array of char buffers, so we only need to loop
252 reading the registry once
253 */
254 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
255 if (ppPaths==NULL) goto done;
256 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
257 /* Loop over all subkeys, allocating a temp sub-buffer. */
258 for(index=0;index<numKeys;index++) {
259 TCHAR keyBuf[MAX_PATH+1];
260 HKEY subKey = 0;
261 DWORD reqdSize = MAX_PATH+1;
262 /* Get the sub-key name */
263 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
264 NULL, NULL, NULL, NULL );
265 if (rc!=ERROR_SUCCESS) goto done;
266 /* Open the sub-key */
267 rc=RegOpenKeyEx(newKey,
268 keyBuf, /* subkey */
269 0, /* reserved */
270 KEY_READ,
271 &subKey);
272 if (rc!=ERROR_SUCCESS) goto done;
273 /* Find the value of the buffer size, malloc, then read it */
274 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
275 if (reqdSize) {
276 ppPaths[index] = malloc(reqdSize);
277 if (ppPaths[index]) {
Mark Hammonde61aca72000-09-10 09:14:53 +0000278 RegQueryValueEx(subKey, NULL, 0, NULL,
279 (LPBYTE)ppPaths[index],
280 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000281 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000282 }
283 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000284 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000285 }
Mark Hammond5edc6272001-02-23 11:38:38 +0000286 /* original datasize from RegQueryInfo doesn't include the \0 */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000287 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
288 if (dataBuf) {
289 TCHAR *szCur = dataBuf;
290 DWORD reqdSize = dataSize;
291 /* Copy our collected strings */
292 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000293 if (index > 0) {
294 *(szCur++) = _T(';');
295 dataSize--;
296 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000297 if (ppPaths[index]) {
298 int len = _tcslen(ppPaths[index]);
299 _tcsncpy(szCur, ppPaths[index], len);
300 szCur += len;
301 dataSize -= len;
302 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000303 }
304 if (skipcore)
305 *szCur = '\0';
306 else {
Mark Hammond5edc6272001-02-23 11:38:38 +0000307 /* If we have no values, we dont need a ';' */
308 if (numKeys) {
309 *(szCur++) = _T(';');
310 dataSize--;
311 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000312 /* Now append the core path entries -
313 this will include the NULL
314 */
315 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
316 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000317 }
318 /* And set the result - caller must free
319 If MBCS, it is fine as is. If Unicode, allocate new
320 buffer and convert.
321 */
322#ifdef UNICODE
323 retval = (char *)malloc(reqdSize+1);
324 if (retval)
325 WideCharToMultiByte(CP_ACP, 0,
326 dataBuf, -1, /* source */
327 retval, dataSize+1, /* dest */
328 NULL, NULL);
329 free(dataBuf);
330#else
331 retval = dataBuf;
332#endif
333 }
334done:
335 /* Loop freeing my temp buffers */
336 if (ppPaths) {
337 for(index=0;index<numKeys;index++)
338 if (ppPaths[index]) free(ppPaths[index]);
339 free(ppPaths);
340 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000341 if (newKey)
342 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000343 if (keyBuf)
344 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000345 return retval;
346}
347#endif /* MS_WIN32 */
348
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000349static void
Thomas Wouters78890102000-07-22 19:25:51 +0000350get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000351{
Thomas Woutersa5345942000-07-22 23:59:33 +0000352 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000353 char *path = getenv("PATH");
354 char *prog = Py_GetProgramName();
355
Guido van Rossumeea14491997-08-13 21:30:44 +0000356#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000357#ifdef UNICODE
358 WCHAR wprogpath[MAXPATHLEN+1];
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000359 /* Windows documents that GetModuleFileName() will "truncate",
360 but makes no mention of the null terminator. Play it safe.
361 PLUS Windows itself defines MAX_PATH as the same, but anyway...
362 */
363 wprogpath[MAXPATHLEN]=_T('\0')';
Guido van Rossum88716bb2000-03-30 19:45:39 +0000364 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000365 WideCharToMultiByte(CP_ACP, 0,
366 wprogpath, -1,
367 progpath, MAXPATHLEN+1,
368 NULL, NULL);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000369 return;
370 }
371#else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000372 /* static init of progpath ensures final char remains \0 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000373 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
374 return;
375#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000376#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000377 if (prog == NULL || *prog == '\0')
378 prog = "python";
379
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000380 /* If there is no slash in the argv0 path, then we have to
381 * assume python is on the user's $PATH, since there's no
382 * other way to find a directory to start the search from. If
383 * $PATH isn't exported, you lose.
384 */
385#ifdef ALTSEP
386 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
387#else
388 if (strchr(prog, SEP))
389#endif
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000390 strncpy(progpath, prog, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000391 else if (path) {
392 while (1) {
393 char *delim = strchr(path, DELIM);
394
395 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000396 size_t len = delim - path;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000397 /* ensure we can't overwrite buffer */
398 len = min(MAXPATHLEN,len);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000399 strncpy(progpath, path, len);
400 *(progpath + len) = '\0';
401 }
402 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000403 strncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000404
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000405 /* join() is safe for MAXPATHLEN+1 size buffer */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000406 join(progpath, prog);
407 if (exists(progpath))
408 break;
409
410 if (!delim) {
411 progpath[0] = '\0';
412 break;
413 }
414 path = delim + 1;
415 }
416 }
417 else
418 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000419}
420
421static void
Thomas Wouters78890102000-07-22 19:25:51 +0000422calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000423{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000424 char argv0_path[MAXPATHLEN+1];
425 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000426 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000427 char *pythonhome = Py_GetPythonHome();
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000428 char *envpath = Py_GETENV("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000429
Guido van Rossum43ff1141998-08-08 23:40:40 +0000430#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000431 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000432 char *machinepath = NULL;
433 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000434#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000435
436 get_progpath();
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000437 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000438 strcpy(argv0_path, progpath);
439 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000440 if (pythonhome == NULL || *pythonhome == '\0') {
441 if (search_for_prefix(argv0_path, LANDMARK))
442 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000443 else
444 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000445 }
446 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000447 strncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000448
Guido van Rossumeea14491997-08-13 21:30:44 +0000449 if (envpath && *envpath == '\0')
450 envpath = NULL;
451
Guido van Rossume02e48b2000-03-29 01:49:47 +0000452
Guido van Rossum43ff1141998-08-08 23:40:40 +0000453#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000454 skiphome = pythonhome==NULL ? 0 : 1;
455 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
456 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
457 /* We only use the default relative PYTHONPATH if we havent
458 anything better to use! */
459 skipdefault = envpath!=NULL || pythonhome!=NULL || \
460 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000461#endif
462
463 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000464 (1) the PYTHONPATH environment variable, if set;
465 (2) for Win32, the machinepath and userpath, if set;
466 (3) the PYTHONPATH config macro, with the leading "."
467 of each component replaced with pythonhome, if set;
468 (4) the directory containing the executable (argv0_path).
469 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000470 Extra rules:
471 - If PYTHONHOME is set (in any way) item (2) is ignored.
472 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000473 */
474
475 /* Calculate size of return buffer */
476 if (pythonhome != NULL) {
477 char *p;
478 bufsz = 1;
479 for (p = PYTHONPATH; *p; p++) {
480 if (*p == DELIM)
481 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000482 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000483 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000484 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000485 else
486 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000487 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000488 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000489#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000490 if (userpath)
491 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000492 if (machinepath)
493 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000494#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000495 if (envpath != NULL)
496 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000497
498 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000499 if (buf == NULL) {
500 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000501 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
502 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000503 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000504 module_search_path = envpath;
505 }
506 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000507 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000508 module_search_path = PYTHONPATH;
509 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000510#ifdef MS_WIN32
511 if (machinepath)
512 free(machinepath);
513 if (userpath)
514 free(userpath);
515#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000516 return;
517 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000518
519 if (envpath) {
520 strcpy(buf, envpath);
521 buf = strchr(buf, '\0');
522 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000523 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000524#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000525 if (userpath) {
526 strcpy(buf, userpath);
527 buf = strchr(buf, '\0');
528 *buf++ = DELIM;
529 free(userpath);
530 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000531 if (machinepath) {
532 strcpy(buf, machinepath);
533 buf = strchr(buf, '\0');
534 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000535 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000536 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000537 if (pythonhome == NULL) {
538 if (!skipdefault) {
539 strcpy(buf, PYTHONPATH);
540 buf = strchr(buf, '\0');
541 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000542 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000543#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000544 if (pythonhome == NULL) {
545 strcpy(buf, PYTHONPATH);
546 buf = strchr(buf, '\0');
547 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000548#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000549 else {
550 char *p = PYTHONPATH;
551 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000552 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000553 for (;;) {
554 q = strchr(p, DELIM);
555 if (q == NULL)
556 n = strlen(p);
557 else
558 n = q-p;
559 if (p[0] == '.' && is_sep(p[1])) {
560 strcpy(buf, pythonhome);
561 buf = strchr(buf, '\0');
562 p++;
563 n--;
564 }
565 strncpy(buf, p, n);
566 buf += n;
567 if (q == NULL)
568 break;
569 *buf++ = DELIM;
570 p = q+1;
571 }
572 }
573 if (argv0_path) {
574 *buf++ = DELIM;
575 strcpy(buf, argv0_path);
576 buf = strchr(buf, '\0');
577 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000578 *buf = '\0';
579}
580
581
582/* External interface */
583
584char *
Thomas Wouters78890102000-07-22 19:25:51 +0000585Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000586{
587 if (!module_search_path)
588 calculate_path();
589 return module_search_path;
590}
591
592char *
Thomas Wouters78890102000-07-22 19:25:51 +0000593Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000594{
Guido van Rossumeea14491997-08-13 21:30:44 +0000595 if (!module_search_path)
596 calculate_path();
597 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000598}
599
600char *
Thomas Wouters78890102000-07-22 19:25:51 +0000601Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000602{
Guido van Rossumeea14491997-08-13 21:30:44 +0000603 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000604}
605
606char *
Thomas Wouters78890102000-07-22 19:25:51 +0000607Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000608{
609 if (!module_search_path)
610 calculate_path();
611 return progpath;
612}