blob: 37a9c0b9900ba689fe8766f7ab6478c783510774 [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
Georg Brandl7eb4b7d2005-07-22 21:49:32 +000014 * If the PYTHONPATH env. var. exists, its entries are added next.
Guido van Rossum88716bb2000-03-30 19:45:39 +000015
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
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000060#ifdef MS_WINDOWS
Guido van Rossum8f1b6511997-08-13 19:55:43 +000061#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];
Just van Rossum52e14d62002-12-30 22:08:05 +000084static char dllpath[MAXPATHLEN+1];
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085static char *module_search_path = NULL;
86
Guido van Rossumeea14491997-08-13 21:30:44 +000087
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000088static int
Thomas Wouters78890102000-07-22 19:25:51 +000089is_sep(char ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000090{
91#ifdef ALTSEP
92 return ch == SEP || ch == ALTSEP;
93#else
94 return ch == SEP;
95#endif
96}
97
Mark Hammond8bf9e3b2000-10-07 11:10:50 +000098/* assumes 'dir' null terminated in bounds. Never writes
99 beyond existing terminator.
100*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101static void
Thomas Wouters78890102000-07-22 19:25:51 +0000102reduce(char *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000104 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 while (i > 0 && !is_sep(dir[i]))
106 --i;
107 dir[i] = '\0';
108}
109
110
111static int
Thomas Wouters78890102000-07-22 19:25:51 +0000112exists(char *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113{
114 struct stat buf;
115 return stat(filename, &buf) == 0;
116}
117
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000118/* Assumes 'filename' MAXPATHLEN+1 bytes long -
119 may extend 'filename' by one character.
120*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000121static int
Thomas Wouters78890102000-07-22 19:25:51 +0000122ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000123{
124 if (exists(filename))
125 return 1;
126
127 /* Check for the compiled version of prefix. */
128 if (strlen(filename) < MAXPATHLEN) {
129 strcat(filename, Py_OptimizeFlag ? "o" : "c");
130 if (exists(filename))
131 return 1;
132 }
133 return 0;
134}
135
Tim Peters8484fbf2004-08-07 19:12:27 +0000136/* Add a path component, by appending stuff to buffer.
137 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
138 NUL-terminated string with no more than MAXPATHLEN characters (not counting
139 the trailing NUL). It's a fatal error if it contains a string longer than
140 that (callers must be careful!). If these requirements are met, it's
141 guaranteed that buffer will still be a NUL-terminated string with no more
142 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
143 stuff as fits will be appended.
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 }
Tim Peters8484fbf2004-08-07 19:12:27 +0000156 if (n > MAXPATHLEN)
157 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158 k = strlen(stuff);
159 if (n + k > MAXPATHLEN)
160 k = MAXPATHLEN - n;
161 strncpy(buffer+n, stuff, k);
162 buffer[n+k] = '\0';
163}
164
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000165/* gotlandmark only called by search_for_prefix, which ensures
166 'prefix' is null terminated in bounds. join() ensures
167 'landmark' can not overflow prefix if too long.
168*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000169static int
Thomas Wouters78890102000-07-22 19:25:51 +0000170gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000171{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000172 int ok;
173 Py_ssize_t n;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000174
175 n = strlen(prefix);
176 join(prefix, landmark);
177 ok = ismodule(prefix);
178 prefix[n] = '\0';
179 return ok;
180}
181
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000182/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
183 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000184static int
Thomas Wouters78890102000-07-22 19:25:51 +0000185search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000186{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000187 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000188 strcpy(prefix, argv0_path);
189 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000190 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000191 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000192 reduce(prefix);
193 } while (prefix[0]);
194 return 0;
195}
196
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000197#ifdef MS_WINDOWS
Guido van Rossum43ff1141998-08-08 23:40:40 +0000198
Guido van Rossum88716bb2000-03-30 19:45:39 +0000199/* a string loaded from the DLL at startup.*/
200extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000201
Guido van Rossumeea14491997-08-13 21:30:44 +0000202
203/* Load a PYTHONPATH value from the registry.
204 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
205
Guido van Rossum88716bb2000-03-30 19:45:39 +0000206 Works in both Unicode and 8bit environments. Only uses the
207 Ex family of functions so it also works with Windows CE.
208
Guido van Rossumeea14491997-08-13 21:30:44 +0000209 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000210
211 XXX - this code is pretty strange, as it used to also
212 work on Win16, where the buffer sizes werent available
213 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000214*/
215
216static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000217getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000218{
219 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000220 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000221 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000222 LONG rc;
223 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000224 TCHAR *dataBuf = NULL;
225 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
226 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000227 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000228 DWORD index;
229 TCHAR *keyBuf = NULL;
230 TCHAR *keyBufPtr;
231 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000232
Guido van Rossum88716bb2000-03-30 19:45:39 +0000233 /* Tried to use sysget("winver") but here is too early :-( */
234 versionLen = _tcslen(PyWin_DLLVersionString);
235 /* Space for all the chars, plus one \0 */
236 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
237 sizeof(TCHAR)*(versionLen-1) +
238 sizeof(keySuffix));
239 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000240
Guido van Rossum88716bb2000-03-30 19:45:39 +0000241 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
242 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
243 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
244 keyBufPtr += versionLen;
245 /* NULL comes with this one! */
246 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
247 /* Open the root Python key */
248 rc=RegOpenKeyEx(keyBase,
249 keyBuf, /* subkey */
250 0, /* reserved */
251 KEY_READ,
252 &newKey);
253 if (rc!=ERROR_SUCCESS) goto done;
254 /* Find out how big our core buffer is, and how many subkeys we have */
255 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
256 NULL, NULL, &dataSize, NULL, NULL);
257 if (rc!=ERROR_SUCCESS) goto done;
258 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
259 /* Allocate a temp array of char buffers, so we only need to loop
260 reading the registry once
261 */
262 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
263 if (ppPaths==NULL) goto done;
264 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
265 /* Loop over all subkeys, allocating a temp sub-buffer. */
266 for(index=0;index<numKeys;index++) {
267 TCHAR keyBuf[MAX_PATH+1];
268 HKEY subKey = 0;
269 DWORD reqdSize = MAX_PATH+1;
270 /* Get the sub-key name */
271 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
272 NULL, NULL, NULL, NULL );
273 if (rc!=ERROR_SUCCESS) goto done;
274 /* Open the sub-key */
275 rc=RegOpenKeyEx(newKey,
276 keyBuf, /* subkey */
277 0, /* reserved */
278 KEY_READ,
279 &subKey);
280 if (rc!=ERROR_SUCCESS) goto done;
281 /* Find the value of the buffer size, malloc, then read it */
282 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
283 if (reqdSize) {
284 ppPaths[index] = malloc(reqdSize);
285 if (ppPaths[index]) {
Mark Hammonde61aca72000-09-10 09:14:53 +0000286 RegQueryValueEx(subKey, NULL, 0, NULL,
287 (LPBYTE)ppPaths[index],
288 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000289 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000290 }
291 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000292 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000293 }
Mark Hammond5edc6272001-02-23 11:38:38 +0000294 /* original datasize from RegQueryInfo doesn't include the \0 */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000295 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
296 if (dataBuf) {
297 TCHAR *szCur = dataBuf;
298 DWORD reqdSize = dataSize;
299 /* Copy our collected strings */
300 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000301 if (index > 0) {
302 *(szCur++) = _T(';');
303 dataSize--;
304 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000305 if (ppPaths[index]) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000306 Py_ssize_t len = _tcslen(ppPaths[index]);
Mark Hammonde61aca72000-09-10 09:14:53 +0000307 _tcsncpy(szCur, ppPaths[index], len);
308 szCur += len;
Tim Petersc7f6cf62006-02-16 00:35:06 +0000309 assert(dataSize > (DWORD)len);
310 dataSize -= (DWORD)len;
Mark Hammonde61aca72000-09-10 09:14:53 +0000311 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000312 }
313 if (skipcore)
314 *szCur = '\0';
315 else {
Mark Hammond5edc6272001-02-23 11:38:38 +0000316 /* If we have no values, we dont need a ';' */
317 if (numKeys) {
318 *(szCur++) = _T(';');
319 dataSize--;
320 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000321 /* Now append the core path entries -
322 this will include the NULL
323 */
324 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
325 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000326 }
327 /* And set the result - caller must free
328 If MBCS, it is fine as is. If Unicode, allocate new
329 buffer and convert.
330 */
331#ifdef UNICODE
332 retval = (char *)malloc(reqdSize+1);
333 if (retval)
334 WideCharToMultiByte(CP_ACP, 0,
335 dataBuf, -1, /* source */
Mark Hammond61bb35f2003-01-29 22:38:29 +0000336 retval, reqdSize+1, /* dest */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000337 NULL, NULL);
338 free(dataBuf);
339#else
340 retval = dataBuf;
341#endif
342 }
343done:
344 /* Loop freeing my temp buffers */
345 if (ppPaths) {
346 for(index=0;index<numKeys;index++)
347 if (ppPaths[index]) free(ppPaths[index]);
348 free(ppPaths);
349 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000350 if (newKey)
351 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000352 if (keyBuf)
353 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000354 return retval;
355}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000356#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000357
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000358static void
Thomas Wouters78890102000-07-22 19:25:51 +0000359get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000360{
Thomas Woutersa5345942000-07-22 23:59:33 +0000361 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000362 char *path = getenv("PATH");
363 char *prog = Py_GetProgramName();
364
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000365#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000366 extern HANDLE PyWin_DLLhModule;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000367#ifdef UNICODE
368 WCHAR wprogpath[MAXPATHLEN+1];
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000369 /* Windows documents that GetModuleFileName() will "truncate",
370 but makes no mention of the null terminator. Play it safe.
371 PLUS Windows itself defines MAX_PATH as the same, but anyway...
372 */
Mark Hammond2795dae2002-07-22 13:28:21 +0000373 wprogpath[MAXPATHLEN]=_T('\0');
Just van Rossum52e14d62002-12-30 22:08:05 +0000374 if (PyWin_DLLhModule &&
375 GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) {
376 WideCharToMultiByte(CP_ACP, 0,
377 wprogpath, -1,
378 dllpath, MAXPATHLEN+1,
379 NULL, NULL);
380 }
Thomas Heller6019f9a2003-08-18 17:53:33 +0000381 wprogpath[MAXPATHLEN]=_T('\0');
Guido van Rossum88716bb2000-03-30 19:45:39 +0000382 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000383 WideCharToMultiByte(CP_ACP, 0,
384 wprogpath, -1,
385 progpath, MAXPATHLEN+1,
386 NULL, NULL);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000387 return;
388 }
389#else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000390 /* static init of progpath ensures final char remains \0 */
Just van Rossum52e14d62002-12-30 22:08:05 +0000391 if (PyWin_DLLhModule)
392 if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN))
393 dllpath[0] = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000394 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
395 return;
396#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000397#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000398 if (prog == NULL || *prog == '\0')
399 prog = "python";
400
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000401 /* If there is no slash in the argv0 path, then we have to
402 * assume python is on the user's $PATH, since there's no
403 * other way to find a directory to start the search from. If
404 * $PATH isn't exported, you lose.
405 */
406#ifdef ALTSEP
407 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
408#else
409 if (strchr(prog, SEP))
410#endif
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000411 strncpy(progpath, prog, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000412 else if (path) {
413 while (1) {
414 char *delim = strchr(path, DELIM);
415
416 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000417 size_t len = delim - path;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000418 /* ensure we can't overwrite buffer */
419 len = min(MAXPATHLEN,len);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000420 strncpy(progpath, path, len);
421 *(progpath + len) = '\0';
422 }
423 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000424 strncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000425
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000426 /* join() is safe for MAXPATHLEN+1 size buffer */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000427 join(progpath, prog);
428 if (exists(progpath))
429 break;
430
431 if (!delim) {
432 progpath[0] = '\0';
433 break;
434 }
435 path = delim + 1;
436 }
437 }
438 else
439 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000440}
441
442static void
Thomas Wouters78890102000-07-22 19:25:51 +0000443calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000444{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000445 char argv0_path[MAXPATHLEN+1];
446 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000447 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000448 char *pythonhome = Py_GetPythonHome();
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000449 char *envpath = Py_GETENV("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000450
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000451#ifdef MS_WINDOWS
Guido van Rossum88716bb2000-03-30 19:45:39 +0000452 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000453 char *machinepath = NULL;
454 char *userpath = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000455 char zip_path[MAXPATHLEN+1];
456 size_t len;
Guido van Rossumeea14491997-08-13 21:30:44 +0000457#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000458
459 get_progpath();
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000460 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000461 strcpy(argv0_path, progpath);
462 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000463 if (pythonhome == NULL || *pythonhome == '\0') {
464 if (search_for_prefix(argv0_path, LANDMARK))
465 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000466 else
467 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000468 }
469 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000470 strncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000471
Guido van Rossumeea14491997-08-13 21:30:44 +0000472 if (envpath && *envpath == '\0')
473 envpath = NULL;
474
Guido van Rossume02e48b2000-03-29 01:49:47 +0000475
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000476#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000477 /* Calculate zip archive path */
478 if (dllpath[0]) /* use name of python DLL */
479 strncpy(zip_path, dllpath, MAXPATHLEN);
480 else /* use name of executable program */
481 strncpy(zip_path, progpath, MAXPATHLEN);
Neal Norwitza8aed022002-12-31 12:35:41 +0000482 zip_path[MAXPATHLEN] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000483 len = strlen(zip_path);
484 if (len > 4) {
485 zip_path[len-3] = 'z'; /* change ending to "zip" */
486 zip_path[len-2] = 'i';
487 zip_path[len-1] = 'p';
488 }
489 else {
490 zip_path[0] = 0;
491 }
492
Guido van Rossum88716bb2000-03-30 19:45:39 +0000493 skiphome = pythonhome==NULL ? 0 : 1;
494 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
495 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
496 /* We only use the default relative PYTHONPATH if we havent
497 anything better to use! */
498 skipdefault = envpath!=NULL || pythonhome!=NULL || \
499 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000500#endif
501
502 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000503 (1) the PYTHONPATH environment variable, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000504 (2) for Win32, the zip archive file path;
505 (3) for Win32, the machinepath and userpath, if set;
506 (4) the PYTHONPATH config macro, with the leading "."
Guido van Rossumeea14491997-08-13 21:30:44 +0000507 of each component replaced with pythonhome, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000508 (5) the directory containing the executable (argv0_path).
509 The length calculation calculates #4 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000510 Extra rules:
Just van Rossum52e14d62002-12-30 22:08:05 +0000511 - If PYTHONHOME is set (in any way) item (3) is ignored.
512 - If registry values are used, (4) and (5) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000513 */
514
515 /* Calculate size of return buffer */
516 if (pythonhome != NULL) {
517 char *p;
518 bufsz = 1;
519 for (p = PYTHONPATH; *p; p++) {
520 if (*p == DELIM)
521 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000522 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000523 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000524 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000525 else
526 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000527 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000528 bufsz += strlen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000529#ifdef MS_WINDOWS
Guido van Rossumeea14491997-08-13 21:30:44 +0000530 if (userpath)
531 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000532 if (machinepath)
533 bufsz += strlen(machinepath) + 1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000534 bufsz += strlen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000535#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000536 if (envpath != NULL)
537 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000538
539 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000540 if (buf == NULL) {
541 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000542 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
543 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000544 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000545 module_search_path = envpath;
546 }
547 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000548 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000549 module_search_path = PYTHONPATH;
550 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000551#ifdef MS_WINDOWS
Guido van Rossum42a97441998-02-19 21:00:45 +0000552 if (machinepath)
553 free(machinepath);
554 if (userpath)
555 free(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000556#endif /* MS_WINDOWS */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000557 return;
558 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000559
560 if (envpath) {
561 strcpy(buf, envpath);
562 buf = strchr(buf, '\0');
563 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000564 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000565#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000566 if (zip_path[0]) {
567 strcpy(buf, zip_path);
568 buf = strchr(buf, '\0');
569 *buf++ = DELIM;
570 }
Guido van Rossum67ab6721998-08-08 19:58:59 +0000571 if (userpath) {
572 strcpy(buf, userpath);
573 buf = strchr(buf, '\0');
574 *buf++ = DELIM;
575 free(userpath);
576 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000577 if (machinepath) {
578 strcpy(buf, machinepath);
579 buf = strchr(buf, '\0');
580 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000581 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000582 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000583 if (pythonhome == NULL) {
584 if (!skipdefault) {
585 strcpy(buf, PYTHONPATH);
586 buf = strchr(buf, '\0');
587 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000588 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000589#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000590 if (pythonhome == NULL) {
591 strcpy(buf, PYTHONPATH);
592 buf = strchr(buf, '\0');
593 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000594#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000595 else {
596 char *p = PYTHONPATH;
597 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000598 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000599 for (;;) {
600 q = strchr(p, DELIM);
601 if (q == NULL)
602 n = strlen(p);
603 else
604 n = q-p;
605 if (p[0] == '.' && is_sep(p[1])) {
606 strcpy(buf, pythonhome);
607 buf = strchr(buf, '\0');
608 p++;
609 n--;
610 }
611 strncpy(buf, p, n);
612 buf += n;
613 if (q == NULL)
614 break;
615 *buf++ = DELIM;
616 p = q+1;
617 }
618 }
619 if (argv0_path) {
620 *buf++ = DELIM;
621 strcpy(buf, argv0_path);
622 buf = strchr(buf, '\0');
623 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000624 *buf = '\0';
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000625 /* Now to pull one last hack/trick. If sys.prefix is
626 empty, then try and find it somewhere on the paths
627 we calculated. We scan backwards, as our general policy
628 is that Python core directories are at the *end* of
629 sys.path. We assume that our "lib" directory is
630 on the path, and that our 'prefix' directory is
631 the parent of that.
632 */
633 if (*prefix=='\0') {
634 char lookBuf[MAXPATHLEN+1];
635 char *look = buf - 1; /* 'buf' is at the end of the buffer */
636 while (1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000637 Py_ssize_t nchars;
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000638 char *lookEnd = look;
639 /* 'look' will end up one character before the
640 start of the path in question - even if this
641 is one character before the start of the buffer
642 */
643 while (*look != DELIM && look >= module_search_path)
644 look--;
645 nchars = lookEnd-look;
646 strncpy(lookBuf, look+1, nchars);
647 lookBuf[nchars] = '\0';
648 /* Up one level to the parent */
649 reduce(lookBuf);
650 if (search_for_prefix(lookBuf, LANDMARK)) {
651 break;
652 }
653 /* If we are out of paths to search - give up */
654 if (look < module_search_path)
655 break;
656 look--;
657 }
658 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000659}
660
661
662/* External interface */
663
664char *
Thomas Wouters78890102000-07-22 19:25:51 +0000665Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000666{
667 if (!module_search_path)
668 calculate_path();
669 return module_search_path;
670}
671
672char *
Thomas Wouters78890102000-07-22 19:25:51 +0000673Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000674{
Guido van Rossumeea14491997-08-13 21:30:44 +0000675 if (!module_search_path)
676 calculate_path();
677 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000678}
679
680char *
Thomas Wouters78890102000-07-22 19:25:51 +0000681Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000682{
Guido van Rossumeea14491997-08-13 21:30:44 +0000683 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000684}
685
686char *
Thomas Wouters78890102000-07-22 19:25:51 +0000687Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000688{
689 if (!module_search_path)
690 calculate_path();
691 return progpath;
692}