blob: 53da3a6d05faee99b72db2ea2ee2095b01e950ea [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"
Victor Stinner361dcdc2020-04-15 03:24:57 +020083#include "pycore_initconfig.h" // PyStatus
84#include "pycore_pathconfig.h" // _PyPathConfig
85#include "osdefs.h" // SEP, ALTSEP
Martin v. Löwis790465f2008-04-05 20:41:37 +000086#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087
Steve Dower4db86bc2016-09-09 09:17:35 -070088#ifndef MS_WINDOWS
89#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000090#endif
91
Steve Dower4db86bc2016-09-09 09:17:35 -070092#include <windows.h>
Steve Dower6a65eba2020-01-29 13:46:33 +110093#include <pathcch.h>
erikjanss6cf82552018-07-25 02:41:46 +020094#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070095
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000097#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098#endif /* HAVE_SYS_TYPES_H */
99
100#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102#endif /* HAVE_SYS_STAT_H */
103
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104#include <string.h>
105
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106/* Search in some common locations for the associated Python libraries.
107 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000108 * Py_GetPath() tries to return a sensible Python module search path.
109 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000110 * The approach is an adaptation for Windows of the strategy used in
111 * ../Modules/getpath.c; it uses the Windows Registry as one of its
112 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000113 *
114 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
115 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000116 */
117
118#ifndef LANDMARK
Victor Stinner85ce0a72019-09-24 00:55:48 +0200119# define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000120#endif
121
Victor Stinner85ce0a72019-09-24 00:55:48 +0200122#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
123
124
Victor Stinner0327bde2017-11-23 17:03:20 +0100125typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200126 const wchar_t *path_env; /* PATH environment variable */
127 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100128
Victor Stinner85ce0a72019-09-24 00:55:48 +0200129 /* Registry key "Software\Python\PythonCore\X.Y\PythonPath"
130 where X.Y is the Python version (major.minor) */
Victor Stinner0327bde2017-11-23 17:03:20 +0100131 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
132 wchar_t *user_path; /* from HKEY_CURRENT_USER */
133
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200134 const wchar_t *pythonpath_env;
Victor Stinner0327bde2017-11-23 17:03:20 +0100135} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000136
Guido van Rossumeea14491997-08-13 21:30:44 +0000137
Victor Stinner0327bde2017-11-23 17:03:20 +0100138/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100140is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000141{
142#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000144#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000146#endif
147}
148
Victor Stinner0327bde2017-11-23 17:03:20 +0100149
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000150/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100151 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000152static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000153reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000154{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700155 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100156 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700157 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100158 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 while (i > 0 && !is_sep(dir[i]))
161 --i;
162 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000163}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164
Victor Stinner0327bde2017-11-23 17:03:20 +0100165
Steve Dowered51b262016-09-17 12:54:06 -0700166static int
167change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
168{
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100169 if (src && src != dest) {
170 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
171 size_t i = src_len;
172 if (i >= MAXPATHLEN+1) {
173 Py_FatalError("buffer overflow in getpathp.c's reduce()");
174 }
175
176 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
177 --i;
178
179 if (i == 0) {
180 dest[0] = '\0';
181 return -1;
182 }
183
184 if (is_sep(src[i])) {
185 i = src_len;
186 }
187
188 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i)) {
189 dest[0] = '\0';
190 return -1;
191 }
192 } else {
193 wchar_t *s = wcsrchr(dest, L'.');
194 if (s) {
195 s[0] = '\0';
196 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100197 }
Steve Dowered51b262016-09-17 12:54:06 -0700198
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100199 if (wcscat_s(dest, MAXPATHLEN+1, ext)) {
Steve Dowered51b262016-09-17 12:54:06 -0700200 dest[0] = '\0';
201 return -1;
202 }
203
204 return 0;
205}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206
Victor Stinner0327bde2017-11-23 17:03:20 +0100207
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000208static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200209exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000212}
213
Victor Stinner0327bde2017-11-23 17:03:20 +0100214
215/* Is module -- check for .pyc too.
216 Assumes 'filename' MAXPATHLEN+1 bytes long -
217 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000218static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100219ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000220{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100221 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700222
Victor Stinner0327bde2017-11-23 17:03:20 +0100223 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100225 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700228 n = wcsnlen_s(filename, MAXPATHLEN+1);
229 if (n < MAXPATHLEN) {
230 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800231 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700232 filename[n + 1] = L'\0';
233 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100234 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700235 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100236 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700237 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 }
239 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000240}
241
Victor Stinner0327bde2017-11-23 17:03:20 +0100242
Tim Peters8484fbf2004-08-07 19:12:27 +0000243/* Add a path component, by appending stuff to buffer.
244 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
245 NUL-terminated string with no more than MAXPATHLEN characters (not counting
246 the trailing NUL). It's a fatal error if it contains a string longer than
247 that (callers must be careful!). If these requirements are met, it's
248 guaranteed that buffer will still be a NUL-terminated string with no more
249 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
250 stuff as fits will be appended.
251*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700252
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000253static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700254join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000255{
Steve Dower6a65eba2020-01-29 13:46:33 +1100256 if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
257 Py_FatalError("buffer overflow in getpathp.c's join()");
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700258 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000259}
260
Victor Stinner85ce0a72019-09-24 00:55:48 +0200261/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
262 and ".." to produce a direct, well-formed path. */
263static PyStatus
264canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800265{
266 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200267 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800268 }
269
Steve Dower6a65eba2020-01-29 13:46:33 +1100270 if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
271 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800272 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200273 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800274}
275
Victor Stinner0327bde2017-11-23 17:03:20 +0100276
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000277/* gotlandmark only called by search_for_prefix, which ensures
278 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100279 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000280static int
Victor Stinnerdec39712019-09-30 14:49:34 +0200281gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000282{
Victor Stinnerdec39712019-09-30 14:49:34 +0200283 wchar_t filename[MAXPATHLEN+1];
284 memset(filename, 0, sizeof(filename));
285 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
286 join(filename, landmark);
287 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000288}
289
Victor Stinner0327bde2017-11-23 17:03:20 +0100290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200292 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000293static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200294search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700297 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100299 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 reduce(prefix);
303 } while (prefix[0]);
304 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000305}
306
Victor Stinner0327bde2017-11-23 17:03:20 +0100307
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100308static int
309get_dllpath(wchar_t *dllpath)
310{
311#ifdef Py_ENABLE_SHARED
312 extern HANDLE PyWin_DLLhModule;
313 if (PyWin_DLLhModule && GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) {
314 return 0;
315 }
316#endif
317 return -1;
318}
319
320
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000321#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000322
Guido van Rossum88716bb2000-03-30 19:45:39 +0000323/* a string loaded from the DLL at startup.*/
324extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000325
Guido van Rossumeea14491997-08-13 21:30:44 +0000326/* Load a PYTHONPATH value from the registry.
327 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
328
Guido van Rossum88716bb2000-03-30 19:45:39 +0000329 Works in both Unicode and 8bit environments. Only uses the
330 Ex family of functions so it also works with Windows CE.
331
Guido van Rossumeea14491997-08-13 21:30:44 +0000332 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000333
334 XXX - this code is pretty strange, as it used to also
335 work on Win16, where the buffer sizes werent available
336 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000337*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000338static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000339getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 HKEY newKey = 0;
342 DWORD dataSize = 0;
343 DWORD numKeys = 0;
344 LONG rc;
345 wchar_t *retval = NULL;
346 WCHAR *dataBuf = NULL;
347 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
348 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700349 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 DWORD index;
351 WCHAR *keyBuf = NULL;
352 WCHAR *keyBufPtr;
353 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 /* Tried to use sysget("winver") but here is too early :-( */
356 versionLen = strlen(PyWin_DLLVersionString);
357 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700358 keyBufLen = sizeof(keyPrefix) +
359 sizeof(WCHAR)*(versionLen-1) +
360 sizeof(keySuffix);
361 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100362 if (keyBuf==NULL) {
363 goto done;
364 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000365
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700366 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200367 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
369 keyBufPtr += versionLen;
370 /* NULL comes with this one! */
371 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
372 /* Open the root Python key */
373 rc=RegOpenKeyExW(keyBase,
374 keyBuf, /* subkey */
375 0, /* reserved */
376 KEY_READ,
377 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100378 if (rc!=ERROR_SUCCESS) {
379 goto done;
380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 /* Find out how big our core buffer is, and how many subkeys we have */
Minmin Gong98e42d12020-05-18 09:50:03 -0700382 rc = RegQueryInfoKeyW(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100384 if (rc!=ERROR_SUCCESS) {
385 goto done;
386 }
387 if (skipcore) {
388 dataSize = 0; /* Only count core ones if we want them! */
389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 /* Allocate a temp array of char buffers, so we only need to loop
391 reading the registry once
392 */
Andy Lester7668a8b2020-03-24 23:26:44 -0500393 ppPaths = PyMem_RawCalloc(numKeys, sizeof(WCHAR *));
Victor Stinner0327bde2017-11-23 17:03:20 +0100394 if (ppPaths==NULL) {
395 goto done;
396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 /* Loop over all subkeys, allocating a temp sub-buffer. */
398 for(index=0;index<numKeys;index++) {
399 WCHAR keyBuf[MAX_PATH+1];
400 HKEY subKey = 0;
401 DWORD reqdSize = MAX_PATH+1;
402 /* Get the sub-key name */
403 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
404 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100405 if (rc!=ERROR_SUCCESS) {
406 goto done;
407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 /* Open the sub-key */
409 rc=RegOpenKeyExW(newKey,
410 keyBuf, /* subkey */
411 0, /* reserved */
412 KEY_READ,
413 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100414 if (rc!=ERROR_SUCCESS) {
415 goto done;
416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Find the value of the buffer size, malloc, then read it */
418 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
419 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200420 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (ppPaths[index]) {
422 RegQueryValueExW(subKey, NULL, 0, NULL,
423 (LPBYTE)ppPaths[index],
424 &reqdSize);
425 dataSize += reqdSize + 1; /* 1 for the ";" */
426 }
427 }
428 RegCloseKey(subKey);
429 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100432 if (dataSize == 0) {
433 goto done;
434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435
436 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200437 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (dataBuf) {
439 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 /* Copy our collected strings */
441 for (index=0;index<numKeys;index++) {
442 if (index > 0) {
443 *(szCur++) = L';';
444 dataSize--;
445 }
446 if (ppPaths[index]) {
447 Py_ssize_t len = wcslen(ppPaths[index]);
448 wcsncpy(szCur, ppPaths[index], len);
449 szCur += len;
450 assert(dataSize > (DWORD)len);
451 dataSize -= (DWORD)len;
452 }
453 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100454 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 else {
luzpaza5293b42017-11-05 07:37:50 -0600458 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (numKeys) {
460 *(szCur++) = L';';
461 dataSize--;
462 }
463 /* Now append the core path entries -
464 this will include the NULL
465 */
466 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
467 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200468 if (rc != ERROR_SUCCESS) {
469 PyMem_RawFree(dataBuf);
470 goto done;
471 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 }
473 /* And set the result - caller must free */
474 retval = dataBuf;
475 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000476done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 /* Loop freeing my temp buffers */
478 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200479 for(index=0; index<numKeys; index++)
480 PyMem_RawFree(ppPaths[index]);
481 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100483 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100485 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200486 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000488}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000489#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000490
Victor Stinner0327bde2017-11-23 17:03:20 +0100491
Victor Stinner331a6a52019-05-27 16:39:22 +0200492static PyStatus
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200493get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100494{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200495 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800496 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100497 wchar_t program_full_path[MAXPATHLEN+1];
498 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100499
Steve Dower9048c492019-06-29 10:34:11 -0700500 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
501 /* GetModuleFileName should never fail when passed NULL */
502 return _PyStatus_ERR("Cannot determine program path");
503 }
504
Steve Dower1c3de542018-12-10 08:11:21 -0800505 /* The launcher may need to force the executable path to a
506 * different environment, so override it here. */
507 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
508 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower9048c492019-06-29 10:34:11 -0700509 /* If overridden, preserve the original full path */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200510 if (pathconfig->base_executable == NULL) {
511 pathconfig->base_executable = PyMem_RawMalloc(
512 sizeof(wchar_t) * (MAXPATHLEN + 1));
513 if (pathconfig->base_executable == NULL) {
514 return _PyStatus_NO_MEMORY();
515 }
516
517 status = canonicalize(pathconfig->base_executable,
518 program_full_path);
519 if (_PyStatus_EXCEPTION(status)) {
520 return status;
521 }
Steve Dower9048c492019-06-29 10:34:11 -0700522 }
523
Steve Dower1c3de542018-12-10 08:11:21 -0800524 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower9048c492019-06-29 10:34:11 -0700525 /* bpo-35873: Clear the environment variable to avoid it being
526 * inherited by child processes. */
527 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100528 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000529
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200530 if (pathconfig->program_full_path == NULL) {
531 pathconfig->program_full_path = PyMem_RawMalloc(
532 sizeof(wchar_t) * (MAXPATHLEN + 1));
533 if (pathconfig->program_full_path == NULL) {
534 return _PyStatus_NO_MEMORY();
535 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000536
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200537 status = canonicalize(pathconfig->program_full_path,
538 program_full_path);
539 if (_PyStatus_EXCEPTION(status)) {
540 return status;
541 }
542 }
543 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000544}
545
Victor Stinner0327bde2017-11-23 17:03:20 +0100546
Victor Stinner85ce0a72019-09-24 00:55:48 +0200547static PyStatus
548read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
549 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700550{
Victor Stinner85ce0a72019-09-24 00:55:48 +0200551 PyStatus status;
552 wchar_t *buf = NULL;
553 wchar_t *wline = NULL;
554 FILE *sp_file;
555
556 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100557 if (sp_file == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200558 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100559 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700560
Steve Dowered51b262016-09-17 12:54:06 -0700561 wcscpy_s(prefix, MAXPATHLEN+1, path);
562 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200563 pathconfig->isolated = 1;
564 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700565
Steve Dower4db86bc2016-09-09 09:17:35 -0700566 size_t bufsiz = MAXPATHLEN;
567 size_t prefixlen = wcslen(prefix);
568
Victor Stinner85ce0a72019-09-24 00:55:48 +0200569 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700570 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200571 status = _PyStatus_NO_MEMORY();
572 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700573 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700574 buf[0] = '\0';
575
576 while (!feof(sp_file)) {
577 char line[MAXPATHLEN + 1];
Victor Stinner85ce0a72019-09-24 00:55:48 +0200578 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100579 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700580 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100581 }
582 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700583 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100584 }
Steve Dowered51b262016-09-17 12:54:06 -0700585 while (*++p) {
586 if (*p == '\r' || *p == '\n') {
587 *p = '\0';
588 break;
589 }
590 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700591
Steve Dowered51b262016-09-17 12:54:06 -0700592 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200593 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700594 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200595 }
596 else if (strncmp(line, "import ", 7) == 0) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200597 status = _PyStatus_ERR("only 'import site' is supported "
598 "in ._pth file");
599 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700600 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700601
Steve Dowered51b262016-09-17 12:54:06 -0700602 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700603 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700604 if (wline == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200605 status = _PyStatus_NO_MEMORY();
606 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700607 }
Steve Dowered51b262016-09-17 12:54:06 -0700608 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700609 wline[wn] = '\0';
610
Steve Dowerc6dd4152016-10-27 14:28:07 -0700611 size_t usedsiz = wcslen(buf);
612 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700613 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700614 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
615 sizeof(wchar_t));
616 if (tmp == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200617 status = _PyStatus_NO_MEMORY();
618 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700619 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700620 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700621 }
622
Steve Dowerc6dd4152016-10-27 14:28:07 -0700623 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700624 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700625 usedsiz += 1;
626 }
Steve Dowered51b262016-09-17 12:54:06 -0700627
Steve Dowerc6dd4152016-10-27 14:28:07 -0700628 errno_t result;
629 _Py_BEGIN_SUPPRESS_IPH
630 result = wcscat_s(buf, bufsiz, prefix);
631 _Py_END_SUPPRESS_IPH
Victor Stinner85ce0a72019-09-24 00:55:48 +0200632
Steve Dowerc6dd4152016-10-27 14:28:07 -0700633 if (result == EINVAL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200634 status = _PyStatus_ERR("invalid argument during ._pth processing");
635 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700636 } else if (result == ERANGE) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200637 status = _PyStatus_ERR("buffer overflow during ._pth processing");
638 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700639 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200640
Steve Dowerc6dd4152016-10-27 14:28:07 -0700641 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700642 join(b, wline);
643
644 PyMem_RawFree(wline);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200645 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700646 }
647
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200648 if (pathconfig->module_search_path == NULL) {
649 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
650 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200651 status = _PyStatus_NO_MEMORY();
652 goto done;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200653 }
654 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700655
Victor Stinner85ce0a72019-09-24 00:55:48 +0200656 *found = 1;
657 status = _PyStatus_OK();
658 goto done;
659
660done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700661 PyMem_RawFree(buf);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200662 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700663 fclose(sp_file);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200664 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100665}
666
667
668static int
Victor Stinnerc4221672019-09-21 01:02:56 +0200669get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
670 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100671{
Steve Dower936a6602020-07-15 22:56:49 +0100672 if (!get_dllpath(filename) &&
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100673 !change_ext(filename, filename, L"._pth") &&
674 exists(filename))
675 {
676 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100677 }
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100678 if (pathconfig->program_full_path[0] &&
679 !change_ext(filename, pathconfig->program_full_path, L"._pth") &&
680 exists(filename))
681 {
682 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100683 }
684 return 0;
685}
686
687
Victor Stinner85ce0a72019-09-24 00:55:48 +0200688static PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200689calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200690 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100691{
Victor Stinnerc4221672019-09-21 01:02:56 +0200692 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100693
Victor Stinnerc4221672019-09-21 01:02:56 +0200694 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200695 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100696 }
697
Victor Stinner85ce0a72019-09-24 00:55:48 +0200698 return read_pth_file(pathconfig, prefix, filename, found);
Victor Stinner0327bde2017-11-23 17:03:20 +0100699}
700
701
702/* Search for an environment configuration file, first in the
703 executable's directory and then in the parent directory.
704 If found, open it for use when searching for prefixes.
705*/
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200706static PyStatus
Victor Stinner85ce0a72019-09-24 00:55:48 +0200707calculate_pyvenv_file(PyCalculatePath *calculate,
708 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100709{
Victor Stinner221fd842019-09-25 02:54:25 +0200710 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100711 const wchar_t *env_cfg = L"pyvenv.cfg";
712
Victor Stinner221fd842019-09-25 02:54:25 +0200713 /* Filename: <argv0_path_len> / "pyvenv.cfg" */
714 wcscpy_s(filename, MAXPATHLEN+1, argv0_path);
715 join(filename, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100716
Victor Stinner221fd842019-09-25 02:54:25 +0200717 FILE *env_file = _Py_wfopen(filename, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100718 if (env_file == NULL) {
719 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100720
Victor Stinner221fd842019-09-25 02:54:25 +0200721 /* Filename: <basename(basename(argv0_path_len))> / "pyvenv.cfg" */
722 reduce(filename);
723 reduce(filename);
724 join(filename, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100725
Victor Stinner221fd842019-09-25 02:54:25 +0200726 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100727 if (env_file == NULL) {
728 errno = 0;
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200729 return _PyStatus_OK();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100730 }
731 }
732
Victor Stinner0327bde2017-11-23 17:03:20 +0100733 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200734 wchar_t *home = NULL;
735 PyStatus status = _Py_FindEnvConfigValue(env_file, L"home", &home);
736 if (_PyStatus_EXCEPTION(status)) {
737 fclose(env_file);
738 return status;
739 }
740 if (home) {
Victor Stinner221fd842019-09-25 02:54:25 +0200741 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200742 PyMem_RawFree(home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100743 }
744 fclose(env_file);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200745 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100746}
747
748
749static void
Victor Stinner85ce0a72019-09-24 00:55:48 +0200750calculate_home_prefix(PyCalculatePath *calculate,
751 const wchar_t *argv0_path,
752 const wchar_t *zip_path,
753 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100754{
Victor Stinner0327bde2017-11-23 17:03:20 +0100755 if (calculate->home == NULL || *calculate->home == '\0') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200756 if (zip_path[0] && exists(zip_path)) {
757 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100758 reduce(prefix);
759 calculate->home = prefix;
760 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200761 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100762 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100763 }
764 else {
765 calculate->home = NULL;
766 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100768 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100769 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100770 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100771}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000772
Victor Stinner9316ee42017-11-25 03:17:57 +0100773
Victor Stinner331a6a52019-05-27 16:39:22 +0200774static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200775calculate_module_search_path(PyCalculatePath *calculate,
776 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200777 const wchar_t *argv0_path,
778 wchar_t *prefix,
779 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100780{
Victor Stinner0327bde2017-11-23 17:03:20 +0100781 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000782#ifdef Py_ENABLE_SHARED
Zackery Spytz676b10512020-03-30 10:04:45 -0600783 if (!Py_IgnoreEnvironmentFlag) {
784 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE,
785 skiphome);
786 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
787 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000788#endif
luzpaza5293b42017-11-05 07:37:50 -0600789 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 anything better to use! */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200791 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100792 calculate->home != NULL ||
793 calculate->machine_path != NULL ||
794 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* We need to construct a path from the following parts.
797 (1) the PYTHONPATH environment variable, if set;
798 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100799 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100801 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 (5) the directory containing the executable (argv0_path).
803 The length calculation calculates #4 first.
804 Extra rules:
805 - If PYTHONHOME is set (in any way) item (3) is ignored.
806 - If registry values are used, (4) and (5) are ignored.
807 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100810 size_t bufsz = 0;
811 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200812 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 bufsz = 1;
814 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100815 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100817 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100819 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700821 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200822 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100823 if (calculate->user_path) {
824 bufsz += wcslen(calculate->user_path) + 1;
825 }
826 if (calculate->machine_path) {
827 bufsz += wcslen(calculate->machine_path) + 1;
828 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200829 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200830 if (calculate->pythonpath_env != NULL) {
831 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100832 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000833
Victor Stinner0327bde2017-11-23 17:03:20 +0100834 wchar_t *buf, *start_buf;
835 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200837 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100839 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000840
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200841 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100842 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200843 calculate->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100844 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 buf = wcschr(buf, L'\0');
847 *buf++ = DELIM;
848 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200849 if (zip_path[0]) {
850 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100851 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100852 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 buf = wcschr(buf, L'\0');
854 *buf++ = DELIM;
855 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100856 if (calculate->user_path) {
857 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100858 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100859 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 buf = wcschr(buf, L'\0');
861 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 if (calculate->machine_path) {
864 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100865 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 buf = wcschr(buf, L'\0');
868 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100870 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100872 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100873 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700876 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700878 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200879 const wchar_t *p = PYTHONPATH;
880 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 size_t n;
882 for (;;) {
883 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100886 }
887 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100892 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100893 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 buf = wcschr(buf, L'\0');
895 p++;
896 n--;
897 }
898 wcsncpy(buf, p, n);
899 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700900 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100901 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100903 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 p = q+1;
905 }
906 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200907 if (argv0_path) {
908 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700910 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700912 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 /* Now to pull one last hack/trick. If sys.prefix is
915 empty, then try and find it somewhere on the paths
916 we calculated. We scan backwards, as our general policy
917 is that Python core directories are at the *end* of
918 sys.path. We assume that our "lib" directory is
919 on the path, and that our 'prefix' directory is
920 the parent of that.
921 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100922 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200924 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 while (1) {
926 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200927 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* 'look' will end up one character before the
929 start of the path in question - even if this
930 is one character before the start of the buffer
931 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100932 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 look--;
934 nchars = lookEnd-look;
935 wcsncpy(lookBuf, look+1, nchars);
936 lookBuf[nchars] = L'\0';
937 /* Up one level to the parent */
938 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100939 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 break;
941 }
942 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100943 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 look--;
947 }
948 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100949
Victor Stinner331a6a52019-05-27 16:39:22 +0200950 pathconfig->module_search_path = start_buf;
951 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100952}
953
954
Victor Stinner331a6a52019-05-27 16:39:22 +0200955static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200956calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100957{
Victor Stinner331a6a52019-05-27 16:39:22 +0200958 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100959
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200960 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200961 if (_PyStatus_EXCEPTION(status)) {
962 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100963 }
964
Victor Stinnerb64de462017-12-01 18:27:09 +0100965 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200966 wchar_t argv0_path[MAXPATHLEN+1];
967 memset(argv0_path, 0, sizeof(argv0_path));
968
969 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
970 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100971
972 wchar_t prefix[MAXPATHLEN+1];
973 memset(prefix, 0, sizeof(prefix));
974
975 /* Search for a sys.path file */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200976 int pth_found = 0;
977 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
978 if (_PyStatus_EXCEPTION(status)) {
979 return status;
980 }
981 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100982 goto done;
983 }
984
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200985 status = calculate_pyvenv_file(calculate,
986 argv0_path, Py_ARRAY_LENGTH(argv0_path));
987 if (_PyStatus_EXCEPTION(status)) {
988 return status;
989 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100990
991 /* Calculate zip archive path from DLL or exe path */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200992 wchar_t zip_path[MAXPATHLEN+1];
993 memset(zip_path, 0, sizeof(zip_path));
994
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100995 if (get_dllpath(zip_path) || change_ext(zip_path, zip_path, L".zip"))
996 {
997 if (change_ext(zip_path, pathconfig->program_full_path, L".zip")) {
998 zip_path[0] = L'\0';
999 }
1000 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001001
Victor Stinner85ce0a72019-09-24 00:55:48 +02001002 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001003
Victor Stinnere2677932019-09-21 01:50:16 +02001004 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001005 status = calculate_module_search_path(calculate, pathconfig,
1006 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001007 if (_PyStatus_EXCEPTION(status)) {
1008 return status;
1009 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001010 }
1011
1012done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001013 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001014 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1015 if (pathconfig->prefix == NULL) {
1016 return _PyStatus_NO_MEMORY();
1017 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001018 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001019 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001020 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1021 if (pathconfig->exec_prefix == NULL) {
1022 return _PyStatus_NO_MEMORY();
1023 }
Steve Dower177a41a2018-11-17 20:41:48 -08001024 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001025
Victor Stinner331a6a52019-05-27 16:39:22 +02001026 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001027}
1028
1029
Victor Stinner85ce0a72019-09-24 00:55:48 +02001030static PyStatus
1031calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1032 const PyConfig *config)
1033{
1034 calculate->home = pathconfig->home;
1035 calculate->path_env = _wgetenv(L"PATH");
1036
Victor Stinner85ce0a72019-09-24 00:55:48 +02001037 calculate->pythonpath_env = config->pythonpath_env;
1038
1039 return _PyStatus_OK();
1040}
1041
1042
Victor Stinner0327bde2017-11-23 17:03:20 +01001043static void
1044calculate_free(PyCalculatePath *calculate)
1045{
1046 PyMem_RawFree(calculate->machine_path);
1047 PyMem_RawFree(calculate->user_path);
1048}
1049
Victor Stinner9316ee42017-11-25 03:17:57 +01001050
Victor Stinner85ce0a72019-09-24 00:55:48 +02001051/* Calculate the Python path configuration.
1052
1053 Inputs:
1054
1055 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1056 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
Victor Stinner85ce0a72019-09-24 00:55:48 +02001057 - PATH environment variable
1058 - __PYVENV_LAUNCHER__ environment variable
1059 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1060 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001061 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001062 - pyvenv.cfg configuration file
1063 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001064 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1065 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001066
1067 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001068
1069 - base_executable
1070 - program_full_path
1071 - module_search_path
1072 - prefix
1073 - exec_prefix
1074 - isolated
1075 - site_import
1076
Victor Stinner85ce0a72019-09-24 00:55:48 +02001077 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001078PyStatus
1079_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001080{
Victor Stinnerc4221672019-09-21 01:02:56 +02001081 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001082 PyCalculatePath calculate;
1083 memset(&calculate, 0, sizeof(calculate));
1084
Victor Stinner85ce0a72019-09-24 00:55:48 +02001085 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001086 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001087 goto done;
1088 }
1089
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001090 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001091
Victor Stinner9316ee42017-11-25 03:17:57 +01001092done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001093 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001094 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001095}
1096
1097
Victor Stinner63941882011-09-29 00:42:28 +02001098/* Load python3.dll before loading any extension module that might refer
1099 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001100 to this python DLL is loaded, not a python3.dll that might be on the path
1101 by chance.
1102 Return whether the DLL was found.
1103*/
1104static int python3_checked = 0;
1105static HANDLE hPython3;
1106int
Victor Stinner31a83932017-12-04 13:39:15 +01001107_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001108{
1109 wchar_t py3path[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +01001110 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001111 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001112 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001113 python3_checked = 1;
1114
1115 /* If there is a python3.dll next to the python3y.dll,
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001116 use that DLL */
1117 if (!get_dllpath(py3path)) {
1118 reduce(py3path);
1119 join(py3path, PY3_DLLNAME);
1120 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1121 if (hPython3 != NULL) {
1122 return 1;
1123 }
Victor Stinnerc4221672019-09-21 01:02:56 +02001124 }
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001125
1126 /* If we can locate python3.dll in our application dir,
1127 use that DLL */
1128 hPython3 = LoadLibraryExW(PY3_DLLNAME, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
Victor Stinner0327bde2017-11-23 17:03:20 +01001129 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001130 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001131 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001132
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001133 /* For back-compat, also search {sys.prefix}\DLLs, though
1134 that has not been a normal install layout for a while */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001135 wcscpy(py3path, Py_GetPrefix());
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001136 if (py3path[0]) {
1137 join(py3path, L"DLLs\\" PY3_DLLNAME);
1138 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1139 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001140 return hPython3 != NULL;
1141}