blob: 669ed9f0b375df9c61ff0b2cf2904ba5625126a5 [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,
Georg Brandl6e47a332008-05-17 19:15:58 +000028 plat-win, etc) are based on the Python Home
Guido van Rossum88716bb2000-03-30 19:45:39 +000029 - 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"
Martin v. Löwis790465f2008-04-05 20:41:37 +000059#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000060
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000061#ifdef MS_WINDOWS
Guido van Rossum8f1b6511997-08-13 19:55:43 +000062#include <windows.h>
63#endif
64
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000066#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#endif /* HAVE_SYS_TYPES_H */
68
69#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000070#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#endif /* HAVE_SYS_STAT_H */
72
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000073#include <string.h>
74
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000075/* Search in some common locations for the associated Python libraries.
76 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000077 * Py_GetPath() tries to return a sensible Python module search path.
78 *
Guido van Rossum42a97441998-02-19 21:00:45 +000079 * The approach is an adaptation for Windows of the strategy used in
80 * ../Modules/getpath.c; it uses the Windows Registry as one of its
81 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000082 */
83
84#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +000085#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000086#endif
87
Martin v. Löwis790465f2008-04-05 20:41:37 +000088static wchar_t prefix[MAXPATHLEN+1];
89static wchar_t progpath[MAXPATHLEN+1];
90static wchar_t dllpath[MAXPATHLEN+1];
91static wchar_t *module_search_path = NULL;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000092
Guido van Rossumeea14491997-08-13 21:30:44 +000093
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000094static int
Martin v. Löwis790465f2008-04-05 20:41:37 +000095is_sep(wchar_t ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000096{
97#ifdef ALTSEP
98 return ch == SEP || ch == ALTSEP;
99#else
100 return ch == SEP;
101#endif
102}
103
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000104/* assumes 'dir' null terminated in bounds. Never writes
105 beyond existing terminator.
106*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000108reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000109{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000110 size_t i = wcslen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000111 while (i > 0 && !is_sep(dir[i]))
112 --i;
113 dir[i] = '\0';
114}
115
116
117static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000118exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119{
Amaury Forgeot d'Arc3d17a5c2008-06-13 01:09:34 +0000120 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000121}
122
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000123/* Assumes 'filename' MAXPATHLEN+1 bytes long -
124 may extend 'filename' by one character.
125*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000126static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000127ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000128{
129 if (exists(filename))
130 return 1;
131
132 /* Check for the compiled version of prefix. */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000133 if (wcslen(filename) < MAXPATHLEN) {
134 wcscat(filename, Py_OptimizeFlag ? L"o" : L"c");
Guido van Rossum43ff1141998-08-08 23:40:40 +0000135 if (exists(filename))
136 return 1;
137 }
138 return 0;
139}
140
Tim Peters8484fbf2004-08-07 19:12:27 +0000141/* Add a path component, by appending stuff to buffer.
142 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
143 NUL-terminated string with no more than MAXPATHLEN characters (not counting
144 the trailing NUL). It's a fatal error if it contains a string longer than
145 that (callers must be careful!). If these requirements are met, it's
146 guaranteed that buffer will still be a NUL-terminated string with no more
147 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
148 stuff as fits will be appended.
149*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000151join(wchar_t *buffer, wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000152{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000153 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000154 if (is_sep(stuff[0]))
155 n = 0;
156 else {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000157 n = wcslen(buffer);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
159 buffer[n++] = SEP;
160 }
Tim Peters8484fbf2004-08-07 19:12:27 +0000161 if (n > MAXPATHLEN)
162 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
Martin v. Löwis790465f2008-04-05 20:41:37 +0000163 k = wcslen(stuff);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000164 if (n + k > MAXPATHLEN)
165 k = MAXPATHLEN - n;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000166 wcsncpy(buffer+n, stuff, k);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000167 buffer[n+k] = '\0';
168}
169
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000170/* gotlandmark only called by search_for_prefix, which ensures
171 'prefix' is null terminated in bounds. join() ensures
172 'landmark' can not overflow prefix if too long.
173*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000174static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000175gotlandmark(wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000176{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000177 int ok;
178 Py_ssize_t n;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000179
Martin v. Löwis790465f2008-04-05 20:41:37 +0000180 n = wcslen(prefix);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000181 join(prefix, landmark);
182 ok = ismodule(prefix);
183 prefix[n] = '\0';
184 return ok;
185}
186
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000187/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
188 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000189static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000190search_for_prefix(wchar_t *argv0_path, wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000191{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000192 /* Search from argv0_path, until landmark is found */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000193 wcscpy(prefix, argv0_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000194 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000195 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000196 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000197 reduce(prefix);
198 } while (prefix[0]);
199 return 0;
200}
201
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000202#ifdef MS_WINDOWS
Guido van Rossum43ff1141998-08-08 23:40:40 +0000203
Guido van Rossum88716bb2000-03-30 19:45:39 +0000204/* a string loaded from the DLL at startup.*/
205extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000206
Guido van Rossumeea14491997-08-13 21:30:44 +0000207
208/* Load a PYTHONPATH value from the registry.
209 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
210
Guido van Rossum88716bb2000-03-30 19:45:39 +0000211 Works in both Unicode and 8bit environments. Only uses the
212 Ex family of functions so it also works with Windows CE.
213
Guido van Rossumeea14491997-08-13 21:30:44 +0000214 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000215
216 XXX - this code is pretty strange, as it used to also
217 work on Win16, where the buffer sizes werent available
218 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000219*/
220
Martin v. Löwis790465f2008-04-05 20:41:37 +0000221static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000222getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000223{
224 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000225 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000226 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000227 LONG rc;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000228 wchar_t *retval = NULL;
229 WCHAR *dataBuf = NULL;
230 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
231 static const WCHAR keySuffix[] = L"\\PythonPath";
Guido van Rossum1c44e282000-06-28 22:20:06 +0000232 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000233 DWORD index;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000234 WCHAR *keyBuf = NULL;
235 WCHAR *keyBufPtr;
236 WCHAR **ppPaths = NULL;
237
Guido van Rossum88716bb2000-03-30 19:45:39 +0000238 /* Tried to use sysget("winver") but here is too early :-( */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000239 versionLen = strlen(PyWin_DLLVersionString);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000240 /* Space for all the chars, plus one \0 */
241 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
Martin v. Löwis790465f2008-04-05 20:41:37 +0000242 sizeof(WCHAR)*(versionLen-1) +
Guido van Rossum88716bb2000-03-30 19:45:39 +0000243 sizeof(keySuffix));
244 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000245
Martin v. Löwis790465f2008-04-05 20:41:37 +0000246 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
247 keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1;
248 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000249 keyBufPtr += versionLen;
250 /* NULL comes with this one! */
251 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
252 /* Open the root Python key */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000253 rc=RegOpenKeyExW(keyBase,
Guido van Rossum88716bb2000-03-30 19:45:39 +0000254 keyBuf, /* subkey */
255 0, /* reserved */
256 KEY_READ,
257 &newKey);
258 if (rc!=ERROR_SUCCESS) goto done;
259 /* Find out how big our core buffer is, and how many subkeys we have */
260 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
261 NULL, NULL, &dataSize, NULL, NULL);
262 if (rc!=ERROR_SUCCESS) goto done;
263 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
264 /* Allocate a temp array of char buffers, so we only need to loop
265 reading the registry once
266 */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000267 ppPaths = malloc( sizeof(WCHAR *) * numKeys );
Guido van Rossum88716bb2000-03-30 19:45:39 +0000268 if (ppPaths==NULL) goto done;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000269 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000270 /* Loop over all subkeys, allocating a temp sub-buffer. */
271 for(index=0;index<numKeys;index++) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000272 WCHAR keyBuf[MAX_PATH+1];
Guido van Rossum88716bb2000-03-30 19:45:39 +0000273 HKEY subKey = 0;
274 DWORD reqdSize = MAX_PATH+1;
275 /* Get the sub-key name */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000276 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
277 NULL, NULL, NULL, NULL );
Guido van Rossum88716bb2000-03-30 19:45:39 +0000278 if (rc!=ERROR_SUCCESS) goto done;
279 /* Open the sub-key */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000280 rc=RegOpenKeyExW(newKey,
Guido van Rossum88716bb2000-03-30 19:45:39 +0000281 keyBuf, /* subkey */
282 0, /* reserved */
283 KEY_READ,
284 &subKey);
285 if (rc!=ERROR_SUCCESS) goto done;
286 /* Find the value of the buffer size, malloc, then read it */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000287 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000288 if (reqdSize) {
289 ppPaths[index] = malloc(reqdSize);
290 if (ppPaths[index]) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000291 RegQueryValueExW(subKey, NULL, 0, NULL,
Mark Hammonde61aca72000-09-10 09:14:53 +0000292 (LPBYTE)ppPaths[index],
293 &reqdSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000294 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000295 }
296 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000297 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000298 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299
300 /* return null if no path to return */
301 if (dataSize == 0) goto done;
302
Mark Hammond5edc6272001-02-23 11:38:38 +0000303 /* original datasize from RegQueryInfo doesn't include the \0 */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000304 dataBuf = malloc((dataSize+1) * sizeof(WCHAR));
Guido van Rossum88716bb2000-03-30 19:45:39 +0000305 if (dataBuf) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000306 WCHAR *szCur = dataBuf;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000307 DWORD reqdSize = dataSize;
308 /* Copy our collected strings */
309 for (index=0;index<numKeys;index++) {
Guido van Rossum88716bb2000-03-30 19:45:39 +0000310 if (index > 0) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000311 *(szCur++) = L';';
Guido van Rossum88716bb2000-03-30 19:45:39 +0000312 dataSize--;
313 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000314 if (ppPaths[index]) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000315 Py_ssize_t len = wcslen(ppPaths[index]);
316 wcsncpy(szCur, ppPaths[index], len);
Mark Hammonde61aca72000-09-10 09:14:53 +0000317 szCur += len;
Tim Petersc7f6cf62006-02-16 00:35:06 +0000318 assert(dataSize > (DWORD)len);
319 dataSize -= (DWORD)len;
Mark Hammonde61aca72000-09-10 09:14:53 +0000320 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000321 }
322 if (skipcore)
323 *szCur = '\0';
324 else {
Mark Hammond5edc6272001-02-23 11:38:38 +0000325 /* If we have no values, we dont need a ';' */
326 if (numKeys) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000327 *(szCur++) = L';';
Mark Hammond5edc6272001-02-23 11:38:38 +0000328 dataSize--;
329 }
Mark Hammonde61aca72000-09-10 09:14:53 +0000330 /* Now append the core path entries -
331 this will include the NULL
332 */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000333 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
334 (LPBYTE)szCur, &dataSize);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000335 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000336 /* And set the result - caller must free */
Guido van Rossum88716bb2000-03-30 19:45:39 +0000337 retval = dataBuf;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000338 }
339done:
340 /* Loop freeing my temp buffers */
341 if (ppPaths) {
342 for(index=0;index<numKeys;index++)
343 if (ppPaths[index]) free(ppPaths[index]);
344 free(ppPaths);
345 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000346 if (newKey)
347 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000348 if (keyBuf)
349 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000350 return retval;
351}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000352#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000353
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000354static void
Thomas Wouters78890102000-07-22 19:25:51 +0000355get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000356{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000357 extern wchar_t *Py_GetProgramName(void);
358 wchar_t *path = _wgetenv(L"PATH");
359 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000360
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000361#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000362 extern HANDLE PyWin_DLLhModule;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000363 /* static init of progpath ensures final char remains \0 */
Just van Rossum52e14d62002-12-30 22:08:05 +0000364 if (PyWin_DLLhModule)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000365 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
Just van Rossum52e14d62002-12-30 22:08:05 +0000366 dllpath[0] = 0;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000367 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
Guido van Rossumeea14491997-08-13 21:30:44 +0000368 return;
369#endif
370 if (prog == NULL || *prog == '\0')
Martin v. Löwis790465f2008-04-05 20:41:37 +0000371 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000372
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000373 /* If there is no slash in the argv0 path, then we have to
374 * assume python is on the user's $PATH, since there's no
375 * other way to find a directory to start the search from. If
376 * $PATH isn't exported, you lose.
377 */
378#ifdef ALTSEP
Martin v. Löwis790465f2008-04-05 20:41:37 +0000379 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000380#else
Martin v. Löwis790465f2008-04-05 20:41:37 +0000381 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000382#endif
Martin v. Löwis790465f2008-04-05 20:41:37 +0000383 wcsncpy(progpath, prog, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000384 else if (path) {
385 while (1) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000386 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000387
388 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000389 size_t len = delim - path;
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000390 /* ensure we can't overwrite buffer */
391 len = min(MAXPATHLEN,len);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000392 wcsncpy(progpath, path, len);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000393 *(progpath + len) = '\0';
394 }
395 else
Martin v. Löwis790465f2008-04-05 20:41:37 +0000396 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000397
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000398 /* join() is safe for MAXPATHLEN+1 size buffer */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000399 join(progpath, prog);
400 if (exists(progpath))
401 break;
402
403 if (!delim) {
404 progpath[0] = '\0';
405 break;
406 }
407 path = delim + 1;
408 }
409 }
410 else
411 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000412}
413
414static void
Thomas Wouters78890102000-07-22 19:25:51 +0000415calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000416{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000417 wchar_t argv0_path[MAXPATHLEN+1];
418 wchar_t *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000419 size_t bufsz;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000420 wchar_t *pythonhome = Py_GetPythonHome();
421 char *_envpath = Py_GETENV("PYTHONPATH");
422 wchar_t wenvpath[MAXPATHLEN+1];
423 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000424
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000425#ifdef MS_WINDOWS
Guido van Rossum88716bb2000-03-30 19:45:39 +0000426 int skiphome, skipdefault;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000427 wchar_t *machinepath = NULL;
428 wchar_t *userpath = NULL;
429 wchar_t zip_path[MAXPATHLEN+1];
Just van Rossum52e14d62002-12-30 22:08:05 +0000430 size_t len;
Guido van Rossumeea14491997-08-13 21:30:44 +0000431#endif
Martin v. Löwis790465f2008-04-05 20:41:37 +0000432 if (_envpath) {
433 size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
434 envpath = wenvpath;
435 if (r == (size_t)-1 || r >= MAXPATHLEN)
436 envpath = NULL;
437 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000438
439 get_progpath();
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000440 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000441 wcscpy(argv0_path, progpath);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000442 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000443 if (pythonhome == NULL || *pythonhome == '\0') {
444 if (search_for_prefix(argv0_path, LANDMARK))
445 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000446 else
447 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000448 }
449 else
Martin v. Löwis790465f2008-04-05 20:41:37 +0000450 wcsncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000451
Guido van Rossumeea14491997-08-13 21:30:44 +0000452 if (envpath && *envpath == '\0')
453 envpath = NULL;
454
Guido van Rossume02e48b2000-03-29 01:49:47 +0000455
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000456#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000457 /* Calculate zip archive path */
458 if (dllpath[0]) /* use name of python DLL */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000459 wcsncpy(zip_path, dllpath, MAXPATHLEN);
Just van Rossum52e14d62002-12-30 22:08:05 +0000460 else /* use name of executable program */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000461 wcsncpy(zip_path, progpath, MAXPATHLEN);
Neal Norwitza8aed022002-12-31 12:35:41 +0000462 zip_path[MAXPATHLEN] = '\0';
Martin v. Löwis790465f2008-04-05 20:41:37 +0000463 len = wcslen(zip_path);
Just van Rossum52e14d62002-12-30 22:08:05 +0000464 if (len > 4) {
465 zip_path[len-3] = 'z'; /* change ending to "zip" */
466 zip_path[len-2] = 'i';
467 zip_path[len-1] = 'p';
468 }
469 else {
470 zip_path[0] = 0;
471 }
472
Guido van Rossum88716bb2000-03-30 19:45:39 +0000473 skiphome = pythonhome==NULL ? 0 : 1;
474 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
475 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
476 /* We only use the default relative PYTHONPATH if we havent
477 anything better to use! */
478 skipdefault = envpath!=NULL || pythonhome!=NULL || \
479 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000480#endif
481
482 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000483 (1) the PYTHONPATH environment variable, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000484 (2) for Win32, the zip archive file path;
485 (3) for Win32, the machinepath and userpath, if set;
486 (4) the PYTHONPATH config macro, with the leading "."
Guido van Rossumeea14491997-08-13 21:30:44 +0000487 of each component replaced with pythonhome, if set;
Just van Rossum52e14d62002-12-30 22:08:05 +0000488 (5) the directory containing the executable (argv0_path).
489 The length calculation calculates #4 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000490 Extra rules:
Just van Rossum52e14d62002-12-30 22:08:05 +0000491 - If PYTHONHOME is set (in any way) item (3) is ignored.
492 - If registry values are used, (4) and (5) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000493 */
494
495 /* Calculate size of return buffer */
496 if (pythonhome != NULL) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000497 wchar_t *p;
Guido van Rossumeea14491997-08-13 21:30:44 +0000498 bufsz = 1;
499 for (p = PYTHONPATH; *p; p++) {
500 if (*p == DELIM)
501 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000502 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000503 bufsz *= wcslen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000504 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000505 else
506 bufsz = 0;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000507 bufsz += wcslen(PYTHONPATH) + 1;
508 bufsz += wcslen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000509#ifdef MS_WINDOWS
Guido van Rossumeea14491997-08-13 21:30:44 +0000510 if (userpath)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000511 bufsz += wcslen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000512 if (machinepath)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000513 bufsz += wcslen(machinepath) + 1;
514 bufsz += wcslen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000515#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000516 if (envpath != NULL)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000517 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000518
Martin v. Löwis790465f2008-04-05 20:41:37 +0000519 module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000520 if (buf == NULL) {
521 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000522 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
523 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000524 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000525 module_search_path = envpath;
526 }
527 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000528 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000529 module_search_path = PYTHONPATH;
530 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000531#ifdef MS_WINDOWS
Guido van Rossum42a97441998-02-19 21:00:45 +0000532 if (machinepath)
533 free(machinepath);
534 if (userpath)
535 free(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000536#endif /* MS_WINDOWS */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000537 return;
538 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000539
540 if (envpath) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000541 wcscpy(buf, envpath);
542 buf = wcschr(buf, L'\0');
Guido van Rossumeea14491997-08-13 21:30:44 +0000543 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000544 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000545#ifdef MS_WINDOWS
Just van Rossum52e14d62002-12-30 22:08:05 +0000546 if (zip_path[0]) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000547 wcscpy(buf, zip_path);
548 buf = wcschr(buf, L'\0');
Just van Rossum52e14d62002-12-30 22:08:05 +0000549 *buf++ = DELIM;
550 }
Guido van Rossum67ab6721998-08-08 19:58:59 +0000551 if (userpath) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000552 wcscpy(buf, userpath);
553 buf = wcschr(buf, L'\0');
Guido van Rossum67ab6721998-08-08 19:58:59 +0000554 *buf++ = DELIM;
555 free(userpath);
556 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000557 if (machinepath) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000558 wcscpy(buf, machinepath);
559 buf = wcschr(buf, L'\0');
Guido van Rossumeea14491997-08-13 21:30:44 +0000560 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000561 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000562 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000563 if (pythonhome == NULL) {
564 if (!skipdefault) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000565 wcscpy(buf, PYTHONPATH);
566 buf = wcschr(buf, L'\0');
Guido van Rossum88716bb2000-03-30 19:45:39 +0000567 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000568 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000569#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000570 if (pythonhome == NULL) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000571 wcscpy(buf, PYTHONPATH);
572 buf = wcschr(buf, L'\0');
Guido van Rossumeea14491997-08-13 21:30:44 +0000573 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000574#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000575 else {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000576 wchar_t *p = PYTHONPATH;
577 wchar_t *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000578 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000579 for (;;) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000580 q = wcschr(p, DELIM);
Guido van Rossumeea14491997-08-13 21:30:44 +0000581 if (q == NULL)
Martin v. Löwis790465f2008-04-05 20:41:37 +0000582 n = wcslen(p);
Guido van Rossumeea14491997-08-13 21:30:44 +0000583 else
584 n = q-p;
585 if (p[0] == '.' && is_sep(p[1])) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000586 wcscpy(buf, pythonhome);
587 buf = wcschr(buf, L'\0');
Guido van Rossumeea14491997-08-13 21:30:44 +0000588 p++;
589 n--;
590 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000591 wcsncpy(buf, p, n);
Guido van Rossumeea14491997-08-13 21:30:44 +0000592 buf += n;
593 if (q == NULL)
594 break;
595 *buf++ = DELIM;
596 p = q+1;
597 }
598 }
599 if (argv0_path) {
600 *buf++ = DELIM;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000601 wcscpy(buf, argv0_path);
602 buf = wcschr(buf, L'\0');
Guido van Rossumeea14491997-08-13 21:30:44 +0000603 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000604 *buf = L'\0';
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000605 /* Now to pull one last hack/trick. If sys.prefix is
606 empty, then try and find it somewhere on the paths
607 we calculated. We scan backwards, as our general policy
608 is that Python core directories are at the *end* of
609 sys.path. We assume that our "lib" directory is
610 on the path, and that our 'prefix' directory is
611 the parent of that.
612 */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000613 if (*prefix==L'\0') {
614 wchar_t lookBuf[MAXPATHLEN+1];
615 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000616 while (1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000617 Py_ssize_t nchars;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000618 wchar_t *lookEnd = look;
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000619 /* 'look' will end up one character before the
620 start of the path in question - even if this
621 is one character before the start of the buffer
622 */
Guido van Rossumd8faa362007-04-27 19:54:29 +0000623 while (look >= module_search_path && *look != DELIM)
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000624 look--;
625 nchars = lookEnd-look;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000626 wcsncpy(lookBuf, look+1, nchars);
627 lookBuf[nchars] = L'\0';
Mark Hammond19fdbfb2001-09-07 14:08:01 +0000628 /* Up one level to the parent */
629 reduce(lookBuf);
630 if (search_for_prefix(lookBuf, LANDMARK)) {
631 break;
632 }
633 /* If we are out of paths to search - give up */
634 if (look < module_search_path)
635 break;
636 look--;
637 }
638 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000639}
640
641
642/* External interface */
643
Martin v. Löwis790465f2008-04-05 20:41:37 +0000644wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000645Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000646{
647 if (!module_search_path)
648 calculate_path();
649 return module_search_path;
650}
651
Martin v. Löwis790465f2008-04-05 20:41:37 +0000652wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000653Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000654{
Guido van Rossumeea14491997-08-13 21:30:44 +0000655 if (!module_search_path)
656 calculate_path();
657 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000658}
659
Martin v. Löwis790465f2008-04-05 20:41:37 +0000660wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000661Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000662{
Guido van Rossumeea14491997-08-13 21:30:44 +0000663 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000664}
665
Martin v. Löwis790465f2008-04-05 20:41:37 +0000666wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000667Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000668{
669 if (!module_search_path)
670 calculate_path();
671 return progpath;
672}