blob: cb9f1db488453ccc5166fc0ab52cb3fda8151018 [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{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100138 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700139
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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 /* Copy our collected strings */
325 for (index=0;index<numKeys;index++) {
326 if (index > 0) {
327 *(szCur++) = L';';
328 dataSize--;
329 }
330 if (ppPaths[index]) {
331 Py_ssize_t len = wcslen(ppPaths[index]);
332 wcsncpy(szCur, ppPaths[index], len);
333 szCur += len;
334 assert(dataSize > (DWORD)len);
335 dataSize -= (DWORD)len;
336 }
337 }
338 if (skipcore)
339 *szCur = '\0';
340 else {
341 /* If we have no values, we dont need a ';' */
342 if (numKeys) {
343 *(szCur++) = L';';
344 dataSize--;
345 }
346 /* Now append the core path entries -
347 this will include the NULL
348 */
349 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
350 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200351 if (rc != ERROR_SUCCESS) {
352 PyMem_RawFree(dataBuf);
353 goto done;
354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 }
356 /* And set the result - caller must free */
357 retval = dataBuf;
358 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000359done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 /* Loop freeing my temp buffers */
361 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200362 for(index=0; index<numKeys; index++)
363 PyMem_RawFree(ppPaths[index]);
364 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 }
366 if (newKey)
367 RegCloseKey(newKey);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200368 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000370}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000371#endif /* Py_ENABLE_SHARED */
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000372#endif /* MS_WINDOWS */
Guido van Rossumeea14491997-08-13 21:30:44 +0000373
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000374static void
Thomas Wouters78890102000-07-22 19:25:51 +0000375get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 extern wchar_t *Py_GetProgramName(void);
378 wchar_t *path = _wgetenv(L"PATH");
379 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000380
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000381#ifdef MS_WINDOWS
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000382#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 extern HANDLE PyWin_DLLhModule;
384 /* static init of progpath ensures final char remains \0 */
385 if (PyWin_DLLhModule)
386 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
387 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000388#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000390#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
392 return;
Guido van Rossumeea14491997-08-13 21:30:44 +0000393#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (prog == NULL || *prog == '\0')
395 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* If there is no slash in the argv0 path, then we have to
398 * assume python is on the user's $PATH, since there's no
399 * other way to find a directory to start the search from. If
400 * $PATH isn't exported, you lose.
401 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000402#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000404#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000406#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 wcsncpy(progpath, prog, MAXPATHLEN);
408 else if (path) {
409 while (1) {
410 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (delim) {
413 size_t len = delim - path;
414 /* ensure we can't overwrite buffer */
415 len = min(MAXPATHLEN,len);
416 wcsncpy(progpath, path, len);
417 *(progpath + len) = '\0';
418 }
419 else
420 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 /* join() is safe for MAXPATHLEN+1 size buffer */
423 join(progpath, prog);
424 if (exists(progpath))
425 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (!delim) {
428 progpath[0] = '\0';
429 break;
430 }
431 path = delim + 1;
432 }
433 }
434 else
435 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000436}
437
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100438static int
439find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
440{
441 int result = 0; /* meaning not found */
442 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
443
444 fseek(env_file, 0, SEEK_SET);
445 while (!feof(env_file)) {
446 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
447 wchar_t tmpbuffer[MAXPATHLEN*2+1];
448 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200449 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100450
451 if (p == NULL)
452 break;
453 n = strlen(p);
454 if (p[n - 1] != '\n') {
455 /* line has overflowed - bail */
456 break;
457 }
458 if (p[0] == '#') /* Comment - skip */
459 continue;
460 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
461 if (decoded != NULL) {
462 Py_ssize_t k;
463 k = PyUnicode_AsWideChar(decoded,
464 tmpbuffer, MAXPATHLEN * 2);
465 Py_DECREF(decoded);
466 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800467 wchar_t * context = NULL;
468 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100469 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800470 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100471 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800472 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100473 if (tok != NULL) {
474 wcsncpy(value, tok, MAXPATHLEN);
475 result = 1;
476 break;
477 }
478 }
479 }
480 }
481 }
482 }
483 return result;
484}
485
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000486static void
Thomas Wouters78890102000-07-22 19:25:51 +0000487calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 wchar_t argv0_path[MAXPATHLEN+1];
490 wchar_t *buf;
491 size_t bufsz;
492 wchar_t *pythonhome = Py_GetPythonHome();
493 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000494
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000495#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 int skiphome, skipdefault;
497 wchar_t *machinepath = NULL;
498 wchar_t *userpath = NULL;
499 wchar_t zip_path[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700500 int applocal = 0;
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (!Py_IgnoreEnvironmentFlag) {
503 envpath = _wgetenv(L"PYTHONPATH");
504 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000505#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 char *_envpath = Py_GETENV("PYTHONPATH");
507 wchar_t wenvpath[MAXPATHLEN+1];
508 if (_envpath) {
509 size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1);
510 envpath = wenvpath;
511 if (r == (size_t)-1 || r >= MAXPATHLEN)
512 envpath = NULL;
513 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000514#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 get_progpath();
517 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700518 wcscpy_s(argv0_path, MAXPATHLEN+1, progpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 reduce(argv0_path);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100520
521 /* Search for an environment configuration file, first in the
522 executable's directory and then in the parent directory.
523 If found, open it for use when searching for prefixes.
524 */
525
526 {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700527 wchar_t envbuffer[MAXPATHLEN+1];
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100528 wchar_t tmpbuffer[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700529 const wchar_t *env_cfg = L"pyvenv.cfg";
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100530 FILE * env_file = NULL;
531
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700532 wcscpy_s(envbuffer, MAXPATHLEN+1, argv0_path);
533 join(envbuffer, env_cfg);
534 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100535 if (env_file == NULL) {
536 errno = 0;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700537 reduce(envbuffer);
538 reduce(envbuffer);
539 join(envbuffer, env_cfg);
540 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100541 if (env_file == NULL) {
542 errno = 0;
543 }
544 }
545 if (env_file != NULL) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700546 /* Look for an 'applocal' variable and, if true, ignore all registry
547 * keys and environment variables, but retain the default paths
548 * (DLLs, Lib) and the zip file. Setting pythonhome here suppresses
549 * the search for LANDMARK below and overrides %PYTHONHOME%.
550 */
551 if (find_env_config_value(env_file, L"applocal", tmpbuffer) &&
552 (applocal = (wcsicmp(tmpbuffer, L"true") == 0))) {
553 envpath = NULL;
554 pythonhome = argv0_path;
555 }
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100556
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100557 /* Look for a 'home' variable and set argv0_path to it, if found */
558 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700559 wcscpy_s(argv0_path, MAXPATHLEN+1, tmpbuffer);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100560 }
561 fclose(env_file);
562 env_file = NULL;
563 }
564 }
565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 if (pythonhome == NULL || *pythonhome == '\0') {
567 if (search_for_prefix(argv0_path, LANDMARK))
568 pythonhome = prefix;
569 else
570 pythonhome = NULL;
571 }
572 else
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700573 wcscpy_s(prefix, MAXPATHLEN+1, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 if (envpath && *envpath == '\0')
576 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000577
Guido van Rossume02e48b2000-03-29 01:49:47 +0000578
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000579#ifdef MS_WINDOWS
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700580 /* Calculate zip archive path from DLL or exe path */
581 if (wcscpy_s(zip_path, MAXPATHLEN+1, dllpath[0] ? dllpath : progpath))
582 /* exceeded buffer length - ignore zip_path */
583 zip_path[0] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 else {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700585 wchar_t *dot = wcsrchr(zip_path, '.');
586 if (!dot || wcscpy_s(dot, MAXPATHLEN+1 - (dot - zip_path), L".zip"))
587 /* exceeded buffer length - ignore zip_path */
588 zip_path[0] = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 }
590
591 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000592#ifdef Py_ENABLE_SHARED
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700593 if (!applocal) {
594 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
595 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
596 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000597#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* We only use the default relative PYTHONPATH if we havent
599 anything better to use! */
600 skipdefault = envpath!=NULL || pythonhome!=NULL || \
601 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000602#endif
603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* We need to construct a path from the following parts.
605 (1) the PYTHONPATH environment variable, if set;
606 (2) for Win32, the zip archive file path;
607 (3) for Win32, the machinepath and userpath, if set;
608 (4) the PYTHONPATH config macro, with the leading "."
609 of each component replaced with pythonhome, if set;
610 (5) the directory containing the executable (argv0_path).
611 The length calculation calculates #4 first.
612 Extra rules:
613 - If PYTHONHOME is set (in any way) item (3) is ignored.
614 - If registry values are used, (4) and (5) are ignored.
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700615 - If applocal is set, (1), (3), and registry values are ignored
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* Calculate size of return buffer */
619 if (pythonhome != NULL) {
620 wchar_t *p;
621 bufsz = 1;
622 for (p = PYTHONPATH; *p; p++) {
623 if (*p == DELIM)
624 bufsz++; /* number of DELIM plus one */
625 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700626 bufsz *= wcslen(pythonhome);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 }
628 else
629 bufsz = 0;
Steve Dowerf64b9d52015-05-23 17:34:50 -0700630 bufsz += wcslen(PYTHONPATH) + 1;
631 bufsz += wcslen(argv0_path) + 1;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000632#ifdef MS_WINDOWS
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700633 if (!applocal && userpath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700634 bufsz += wcslen(userpath) + 1;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700635 if (!applocal && machinepath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700636 bufsz += wcslen(machinepath) + 1;
637 bufsz += wcslen(zip_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000638#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 if (envpath != NULL)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700640 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000641
Victor Stinner1a7425f2013-07-07 16:25:15 +0200642 module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 if (buf == NULL) {
644 /* We can't exit, so print a warning and limp along */
645 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
646 if (envpath) {
647 fprintf(stderr, "Using environment $PYTHONPATH.\n");
648 module_search_path = envpath;
649 }
650 else {
651 fprintf(stderr, "Using default static path.\n");
652 module_search_path = PYTHONPATH;
653 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000654#ifdef MS_WINDOWS
Victor Stinner1a7425f2013-07-07 16:25:15 +0200655 PyMem_RawFree(machinepath);
656 PyMem_RawFree(userpath);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000657#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 return;
659 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (envpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700662 if (wcscpy_s(buf, bufsz - (buf - module_search_path), envpath))
663 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 buf = wcschr(buf, L'\0');
665 *buf++ = DELIM;
666 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000667#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (zip_path[0]) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700669 if (wcscpy_s(buf, bufsz - (buf - module_search_path), zip_path))
670 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 buf = wcschr(buf, L'\0');
672 *buf++ = DELIM;
673 }
674 if (userpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700675 if (wcscpy_s(buf, bufsz - (buf - module_search_path), userpath))
676 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 buf = wcschr(buf, L'\0');
678 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200679 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 }
681 if (machinepath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700682 if (wcscpy_s(buf, bufsz - (buf - module_search_path), machinepath))
683 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 buf = wcschr(buf, L'\0');
685 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200686 PyMem_RawFree(machinepath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 }
688 if (pythonhome == NULL) {
689 if (!skipdefault) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700690 if (wcscpy_s(buf, bufsz - (buf - module_search_path), PYTHONPATH))
691 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700693 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 }
695 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000696#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 if (pythonhome == NULL) {
698 wcscpy(buf, PYTHONPATH);
699 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700700 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000702#endif /* MS_WINDOWS */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 else {
704 wchar_t *p = PYTHONPATH;
705 wchar_t *q;
706 size_t n;
707 for (;;) {
708 q = wcschr(p, DELIM);
709 if (q == NULL)
710 n = wcslen(p);
711 else
712 n = q-p;
713 if (p[0] == '.' && is_sep(p[1])) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700714 if (wcscpy_s(buf, bufsz - (buf - module_search_path), pythonhome))
715 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 buf = wcschr(buf, L'\0');
717 p++;
718 n--;
719 }
720 wcsncpy(buf, p, n);
721 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700722 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 if (q == NULL)
724 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 p = q+1;
726 }
727 }
728 if (argv0_path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 wcscpy(buf, argv0_path);
730 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700731 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700733 *(buf - 1) = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* Now to pull one last hack/trick. If sys.prefix is
735 empty, then try and find it somewhere on the paths
736 we calculated. We scan backwards, as our general policy
737 is that Python core directories are at the *end* of
738 sys.path. We assume that our "lib" directory is
739 on the path, and that our 'prefix' directory is
740 the parent of that.
741 */
742 if (*prefix==L'\0') {
743 wchar_t lookBuf[MAXPATHLEN+1];
744 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
745 while (1) {
746 Py_ssize_t nchars;
747 wchar_t *lookEnd = look;
748 /* 'look' will end up one character before the
749 start of the path in question - even if this
750 is one character before the start of the buffer
751 */
752 while (look >= module_search_path && *look != DELIM)
753 look--;
754 nchars = lookEnd-look;
755 wcsncpy(lookBuf, look+1, nchars);
756 lookBuf[nchars] = L'\0';
757 /* Up one level to the parent */
758 reduce(lookBuf);
759 if (search_for_prefix(lookBuf, LANDMARK)) {
760 break;
761 }
762 /* If we are out of paths to search - give up */
763 if (look < module_search_path)
764 break;
765 look--;
766 }
767 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000768}
769
770
771/* External interface */
772
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000773void
774Py_SetPath(const wchar_t *path)
775{
776 if (module_search_path != NULL) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200777 PyMem_RawFree(module_search_path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000778 module_search_path = NULL;
779 }
780 if (path != NULL) {
781 extern wchar_t *Py_GetProgramName(void);
782 wchar_t *prog = Py_GetProgramName();
783 wcsncpy(progpath, prog, MAXPATHLEN);
784 prefix[0] = L'\0';
Victor Stinner1a7425f2013-07-07 16:25:15 +0200785 module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000786 if (module_search_path != NULL)
787 wcscpy(module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200788 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000789}
790
Martin v. Löwis790465f2008-04-05 20:41:37 +0000791wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000792Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (!module_search_path)
795 calculate_path();
796 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000797}
798
Martin v. Löwis790465f2008-04-05 20:41:37 +0000799wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000800Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 if (!module_search_path)
803 calculate_path();
804 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000805}
806
Martin v. Löwis790465f2008-04-05 20:41:37 +0000807wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000808Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000811}
812
Martin v. Löwis790465f2008-04-05 20:41:37 +0000813wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000814Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 if (!module_search_path)
817 calculate_path();
818 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000819}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000820
Victor Stinner63941882011-09-29 00:42:28 +0200821/* Load python3.dll before loading any extension module that might refer
822 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000823 to this python DLL is loaded, not a python3.dll that might be on the path
824 by chance.
825 Return whether the DLL was found.
826*/
827static int python3_checked = 0;
828static HANDLE hPython3;
829int
830_Py_CheckPython3()
831{
832 wchar_t py3path[MAXPATHLEN+1];
833 wchar_t *s;
834 if (python3_checked)
835 return hPython3 != NULL;
836 python3_checked = 1;
837
838 /* If there is a python3.dll next to the python3y.dll,
839 assume this is a build tree; use that DLL */
840 wcscpy(py3path, dllpath);
841 s = wcsrchr(py3path, L'\\');
842 if (!s)
843 s = py3path;
844 wcscpy(s, L"\\python3.dll");
845 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
846 if (hPython3 != NULL)
847 return 1;
848
849 /* Check sys.prefix\DLLs\python3.dll */
850 wcscpy(py3path, Py_GetPrefix());
851 wcscat(py3path, L"\\DLLs\\python3.dll");
852 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
853 return hPython3 != NULL;
854}