blob: deb40e76f3622191027fae4076bc3725a7b1596c [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001
2/* Return the initial module search path. */
Jesus Ceaf1af7052012-10-05 02:48:46 +02003/* Used by DOS, 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
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000054 * An embedding application can use Py_SetPath() to override all of
55 these authomatic path computations.
56
Guido van Rossum88716bb2000-03-30 19:45:39 +000057 ---------------------------------------------------------------- */
58
59
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000060#include "Python.h"
61#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000062#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000063
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000064#ifdef MS_WINDOWS
Guido van Rossum8f1b6511997-08-13 19:55:43 +000065#include <windows.h>
66#endif
67
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000069#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#endif /* HAVE_SYS_TYPES_H */
71
72#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000073#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#endif /* HAVE_SYS_STAT_H */
75
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000076#include <string.h>
77
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000078/* Search in some common locations for the associated Python libraries.
79 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000080 * Py_GetPath() tries to return a sensible Python module search path.
81 *
Guido van Rossum42a97441998-02-19 21:00:45 +000082 * The approach is an adaptation for Windows of the strategy used in
83 * ../Modules/getpath.c; it uses the Windows Registry as one of its
84 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000085 *
86 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
87 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000088 */
89
90#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +000091#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000092#endif
93
Martin v. Löwis790465f2008-04-05 20:41:37 +000094static wchar_t prefix[MAXPATHLEN+1];
95static wchar_t progpath[MAXPATHLEN+1];
96static wchar_t dllpath[MAXPATHLEN+1];
97static wchar_t *module_search_path = NULL;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000098
Guido van Rossumeea14491997-08-13 21:30:44 +000099
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101is_sep(wchar_t ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102{
103#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107#endif
108}
109
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000110/* assumes 'dir' null terminated in bounds. Never writes
111 beyond existing terminator.
112*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000114reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 size_t i = wcslen(dir);
117 while (i > 0 && !is_sep(dir[i]))
118 --i;
119 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000120}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000122
123static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000124exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000127}
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129/* Assumes 'filename' MAXPATHLEN+1 bytes long -
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000130 may extend 'filename' by one character.
131*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000132static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (exists(filename))
136 return 1;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 /* Check for the compiled version of prefix. */
139 if (wcslen(filename) < MAXPATHLEN) {
140 wcscat(filename, Py_OptimizeFlag ? L"o" : L"c");
141 if (exists(filename))
142 return 1;
143 }
144 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000145}
146
Tim Peters8484fbf2004-08-07 19:12:27 +0000147/* Add a path component, by appending stuff to buffer.
148 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
149 NUL-terminated string with no more than MAXPATHLEN characters (not counting
150 the trailing NUL). It's a fatal error if it contains a string longer than
151 that (callers must be careful!). If these requirements are met, it's
152 guaranteed that buffer will still be a NUL-terminated string with no more
153 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
154 stuff as fits will be appended.
155*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000156static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000157join(wchar_t *buffer, wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 size_t n, k;
160 if (is_sep(stuff[0]))
161 n = 0;
162 else {
163 n = wcslen(buffer);
164 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
165 buffer[n++] = SEP;
166 }
167 if (n > MAXPATHLEN)
168 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
169 k = wcslen(stuff);
170 if (n + k > MAXPATHLEN)
171 k = MAXPATHLEN - n;
172 wcsncpy(buffer+n, stuff, k);
173 buffer[n+k] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000174}
175
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000176/* gotlandmark only called by search_for_prefix, which ensures
177 'prefix' is null terminated in bounds. join() ensures
178 'landmark' can not overflow prefix if too long.
179*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000180static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000181gotlandmark(wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 int ok;
184 Py_ssize_t n;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 n = wcslen(prefix);
187 join(prefix, landmark);
188 ok = ismodule(prefix);
189 prefix[n] = '\0';
190 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000191}
192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000194 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000195static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000196search_for_prefix(wchar_t *argv0_path, wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* Search from argv0_path, until landmark is found */
199 wcscpy(prefix, argv0_path);
200 do {
201 if (gotlandmark(landmark))
202 return 1;
203 reduce(prefix);
204 } while (prefix[0]);
205 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206}
207
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000208#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000209#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000210
Guido van Rossum88716bb2000-03-30 19:45:39 +0000211/* a string loaded from the DLL at startup.*/
212extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000213
Guido van Rossumeea14491997-08-13 21:30:44 +0000214
215/* Load a PYTHONPATH value from the registry.
216 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
217
Guido van Rossum88716bb2000-03-30 19:45:39 +0000218 Works in both Unicode and 8bit environments. Only uses the
219 Ex family of functions so it also works with Windows CE.
220
Guido van Rossumeea14491997-08-13 21:30:44 +0000221 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000222
223 XXX - this code is pretty strange, as it used to also
224 work on Win16, where the buffer sizes werent available
225 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000226*/
227
Martin v. Löwis790465f2008-04-05 20:41:37 +0000228static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000229getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000230{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 HKEY newKey = 0;
232 DWORD dataSize = 0;
233 DWORD numKeys = 0;
234 LONG rc;
235 wchar_t *retval = NULL;
236 WCHAR *dataBuf = NULL;
237 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
238 static const WCHAR keySuffix[] = L"\\PythonPath";
239 size_t versionLen;
240 DWORD index;
241 WCHAR *keyBuf = NULL;
242 WCHAR *keyBufPtr;
243 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 /* Tried to use sysget("winver") but here is too early :-( */
246 versionLen = strlen(PyWin_DLLVersionString);
247 /* Space for all the chars, plus one \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200248 keyBuf = keyBufPtr = PyMem_RawMalloc(sizeof(keyPrefix) +
249 sizeof(WCHAR)*(versionLen-1) +
250 sizeof(keySuffix));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (keyBuf==NULL) goto done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200254 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
256 keyBufPtr += versionLen;
257 /* NULL comes with this one! */
258 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
259 /* Open the root Python key */
260 rc=RegOpenKeyExW(keyBase,
261 keyBuf, /* subkey */
262 0, /* reserved */
263 KEY_READ,
264 &newKey);
265 if (rc!=ERROR_SUCCESS) goto done;
266 /* Find out how big our core buffer is, and how many subkeys we have */
267 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
268 NULL, NULL, &dataSize, NULL, NULL);
269 if (rc!=ERROR_SUCCESS) goto done;
270 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
271 /* Allocate a temp array of char buffers, so we only need to loop
272 reading the registry once
273 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200274 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (ppPaths==NULL) goto done;
276 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
277 /* Loop over all subkeys, allocating a temp sub-buffer. */
278 for(index=0;index<numKeys;index++) {
279 WCHAR keyBuf[MAX_PATH+1];
280 HKEY subKey = 0;
281 DWORD reqdSize = MAX_PATH+1;
282 /* Get the sub-key name */
283 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
284 NULL, NULL, NULL, NULL );
285 if (rc!=ERROR_SUCCESS) goto done;
286 /* Open the sub-key */
287 rc=RegOpenKeyExW(newKey,
288 keyBuf, /* subkey */
289 0, /* reserved */
290 KEY_READ,
291 &subKey);
292 if (rc!=ERROR_SUCCESS) goto done;
293 /* Find the value of the buffer size, malloc, then read it */
294 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
295 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200296 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 if (ppPaths[index]) {
298 RegQueryValueExW(subKey, NULL, 0, NULL,
299 (LPBYTE)ppPaths[index],
300 &reqdSize);
301 dataSize += reqdSize + 1; /* 1 for the ";" */
302 }
303 }
304 RegCloseKey(subKey);
305 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 /* return null if no path to return */
308 if (dataSize == 0) goto done;
309
310 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200311 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (dataBuf) {
313 WCHAR *szCur = dataBuf;
314 DWORD reqdSize = dataSize;
315 /* Copy our collected strings */
316 for (index=0;index<numKeys;index++) {
317 if (index > 0) {
318 *(szCur++) = L';';
319 dataSize--;
320 }
321 if (ppPaths[index]) {
322 Py_ssize_t len = wcslen(ppPaths[index]);
323 wcsncpy(szCur, ppPaths[index], len);
324 szCur += len;
325 assert(dataSize > (DWORD)len);
326 dataSize -= (DWORD)len;
327 }
328 }
329 if (skipcore)
330 *szCur = '\0';
331 else {
332 /* If we have no values, we dont need a ';' */
333 if (numKeys) {
334 *(szCur++) = L';';
335 dataSize--;
336 }
337 /* Now append the core path entries -
338 this will include the NULL
339 */
340 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
341 (LPBYTE)szCur, &dataSize);
342 }
343 /* And set the result - caller must free */
344 retval = dataBuf;
345 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000346done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 /* Loop freeing my temp buffers */
348 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200349 for(index=0; index<numKeys; index++)
350 PyMem_RawFree(ppPaths[index]);
351 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 }
353 if (newKey)
354 RegCloseKey(newKey);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200355 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000357}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000358#endif /* Py_ENABLE_SHARED */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000359#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000360
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000361static void
Thomas Wouters78890102000-07-22 19:25:51 +0000362get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 extern wchar_t *Py_GetProgramName(void);
365 wchar_t *path = _wgetenv(L"PATH");
366 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000367
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000368#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000369#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 extern HANDLE PyWin_DLLhModule;
371 /* static init of progpath ensures final char remains \0 */
372 if (PyWin_DLLhModule)
373 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
374 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000375#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000377#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
379 return;
Guido van Rossumeea14491997-08-13 21:30:44 +0000380#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (prog == NULL || *prog == '\0')
382 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 /* If there is no slash in the argv0 path, then we have to
385 * assume python is on the user's $PATH, since there's no
386 * other way to find a directory to start the search from. If
387 * $PATH isn't exported, you lose.
388 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000389#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000391#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 wcsncpy(progpath, prog, MAXPATHLEN);
395 else if (path) {
396 while (1) {
397 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (delim) {
400 size_t len = delim - path;
401 /* ensure we can't overwrite buffer */
402 len = min(MAXPATHLEN,len);
403 wcsncpy(progpath, path, len);
404 *(progpath + len) = '\0';
405 }
406 else
407 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 /* join() is safe for MAXPATHLEN+1 size buffer */
410 join(progpath, prog);
411 if (exists(progpath))
412 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (!delim) {
415 progpath[0] = '\0';
416 break;
417 }
418 path = delim + 1;
419 }
420 }
421 else
422 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000423}
424
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100425static int
426find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
427{
428 int result = 0; /* meaning not found */
429 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
430
431 fseek(env_file, 0, SEEK_SET);
432 while (!feof(env_file)) {
433 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
434 wchar_t tmpbuffer[MAXPATHLEN*2+1];
435 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200436 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100437
438 if (p == NULL)
439 break;
440 n = strlen(p);
441 if (p[n - 1] != '\n') {
442 /* line has overflowed - bail */
443 break;
444 }
445 if (p[0] == '#') /* Comment - skip */
446 continue;
447 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
448 if (decoded != NULL) {
449 Py_ssize_t k;
450 k = PyUnicode_AsWideChar(decoded,
451 tmpbuffer, MAXPATHLEN * 2);
452 Py_DECREF(decoded);
453 if (k >= 0) {
454 wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n");
455 if ((tok != NULL) && !wcscmp(tok, key)) {
456 tok = wcstok(NULL, L" \t");
457 if ((tok != NULL) && !wcscmp(tok, L"=")) {
458 tok = wcstok(NULL, L"\r\n");
459 if (tok != NULL) {
460 wcsncpy(value, tok, MAXPATHLEN);
461 result = 1;
462 break;
463 }
464 }
465 }
466 }
467 }
468 }
469 return result;
470}
471
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000472static void
Thomas Wouters78890102000-07-22 19:25:51 +0000473calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 wchar_t argv0_path[MAXPATHLEN+1];
476 wchar_t *buf;
477 size_t bufsz;
478 wchar_t *pythonhome = Py_GetPythonHome();
479 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000480
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000481#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 int skiphome, skipdefault;
483 wchar_t *machinepath = NULL;
484 wchar_t *userpath = NULL;
485 wchar_t zip_path[MAXPATHLEN+1];
486 size_t len;
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (!Py_IgnoreEnvironmentFlag) {
489 envpath = _wgetenv(L"PYTHONPATH");
490 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000491#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 char *_envpath = Py_GETENV("PYTHONPATH");
493 wchar_t wenvpath[MAXPATHLEN+1];
494 if (_envpath) {
495 size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
496 envpath = wenvpath;
497 if (r == (size_t)-1 || r >= MAXPATHLEN)
498 envpath = NULL;
499 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000500#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 get_progpath();
503 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
504 wcscpy(argv0_path, progpath);
505 reduce(argv0_path);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100506
507 /* Search for an environment configuration file, first in the
508 executable's directory and then in the parent directory.
509 If found, open it for use when searching for prefixes.
510 */
511
512 {
513 wchar_t tmpbuffer[MAXPATHLEN+1];
514 wchar_t *env_cfg = L"pyvenv.cfg";
515 FILE * env_file = NULL;
516
517 wcscpy(tmpbuffer, argv0_path);
518 join(tmpbuffer, env_cfg);
519 env_file = _Py_wfopen(tmpbuffer, L"r");
520 if (env_file == NULL) {
521 errno = 0;
522 reduce(tmpbuffer);
523 reduce(tmpbuffer);
524 join(tmpbuffer, env_cfg);
525 env_file = _Py_wfopen(tmpbuffer, L"r");
526 if (env_file == NULL) {
527 errno = 0;
528 }
529 }
530 if (env_file != NULL) {
531 /* Look for a 'home' variable and set argv0_path to it, if found */
532 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
533 wcscpy(argv0_path, tmpbuffer);
534 }
535 fclose(env_file);
536 env_file = NULL;
537 }
538 }
539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (pythonhome == NULL || *pythonhome == '\0') {
541 if (search_for_prefix(argv0_path, LANDMARK))
542 pythonhome = prefix;
543 else
544 pythonhome = NULL;
545 }
546 else
547 wcsncpy(prefix, pythonhome, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 if (envpath && *envpath == '\0')
550 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000551
Guido van Rossume02e48b2000-03-29 01:49:47 +0000552
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000553#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* Calculate zip archive path */
555 if (dllpath[0]) /* use name of python DLL */
556 wcsncpy(zip_path, dllpath, MAXPATHLEN);
557 else /* use name of executable program */
558 wcsncpy(zip_path, progpath, MAXPATHLEN);
559 zip_path[MAXPATHLEN] = '\0';
560 len = wcslen(zip_path);
561 if (len > 4) {
562 zip_path[len-3] = 'z'; /* change ending to "zip" */
563 zip_path[len-2] = 'i';
564 zip_path[len-1] = 'p';
565 }
566 else {
567 zip_path[0] = 0;
568 }
569
570 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000571#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
573 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000574#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* We only use the default relative PYTHONPATH if we havent
576 anything better to use! */
577 skipdefault = envpath!=NULL || pythonhome!=NULL || \
578 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000579#endif
580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* We need to construct a path from the following parts.
582 (1) the PYTHONPATH environment variable, if set;
583 (2) for Win32, the zip archive file path;
584 (3) for Win32, the machinepath and userpath, if set;
585 (4) the PYTHONPATH config macro, with the leading "."
586 of each component replaced with pythonhome, if set;
587 (5) the directory containing the executable (argv0_path).
588 The length calculation calculates #4 first.
589 Extra rules:
590 - If PYTHONHOME is set (in any way) item (3) is ignored.
591 - If registry values are used, (4) and (5) are ignored.
592 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 /* Calculate size of return buffer */
595 if (pythonhome != NULL) {
596 wchar_t *p;
597 bufsz = 1;
598 for (p = PYTHONPATH; *p; p++) {
599 if (*p == DELIM)
600 bufsz++; /* number of DELIM plus one */
601 }
602 bufsz *= wcslen(pythonhome);
603 }
604 else
605 bufsz = 0;
606 bufsz += wcslen(PYTHONPATH) + 1;
607 bufsz += wcslen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000608#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 if (userpath)
610 bufsz += wcslen(userpath) + 1;
611 if (machinepath)
612 bufsz += wcslen(machinepath) + 1;
613 bufsz += wcslen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000614#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (envpath != NULL)
616 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000617
Victor Stinner1a7425f2013-07-07 16:25:15 +0200618 module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 if (buf == NULL) {
620 /* We can't exit, so print a warning and limp along */
621 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
622 if (envpath) {
623 fprintf(stderr, "Using environment $PYTHONPATH.\n");
624 module_search_path = envpath;
625 }
626 else {
627 fprintf(stderr, "Using default static path.\n");
628 module_search_path = PYTHONPATH;
629 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000630#ifdef MS_WINDOWS
Victor Stinner1a7425f2013-07-07 16:25:15 +0200631 PyMem_RawFree(machinepath);
632 PyMem_RawFree(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000633#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return;
635 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 if (envpath) {
638 wcscpy(buf, envpath);
639 buf = wcschr(buf, L'\0');
640 *buf++ = DELIM;
641 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000642#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (zip_path[0]) {
644 wcscpy(buf, zip_path);
645 buf = wcschr(buf, L'\0');
646 *buf++ = DELIM;
647 }
648 if (userpath) {
649 wcscpy(buf, userpath);
650 buf = wcschr(buf, L'\0');
651 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200652 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 }
654 if (machinepath) {
655 wcscpy(buf, machinepath);
656 buf = wcschr(buf, L'\0');
657 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200658 PyMem_RawFree(machinepath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 }
660 if (pythonhome == NULL) {
661 if (!skipdefault) {
662 wcscpy(buf, PYTHONPATH);
663 buf = wcschr(buf, L'\0');
664 }
665 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000666#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 if (pythonhome == NULL) {
668 wcscpy(buf, PYTHONPATH);
669 buf = wcschr(buf, L'\0');
670 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000671#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 else {
673 wchar_t *p = PYTHONPATH;
674 wchar_t *q;
675 size_t n;
676 for (;;) {
677 q = wcschr(p, DELIM);
678 if (q == NULL)
679 n = wcslen(p);
680 else
681 n = q-p;
682 if (p[0] == '.' && is_sep(p[1])) {
683 wcscpy(buf, pythonhome);
684 buf = wcschr(buf, L'\0');
685 p++;
686 n--;
687 }
688 wcsncpy(buf, p, n);
689 buf += n;
690 if (q == NULL)
691 break;
692 *buf++ = DELIM;
693 p = q+1;
694 }
695 }
696 if (argv0_path) {
697 *buf++ = DELIM;
698 wcscpy(buf, argv0_path);
699 buf = wcschr(buf, L'\0');
700 }
701 *buf = L'\0';
702 /* Now to pull one last hack/trick. If sys.prefix is
703 empty, then try and find it somewhere on the paths
704 we calculated. We scan backwards, as our general policy
705 is that Python core directories are at the *end* of
706 sys.path. We assume that our "lib" directory is
707 on the path, and that our 'prefix' directory is
708 the parent of that.
709 */
710 if (*prefix==L'\0') {
711 wchar_t lookBuf[MAXPATHLEN+1];
712 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
713 while (1) {
714 Py_ssize_t nchars;
715 wchar_t *lookEnd = look;
716 /* 'look' will end up one character before the
717 start of the path in question - even if this
718 is one character before the start of the buffer
719 */
720 while (look >= module_search_path && *look != DELIM)
721 look--;
722 nchars = lookEnd-look;
723 wcsncpy(lookBuf, look+1, nchars);
724 lookBuf[nchars] = L'\0';
725 /* Up one level to the parent */
726 reduce(lookBuf);
727 if (search_for_prefix(lookBuf, LANDMARK)) {
728 break;
729 }
730 /* If we are out of paths to search - give up */
731 if (look < module_search_path)
732 break;
733 look--;
734 }
735 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000736}
737
738
739/* External interface */
740
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000741void
742Py_SetPath(const wchar_t *path)
743{
744 if (module_search_path != NULL) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200745 PyMem_RawFree(module_search_path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000746 module_search_path = NULL;
747 }
748 if (path != NULL) {
749 extern wchar_t *Py_GetProgramName(void);
750 wchar_t *prog = Py_GetProgramName();
751 wcsncpy(progpath, prog, MAXPATHLEN);
752 prefix[0] = L'\0';
Victor Stinner1a7425f2013-07-07 16:25:15 +0200753 module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000754 if (module_search_path != NULL)
755 wcscpy(module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200756 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000757}
758
Martin v. Löwis790465f2008-04-05 20:41:37 +0000759wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000760Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 if (!module_search_path)
763 calculate_path();
764 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000765}
766
Martin v. Löwis790465f2008-04-05 20:41:37 +0000767wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000768Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (!module_search_path)
771 calculate_path();
772 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000773}
774
Martin v. Löwis790465f2008-04-05 20:41:37 +0000775wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000776Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000779}
780
Martin v. Löwis790465f2008-04-05 20:41:37 +0000781wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000782Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (!module_search_path)
785 calculate_path();
786 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000787}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000788
Victor Stinner63941882011-09-29 00:42:28 +0200789/* Load python3.dll before loading any extension module that might refer
790 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000791 to this python DLL is loaded, not a python3.dll that might be on the path
792 by chance.
793 Return whether the DLL was found.
794*/
795static int python3_checked = 0;
796static HANDLE hPython3;
797int
798_Py_CheckPython3()
799{
800 wchar_t py3path[MAXPATHLEN+1];
801 wchar_t *s;
802 if (python3_checked)
803 return hPython3 != NULL;
804 python3_checked = 1;
805
806 /* If there is a python3.dll next to the python3y.dll,
807 assume this is a build tree; use that DLL */
808 wcscpy(py3path, dllpath);
809 s = wcsrchr(py3path, L'\\');
810 if (!s)
811 s = py3path;
812 wcscpy(s, L"\\python3.dll");
813 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
814 if (hPython3 != NULL)
815 return 1;
816
817 /* Check sys.prefix\DLLs\python3.dll */
818 wcscpy(py3path, Py_GetPrefix());
819 wcscat(py3path, L"\\DLLs\\python3.dll");
820 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
821 return hPython3 != NULL;
822}