blob: c05754a29ede2acf12c54ac51fc10b51195759d3 [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
Steve Dower4db86bc2016-09-09 09:17:35 -07009 are actually fetched is different). The presence of a sys.path file
10 alongside the program overrides these rules - see below.
Guido van Rossum88716bb2000-03-30 19:45:39 +000011
12 * Python always adds an empty entry at the start, which corresponds
13 to the current directory.
14
Georg Brandl7eb4b7d2005-07-22 21:49:32 +000015 * If the PYTHONPATH env. var. exists, its entries are added next.
Guido van Rossum88716bb2000-03-30 19:45:39 +000016
17 * We look in the registry for "application paths" - that is, sub-keys
18 under the main PythonPath registry key. These are added next (the
19 order of sub-key processing is undefined).
20 HKEY_CURRENT_USER is searched and added first.
21 HKEY_LOCAL_MACHINE is searched and added next.
22 (Note that all known installers only use HKLM, so HKCU is typically
23 empty)
24
25 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
26 is set, we believe it. Otherwise, we use the path of our host .EXE's
Martin Panterfd13c0f2016-09-10 10:45:28 +000027 to try and locate one of our "landmarks" and deduce our home.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 - If we DO have a Python Home: The relevant sub-directories (Lib,
Zachary Warec4b53af2016-09-09 17:59:49 -070029 DLLs, etc) are based on the Python Home
Guido van Rossum88716bb2000-03-30 19:45:39 +000030 - If we DO NOT have a Python Home, the core Python Path is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 loaded from the registry. This is the main PythonPath key,
Guido van Rossum88716bb2000-03-30 19:45:39 +000032 and both HKLM and HKCU are combined to form the path)
33
34 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
35 specified, and can't locate any Registry entries (ie, we have _nothing_
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 we can assume is a good path), a default path with relative entries is
Zachary Warec4b53af2016-09-09 17:59:49 -070037 used (eg. .\Lib;.\DLLs, etc)
Guido van Rossum88716bb2000-03-30 19:45:39 +000038
39
Steve Dower4db86bc2016-09-09 09:17:35 -070040 If a sys.path file exists adjacent to python.exe, it must contain a
41 list of paths to add to sys.path, one per line (like a .pth file but without
42 the ability to execute arbitrary code). Each path is relative to the
43 directory containing the file. No other paths are added to the search path,
44 and the registry finder is not enabled.
45
Guido van Rossum88716bb2000-03-30 19:45:39 +000046 The end result of all this is:
47 * When running python.exe, or any other .exe in the main Python directory
48 (either an installed version, or directly from the PCbuild directory),
49 the core path is deduced, and the core paths in the registry are
50 ignored. Other "application paths" in the registry are always read.
51
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 * When Python is hosted in another exe (different directory, embedded via
Guido van Rossum88716bb2000-03-30 19:45:39 +000053 COM, etc), the Python Home will not be deduced, so the core path from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 the registry is used. Other "application paths" in the registry are
Guido van Rossum88716bb2000-03-30 19:45:39 +000055 always read.
56
57 * If Python can't find its home and there is no registry (eg, frozen
58 exe, some very strange installation setup) you get a path with
59 some default, but relative, paths.
60
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000061 * An embedding application can use Py_SetPath() to override all of
Steve Dower4db86bc2016-09-09 09:17:35 -070062 these automatic path computations.
63
64 * An isolation install of Python can disable all implicit paths by
65 providing a sys.path file.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000066
Guido van Rossum88716bb2000-03-30 19:45:39 +000067 ---------------------------------------------------------------- */
68
69
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000070#include "Python.h"
71#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000072#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000073
Steve Dower4db86bc2016-09-09 09:17:35 -070074#ifndef MS_WINDOWS
75#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000076#endif
77
Steve Dower4db86bc2016-09-09 09:17:35 -070078#include <windows.h>
79#include <Shlwapi.h>
80
Thomas Wouters0e3f5912006-08-11 14:57:12 +000081#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000082#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083#endif /* HAVE_SYS_TYPES_H */
84
85#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000086#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087#endif /* HAVE_SYS_STAT_H */
88
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000089#include <string.h>
90
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000091/* Search in some common locations for the associated Python libraries.
92 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000093 * Py_GetPath() tries to return a sensible Python module search path.
94 *
Guido van Rossum42a97441998-02-19 21:00:45 +000095 * The approach is an adaptation for Windows of the strategy used in
96 * ../Modules/getpath.c; it uses the Windows Registry as one of its
97 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000098 *
99 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
100 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101 */
102
103#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000104#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105#endif
106
Martin v. Löwis790465f2008-04-05 20:41:37 +0000107static wchar_t prefix[MAXPATHLEN+1];
108static wchar_t progpath[MAXPATHLEN+1];
109static wchar_t dllpath[MAXPATHLEN+1];
110static wchar_t *module_search_path = NULL;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000111
Guido van Rossumeea14491997-08-13 21:30:44 +0000112
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114is_sep(wchar_t ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115{
116#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000118#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000120#endif
121}
122
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000123/* assumes 'dir' null terminated in bounds. Never writes
124 beyond existing terminator.
125*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000126static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000127reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000128{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700129 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
130 if (i >= MAXPATHLEN+1)
131 Py_FatalError("buffer overflow in getpathp.c's reduce()");
132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 while (i > 0 && !is_sep(dir[i]))
134 --i;
135 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000136}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138
139static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000140exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000143}
144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145/* Assumes 'filename' MAXPATHLEN+1 bytes long -
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000146 may extend 'filename' by one character.
147*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000148static int
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700149ismodule(wchar_t *filename, int update_filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000150{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100151 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 if (exists(filename))
154 return 1;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700157 n = wcsnlen_s(filename, MAXPATHLEN+1);
158 if (n < MAXPATHLEN) {
159 int exist = 0;
160 filename[n] = Py_OptimizeFlag ? L'o' : L'c';
161 filename[n + 1] = L'\0';
162 exist = exists(filename);
163 if (!update_filename)
164 filename[n] = L'\0';
165 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 }
167 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000168}
169
Tim Peters8484fbf2004-08-07 19:12:27 +0000170/* Add a path component, by appending stuff to buffer.
171 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
172 NUL-terminated string with no more than MAXPATHLEN characters (not counting
173 the trailing NUL). It's a fatal error if it contains a string longer than
174 that (callers must be careful!). If these requirements are met, it's
175 guaranteed that buffer will still be a NUL-terminated string with no more
176 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
177 stuff as fits will be appended.
178*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700179
180static int _PathCchCombineEx_Initialized = 0;
181typedef HRESULT(__stdcall *PPathCchCombineEx)(PWSTR pszPathOut, size_t cchPathOut, PCWSTR pszPathIn, PCWSTR pszMore, unsigned long dwFlags);
182static PPathCchCombineEx _PathCchCombineEx;
183
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000184static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700185join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000186{
Steve Dower4db86bc2016-09-09 09:17:35 -0700187 if (_PathCchCombineEx_Initialized == 0) {
188 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
189 if (pathapi)
190 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
191 else
192 _PathCchCombineEx = NULL;
193 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700195
Steve Dower4db86bc2016-09-09 09:17:35 -0700196 if (_PathCchCombineEx) {
197 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0)))
198 Py_FatalError("buffer overflow in getpathp.c's join()");
199 } else {
Steve Dower3ceb5732016-09-09 15:53:58 -0700200 if (!PathCombineW(buffer, buffer, stuff))
Steve Dower4db86bc2016-09-09 09:17:35 -0700201 Py_FatalError("buffer overflow in getpathp.c's join()");
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700202 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000203}
204
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000205/* gotlandmark only called by search_for_prefix, which ensures
206 'prefix' is null terminated in bounds. join() ensures
207 'landmark' can not overflow prefix if too long.
208*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000209static int
Steve Dower50289382016-09-09 14:22:43 -0700210gotlandmark(const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700213 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700216 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 prefix[n] = '\0';
218 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000219}
220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000222 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000223static int
Steve Dower50289382016-09-09 14:22:43 -0700224search_for_prefix(wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700227 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 do {
229 if (gotlandmark(landmark))
230 return 1;
231 reduce(prefix);
232 } while (prefix[0]);
233 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000234}
235
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000236#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000237
Guido van Rossum88716bb2000-03-30 19:45:39 +0000238/* a string loaded from the DLL at startup.*/
239extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000240
Guido van Rossumeea14491997-08-13 21:30:44 +0000241
242/* Load a PYTHONPATH value from the registry.
243 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
244
Guido van Rossum88716bb2000-03-30 19:45:39 +0000245 Works in both Unicode and 8bit environments. Only uses the
246 Ex family of functions so it also works with Windows CE.
247
Guido van Rossumeea14491997-08-13 21:30:44 +0000248 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000249
250 XXX - this code is pretty strange, as it used to also
251 work on Win16, where the buffer sizes werent available
252 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000253*/
254
Martin v. Löwis790465f2008-04-05 20:41:37 +0000255static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000256getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 HKEY newKey = 0;
259 DWORD dataSize = 0;
260 DWORD numKeys = 0;
261 LONG rc;
262 wchar_t *retval = NULL;
263 WCHAR *dataBuf = NULL;
264 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
265 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700266 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 DWORD index;
268 WCHAR *keyBuf = NULL;
269 WCHAR *keyBufPtr;
270 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 /* Tried to use sysget("winver") but here is too early :-( */
273 versionLen = strlen(PyWin_DLLVersionString);
274 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700275 keyBufLen = sizeof(keyPrefix) +
276 sizeof(WCHAR)*(versionLen-1) +
277 sizeof(keySuffix);
278 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 if (keyBuf==NULL) goto done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700281 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200282 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
284 keyBufPtr += versionLen;
285 /* NULL comes with this one! */
286 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
287 /* Open the root Python key */
288 rc=RegOpenKeyExW(keyBase,
289 keyBuf, /* subkey */
290 0, /* reserved */
291 KEY_READ,
292 &newKey);
293 if (rc!=ERROR_SUCCESS) goto done;
294 /* Find out how big our core buffer is, and how many subkeys we have */
295 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
296 NULL, NULL, &dataSize, NULL, NULL);
297 if (rc!=ERROR_SUCCESS) goto done;
298 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
299 /* Allocate a temp array of char buffers, so we only need to loop
300 reading the registry once
301 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200302 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (ppPaths==NULL) goto done;
304 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
305 /* Loop over all subkeys, allocating a temp sub-buffer. */
306 for(index=0;index<numKeys;index++) {
307 WCHAR keyBuf[MAX_PATH+1];
308 HKEY subKey = 0;
309 DWORD reqdSize = MAX_PATH+1;
310 /* Get the sub-key name */
311 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
312 NULL, NULL, NULL, NULL );
313 if (rc!=ERROR_SUCCESS) goto done;
314 /* Open the sub-key */
315 rc=RegOpenKeyExW(newKey,
316 keyBuf, /* subkey */
317 0, /* reserved */
318 KEY_READ,
319 &subKey);
320 if (rc!=ERROR_SUCCESS) goto done;
321 /* Find the value of the buffer size, malloc, then read it */
322 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
323 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200324 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (ppPaths[index]) {
326 RegQueryValueExW(subKey, NULL, 0, NULL,
327 (LPBYTE)ppPaths[index],
328 &reqdSize);
329 dataSize += reqdSize + 1; /* 1 for the ";" */
330 }
331 }
332 RegCloseKey(subKey);
333 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* return null if no path to return */
336 if (dataSize == 0) goto done;
337
338 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200339 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (dataBuf) {
341 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* Copy our collected strings */
343 for (index=0;index<numKeys;index++) {
344 if (index > 0) {
345 *(szCur++) = L';';
346 dataSize--;
347 }
348 if (ppPaths[index]) {
349 Py_ssize_t len = wcslen(ppPaths[index]);
350 wcsncpy(szCur, ppPaths[index], len);
351 szCur += len;
352 assert(dataSize > (DWORD)len);
353 dataSize -= (DWORD)len;
354 }
355 }
356 if (skipcore)
357 *szCur = '\0';
358 else {
359 /* If we have no values, we dont need a ';' */
360 if (numKeys) {
361 *(szCur++) = L';';
362 dataSize--;
363 }
364 /* Now append the core path entries -
365 this will include the NULL
366 */
367 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
368 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200369 if (rc != ERROR_SUCCESS) {
370 PyMem_RawFree(dataBuf);
371 goto done;
372 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 }
374 /* And set the result - caller must free */
375 retval = dataBuf;
376 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000377done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 /* Loop freeing my temp buffers */
379 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200380 for(index=0; index<numKeys; index++)
381 PyMem_RawFree(ppPaths[index]);
382 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 }
384 if (newKey)
385 RegCloseKey(newKey);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200386 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000388}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000389#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000390
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000391static void
Thomas Wouters78890102000-07-22 19:25:51 +0000392get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 extern wchar_t *Py_GetProgramName(void);
395 wchar_t *path = _wgetenv(L"PATH");
396 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000397
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000398#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 extern HANDLE PyWin_DLLhModule;
400 /* static init of progpath ensures final char remains \0 */
401 if (PyWin_DLLhModule)
402 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
403 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000404#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000406#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
408 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 if (prog == NULL || *prog == '\0')
410 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* If there is no slash in the argv0 path, then we have to
413 * assume python is on the user's $PATH, since there's no
414 * other way to find a directory to start the search from. If
415 * $PATH isn't exported, you lose.
416 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000417#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000419#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000421#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 wcsncpy(progpath, prog, MAXPATHLEN);
423 else if (path) {
424 while (1) {
425 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (delim) {
428 size_t len = delim - path;
429 /* ensure we can't overwrite buffer */
430 len = min(MAXPATHLEN,len);
431 wcsncpy(progpath, path, len);
432 *(progpath + len) = '\0';
433 }
434 else
435 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* join() is safe for MAXPATHLEN+1 size buffer */
438 join(progpath, prog);
439 if (exists(progpath))
440 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (!delim) {
443 progpath[0] = '\0';
444 break;
445 }
446 path = delim + 1;
447 }
448 }
449 else
450 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000451}
452
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100453static int
454find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
455{
456 int result = 0; /* meaning not found */
457 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
458
459 fseek(env_file, 0, SEEK_SET);
460 while (!feof(env_file)) {
461 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
462 wchar_t tmpbuffer[MAXPATHLEN*2+1];
463 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200464 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100465
466 if (p == NULL)
467 break;
468 n = strlen(p);
469 if (p[n - 1] != '\n') {
470 /* line has overflowed - bail */
471 break;
472 }
473 if (p[0] == '#') /* Comment - skip */
474 continue;
475 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
476 if (decoded != NULL) {
477 Py_ssize_t k;
478 k = PyUnicode_AsWideChar(decoded,
479 tmpbuffer, MAXPATHLEN * 2);
480 Py_DECREF(decoded);
481 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800482 wchar_t * context = NULL;
483 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100484 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800485 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100486 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800487 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100488 if (tok != NULL) {
489 wcsncpy(value, tok, MAXPATHLEN);
490 result = 1;
491 break;
492 }
493 }
494 }
495 }
496 }
497 }
498 return result;
499}
500
Steve Dower4db86bc2016-09-09 09:17:35 -0700501static int
502read_sys_path_file(const wchar_t *path, const wchar_t *prefix)
503{
504 FILE *sp_file = _Py_wfopen(path, L"r");
505 if (sp_file == NULL)
506 return -1;
507
508 size_t bufsiz = MAXPATHLEN;
509 size_t prefixlen = wcslen(prefix);
510
511 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
512 buf[0] = '\0';
513
514 while (!feof(sp_file)) {
515 char line[MAXPATHLEN + 1];
516 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
517 if (!p)
518 break;
519
520 DWORD n = strlen(line);
521 if (n == 0 || p[n - 1] != '\n')
522 break;
523 if (n > 2 && p[n - 1] == '\r')
524 --n;
525
526 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, n - 1, NULL, 0);
527 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
528 wn = MultiByteToWideChar(CP_UTF8, 0, line, n - 1, wline, wn);
529 wline[wn] = '\0';
530
531 while (wn + prefixlen + 4 > bufsiz) {
532 bufsiz += MAXPATHLEN;
533 buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));
534 if (!buf) {
535 PyMem_RawFree(wline);
536 goto error;
537 }
538 }
539
540 if (buf[0])
541 wcscat_s(buf, bufsiz, L";");
542 wchar_t *b = &buf[wcslen(buf)];
543
544 wcscat_s(buf, bufsiz, prefix);
545 join(b, wline);
546
547 PyMem_RawFree(wline);
548 }
549
550 module_search_path = buf;
551
552 fclose(sp_file);
553 return 0;
554
555error:
556 PyMem_RawFree(buf);
557 fclose(sp_file);
558 return -1;
559}
560
561
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000562static void
Thomas Wouters78890102000-07-22 19:25:51 +0000563calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 wchar_t argv0_path[MAXPATHLEN+1];
566 wchar_t *buf;
567 size_t bufsz;
568 wchar_t *pythonhome = Py_GetPythonHome();
569 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 int skiphome, skipdefault;
572 wchar_t *machinepath = NULL;
573 wchar_t *userpath = NULL;
574 wchar_t zip_path[MAXPATHLEN+1];
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (!Py_IgnoreEnvironmentFlag) {
577 envpath = _wgetenv(L"PYTHONPATH");
578 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 get_progpath();
581 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700582 wcscpy_s(argv0_path, MAXPATHLEN+1, progpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 reduce(argv0_path);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100584
Steve Dower4db86bc2016-09-09 09:17:35 -0700585 /* Search for a sys.path file */
586 {
587 wchar_t spbuffer[MAXPATHLEN+1];
588
589 wcscpy_s(spbuffer, MAXPATHLEN+1, argv0_path);
590 join(spbuffer, L"sys.path");
591 if (exists(spbuffer) && read_sys_path_file(spbuffer, argv0_path) == 0) {
592 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
593 Py_IsolatedFlag = 1;
594 Py_NoSiteFlag = 1;
595 return;
596 }
597 }
598
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100599 /* Search for an environment configuration file, first in the
600 executable's directory and then in the parent directory.
601 If found, open it for use when searching for prefixes.
602 */
603
604 {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700605 wchar_t envbuffer[MAXPATHLEN+1];
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100606 wchar_t tmpbuffer[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700607 const wchar_t *env_cfg = L"pyvenv.cfg";
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100608 FILE * env_file = NULL;
609
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700610 wcscpy_s(envbuffer, MAXPATHLEN+1, argv0_path);
611 join(envbuffer, env_cfg);
612 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100613 if (env_file == NULL) {
614 errno = 0;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700615 reduce(envbuffer);
616 reduce(envbuffer);
617 join(envbuffer, env_cfg);
618 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100619 if (env_file == NULL) {
620 errno = 0;
621 }
622 }
623 if (env_file != NULL) {
624 /* Look for a 'home' variable and set argv0_path to it, if found */
625 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700626 wcscpy_s(argv0_path, MAXPATHLEN+1, tmpbuffer);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100627 }
628 fclose(env_file);
629 env_file = NULL;
630 }
631 }
632
Steve Dower50289382016-09-09 14:22:43 -0700633 /* Calculate zip archive path from DLL or exe path */
634 if (wcscpy_s(zip_path, MAXPATHLEN + 1, dllpath[0] ? dllpath : progpath)) {
635 /* exceeded buffer length - ignore zip_path */
636 zip_path[0] = '\0';
637 } else {
638 wchar_t *dot = wcsrchr(zip_path, '.');
639 if (!dot || wcscpy_s(dot, MAXPATHLEN + 1 - (dot - zip_path), L".zip")) {
640 /* exceeded buffer length - ignore zip_path */
641 zip_path[0] = L'\0';
642 }
643 }
644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (pythonhome == NULL || *pythonhome == '\0') {
Steve Dower50289382016-09-09 14:22:43 -0700646 if (zip_path[0] && exists(zip_path)) {
647 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
648 reduce(prefix);
649 pythonhome = prefix;
650 } else if (search_for_prefix(argv0_path, LANDMARK))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 pythonhome = prefix;
652 else
653 pythonhome = NULL;
654 }
655 else
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700656 wcscpy_s(prefix, MAXPATHLEN+1, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (envpath && *envpath == '\0')
659 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000660
Guido van Rossume02e48b2000-03-29 01:49:47 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000663#ifdef Py_ENABLE_SHARED
Steve Dower4db86bc2016-09-09 09:17:35 -0700664 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
665 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000666#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 /* We only use the default relative PYTHONPATH if we havent
668 anything better to use! */
669 skipdefault = envpath!=NULL || pythonhome!=NULL || \
670 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 /* We need to construct a path from the following parts.
673 (1) the PYTHONPATH environment variable, if set;
674 (2) for Win32, the zip archive file path;
675 (3) for Win32, the machinepath and userpath, if set;
676 (4) the PYTHONPATH config macro, with the leading "."
677 of each component replaced with pythonhome, if set;
678 (5) the directory containing the executable (argv0_path).
679 The length calculation calculates #4 first.
680 Extra rules:
681 - If PYTHONHOME is set (in any way) item (3) is ignored.
682 - If registry values are used, (4) and (5) are ignored.
683 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* Calculate size of return buffer */
686 if (pythonhome != NULL) {
687 wchar_t *p;
688 bufsz = 1;
689 for (p = PYTHONPATH; *p; p++) {
690 if (*p == DELIM)
691 bufsz++; /* number of DELIM plus one */
692 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700693 bufsz *= wcslen(pythonhome);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 }
695 else
696 bufsz = 0;
Steve Dowerf64b9d52015-05-23 17:34:50 -0700697 bufsz += wcslen(PYTHONPATH) + 1;
698 bufsz += wcslen(argv0_path) + 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700699 if (userpath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700700 bufsz += wcslen(userpath) + 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700701 if (machinepath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700702 bufsz += wcslen(machinepath) + 1;
703 bufsz += wcslen(zip_path) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (envpath != NULL)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700705 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000706
Victor Stinner1a7425f2013-07-07 16:25:15 +0200707 module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (buf == NULL) {
709 /* We can't exit, so print a warning and limp along */
710 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
711 if (envpath) {
712 fprintf(stderr, "Using environment $PYTHONPATH.\n");
713 module_search_path = envpath;
714 }
715 else {
716 fprintf(stderr, "Using default static path.\n");
717 module_search_path = PYTHONPATH;
718 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200719 PyMem_RawFree(machinepath);
720 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 return;
722 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 if (envpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700725 if (wcscpy_s(buf, bufsz - (buf - module_search_path), envpath))
726 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 buf = wcschr(buf, L'\0');
728 *buf++ = DELIM;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 if (zip_path[0]) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700731 if (wcscpy_s(buf, bufsz - (buf - module_search_path), zip_path))
732 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 buf = wcschr(buf, L'\0');
734 *buf++ = DELIM;
735 }
736 if (userpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700737 if (wcscpy_s(buf, bufsz - (buf - module_search_path), userpath))
738 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 buf = wcschr(buf, L'\0');
740 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200741 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 }
743 if (machinepath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700744 if (wcscpy_s(buf, bufsz - (buf - module_search_path), machinepath))
745 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 buf = wcschr(buf, L'\0');
747 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200748 PyMem_RawFree(machinepath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 }
750 if (pythonhome == NULL) {
751 if (!skipdefault) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700752 if (wcscpy_s(buf, bufsz - (buf - module_search_path), PYTHONPATH))
753 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700755 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700757 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 wchar_t *p = PYTHONPATH;
759 wchar_t *q;
760 size_t n;
761 for (;;) {
762 q = wcschr(p, DELIM);
763 if (q == NULL)
764 n = wcslen(p);
765 else
766 n = q-p;
767 if (p[0] == '.' && is_sep(p[1])) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700768 if (wcscpy_s(buf, bufsz - (buf - module_search_path), pythonhome))
769 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 buf = wcschr(buf, L'\0');
771 p++;
772 n--;
773 }
774 wcsncpy(buf, p, n);
775 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700776 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 if (q == NULL)
778 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 p = q+1;
780 }
781 }
782 if (argv0_path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 wcscpy(buf, argv0_path);
784 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700785 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700787 *(buf - 1) = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* Now to pull one last hack/trick. If sys.prefix is
789 empty, then try and find it somewhere on the paths
790 we calculated. We scan backwards, as our general policy
791 is that Python core directories are at the *end* of
792 sys.path. We assume that our "lib" directory is
793 on the path, and that our 'prefix' directory is
794 the parent of that.
795 */
796 if (*prefix==L'\0') {
797 wchar_t lookBuf[MAXPATHLEN+1];
798 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
799 while (1) {
800 Py_ssize_t nchars;
801 wchar_t *lookEnd = look;
802 /* 'look' will end up one character before the
803 start of the path in question - even if this
804 is one character before the start of the buffer
805 */
806 while (look >= module_search_path && *look != DELIM)
807 look--;
808 nchars = lookEnd-look;
809 wcsncpy(lookBuf, look+1, nchars);
810 lookBuf[nchars] = L'\0';
811 /* Up one level to the parent */
812 reduce(lookBuf);
813 if (search_for_prefix(lookBuf, LANDMARK)) {
814 break;
815 }
816 /* If we are out of paths to search - give up */
817 if (look < module_search_path)
818 break;
819 look--;
820 }
821 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000822}
823
824
825/* External interface */
826
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000827void
828Py_SetPath(const wchar_t *path)
829{
830 if (module_search_path != NULL) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200831 PyMem_RawFree(module_search_path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000832 module_search_path = NULL;
833 }
834 if (path != NULL) {
835 extern wchar_t *Py_GetProgramName(void);
836 wchar_t *prog = Py_GetProgramName();
837 wcsncpy(progpath, prog, MAXPATHLEN);
838 prefix[0] = L'\0';
Victor Stinner1a7425f2013-07-07 16:25:15 +0200839 module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000840 if (module_search_path != NULL)
841 wcscpy(module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200842 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000843}
844
Martin v. Löwis790465f2008-04-05 20:41:37 +0000845wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000846Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 if (!module_search_path)
849 calculate_path();
850 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000851}
852
Martin v. Löwis790465f2008-04-05 20:41:37 +0000853wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000854Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 if (!module_search_path)
857 calculate_path();
858 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000859}
860
Martin v. Löwis790465f2008-04-05 20:41:37 +0000861wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000862Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000865}
866
Martin v. Löwis790465f2008-04-05 20:41:37 +0000867wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000868Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (!module_search_path)
871 calculate_path();
872 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000873}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000874
Victor Stinner63941882011-09-29 00:42:28 +0200875/* Load python3.dll before loading any extension module that might refer
876 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000877 to this python DLL is loaded, not a python3.dll that might be on the path
878 by chance.
879 Return whether the DLL was found.
880*/
881static int python3_checked = 0;
882static HANDLE hPython3;
883int
884_Py_CheckPython3()
885{
886 wchar_t py3path[MAXPATHLEN+1];
887 wchar_t *s;
888 if (python3_checked)
889 return hPython3 != NULL;
890 python3_checked = 1;
891
892 /* If there is a python3.dll next to the python3y.dll,
893 assume this is a build tree; use that DLL */
894 wcscpy(py3path, dllpath);
895 s = wcsrchr(py3path, L'\\');
896 if (!s)
897 s = py3path;
898 wcscpy(s, L"\\python3.dll");
899 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
900 if (hPython3 != NULL)
901 return 1;
902
903 /* Check sys.prefix\DLLs\python3.dll */
904 wcscpy(py3path, Py_GetPrefix());
905 wcscat(py3path, L"\\DLLs\\python3.dll");
906 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
907 return hPython3 != NULL;
908}