blob: 31f973eedba1e968a2ae47d743fced6d6d677dc5 [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 Dowered51b262016-09-17 12:54:06 -07009 are actually fetched is different). The presence of a python._pth or
10 pythonXY._pth file alongside the program overrides these rules - see
11 below.
Guido van Rossum88716bb2000-03-30 19:45:39 +000012
13 * Python always adds an empty entry at the start, which corresponds
14 to the current directory.
15
Georg Brandl7eb4b7d2005-07-22 21:49:32 +000016 * If the PYTHONPATH env. var. exists, its entries are added next.
Guido van Rossum88716bb2000-03-30 19:45:39 +000017
18 * We look in the registry for "application paths" - that is, sub-keys
19 under the main PythonPath registry key. These are added next (the
20 order of sub-key processing is undefined).
21 HKEY_CURRENT_USER is searched and added first.
22 HKEY_LOCAL_MACHINE is searched and added next.
23 (Note that all known installers only use HKLM, so HKCU is typically
24 empty)
25
26 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
27 is set, we believe it. Otherwise, we use the path of our host .EXE's
Martin Panterfd13c0f2016-09-10 10:45:28 +000028 to try and locate one of our "landmarks" and deduce our home.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 - If we DO have a Python Home: The relevant sub-directories (Lib,
Zachary Warec4b53af2016-09-09 17:59:49 -070030 DLLs, etc) are based on the Python Home
Guido van Rossum88716bb2000-03-30 19:45:39 +000031 - If we DO NOT have a Python Home, the core Python Path is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 loaded from the registry. This is the main PythonPath key,
Guido van Rossum88716bb2000-03-30 19:45:39 +000033 and both HKLM and HKCU are combined to form the path)
34
35 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
36 specified, and can't locate any Registry entries (ie, we have _nothing_
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 we can assume is a good path), a default path with relative entries is
Zachary Warec4b53af2016-09-09 17:59:49 -070038 used (eg. .\Lib;.\DLLs, etc)
Guido van Rossum88716bb2000-03-30 19:45:39 +000039
40
Steve Dowered51b262016-09-17 12:54:06 -070041 If a '._pth' file exists adjacent to the executable with the same base name
42 (e.g. python._pth adjacent to python.exe) or adjacent to the shared library
43 (e.g. python36._pth adjacent to python36.dll), it is used in preference to
44 the above process. The shared library file takes precedence over the
45 executable. The path file must contain a list of paths to add to sys.path,
46 one per line. Each path is relative to the directory containing the file.
47 Blank lines and comments beginning with '#' are permitted.
48
49 In the presence of this ._pth file, no other paths are added to the search
50 path, the registry finder is not enabled, site.py is not imported and
51 isolated mode is enabled. The site package can be enabled by including a
52 line reading "import site"; no other imports are recognized. Any invalid
53 entry (other than directories that do not exist) will result in immediate
54 termination of the program.
55
Steve Dower4db86bc2016-09-09 09:17:35 -070056
Guido van Rossum88716bb2000-03-30 19:45:39 +000057 The end result of all this is:
58 * When running python.exe, or any other .exe in the main Python directory
59 (either an installed version, or directly from the PCbuild directory),
60 the core path is deduced, and the core paths in the registry are
61 ignored. Other "application paths" in the registry are always read.
62
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 * When Python is hosted in another exe (different directory, embedded via
Guido van Rossum88716bb2000-03-30 19:45:39 +000064 COM, etc), the Python Home will not be deduced, so the core path from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 the registry is used. Other "application paths" in the registry are
Guido van Rossum88716bb2000-03-30 19:45:39 +000066 always read.
67
68 * If Python can't find its home and there is no registry (eg, frozen
69 exe, some very strange installation setup) you get a path with
70 some default, but relative, paths.
71
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000072 * An embedding application can use Py_SetPath() to override all of
Steve Dower4db86bc2016-09-09 09:17:35 -070073 these automatic path computations.
74
Steve Dowered51b262016-09-17 12:54:06 -070075 * An install of Python can fully specify the contents of sys.path using
76 either a 'EXENAME._pth' or 'DLLNAME._pth' file, optionally including
77 "import site" to enable the site module.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000078
Guido van Rossum88716bb2000-03-30 19:45:39 +000079 ---------------------------------------------------------------- */
80
81
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000082#include "Python.h"
83#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000084#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085
Steve Dower4db86bc2016-09-09 09:17:35 -070086#ifndef MS_WINDOWS
87#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000088#endif
89
Steve Dower4db86bc2016-09-09 09:17:35 -070090#include <windows.h>
91#include <Shlwapi.h>
92
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000094#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#endif /* HAVE_SYS_TYPES_H */
96
97#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000098#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099#endif /* HAVE_SYS_STAT_H */
100
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#include <string.h>
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103/* Search in some common locations for the associated Python libraries.
104 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 * Py_GetPath() tries to return a sensible Python module search path.
106 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000107 * The approach is an adaptation for Windows of the strategy used in
108 * ../Modules/getpath.c; it uses the Windows Registry as one of its
109 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000110 *
111 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
112 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113 */
114
115#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000116#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000117#endif
118
Martin v. Löwis790465f2008-04-05 20:41:37 +0000119static wchar_t prefix[MAXPATHLEN+1];
120static wchar_t progpath[MAXPATHLEN+1];
121static wchar_t dllpath[MAXPATHLEN+1];
122static wchar_t *module_search_path = NULL;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000123
Guido van Rossumeea14491997-08-13 21:30:44 +0000124
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000125static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126is_sep(wchar_t ch) /* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000127{
128#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000130#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000132#endif
133}
134
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000135/* assumes 'dir' null terminated in bounds. Never writes
136 beyond existing terminator.
137*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000139reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700141 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
142 if (i >= MAXPATHLEN+1)
143 Py_FatalError("buffer overflow in getpathp.c's reduce()");
144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 while (i > 0 && !is_sep(dir[i]))
146 --i;
147 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000148}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149
Steve Dowered51b262016-09-17 12:54:06 -0700150static int
151change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
152{
153 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
154 size_t i = src_len;
155 if (i >= MAXPATHLEN+1)
156 Py_FatalError("buffer overflow in getpathp.c's reduce()");
157
158 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
159 --i;
160
161 if (i == 0) {
162 dest[0] = '\0';
163 return -1;
164 }
165
166 if (is_sep(src[i]))
167 i = src_len;
168
169 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
170 wcscat_s(dest, MAXPATHLEN+1, ext)) {
171 dest[0] = '\0';
172 return -1;
173 }
174
175 return 0;
176}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000177
178static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000179exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000182}
183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184/* Assumes 'filename' MAXPATHLEN+1 bytes long -
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000185 may extend 'filename' by one character.
186*/
Guido van Rossum43ff1141998-08-08 23:40:40 +0000187static int
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700188ismodule(wchar_t *filename, int update_filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000189{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100190 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 if (exists(filename))
193 return 1;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700196 n = wcsnlen_s(filename, MAXPATHLEN+1);
197 if (n < MAXPATHLEN) {
198 int exist = 0;
199 filename[n] = Py_OptimizeFlag ? L'o' : L'c';
200 filename[n + 1] = L'\0';
201 exist = exists(filename);
202 if (!update_filename)
203 filename[n] = L'\0';
204 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 }
206 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000207}
208
Tim Peters8484fbf2004-08-07 19:12:27 +0000209/* Add a path component, by appending stuff to buffer.
210 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
211 NUL-terminated string with no more than MAXPATHLEN characters (not counting
212 the trailing NUL). It's a fatal error if it contains a string longer than
213 that (callers must be careful!). If these requirements are met, it's
214 guaranteed that buffer will still be a NUL-terminated string with no more
215 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
216 stuff as fits will be appended.
217*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700218
219static int _PathCchCombineEx_Initialized = 0;
220typedef HRESULT(__stdcall *PPathCchCombineEx)(PWSTR pszPathOut, size_t cchPathOut, PCWSTR pszPathIn, PCWSTR pszMore, unsigned long dwFlags);
221static PPathCchCombineEx _PathCchCombineEx;
222
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000223static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700224join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000225{
Steve Dower4db86bc2016-09-09 09:17:35 -0700226 if (_PathCchCombineEx_Initialized == 0) {
227 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
228 if (pathapi)
229 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
230 else
231 _PathCchCombineEx = NULL;
232 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700234
Steve Dower4db86bc2016-09-09 09:17:35 -0700235 if (_PathCchCombineEx) {
236 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0)))
237 Py_FatalError("buffer overflow in getpathp.c's join()");
238 } else {
Steve Dower3ceb5732016-09-09 15:53:58 -0700239 if (!PathCombineW(buffer, buffer, stuff))
Steve Dower4db86bc2016-09-09 09:17:35 -0700240 Py_FatalError("buffer overflow in getpathp.c's join()");
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700241 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000242}
243
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000244/* gotlandmark only called by search_for_prefix, which ensures
245 'prefix' is null terminated in bounds. join() ensures
246 'landmark' can not overflow prefix if too long.
247*/
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000248static int
Steve Dower50289382016-09-09 14:22:43 -0700249gotlandmark(const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700252 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700255 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 prefix[n] = '\0';
257 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000258}
259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000261 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000262static int
Steve Dower50289382016-09-09 14:22:43 -0700263search_for_prefix(wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700266 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 do {
268 if (gotlandmark(landmark))
269 return 1;
270 reduce(prefix);
271 } while (prefix[0]);
272 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000273}
274
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000275#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000276
Guido van Rossum88716bb2000-03-30 19:45:39 +0000277/* a string loaded from the DLL at startup.*/
278extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000279
Guido van Rossumeea14491997-08-13 21:30:44 +0000280
281/* Load a PYTHONPATH value from the registry.
282 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
283
Guido van Rossum88716bb2000-03-30 19:45:39 +0000284 Works in both Unicode and 8bit environments. Only uses the
285 Ex family of functions so it also works with Windows CE.
286
Guido van Rossumeea14491997-08-13 21:30:44 +0000287 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000288
289 XXX - this code is pretty strange, as it used to also
290 work on Win16, where the buffer sizes werent available
291 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000292*/
293
Martin v. Löwis790465f2008-04-05 20:41:37 +0000294static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000295getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 HKEY newKey = 0;
298 DWORD dataSize = 0;
299 DWORD numKeys = 0;
300 LONG rc;
301 wchar_t *retval = NULL;
302 WCHAR *dataBuf = NULL;
303 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
304 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700305 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 DWORD index;
307 WCHAR *keyBuf = NULL;
308 WCHAR *keyBufPtr;
309 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 /* Tried to use sysget("winver") but here is too early :-( */
312 versionLen = strlen(PyWin_DLLVersionString);
313 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700314 keyBufLen = sizeof(keyPrefix) +
315 sizeof(WCHAR)*(versionLen-1) +
316 sizeof(keySuffix);
317 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (keyBuf==NULL) goto done;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000319
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700320 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200321 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
323 keyBufPtr += versionLen;
324 /* NULL comes with this one! */
325 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
326 /* Open the root Python key */
327 rc=RegOpenKeyExW(keyBase,
328 keyBuf, /* subkey */
329 0, /* reserved */
330 KEY_READ,
331 &newKey);
332 if (rc!=ERROR_SUCCESS) goto done;
333 /* Find out how big our core buffer is, and how many subkeys we have */
334 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
335 NULL, NULL, &dataSize, NULL, NULL);
336 if (rc!=ERROR_SUCCESS) goto done;
337 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
338 /* Allocate a temp array of char buffers, so we only need to loop
339 reading the registry once
340 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200341 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (ppPaths==NULL) goto done;
343 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
344 /* Loop over all subkeys, allocating a temp sub-buffer. */
345 for(index=0;index<numKeys;index++) {
346 WCHAR keyBuf[MAX_PATH+1];
347 HKEY subKey = 0;
348 DWORD reqdSize = MAX_PATH+1;
349 /* Get the sub-key name */
350 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
351 NULL, NULL, NULL, NULL );
352 if (rc!=ERROR_SUCCESS) goto done;
353 /* Open the sub-key */
354 rc=RegOpenKeyExW(newKey,
355 keyBuf, /* subkey */
356 0, /* reserved */
357 KEY_READ,
358 &subKey);
359 if (rc!=ERROR_SUCCESS) goto done;
360 /* Find the value of the buffer size, malloc, then read it */
361 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
362 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200363 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (ppPaths[index]) {
365 RegQueryValueExW(subKey, NULL, 0, NULL,
366 (LPBYTE)ppPaths[index],
367 &reqdSize);
368 dataSize += reqdSize + 1; /* 1 for the ";" */
369 }
370 }
371 RegCloseKey(subKey);
372 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* return null if no path to return */
375 if (dataSize == 0) goto done;
376
377 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200378 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (dataBuf) {
380 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 /* Copy our collected strings */
382 for (index=0;index<numKeys;index++) {
383 if (index > 0) {
384 *(szCur++) = L';';
385 dataSize--;
386 }
387 if (ppPaths[index]) {
388 Py_ssize_t len = wcslen(ppPaths[index]);
389 wcsncpy(szCur, ppPaths[index], len);
390 szCur += len;
391 assert(dataSize > (DWORD)len);
392 dataSize -= (DWORD)len;
393 }
394 }
395 if (skipcore)
396 *szCur = '\0';
397 else {
398 /* If we have no values, we dont need a ';' */
399 if (numKeys) {
400 *(szCur++) = L';';
401 dataSize--;
402 }
403 /* Now append the core path entries -
404 this will include the NULL
405 */
406 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
407 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200408 if (rc != ERROR_SUCCESS) {
409 PyMem_RawFree(dataBuf);
410 goto done;
411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 }
413 /* And set the result - caller must free */
414 retval = dataBuf;
415 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000416done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Loop freeing my temp buffers */
418 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200419 for(index=0; index<numKeys; index++)
420 PyMem_RawFree(ppPaths[index]);
421 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 }
423 if (newKey)
424 RegCloseKey(newKey);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200425 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000427}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000428#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000429
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000430static void
Thomas Wouters78890102000-07-22 19:25:51 +0000431get_progpath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 extern wchar_t *Py_GetProgramName(void);
434 wchar_t *path = _wgetenv(L"PATH");
435 wchar_t *prog = Py_GetProgramName();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000436
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000437#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 extern HANDLE PyWin_DLLhModule;
439 /* static init of progpath ensures final char remains \0 */
440 if (PyWin_DLLhModule)
441 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN))
442 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000443#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000445#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN))
447 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (prog == NULL || *prog == '\0')
449 prog = L"python";
Guido van Rossumeea14491997-08-13 21:30:44 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 /* If there is no slash in the argv0 path, then we have to
452 * assume python is on the user's $PATH, since there's no
453 * other way to find a directory to start the search from. If
454 * $PATH isn't exported, you lose.
455 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000456#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (wcschr(prog, SEP) || wcschr(prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000458#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (wcschr(prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 wcsncpy(progpath, prog, MAXPATHLEN);
462 else if (path) {
463 while (1) {
464 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 if (delim) {
467 size_t len = delim - path;
468 /* ensure we can't overwrite buffer */
469 len = min(MAXPATHLEN,len);
470 wcsncpy(progpath, path, len);
471 *(progpath + len) = '\0';
472 }
473 else
474 wcsncpy(progpath, path, MAXPATHLEN);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* join() is safe for MAXPATHLEN+1 size buffer */
477 join(progpath, prog);
478 if (exists(progpath))
479 break;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (!delim) {
482 progpath[0] = '\0';
483 break;
484 }
485 path = delim + 1;
486 }
487 }
488 else
489 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000490}
491
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100492static int
493find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
494{
495 int result = 0; /* meaning not found */
496 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
497
498 fseek(env_file, 0, SEEK_SET);
499 while (!feof(env_file)) {
500 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
501 wchar_t tmpbuffer[MAXPATHLEN*2+1];
502 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200503 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100504
505 if (p == NULL)
506 break;
507 n = strlen(p);
508 if (p[n - 1] != '\n') {
509 /* line has overflowed - bail */
510 break;
511 }
512 if (p[0] == '#') /* Comment - skip */
513 continue;
514 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
515 if (decoded != NULL) {
516 Py_ssize_t k;
517 k = PyUnicode_AsWideChar(decoded,
518 tmpbuffer, MAXPATHLEN * 2);
519 Py_DECREF(decoded);
520 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800521 wchar_t * context = NULL;
522 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100523 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800524 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100525 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800526 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100527 if (tok != NULL) {
528 wcsncpy(value, tok, MAXPATHLEN);
529 result = 1;
530 break;
531 }
532 }
533 }
534 }
535 }
536 }
537 return result;
538}
539
Steve Dower4db86bc2016-09-09 09:17:35 -0700540static int
Steve Dowered51b262016-09-17 12:54:06 -0700541read_pth_file(const wchar_t *path, wchar_t *prefix, int *isolated, int *nosite)
Steve Dower4db86bc2016-09-09 09:17:35 -0700542{
543 FILE *sp_file = _Py_wfopen(path, L"r");
544 if (sp_file == NULL)
545 return -1;
546
Steve Dowered51b262016-09-17 12:54:06 -0700547 wcscpy_s(prefix, MAXPATHLEN+1, path);
548 reduce(prefix);
549 *isolated = 1;
550 *nosite = 1;
551
Steve Dower4db86bc2016-09-09 09:17:35 -0700552 size_t bufsiz = MAXPATHLEN;
553 size_t prefixlen = wcslen(prefix);
554
555 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
556 buf[0] = '\0';
557
558 while (!feof(sp_file)) {
559 char line[MAXPATHLEN + 1];
560 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
561 if (!p)
562 break;
Steve Dowered51b262016-09-17 12:54:06 -0700563 if (*p == '\0' || *p == '#')
564 continue;
565 while (*++p) {
566 if (*p == '\r' || *p == '\n') {
567 *p = '\0';
568 break;
569 }
570 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700571
Steve Dowered51b262016-09-17 12:54:06 -0700572 if (strcmp(line, "import site") == 0) {
573 *nosite = 0;
574 continue;
575 } else if (strncmp(line, "import ", 7) == 0) {
576 Py_FatalError("only 'import site' is supported in ._pth file");
577 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700578
Steve Dowered51b262016-09-17 12:54:06 -0700579 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700580 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Steve Dowered51b262016-09-17 12:54:06 -0700581 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700582 wline[wn] = '\0';
583
584 while (wn + prefixlen + 4 > bufsiz) {
585 bufsiz += MAXPATHLEN;
586 buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));
587 if (!buf) {
588 PyMem_RawFree(wline);
589 goto error;
590 }
591 }
592
593 if (buf[0])
594 wcscat_s(buf, bufsiz, L";");
Steve Dowered51b262016-09-17 12:54:06 -0700595
Steve Dower4db86bc2016-09-09 09:17:35 -0700596 wchar_t *b = &buf[wcslen(buf)];
Steve Dower4db86bc2016-09-09 09:17:35 -0700597 wcscat_s(buf, bufsiz, prefix);
598 join(b, wline);
599
600 PyMem_RawFree(wline);
601 }
602
603 module_search_path = buf;
604
605 fclose(sp_file);
606 return 0;
607
608error:
609 PyMem_RawFree(buf);
610 fclose(sp_file);
611 return -1;
612}
613
614
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000615static void
Thomas Wouters78890102000-07-22 19:25:51 +0000616calculate_path(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 wchar_t argv0_path[MAXPATHLEN+1];
619 wchar_t *buf;
620 size_t bufsz;
621 wchar_t *pythonhome = Py_GetPythonHome();
622 wchar_t *envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 int skiphome, skipdefault;
625 wchar_t *machinepath = NULL;
626 wchar_t *userpath = NULL;
627 wchar_t zip_path[MAXPATHLEN+1];
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 if (!Py_IgnoreEnvironmentFlag) {
630 envpath = _wgetenv(L"PYTHONPATH");
631 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 get_progpath();
634 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700635 wcscpy_s(argv0_path, MAXPATHLEN+1, progpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 reduce(argv0_path);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100637
Steve Dower4db86bc2016-09-09 09:17:35 -0700638 /* Search for a sys.path file */
639 {
640 wchar_t spbuffer[MAXPATHLEN+1];
641
Steve Dowered51b262016-09-17 12:54:06 -0700642 if ((dllpath[0] && !change_ext(spbuffer, dllpath, L"._pth") && exists(spbuffer)) ||
643 (progpath[0] && !change_ext(spbuffer, progpath, L"._pth") && exists(spbuffer))) {
644
645 if (!read_pth_file(spbuffer, prefix, &Py_IsolatedFlag, &Py_NoSiteFlag)) {
646 return;
647 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700648 }
649 }
650
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100651 /* Search for an environment configuration file, first in the
652 executable's directory and then in the parent directory.
653 If found, open it for use when searching for prefixes.
654 */
655
656 {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700657 wchar_t envbuffer[MAXPATHLEN+1];
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100658 wchar_t tmpbuffer[MAXPATHLEN+1];
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700659 const wchar_t *env_cfg = L"pyvenv.cfg";
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100660 FILE * env_file = NULL;
661
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700662 wcscpy_s(envbuffer, MAXPATHLEN+1, argv0_path);
663 join(envbuffer, env_cfg);
664 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100665 if (env_file == NULL) {
666 errno = 0;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700667 reduce(envbuffer);
668 reduce(envbuffer);
669 join(envbuffer, env_cfg);
670 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100671 if (env_file == NULL) {
672 errno = 0;
673 }
674 }
675 if (env_file != NULL) {
676 /* Look for a 'home' variable and set argv0_path to it, if found */
677 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700678 wcscpy_s(argv0_path, MAXPATHLEN+1, tmpbuffer);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100679 }
680 fclose(env_file);
681 env_file = NULL;
682 }
683 }
684
Steve Dower50289382016-09-09 14:22:43 -0700685 /* Calculate zip archive path from DLL or exe path */
Steve Dowered51b262016-09-17 12:54:06 -0700686 change_ext(zip_path, dllpath[0] ? dllpath : progpath, L".zip");
Steve Dower50289382016-09-09 14:22:43 -0700687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (pythonhome == NULL || *pythonhome == '\0') {
Steve Dower50289382016-09-09 14:22:43 -0700689 if (zip_path[0] && exists(zip_path)) {
690 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
691 reduce(prefix);
692 pythonhome = prefix;
693 } else if (search_for_prefix(argv0_path, LANDMARK))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 pythonhome = prefix;
695 else
696 pythonhome = NULL;
697 }
698 else
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700699 wcscpy_s(prefix, MAXPATHLEN+1, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (envpath && *envpath == '\0')
702 envpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000703
Guido van Rossume02e48b2000-03-29 01:49:47 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 skiphome = pythonhome==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000706#ifdef Py_ENABLE_SHARED
Steve Dower4db86bc2016-09-09 09:17:35 -0700707 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
708 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000709#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 /* We only use the default relative PYTHONPATH if we havent
711 anything better to use! */
712 skipdefault = envpath!=NULL || pythonhome!=NULL || \
713 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 /* We need to construct a path from the following parts.
716 (1) the PYTHONPATH environment variable, if set;
717 (2) for Win32, the zip archive file path;
718 (3) for Win32, the machinepath and userpath, if set;
719 (4) the PYTHONPATH config macro, with the leading "."
720 of each component replaced with pythonhome, if set;
721 (5) the directory containing the executable (argv0_path).
722 The length calculation calculates #4 first.
723 Extra rules:
724 - If PYTHONHOME is set (in any way) item (3) is ignored.
725 - If registry values are used, (4) and (5) are ignored.
726 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 /* Calculate size of return buffer */
729 if (pythonhome != NULL) {
730 wchar_t *p;
731 bufsz = 1;
732 for (p = PYTHONPATH; *p; p++) {
733 if (*p == DELIM)
734 bufsz++; /* number of DELIM plus one */
735 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700736 bufsz *= wcslen(pythonhome);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 }
738 else
739 bufsz = 0;
Steve Dowerf64b9d52015-05-23 17:34:50 -0700740 bufsz += wcslen(PYTHONPATH) + 1;
741 bufsz += wcslen(argv0_path) + 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700742 if (userpath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700743 bufsz += wcslen(userpath) + 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700744 if (machinepath)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700745 bufsz += wcslen(machinepath) + 1;
746 bufsz += wcslen(zip_path) + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 if (envpath != NULL)
Steve Dowerf64b9d52015-05-23 17:34:50 -0700748 bufsz += wcslen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000749
Victor Stinner1a7425f2013-07-07 16:25:15 +0200750 module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 if (buf == NULL) {
752 /* We can't exit, so print a warning and limp along */
753 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
754 if (envpath) {
755 fprintf(stderr, "Using environment $PYTHONPATH.\n");
756 module_search_path = envpath;
757 }
758 else {
759 fprintf(stderr, "Using default static path.\n");
760 module_search_path = PYTHONPATH;
761 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200762 PyMem_RawFree(machinepath);
763 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 return;
765 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 if (envpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700768 if (wcscpy_s(buf, bufsz - (buf - module_search_path), envpath))
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 *buf++ = DELIM;
772 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (zip_path[0]) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700774 if (wcscpy_s(buf, bufsz - (buf - module_search_path), zip_path))
775 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 buf = wcschr(buf, L'\0');
777 *buf++ = DELIM;
778 }
779 if (userpath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700780 if (wcscpy_s(buf, bufsz - (buf - module_search_path), userpath))
781 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 buf = wcschr(buf, L'\0');
783 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200784 PyMem_RawFree(userpath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 }
786 if (machinepath) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700787 if (wcscpy_s(buf, bufsz - (buf - module_search_path), machinepath))
788 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 buf = wcschr(buf, L'\0');
790 *buf++ = DELIM;
Victor Stinner1a7425f2013-07-07 16:25:15 +0200791 PyMem_RawFree(machinepath);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 }
793 if (pythonhome == NULL) {
794 if (!skipdefault) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700795 if (wcscpy_s(buf, bufsz - (buf - module_search_path), PYTHONPATH))
796 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700798 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700800 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 wchar_t *p = PYTHONPATH;
802 wchar_t *q;
803 size_t n;
804 for (;;) {
805 q = wcschr(p, DELIM);
806 if (q == NULL)
807 n = wcslen(p);
808 else
809 n = q-p;
810 if (p[0] == '.' && is_sep(p[1])) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700811 if (wcscpy_s(buf, bufsz - (buf - module_search_path), pythonhome))
812 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 buf = wcschr(buf, L'\0');
814 p++;
815 n--;
816 }
817 wcsncpy(buf, p, n);
818 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700819 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 if (q == NULL)
821 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 p = q+1;
823 }
824 }
825 if (argv0_path) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 wcscpy(buf, argv0_path);
827 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700828 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700830 *(buf - 1) = L'\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 /* Now to pull one last hack/trick. If sys.prefix is
832 empty, then try and find it somewhere on the paths
833 we calculated. We scan backwards, as our general policy
834 is that Python core directories are at the *end* of
835 sys.path. We assume that our "lib" directory is
836 on the path, and that our 'prefix' directory is
837 the parent of that.
838 */
839 if (*prefix==L'\0') {
840 wchar_t lookBuf[MAXPATHLEN+1];
841 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
842 while (1) {
843 Py_ssize_t nchars;
844 wchar_t *lookEnd = look;
845 /* 'look' will end up one character before the
846 start of the path in question - even if this
847 is one character before the start of the buffer
848 */
849 while (look >= module_search_path && *look != DELIM)
850 look--;
851 nchars = lookEnd-look;
852 wcsncpy(lookBuf, look+1, nchars);
853 lookBuf[nchars] = L'\0';
854 /* Up one level to the parent */
855 reduce(lookBuf);
856 if (search_for_prefix(lookBuf, LANDMARK)) {
857 break;
858 }
859 /* If we are out of paths to search - give up */
860 if (look < module_search_path)
861 break;
862 look--;
863 }
864 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000865}
866
867
868/* External interface */
869
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000870void
871Py_SetPath(const wchar_t *path)
872{
873 if (module_search_path != NULL) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200874 PyMem_RawFree(module_search_path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000875 module_search_path = NULL;
876 }
877 if (path != NULL) {
878 extern wchar_t *Py_GetProgramName(void);
879 wchar_t *prog = Py_GetProgramName();
880 wcsncpy(progpath, prog, MAXPATHLEN);
881 prefix[0] = L'\0';
Victor Stinner1a7425f2013-07-07 16:25:15 +0200882 module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000883 if (module_search_path != NULL)
884 wcscpy(module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200885 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000886}
887
Martin v. Löwis790465f2008-04-05 20:41:37 +0000888wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000889Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (!module_search_path)
892 calculate_path();
893 return module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000894}
895
Martin v. Löwis790465f2008-04-05 20:41:37 +0000896wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000897Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (!module_search_path)
900 calculate_path();
901 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000902}
903
Martin v. Löwis790465f2008-04-05 20:41:37 +0000904wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000905Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000906{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000908}
909
Martin v. Löwis790465f2008-04-05 20:41:37 +0000910wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +0000911Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000912{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 if (!module_search_path)
914 calculate_path();
915 return progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000916}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000917
Victor Stinner63941882011-09-29 00:42:28 +0200918/* Load python3.dll before loading any extension module that might refer
919 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000920 to this python DLL is loaded, not a python3.dll that might be on the path
921 by chance.
922 Return whether the DLL was found.
923*/
924static int python3_checked = 0;
925static HANDLE hPython3;
926int
927_Py_CheckPython3()
928{
929 wchar_t py3path[MAXPATHLEN+1];
930 wchar_t *s;
931 if (python3_checked)
932 return hPython3 != NULL;
933 python3_checked = 1;
934
935 /* If there is a python3.dll next to the python3y.dll,
936 assume this is a build tree; use that DLL */
937 wcscpy(py3path, dllpath);
938 s = wcsrchr(py3path, L'\\');
939 if (!s)
940 s = py3path;
941 wcscpy(s, L"\\python3.dll");
942 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
943 if (hPython3 != NULL)
944 return 1;
945
946 /* Check sys.prefix\DLLs\python3.dll */
947 wcscpy(py3path, Py_GetPrefix());
948 wcscat(py3path, L"\\DLLs\\python3.dll");
949 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
950 return hPython3 != NULL;
951}