blob: d61d3ea5caf4d333594f694ec98b592e58df792c [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
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
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000136/* guarantees buffer will never overflow MAXPATHLEN+1 bytes */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000137static void
Thomas Wouters78890102000-07-22 19:25:51 +0000138join(char *buffer, char *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000140 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000141 if (is_sep(stuff[0]))
142 n = 0;
143 else {
144 n = strlen(buffer);
145 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
146 buffer[n++] = SEP;
147 }
148 k = strlen(stuff);
149 if (n + k > MAXPATHLEN)
150 k = MAXPATHLEN - n;
151 strncpy(buffer+n, stuff, k);
152 buffer[n+k] = '\0';
153}
154
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000155/* gotlandmark only called by search_for_prefix, which ensures
156 'prefix' is null terminated in bounds. join() ensures
157 'landmark' can not overflow prefix if too long.
158*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000159static int
Thomas Wouters78890102000-07-22 19:25:51 +0000160gotlandmark(char *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000161{
162 int n, ok;
163
164 n = strlen(prefix);
165 join(prefix, landmark);
166 ok = ismodule(prefix);
167 prefix[n] = '\0';
168 return ok;
169}
170
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000171/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
172 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000173static int
Thomas Wouters78890102000-07-22 19:25:51 +0000174search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000175{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000176 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000177 strcpy(prefix, argv0_path);
178 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000179 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000180 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000181 reduce(prefix);
182 } while (prefix[0]);
183 return 0;
184}
185
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000186#ifdef MS_WINDOWS
Guido van Rossum43ff1141998-08-08 23:40:40 +0000187
Guido van Rossum88716bb2000-03-30 19:45:39 +0000188/* a string loaded from the DLL at startup.*/
189extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000190
Guido van Rossumeea14491997-08-13 21:30:44 +0000191
192/* Load a PYTHONPATH value from the registry.
193 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
194
Guido van Rossum88716bb2000-03-30 19:45:39 +0000195 Works in both Unicode and 8bit environments. Only uses the
196 Ex family of functions so it also works with Windows CE.
197
Guido van Rossumeea14491997-08-13 21:30:44 +0000198 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000199
200 XXX - this code is pretty strange, as it used to also
201 work on Win16, where the buffer sizes werent available
202 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000203*/
204
205static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000206getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000207{
208 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000209 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000210 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000211 LONG rc;
212 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000213 TCHAR *dataBuf = NULL;
214 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
215 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000216 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000217 DWORD index;
218 TCHAR *keyBuf = NULL;
219 TCHAR *keyBufPtr;
220 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000221
Guido van Rossum88716bb2000-03-30 19:45:39 +0000222 /* Tried to use sysget("winver") but here is too early :-( */
223 versionLen = _tcslen(PyWin_DLLVersionString);
224 /* Space for all the chars, plus one \0 */
225 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
226 sizeof(TCHAR)*(versionLen-1) +
227 sizeof(keySuffix));
228 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000229
Guido van Rossum88716bb2000-03-30 19:45:39 +0000230 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
231 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
232 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
233 keyBufPtr += versionLen;
234 /* NULL comes with this one! */
235 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
236 /* Open the root Python key */
237 rc=RegOpenKeyEx(keyBase,
238 keyBuf, /* subkey */
239 0, /* reserved */
240 KEY_READ,
241 &newKey);
242 if (rc!=ERROR_SUCCESS) goto done;
243 /* Find out how big our core buffer is, and how many subkeys we have */
244 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
245 NULL, NULL, &dataSize, NULL, NULL);
246 if (rc!=ERROR_SUCCESS) goto done;
247 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
248 /* Allocate a temp array of char buffers, so we only need to loop
249 reading the registry once
250 */
251 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
252 if (ppPaths==NULL) goto done;
253 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
254 /* Loop over all subkeys, allocating a temp sub-buffer. */
255 for(index=0;index<numKeys;index++) {
256 TCHAR keyBuf[MAX_PATH+1];
257 HKEY subKey = 0;
258 DWORD reqdSize = MAX_PATH+1;
259 /* Get the sub-key name */
260 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
261 NULL, NULL, NULL, NULL );
262 if (rc!=ERROR_SUCCESS) goto done;
263 /* Open the sub-key */
264 rc=RegOpenKeyEx(newKey,
265 keyBuf, /* subkey */
266 0, /* reserved */
267 KEY_READ,
268 &subKey);
269 if (rc!=ERROR_SUCCESS) goto done;
270 /* Find the value of the buffer size, malloc, then read it */
271 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
272 if (reqdSize) {
273 ppPaths[index] = malloc(reqdSize);
274 if (ppPaths[index]) {
Mark Hammonde61aca72000-09-10 09:14:53 +0000275 RegQueryValueEx(subKey, NULL, 0, NULL,
276 (LPBYTE)ppPaths[index],
277 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000278 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000279 }
280 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000281 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000282 }
Mark Hammond5edc6272001-02-23 11:38:38 +0000283 /* original datasize from RegQueryInfo doesn't include the \0 */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000284 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
285 if (dataBuf) {
286 TCHAR *szCur = dataBuf;
287 DWORD reqdSize = dataSize;
288 /* Copy our collected strings */
289 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000290 if (index > 0) {
291 *(szCur++) = _T(';');
292 dataSize--;
293 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000294 if (ppPaths[index]) {
295 int len = _tcslen(ppPaths[index]);
296 _tcsncpy(szCur, ppPaths[index], len);
297 szCur += len;
298 dataSize -= len;
299 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000300 }
301 if (skipcore)
302 *szCur = '\0';
303 else {
Mark Hammond5edc6272001-02-23 11:38:38 +0000304 /* If we have no values, we dont need a ';' */
305 if (numKeys) {
306 *(szCur++) = _T(';');
307 dataSize--;
308 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000309 /* Now append the core path entries -
310 this will include the NULL
311 */
312 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
313 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000314 }
315 /* And set the result - caller must free
316 If MBCS, it is fine as is. If Unicode, allocate new
317 buffer and convert.
318 */
319#ifdef UNICODE
320 retval = (char *)malloc(reqdSize+1);
321 if (retval)
322 WideCharToMultiByte(CP_ACP, 0,
323 dataBuf, -1, /* source */
Mark Hammond61bb35f2003-01-29 22:38:29 +0000324 retval, reqdSize+1, /* dest */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000325 NULL, NULL);
326 free(dataBuf);
327#else
328 retval = dataBuf;
329#endif
330 }
331done:
332 /* Loop freeing my temp buffers */
333 if (ppPaths) {
334 for(index=0;index<numKeys;index++)
335 if (ppPaths[index]) free(ppPaths[index]);
336 free(ppPaths);
337 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000338 if (newKey)
339 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000340 if (keyBuf)
341 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000342 return retval;
343}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000344#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000345
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000346static void
Thomas Wouters78890102000-07-22 19:25:51 +0000347get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000348{
Thomas Woutersa5345942000-07-22 23:59:33 +0000349 extern char *Py_GetProgramName(void);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000350 char *path = getenv("PATH");
351 char *prog = Py_GetProgramName();
352
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000353#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000354 extern HANDLE PyWin_DLLhModule;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000355#ifdef UNICODE
356 WCHAR wprogpath[MAXPATHLEN+1];
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000357 /* Windows documents that GetModuleFileName() will "truncate",
358 but makes no mention of the null terminator. Play it safe.
359 PLUS Windows itself defines MAX_PATH as the same, but anyway...
360 */
Mark Hammond2795dae2002-07-22 13:28:21 +0000361 wprogpath[MAXPATHLEN]=_T('\0');
Just van Rossum52e14d62002-12-30 22:08:05 +0000362 if (PyWin_DLLhModule &&
363 GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) {
364 WideCharToMultiByte(CP_ACP, 0,
365 wprogpath, -1,
366 dllpath, MAXPATHLEN+1,
367 NULL, NULL);
368 }
369 wprogpath[MAXPATHLEN]=_T('\0')';
Guido van Rossum88716bb2000-03-30 19:45:39 +0000370 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000371 WideCharToMultiByte(CP_ACP, 0,
372 wprogpath, -1,
373 progpath, MAXPATHLEN+1,
374 NULL, NULL);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000375 return;
376 }
377#else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000378 /* static init of progpath ensures final char remains \0 */
Just van Rossum52e14d62002-12-30 22:08:05 +0000379 if (PyWin_DLLhModule)
380 if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN))
381 dllpath[0] = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000382 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
383 return;
384#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000385#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000386 if (prog == NULL || *prog == '\0')
387 prog = "python";
388
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000389 /* If there is no slash in the argv0 path, then we have to
390 * assume python is on the user's $PATH, since there's no
391 * other way to find a directory to start the search from. If
392 * $PATH isn't exported, you lose.
393 */
394#ifdef ALTSEP
395 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
396#else
397 if (strchr(prog, SEP))
398#endif
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000399 strncpy(progpath, prog, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000400 else if (path) {
401 while (1) {
402 char *delim = strchr(path, DELIM);
403
404 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000405 size_t len = delim - path;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000406 /* ensure we can't overwrite buffer */
407 len = min(MAXPATHLEN,len);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000408 strncpy(progpath, path, len);
409 *(progpath + len) = '\0';
410 }
411 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000412 strncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000413
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000414 /* join() is safe for MAXPATHLEN+1 size buffer */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000415 join(progpath, prog);
416 if (exists(progpath))
417 break;
418
419 if (!delim) {
420 progpath[0] = '\0';
421 break;
422 }
423 path = delim + 1;
424 }
425 }
426 else
427 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000428}
429
430static void
Thomas Wouters78890102000-07-22 19:25:51 +0000431calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000432{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000433 char argv0_path[MAXPATHLEN+1];
434 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000435 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000436 char *pythonhome = Py_GetPythonHome();
Neil Schemenauer7d4bb9f2001-07-23 16:30:27 +0000437 char *envpath = Py_GETENV("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000438
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000439#ifdef MS_WINDOWS
Guido van Rossum88716bb2000-03-30 19:45:39 +0000440 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000441 char *machinepath = NULL;
442 char *userpath = NULL;
Just van Rossum52e14d62002-12-30 22:08:05 +0000443 char zip_path[MAXPATHLEN+1];
444 size_t len;
Guido van Rossumeea14491997-08-13 21:30:44 +0000445#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000446
447 get_progpath();
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000448 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000449 strcpy(argv0_path, progpath);
450 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000451 if (pythonhome == NULL || *pythonhome == '\0') {
452 if (search_for_prefix(argv0_path, LANDMARK))
453 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000454 else
455 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000456 }
457 else
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000458 strncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000459
Guido van Rossumeea14491997-08-13 21:30:44 +0000460 if (envpath && *envpath == '\0')
461 envpath = NULL;
462
Guido van Rossume02e48b2000-03-29 01:49:47 +0000463
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000464#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000465 /* Calculate zip archive path */
466 if (dllpath[0]) /* use name of python DLL */
467 strncpy(zip_path, dllpath, MAXPATHLEN);
468 else /* use name of executable program */
469 strncpy(zip_path, progpath, MAXPATHLEN);
Neal Norwitza8aed022002-12-31 12:35:41 +0000470 zip_path[MAXPATHLEN] = '\0';
Just van Rossum52e14d62002-12-30 22:08:05 +0000471 len = strlen(zip_path);
472 if (len > 4) {
473 zip_path[len-3] = 'z'; /* change ending to "zip" */
474 zip_path[len-2] = 'i';
475 zip_path[len-1] = 'p';
476 }
477 else {
478 zip_path[0] = 0;
479 }
480
Guido van Rossum88716bb2000-03-30 19:45:39 +0000481 skiphome = pythonhome==NULL ? 0 : 1;
482 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
483 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
484 /* We only use the default relative PYTHONPATH if we havent
485 anything better to use! */
486 skipdefault = envpath!=NULL || pythonhome!=NULL || \
487 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000488#endif
489
490 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000491 (1) the PYTHONPATH environment variable, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000492 (2) for Win32, the zip archive file path;
493 (3) for Win32, the machinepath and userpath, if set;
494 (4) the PYTHONPATH config macro, with the leading "."
Guido van Rossumeea14491997-08-13 21:30:44 +0000495 of each component replaced with pythonhome, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000496 (5) the directory containing the executable (argv0_path).
497 The length calculation calculates #4 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000498 Extra rules:
Just van Rossum52e14d62002-12-30 22:08:05 +0000499 - If PYTHONHOME is set (in any way) item (3) is ignored.
500 - If registry values are used, (4) and (5) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000501 */
502
503 /* Calculate size of return buffer */
504 if (pythonhome != NULL) {
505 char *p;
506 bufsz = 1;
507 for (p = PYTHONPATH; *p; p++) {
508 if (*p == DELIM)
509 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000510 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000511 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000512 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000513 else
514 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000515 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000516 bufsz += strlen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000517#ifdef MS_WINDOWS
Guido van Rossumeea14491997-08-13 21:30:44 +0000518 if (userpath)
519 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000520 if (machinepath)
521 bufsz += strlen(machinepath) + 1;
Just van Rossum52e14d62002-12-30 22:08:05 +0000522 bufsz += strlen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000523#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000524 if (envpath != NULL)
525 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000526
527 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000528 if (buf == NULL) {
529 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000530 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
531 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000532 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000533 module_search_path = envpath;
534 }
535 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000536 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000537 module_search_path = PYTHONPATH;
538 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000539#ifdef MS_WINDOWS
Guido van Rossum42a97441998-02-19 21:00:45 +0000540 if (machinepath)
541 free(machinepath);
542 if (userpath)
543 free(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000544#endif /* MS_WINDOWS */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000545 return;
546 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000547
548 if (envpath) {
549 strcpy(buf, envpath);
550 buf = strchr(buf, '\0');
551 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000552 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000553#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000554 if (zip_path[0]) {
555 strcpy(buf, zip_path);
556 buf = strchr(buf, '\0');
557 *buf++ = DELIM;
558 }
Guido van Rossum67ab6721998-08-08 19:58:59 +0000559 if (userpath) {
560 strcpy(buf, userpath);
561 buf = strchr(buf, '\0');
562 *buf++ = DELIM;
563 free(userpath);
564 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000565 if (machinepath) {
566 strcpy(buf, machinepath);
567 buf = strchr(buf, '\0');
568 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000569 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000570 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000571 if (pythonhome == NULL) {
572 if (!skipdefault) {
573 strcpy(buf, PYTHONPATH);
574 buf = strchr(buf, '\0');
575 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000576 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000577#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000578 if (pythonhome == NULL) {
579 strcpy(buf, PYTHONPATH);
580 buf = strchr(buf, '\0');
581 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000582#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000583 else {
584 char *p = PYTHONPATH;
585 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000586 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000587 for (;;) {
588 q = strchr(p, DELIM);
589 if (q == NULL)
590 n = strlen(p);
591 else
592 n = q-p;
593 if (p[0] == '.' && is_sep(p[1])) {
594 strcpy(buf, pythonhome);
595 buf = strchr(buf, '\0');
596 p++;
597 n--;
598 }
599 strncpy(buf, p, n);
600 buf += n;
601 if (q == NULL)
602 break;
603 *buf++ = DELIM;
604 p = q+1;
605 }
606 }
607 if (argv0_path) {
608 *buf++ = DELIM;
609 strcpy(buf, argv0_path);
610 buf = strchr(buf, '\0');
611 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000612 *buf = '\0';
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000613 /* Now to pull one last hack/trick. If sys.prefix is
614 empty, then try and find it somewhere on the paths
615 we calculated. We scan backwards, as our general policy
616 is that Python core directories are at the *end* of
617 sys.path. We assume that our "lib" directory is
618 on the path, and that our 'prefix' directory is
619 the parent of that.
620 */
621 if (*prefix=='\0') {
622 char lookBuf[MAXPATHLEN+1];
623 char *look = buf - 1; /* 'buf' is at the end of the buffer */
624 while (1) {
625 int nchars;
626 char *lookEnd = look;
627 /* 'look' will end up one character before the
628 start of the path in question - even if this
629 is one character before the start of the buffer
630 */
631 while (*look != DELIM && look >= module_search_path)
632 look--;
633 nchars = lookEnd-look;
634 strncpy(lookBuf, look+1, nchars);
635 lookBuf[nchars] = '\0';
636 /* Up one level to the parent */
637 reduce(lookBuf);
638 if (search_for_prefix(lookBuf, LANDMARK)) {
639 break;
640 }
641 /* If we are out of paths to search - give up */
642 if (look < module_search_path)
643 break;
644 look--;
645 }
646 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000647}
648
649
650/* External interface */
651
652char *
Thomas Wouters78890102000-07-22 19:25:51 +0000653Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000654{
655 if (!module_search_path)
656 calculate_path();
657 return module_search_path;
658}
659
660char *
Thomas Wouters78890102000-07-22 19:25:51 +0000661Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000662{
Guido van Rossumeea14491997-08-13 21:30:44 +0000663 if (!module_search_path)
664 calculate_path();
665 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000666}
667
668char *
Thomas Wouters78890102000-07-22 19:25:51 +0000669Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000670{
Guido van Rossumeea14491997-08-13 21:30:44 +0000671 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000672}
673
674char *
Thomas Wouters78890102000-07-22 19:25:51 +0000675Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000676{
677 if (!module_search_path)
678 calculate_path();
679 return progpath;
680}