blob: 0fe04c7fa44f4358a01d684be97ab80c7caed256 [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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these
Guido van Rossum88716bb2000-03-30 19:45:39 +00009 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.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 loaded from the registry. This is the main PythonPath key,
Guido van Rossum88716bb2000-03-30 19:45:39 +000031 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_
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 we can assume is a good path), a default path with relative entries is
Guido van Rossum88716bb2000-03-30 19:45:39 +000036 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 * When Python is hosted in another exe (different directory, embedded via
Guido van Rossum88716bb2000-03-30 19:45:39 +000046 COM, etc), the Python Home will not be deduced, so the core path from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095is_sep(wchar_t ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000096{
97#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000099#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 size_t i = wcslen(dir);
111 while (i > 0 && !is_sep(dir[i]))
112 --i;
113 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000114}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000116
117static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000118exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000121}
122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123/* Assumes 'filename' MAXPATHLEN+1 bytes long -
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000124 may extend 'filename' by one character.
125*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000126static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 if (exists(filename))
130 return 1;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 /* Check for the compiled version of prefix. */
133 if (wcslen(filename) < MAXPATHLEN) {
134 wcscat(filename, Py_OptimizeFlag ? L"o" : L"c");
135 if (exists(filename))
136 return 1;
137 }
138 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000139}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 size_t n, k;
154 if (is_sep(stuff[0]))
155 n = 0;
156 else {
157 n = wcslen(buffer);
158 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
159 buffer[n++] = SEP;
160 }
161 if (n > MAXPATHLEN)
162 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
163 k = wcslen(stuff);
164 if (n + k > MAXPATHLEN)
165 k = MAXPATHLEN - n;
166 wcsncpy(buffer+n, stuff, k);
167 buffer[n+k] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000168}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 int ok;
178 Py_ssize_t n;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 n = wcslen(prefix);
181 join(prefix, landmark);
182 ok = ismodule(prefix);
183 prefix[n] = '\0';
184 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000185}
186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000188 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 /* Search from argv0_path, until landmark is found */
193 wcscpy(prefix, argv0_path);
194 do {
195 if (gotlandmark(landmark))
196 return 1;
197 reduce(prefix);
198 } while (prefix[0]);
199 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000200}
201
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000202#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000203#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000204
Guido van Rossum88716bb2000-03-30 19:45:39 +0000205/* a string loaded from the DLL at startup.*/
206extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000207
Guido van Rossumeea14491997-08-13 21:30:44 +0000208
209/* Load a PYTHONPATH value from the registry.
210 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
211
Guido van Rossum88716bb2000-03-30 19:45:39 +0000212 Works in both Unicode and 8bit environments. Only uses the
213 Ex family of functions so it also works with Windows CE.
214
Guido van Rossumeea14491997-08-13 21:30:44 +0000215 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000216
217 XXX - this code is pretty strange, as it used to also
218 work on Win16, where the buffer sizes werent available
219 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000220*/
221
Martin v. Löwis790465f2008-04-05 20:41:37 +0000222static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000223getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 HKEY newKey = 0;
226 DWORD dataSize = 0;
227 DWORD numKeys = 0;
228 LONG rc;
229 wchar_t *retval = NULL;
230 WCHAR *dataBuf = NULL;
231 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
232 static const WCHAR keySuffix[] = L"\\PythonPath";
233 size_t versionLen;
234 DWORD index;
235 WCHAR *keyBuf = NULL;
236 WCHAR *keyBufPtr;
237 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 /* Tried to use sysget("winver") but here is too early :-( */
240 versionLen = strlen(PyWin_DLLVersionString);
241 /* Space for all the chars, plus one \0 */
242 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
243 sizeof(WCHAR)*(versionLen-1) +
244 sizeof(keySuffix));
245 if (keyBuf==NULL) goto done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
248 keyBufPtr += sizeof(keyPrefix)/sizeof(WCHAR) - 1;
249 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
250 keyBufPtr += versionLen;
251 /* NULL comes with this one! */
252 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
253 /* Open the root Python key */
254 rc=RegOpenKeyExW(keyBase,
255 keyBuf, /* subkey */
256 0, /* reserved */
257 KEY_READ,
258 &newKey);
259 if (rc!=ERROR_SUCCESS) goto done;
260 /* Find out how big our core buffer is, and how many subkeys we have */
261 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
262 NULL, NULL, &dataSize, NULL, NULL);
263 if (rc!=ERROR_SUCCESS) goto done;
264 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
265 /* Allocate a temp array of char buffers, so we only need to loop
266 reading the registry once
267 */
268 ppPaths = malloc( sizeof(WCHAR *) * numKeys );
269 if (ppPaths==NULL) goto done;
270 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
271 /* Loop over all subkeys, allocating a temp sub-buffer. */
272 for(index=0;index<numKeys;index++) {
273 WCHAR keyBuf[MAX_PATH+1];
274 HKEY subKey = 0;
275 DWORD reqdSize = MAX_PATH+1;
276 /* Get the sub-key name */
277 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
278 NULL, NULL, NULL, NULL );
279 if (rc!=ERROR_SUCCESS) goto done;
280 /* Open the sub-key */
281 rc=RegOpenKeyExW(newKey,
282 keyBuf, /* subkey */
283 0, /* reserved */
284 KEY_READ,
285 &subKey);
286 if (rc!=ERROR_SUCCESS) goto done;
287 /* Find the value of the buffer size, malloc, then read it */
288 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
289 if (reqdSize) {
290 ppPaths[index] = malloc(reqdSize);
291 if (ppPaths[index]) {
292 RegQueryValueExW(subKey, NULL, 0, NULL,
293 (LPBYTE)ppPaths[index],
294 &reqdSize);
295 dataSize += reqdSize + 1; /* 1 for the ";" */
296 }
297 }
298 RegCloseKey(subKey);
299 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* return null if no path to return */
302 if (dataSize == 0) goto done;
303
304 /* original datasize from RegQueryInfo doesn't include the \0 */
305 dataBuf = malloc((dataSize+1) * sizeof(WCHAR));
306 if (dataBuf) {
307 WCHAR *szCur = dataBuf;
308 DWORD reqdSize = dataSize;
309 /* Copy our collected strings */
310 for (index=0;index<numKeys;index++) {
311 if (index > 0) {
312 *(szCur++) = L';';
313 dataSize--;
314 }
315 if (ppPaths[index]) {
316 Py_ssize_t len = wcslen(ppPaths[index]);
317 wcsncpy(szCur, ppPaths[index], len);
318 szCur += len;
319 assert(dataSize > (DWORD)len);
320 dataSize -= (DWORD)len;
321 }
322 }
323 if (skipcore)
324 *szCur = '\0';
325 else {
326 /* If we have no values, we dont need a ';' */
327 if (numKeys) {
328 *(szCur++) = L';';
329 dataSize--;
330 }
331 /* Now append the core path entries -
332 this will include the NULL
333 */
334 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
335 (LPBYTE)szCur, &dataSize);
336 }
337 /* And set the result - caller must free */
338 retval = dataBuf;
339 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000340done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* Loop freeing my temp buffers */
342 if (ppPaths) {
343 for(index=0;index<numKeys;index++)
344 if (ppPaths[index]) free(ppPaths[index]);
345 free(ppPaths);
346 }
347 if (newKey)
348 RegCloseKey(newKey);
349 if (keyBuf)
350 free(keyBuf);
351 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000352}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000353#endif /* Py_ENABLE_SHARED */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000354#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000355
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000356static void
Thomas Wouters78890102000-07-22 19:25:51 +0000357get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 extern wchar_t *Py_GetProgramName(void);
360 wchar_t *path = _wgetenv(L"PATH");
361 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000362
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000363#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000364#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 extern HANDLE PyWin_DLLhModule;
366 /* static init of progpath ensures final char remains \0 */
367 if (PyWin_DLLhModule)
368 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
369 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000370#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000372#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
374 return;
Guido van Rossumeea14491997-08-13 21:30:44 +0000375#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 if (prog == NULL || *prog == '\0')
377 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* If there is no slash in the argv0 path, then we have to
380 * assume python is on the user's $PATH, since there's no
381 * other way to find a directory to start the search from. If
382 * $PATH isn't exported, you lose.
383 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000384#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000386#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000388#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 wcsncpy(progpath, prog, MAXPATHLEN);
390 else if (path) {
391 while (1) {
392 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (delim) {
395 size_t len = delim - path;
396 /* ensure we can't overwrite buffer */
397 len = min(MAXPATHLEN,len);
398 wcsncpy(progpath, path, len);
399 *(progpath + len) = '\0';
400 }
401 else
402 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* join() is safe for MAXPATHLEN+1 size buffer */
405 join(progpath, prog);
406 if (exists(progpath))
407 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (!delim) {
410 progpath[0] = '\0';
411 break;
412 }
413 path = delim + 1;
414 }
415 }
416 else
417 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000418}
419
420static void
Thomas Wouters78890102000-07-22 19:25:51 +0000421calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 wchar_t argv0_path[MAXPATHLEN+1];
424 wchar_t *buf;
425 size_t bufsz;
426 wchar_t *pythonhome = Py_GetPythonHome();
427 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000428
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000429#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 int skiphome, skipdefault;
431 wchar_t *machinepath = NULL;
432 wchar_t *userpath = NULL;
433 wchar_t zip_path[MAXPATHLEN+1];
434 size_t len;
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (!Py_IgnoreEnvironmentFlag) {
437 envpath = _wgetenv(L"PYTHONPATH");
438 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000439#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 char *_envpath = Py_GETENV("PYTHONPATH");
441 wchar_t wenvpath[MAXPATHLEN+1];
442 if (_envpath) {
443 size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
444 envpath = wenvpath;
445 if (r == (size_t)-1 || r >= MAXPATHLEN)
446 envpath = NULL;
447 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000448#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 get_progpath();
451 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
452 wcscpy(argv0_path, progpath);
453 reduce(argv0_path);
454 if (pythonhome == NULL || *pythonhome == '\0') {
455 if (search_for_prefix(argv0_path, LANDMARK))
456 pythonhome = prefix;
457 else
458 pythonhome = NULL;
459 }
460 else
461 wcsncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (envpath && *envpath == '\0')
464 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000465
Guido van Rossume02e48b2000-03-29 01:49:47 +0000466
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000467#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 /* Calculate zip archive path */
469 if (dllpath[0]) /* use name of python DLL */
470 wcsncpy(zip_path, dllpath, MAXPATHLEN);
471 else /* use name of executable program */
472 wcsncpy(zip_path, progpath, MAXPATHLEN);
473 zip_path[MAXPATHLEN] = '\0';
474 len = wcslen(zip_path);
475 if (len > 4) {
476 zip_path[len-3] = 'z'; /* change ending to "zip" */
477 zip_path[len-2] = 'i';
478 zip_path[len-1] = 'p';
479 }
480 else {
481 zip_path[0] = 0;
482 }
483
484 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000485#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
487 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000488#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* We only use the default relative PYTHONPATH if we havent
490 anything better to use! */
491 skipdefault = envpath!=NULL || pythonhome!=NULL || \
492 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000493#endif
494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* We need to construct a path from the following parts.
496 (1) the PYTHONPATH environment variable, if set;
497 (2) for Win32, the zip archive file path;
498 (3) for Win32, the machinepath and userpath, if set;
499 (4) the PYTHONPATH config macro, with the leading "."
500 of each component replaced with pythonhome, if set;
501 (5) the directory containing the executable (argv0_path).
502 The length calculation calculates #4 first.
503 Extra rules:
504 - If PYTHONHOME is set (in any way) item (3) is ignored.
505 - If registry values are used, (4) and (5) are ignored.
506 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 /* Calculate size of return buffer */
509 if (pythonhome != NULL) {
510 wchar_t *p;
511 bufsz = 1;
512 for (p = PYTHONPATH; *p; p++) {
513 if (*p == DELIM)
514 bufsz++; /* number of DELIM plus one */
515 }
516 bufsz *= wcslen(pythonhome);
517 }
518 else
519 bufsz = 0;
520 bufsz += wcslen(PYTHONPATH) + 1;
521 bufsz += wcslen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000522#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (userpath)
524 bufsz += wcslen(userpath) + 1;
525 if (machinepath)
526 bufsz += wcslen(machinepath) + 1;
527 bufsz += wcslen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000528#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (envpath != NULL)
530 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 module_search_path = buf = malloc(bufsz*sizeof(wchar_t));
533 if (buf == NULL) {
534 /* We can't exit, so print a warning and limp along */
535 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
536 if (envpath) {
537 fprintf(stderr, "Using environment $PYTHONPATH.\n");
538 module_search_path = envpath;
539 }
540 else {
541 fprintf(stderr, "Using default static path.\n");
542 module_search_path = PYTHONPATH;
543 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000544#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (machinepath)
546 free(machinepath);
547 if (userpath)
548 free(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000549#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return;
551 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (envpath) {
554 wcscpy(buf, envpath);
555 buf = wcschr(buf, L'\0');
556 *buf++ = DELIM;
557 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000558#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 if (zip_path[0]) {
560 wcscpy(buf, zip_path);
561 buf = wcschr(buf, L'\0');
562 *buf++ = DELIM;
563 }
564 if (userpath) {
565 wcscpy(buf, userpath);
566 buf = wcschr(buf, L'\0');
567 *buf++ = DELIM;
568 free(userpath);
569 }
570 if (machinepath) {
571 wcscpy(buf, machinepath);
572 buf = wcschr(buf, L'\0');
573 *buf++ = DELIM;
574 free(machinepath);
575 }
576 if (pythonhome == NULL) {
577 if (!skipdefault) {
578 wcscpy(buf, PYTHONPATH);
579 buf = wcschr(buf, L'\0');
580 }
581 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000582#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 if (pythonhome == NULL) {
584 wcscpy(buf, PYTHONPATH);
585 buf = wcschr(buf, L'\0');
586 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000587#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 else {
589 wchar_t *p = PYTHONPATH;
590 wchar_t *q;
591 size_t n;
592 for (;;) {
593 q = wcschr(p, DELIM);
594 if (q == NULL)
595 n = wcslen(p);
596 else
597 n = q-p;
598 if (p[0] == '.' && is_sep(p[1])) {
599 wcscpy(buf, pythonhome);
600 buf = wcschr(buf, L'\0');
601 p++;
602 n--;
603 }
604 wcsncpy(buf, p, n);
605 buf += n;
606 if (q == NULL)
607 break;
608 *buf++ = DELIM;
609 p = q+1;
610 }
611 }
612 if (argv0_path) {
613 *buf++ = DELIM;
614 wcscpy(buf, argv0_path);
615 buf = wcschr(buf, L'\0');
616 }
617 *buf = L'\0';
618 /* Now to pull one last hack/trick. If sys.prefix is
619 empty, then try and find it somewhere on the paths
620 we calculated. We scan backwards, as our general policy
621 is that Python core directories are at the *end* of
622 sys.path. We assume that our "lib" directory is
623 on the path, and that our 'prefix' directory is
624 the parent of that.
625 */
626 if (*prefix==L'\0') {
627 wchar_t lookBuf[MAXPATHLEN+1];
628 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
629 while (1) {
630 Py_ssize_t nchars;
631 wchar_t *lookEnd = look;
632 /* 'look' will end up one character before the
633 start of the path in question - even if this
634 is one character before the start of the buffer
635 */
636 while (look >= module_search_path && *look != DELIM)
637 look--;
638 nchars = lookEnd-look;
639 wcsncpy(lookBuf, look+1, nchars);
640 lookBuf[nchars] = L'\0';
641 /* Up one level to the parent */
642 reduce(lookBuf);
643 if (search_for_prefix(lookBuf, LANDMARK)) {
644 break;
645 }
646 /* If we are out of paths to search - give up */
647 if (look < module_search_path)
648 break;
649 look--;
650 }
651 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000652}
653
654
655/* External interface */
656
Martin v. Löwis790465f2008-04-05 20:41:37 +0000657wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000658Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 if (!module_search_path)
661 calculate_path();
662 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000663}
664
Martin v. Löwis790465f2008-04-05 20:41:37 +0000665wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000666Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (!module_search_path)
669 calculate_path();
670 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000671}
672
Martin v. Löwis790465f2008-04-05 20:41:37 +0000673wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000674Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000677}
678
Martin v. Löwis790465f2008-04-05 20:41:37 +0000679wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000680Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 if (!module_search_path)
683 calculate_path();
684 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000685}