blob: 25b328b260c10bb8f76c866b08eb91a9d089ca2b [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{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700116 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
117 if (i >= MAXPATHLEN+1)
118 Py_FatalError("buffer overflow in getpathp.c's reduce()");
119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 while (i > 0 && !is_sep(dir[i]))
121 --i;
122 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000123}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000125
126static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000127exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000130}
131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132/* Assumes 'filename' MAXPATHLEN+1 bytes long -
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000133 may extend 'filename' by one character.
134*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000135static int
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700136ismodule(wchar_t *filename, int update_filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000137{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700138 int n;
139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (exists(filename))
141 return 1;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700144 n = wcsnlen_s(filename, MAXPATHLEN+1);
145 if (n < MAXPATHLEN) {
146 int exist = 0;
147 filename[n] = Py_OptimizeFlag ? L'o' : L'c';
148 filename[n + 1] = L'\0';
149 exist = exists(filename);
150 if (!update_filename)
151 filename[n] = L'\0';
152 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 }
154 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000155}
156
Tim Peters8484fbf2004-08-07 19:12:27 +0000157/* Add a path component, by appending stuff to buffer.
158 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
159 NUL-terminated string with no more than MAXPATHLEN characters (not counting
160 the trailing NUL). It's a fatal error if it contains a string longer than
161 that (callers must be careful!). If these requirements are met, it's
162 guaranteed that buffer will still be a NUL-terminated string with no more
163 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
164 stuff as fits will be appended.
165*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000166static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700167join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000168{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700169 size_t n;
170 if (is_sep(stuff[0]) ||
171 (wcsnlen_s(stuff, 4) >= 3 && stuff[1] == ':' && is_sep(stuff[2]))) {
172 if (wcscpy_s(buffer, MAXPATHLEN+1, stuff) != 0)
173 Py_FatalError("buffer overflow in getpathp.c's join()");
174 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700176
177 n = wcsnlen_s(buffer, MAXPATHLEN+1);
178 if (n > 0 && !is_sep(buffer[n - 1]) && n < MAXPATHLEN) {
179 buffer[n] = SEP;
180 buffer[n + 1] = '\0';
181 }
182 if (wcscat_s(buffer, MAXPATHLEN+1, stuff) != 0)
183 Py_FatalError("buffer overflow in getpathp.c's join()");
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000184}
185
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000186/* gotlandmark only called by search_for_prefix, which ensures
187 'prefix' is null terminated in bounds. join() ensures
188 'landmark' can not overflow prefix if too long.
189*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000190static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000191gotlandmark(wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700194 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700197 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 prefix[n] = '\0';
199 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000200}
201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000203 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000204static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000205search_for_prefix(wchar_t *argv0_path, wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700208 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 do {
210 if (gotlandmark(landmark))
211 return 1;
212 reduce(prefix);
213 } while (prefix[0]);
214 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000215}
216
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000217#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000218#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000219
Guido van Rossum88716bb2000-03-30 19:45:39 +0000220/* a string loaded from the DLL at startup.*/
221extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000222
Guido van Rossumeea14491997-08-13 21:30:44 +0000223
224/* Load a PYTHONPATH value from the registry.
225 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
226
Guido van Rossum88716bb2000-03-30 19:45:39 +0000227 Works in both Unicode and 8bit environments. Only uses the
228 Ex family of functions so it also works with Windows CE.
229
Guido van Rossumeea14491997-08-13 21:30:44 +0000230 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000231
232 XXX - this code is pretty strange, as it used to also
233 work on Win16, where the buffer sizes werent available
234 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000235*/
236
Martin v. Löwis790465f2008-04-05 20:41:37 +0000237static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000238getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 HKEY newKey = 0;
241 DWORD dataSize = 0;
242 DWORD numKeys = 0;
243 LONG rc;
244 wchar_t *retval = NULL;
245 WCHAR *dataBuf = NULL;
246 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
247 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700248 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 DWORD index;
250 WCHAR *keyBuf = NULL;
251 WCHAR *keyBufPtr;
252 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 /* Tried to use sysget("winver") but here is too early :-( */
255 versionLen = strlen(PyWin_DLLVersionString);
256 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700257 keyBufLen = sizeof(keyPrefix) +
258 sizeof(WCHAR)*(versionLen-1) +
259 sizeof(keySuffix);
260 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (keyBuf==NULL) goto done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000262
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700263 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200264 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
266 keyBufPtr += versionLen;
267 /* NULL comes with this one! */
268 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
269 /* Open the root Python key */
270 rc=RegOpenKeyExW(keyBase,
271 keyBuf, /* subkey */
272 0, /* reserved */
273 KEY_READ,
274 &newKey);
275 if (rc!=ERROR_SUCCESS) goto done;
276 /* Find out how big our core buffer is, and how many subkeys we have */
277 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
278 NULL, NULL, &dataSize, NULL, NULL);
279 if (rc!=ERROR_SUCCESS) goto done;
280 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
281 /* Allocate a temp array of char buffers, so we only need to loop
282 reading the registry once
283 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200284 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 if (ppPaths==NULL) goto done;
286 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
287 /* Loop over all subkeys, allocating a temp sub-buffer. */
288 for(index=0;index<numKeys;index++) {
289 WCHAR keyBuf[MAX_PATH+1];
290 HKEY subKey = 0;
291 DWORD reqdSize = MAX_PATH+1;
292 /* Get the sub-key name */
293 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
294 NULL, NULL, NULL, NULL );
295 if (rc!=ERROR_SUCCESS) goto done;
296 /* Open the sub-key */
297 rc=RegOpenKeyExW(newKey,
298 keyBuf, /* subkey */
299 0, /* reserved */
300 KEY_READ,
301 &subKey);
302 if (rc!=ERROR_SUCCESS) goto done;
303 /* Find the value of the buffer size, malloc, then read it */
304 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
305 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200306 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (ppPaths[index]) {
308 RegQueryValueExW(subKey, NULL, 0, NULL,
309 (LPBYTE)ppPaths[index],
310 &reqdSize);
311 dataSize += reqdSize + 1; /* 1 for the ";" */
312 }
313 }
314 RegCloseKey(subKey);
315 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 /* return null if no path to return */
318 if (dataSize == 0) goto done;
319
320 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200321 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (dataBuf) {
323 WCHAR *szCur = dataBuf;
324 DWORD reqdSize = dataSize;
325 /* Copy our collected strings */
326 for (index=0;index<numKeys;index++) {
327 if (index > 0) {
328 *(szCur++) = L';';
329 dataSize--;
330 }
331 if (ppPaths[index]) {
332 Py_ssize_t len = wcslen(ppPaths[index]);
333 wcsncpy(szCur, ppPaths[index], len);
334 szCur += len;
335 assert(dataSize > (DWORD)len);
336 dataSize -= (DWORD)len;
337 }
338 }
339 if (skipcore)
340 *szCur = '\0';
341 else {
342 /* If we have no values, we dont need a ';' */
343 if (numKeys) {
344 *(szCur++) = L';';
345 dataSize--;
346 }
347 /* Now append the core path entries -
348 this will include the NULL
349 */
350 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
351 (LPBYTE)szCur, &dataSize);
352 }
353 /* And set the result - caller must free */
354 retval = dataBuf;
355 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000356done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 /* Loop freeing my temp buffers */
358 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200359 for(index=0; index<numKeys; index++)
360 PyMem_RawFree(ppPaths[index]);
361 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 }
363 if (newKey)
364 RegCloseKey(newKey);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200365 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000367}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000368#endif /* Py_ENABLE_SHARED */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000369#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000370
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000371static void
Thomas Wouters78890102000-07-22 19:25:51 +0000372get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 extern wchar_t *Py_GetProgramName(void);
375 wchar_t *path = _wgetenv(L"PATH");
376 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000377
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000378#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000379#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 extern HANDLE PyWin_DLLhModule;
381 /* static init of progpath ensures final char remains \0 */
382 if (PyWin_DLLhModule)
383 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
384 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000385#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000387#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
389 return;
Guido van Rossumeea14491997-08-13 21:30:44 +0000390#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (prog == NULL || *prog == '\0')
392 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 /* If there is no slash in the argv0 path, then we have to
395 * assume python is on the user's $PATH, since there's no
396 * other way to find a directory to start the search from. If
397 * $PATH isn't exported, you lose.
398 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000399#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000401#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000403#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 wcsncpy(progpath, prog, MAXPATHLEN);
405 else if (path) {
406 while (1) {
407 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (delim) {
410 size_t len = delim - path;
411 /* ensure we can't overwrite buffer */
412 len = min(MAXPATHLEN,len);
413 wcsncpy(progpath, path, len);
414 *(progpath + len) = '\0';
415 }
416 else
417 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 /* join() is safe for MAXPATHLEN+1 size buffer */
420 join(progpath, prog);
421 if (exists(progpath))
422 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (!delim) {
425 progpath[0] = '\0';
426 break;
427 }
428 path = delim + 1;
429 }
430 }
431 else
432 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000433}
434
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100435static int
436find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
437{
438 int result = 0; /* meaning not found */
439 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
440
441 fseek(env_file, 0, SEEK_SET);
442 while (!feof(env_file)) {
443 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
444 wchar_t tmpbuffer[MAXPATHLEN*2+1];
445 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200446 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100447
448 if (p == NULL)
449 break;
450 n = strlen(p);
451 if (p[n - 1] != '\n') {
452 /* line has overflowed - bail */
453 break;
454 }
455 if (p[0] == '#') /* Comment - skip */
456 continue;
457 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
458 if (decoded != NULL) {
459 Py_ssize_t k;
460 k = PyUnicode_AsWideChar(decoded,
461 tmpbuffer, MAXPATHLEN * 2);
462 Py_DECREF(decoded);
463 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800464 wchar_t * context = NULL;
465 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100466 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800467 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100468 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800469 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100470 if (tok != NULL) {
471 wcsncpy(value, tok, MAXPATHLEN);
472 result = 1;
473 break;
474 }
475 }
476 }
477 }
478 }
479 }
480 return result;
481}
482
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000483static void
Thomas Wouters78890102000-07-22 19:25:51 +0000484calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 wchar_t argv0_path[MAXPATHLEN+1];
487 wchar_t *buf;
488 size_t bufsz;
489 wchar_t *pythonhome = Py_GetPythonHome();
490 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000491
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000492#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 int skiphome, skipdefault;
494 wchar_t *machinepath = NULL;
495 wchar_t *userpath = NULL;
496 wchar_t zip_path[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700497 int applocal = 0;
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (!Py_IgnoreEnvironmentFlag) {
500 envpath = _wgetenv(L"PYTHONPATH");
501 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000502#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 char *_envpath = Py_GETENV("PYTHONPATH");
504 wchar_t wenvpath[MAXPATHLEN+1];
505 if (_envpath) {
506 size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
507 envpath = wenvpath;
508 if (r == (size_t)-1 || r >= MAXPATHLEN)
509 envpath = NULL;
510 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000511#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 get_progpath();
514 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700515 wcscpy_s(argv0_path, MAXPATHLEN+1, progpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 reduce(argv0_path);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100517
518 /* Search for an environment configuration file, first in the
519 executable's directory and then in the parent directory.
520 If found, open it for use when searching for prefixes.
521 */
522
523 {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700524 wchar_t envbuffer[MAXPATHLEN+1];
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100525 wchar_t tmpbuffer[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700526 const wchar_t *env_cfg = L"pyvenv.cfg";
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100527 FILE * env_file = NULL;
528
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700529 wcscpy_s(envbuffer, MAXPATHLEN+1, argv0_path);
530 join(envbuffer, env_cfg);
531 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100532 if (env_file == NULL) {
533 errno = 0;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700534 reduce(envbuffer);
535 reduce(envbuffer);
536 join(envbuffer, env_cfg);
537 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100538 if (env_file == NULL) {
539 errno = 0;
540 }
541 }
542 if (env_file != NULL) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700543 /* Look for an 'applocal' variable and, if true, ignore all registry
544 * keys and environment variables, but retain the default paths
545 * (DLLs, Lib) and the zip file. Setting pythonhome here suppresses
546 * the search for LANDMARK below and overrides %PYTHONHOME%.
547 */
548 if (find_env_config_value(env_file, L"applocal", tmpbuffer) &&
549 (applocal = (wcsicmp(tmpbuffer, L"true") == 0))) {
550 envpath = NULL;
551 pythonhome = argv0_path;
552 }
553
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100554 /* Look for a 'home' variable and set argv0_path to it, if found */
555 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700556 wcscpy_s(argv0_path, MAXPATHLEN+1, tmpbuffer);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100557 }
558 fclose(env_file);
559 env_file = NULL;
560 }
561 }
562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (pythonhome == NULL || *pythonhome == '\0') {
564 if (search_for_prefix(argv0_path, LANDMARK))
565 pythonhome = prefix;
566 else
567 pythonhome = NULL;
568 }
569 else
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700570 wcscpy_s(prefix, MAXPATHLEN+1, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (envpath && *envpath == '\0')
573 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000574
Guido van Rossume02e48b2000-03-29 01:49:47 +0000575
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000576#ifdef MS_WINDOWS
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700577 /* Calculate zip archive path from DLL or exe path */
578 if (wcscpy_s(zip_path, MAXPATHLEN+1, dllpath[0] ? dllpath : progpath))
579 /* exceeded buffer length - ignore zip_path */
580 zip_path[0] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 else {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700582 wchar_t *dot = wcsrchr(zip_path, '.');
583 if (!dot || wcscpy_s(dot, MAXPATHLEN+1 - (dot - zip_path), L".zip"))
584 /* exceeded buffer length - ignore zip_path */
585 zip_path[0] = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 }
587
588 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000589#ifdef Py_ENABLE_SHARED
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700590 if (!applocal) {
591 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
592 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
593 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000594#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* We only use the default relative PYTHONPATH if we havent
596 anything better to use! */
597 skipdefault = envpath!=NULL || pythonhome!=NULL || \
598 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000599#endif
600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /* We need to construct a path from the following parts.
602 (1) the PYTHONPATH environment variable, if set;
603 (2) for Win32, the zip archive file path;
604 (3) for Win32, the machinepath and userpath, if set;
605 (4) the PYTHONPATH config macro, with the leading "."
606 of each component replaced with pythonhome, if set;
607 (5) the directory containing the executable (argv0_path).
608 The length calculation calculates #4 first.
609 Extra rules:
610 - If PYTHONHOME is set (in any way) item (3) is ignored.
611 - If registry values are used, (4) and (5) are ignored.
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700612 - If applocal is set, (1), (3), and registry values are ignored
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* Calculate size of return buffer */
616 if (pythonhome != NULL) {
617 wchar_t *p;
618 bufsz = 1;
619 for (p = PYTHONPATH; *p; p++) {
620 if (*p == DELIM)
621 bufsz++; /* number of DELIM plus one */
622 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700623 bufsz *= wcslen(pythonhome);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
625 else
626 bufsz = 0;
Steve Dowerf64b9d52015-05-23 17:34:50 -0700627 bufsz += wcslen(PYTHONPATH) + 1;
628 bufsz += wcslen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000629#ifdef MS_WINDOWS
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700630 if (!applocal && userpath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700631 bufsz += wcslen(userpath) + 1;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700632 if (!applocal && machinepath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700633 bufsz += wcslen(machinepath) + 1;
634 bufsz += wcslen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000635#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (envpath != NULL)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700637 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000638
Victor Stinner1a7425f2013-07-07 16:25:15 +0200639 module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (buf == NULL) {
641 /* We can't exit, so print a warning and limp along */
642 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
643 if (envpath) {
644 fprintf(stderr, "Using environment $PYTHONPATH.\n");
645 module_search_path = envpath;
646 }
647 else {
648 fprintf(stderr, "Using default static path.\n");
649 module_search_path = PYTHONPATH;
650 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000651#ifdef MS_WINDOWS
Victor Stinner1a7425f2013-07-07 16:25:15 +0200652 PyMem_RawFree(machinepath);
653 PyMem_RawFree(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000654#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return;
656 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (envpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700659 if (wcscpy_s(buf, bufsz - (buf - module_search_path), envpath))
660 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 buf = wcschr(buf, L'\0');
662 *buf++ = DELIM;
663 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000664#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (zip_path[0]) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700666 if (wcscpy_s(buf, bufsz - (buf - module_search_path), zip_path))
667 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 buf = wcschr(buf, L'\0');
669 *buf++ = DELIM;
670 }
671 if (userpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700672 if (wcscpy_s(buf, bufsz - (buf - module_search_path), userpath))
673 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 buf = wcschr(buf, L'\0');
675 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200676 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 }
678 if (machinepath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700679 if (wcscpy_s(buf, bufsz - (buf - module_search_path), machinepath))
680 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 buf = wcschr(buf, L'\0');
682 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200683 PyMem_RawFree(machinepath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 }
685 if (pythonhome == NULL) {
686 if (!skipdefault) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700687 if (wcscpy_s(buf, bufsz - (buf - module_search_path), PYTHONPATH))
688 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700690 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 }
692 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000693#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 if (pythonhome == NULL) {
695 wcscpy(buf, PYTHONPATH);
696 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700697 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000699#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 else {
701 wchar_t *p = PYTHONPATH;
702 wchar_t *q;
703 size_t n;
704 for (;;) {
705 q = wcschr(p, DELIM);
706 if (q == NULL)
707 n = wcslen(p);
708 else
709 n = q-p;
710 if (p[0] == '.' && is_sep(p[1])) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700711 if (wcscpy_s(buf, bufsz - (buf - module_search_path), pythonhome))
712 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 buf = wcschr(buf, L'\0');
714 p++;
715 n--;
716 }
717 wcsncpy(buf, p, n);
718 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700719 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 if (q == NULL)
721 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 p = q+1;
723 }
724 }
725 if (argv0_path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 wcscpy(buf, argv0_path);
727 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700728 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700730 *(buf - 1) = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 /* Now to pull one last hack/trick. If sys.prefix is
732 empty, then try and find it somewhere on the paths
733 we calculated. We scan backwards, as our general policy
734 is that Python core directories are at the *end* of
735 sys.path. We assume that our "lib" directory is
736 on the path, and that our 'prefix' directory is
737 the parent of that.
738 */
739 if (*prefix==L'\0') {
740 wchar_t lookBuf[MAXPATHLEN+1];
741 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
742 while (1) {
743 Py_ssize_t nchars;
744 wchar_t *lookEnd = look;
745 /* 'look' will end up one character before the
746 start of the path in question - even if this
747 is one character before the start of the buffer
748 */
749 while (look >= module_search_path && *look != DELIM)
750 look--;
751 nchars = lookEnd-look;
752 wcsncpy(lookBuf, look+1, nchars);
753 lookBuf[nchars] = L'\0';
754 /* Up one level to the parent */
755 reduce(lookBuf);
756 if (search_for_prefix(lookBuf, LANDMARK)) {
757 break;
758 }
759 /* If we are out of paths to search - give up */
760 if (look < module_search_path)
761 break;
762 look--;
763 }
764 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000765}
766
767
768/* External interface */
769
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000770void
771Py_SetPath(const wchar_t *path)
772{
773 if (module_search_path != NULL) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200774 PyMem_RawFree(module_search_path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000775 module_search_path = NULL;
776 }
777 if (path != NULL) {
778 extern wchar_t *Py_GetProgramName(void);
779 wchar_t *prog = Py_GetProgramName();
780 wcsncpy(progpath, prog, MAXPATHLEN);
781 prefix[0] = L'\0';
Victor Stinner1a7425f2013-07-07 16:25:15 +0200782 module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000783 if (module_search_path != NULL)
784 wcscpy(module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200785 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000786}
787
Martin v. Löwis790465f2008-04-05 20:41:37 +0000788wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000789Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (!module_search_path)
792 calculate_path();
793 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000794}
795
Martin v. Löwis790465f2008-04-05 20:41:37 +0000796wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000797Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 if (!module_search_path)
800 calculate_path();
801 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000802}
803
Martin v. Löwis790465f2008-04-05 20:41:37 +0000804wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000805Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000808}
809
Martin v. Löwis790465f2008-04-05 20:41:37 +0000810wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000811Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (!module_search_path)
814 calculate_path();
815 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000816}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000817
Victor Stinner63941882011-09-29 00:42:28 +0200818/* Load python3.dll before loading any extension module that might refer
819 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000820 to this python DLL is loaded, not a python3.dll that might be on the path
821 by chance.
822 Return whether the DLL was found.
823*/
824static int python3_checked = 0;
825static HANDLE hPython3;
826int
827_Py_CheckPython3()
828{
829 wchar_t py3path[MAXPATHLEN+1];
830 wchar_t *s;
831 if (python3_checked)
832 return hPython3 != NULL;
833 python3_checked = 1;
834
835 /* If there is a python3.dll next to the python3y.dll,
836 assume this is a build tree; use that DLL */
837 wcscpy(py3path, dllpath);
838 s = wcsrchr(py3path, L'\\');
839 if (!s)
840 s = py3path;
841 wcscpy(s, L"\\python3.dll");
842 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
843 if (hPython3 != NULL)
844 return 1;
845
846 /* Check sys.prefix\DLLs\python3.dll */
847 wcscpy(py3path, Py_GetPrefix());
848 wcscat(py3path, L"\\DLLs\\python3.dll");
849 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
850 return hPython3 != NULL;
851}