blob: 7c0eeab5dbab4a36ee5957ac764c6dc026a45158 [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
Łukasz Langa8c1e1da2021-09-22 01:33:59 +020032 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>
Steve Dower4db86bc2016-09-09 09:17:35 -070094
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000096#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097#endif /* HAVE_SYS_TYPES_H */
98
99#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101#endif /* HAVE_SYS_STAT_H */
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103#include <string.h>
104
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105/* Search in some common locations for the associated Python libraries.
106 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107 * Py_GetPath() tries to return a sensible Python module search path.
108 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000109 * The approach is an adaptation for Windows of the strategy used in
110 * ../Modules/getpath.c; it uses the Windows Registry as one of its
111 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000112 *
113 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
114 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115 */
116
117#ifndef LANDMARK
Victor Stinner85ce0a72019-09-24 00:55:48 +0200118# define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119#endif
120
Victor Stinner85ce0a72019-09-24 00:55:48 +0200121#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
122
123
Victor Stinner0327bde2017-11-23 17:03:20 +0100124typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200125 const wchar_t *path_env; /* PATH environment variable */
126 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100127
Victor Stinner85ce0a72019-09-24 00:55:48 +0200128 /* Registry key "Software\Python\PythonCore\X.Y\PythonPath"
129 where X.Y is the Python version (major.minor) */
Victor Stinner0327bde2017-11-23 17:03:20 +0100130 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
131 wchar_t *user_path; /* from HKEY_CURRENT_USER */
132
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200133 const wchar_t *pythonpath_env;
Victor Stinner0327bde2017-11-23 17:03:20 +0100134} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000135
Guido van Rossumeea14491997-08-13 21:30:44 +0000136
Victor Stinner0327bde2017-11-23 17:03:20 +0100137/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100139is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140{
141#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000143#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000145#endif
146}
147
Victor Stinner0327bde2017-11-23 17:03:20 +0100148
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000149/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100150 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000151static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000152reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000153{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700154 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100155 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700156 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100157 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 while (i > 0 && !is_sep(dir[i]))
160 --i;
161 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000162}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163
Victor Stinner0327bde2017-11-23 17:03:20 +0100164
Steve Dowered51b262016-09-17 12:54:06 -0700165static int
166change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
167{
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100168 if (src && src != dest) {
169 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
170 size_t i = src_len;
171 if (i >= MAXPATHLEN+1) {
172 Py_FatalError("buffer overflow in getpathp.c's reduce()");
173 }
174
175 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
176 --i;
177
178 if (i == 0) {
179 dest[0] = '\0';
180 return -1;
181 }
182
183 if (is_sep(src[i])) {
184 i = src_len;
185 }
186
187 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i)) {
188 dest[0] = '\0';
189 return -1;
190 }
191 } else {
192 wchar_t *s = wcsrchr(dest, L'.');
193 if (s) {
194 s[0] = '\0';
195 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100196 }
Steve Dowered51b262016-09-17 12:54:06 -0700197
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100198 if (wcscat_s(dest, MAXPATHLEN+1, ext)) {
Steve Dowered51b262016-09-17 12:54:06 -0700199 dest[0] = '\0';
200 return -1;
201 }
202
203 return 0;
204}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000205
Victor Stinner0327bde2017-11-23 17:03:20 +0100206
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000207static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200208exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000211}
212
Victor Stinner0327bde2017-11-23 17:03:20 +0100213
214/* Is module -- check for .pyc too.
215 Assumes 'filename' MAXPATHLEN+1 bytes long -
216 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000217static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100218ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000219{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100220 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700221
Victor Stinner0327bde2017-11-23 17:03:20 +0100222 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100224 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700227 n = wcsnlen_s(filename, MAXPATHLEN+1);
228 if (n < MAXPATHLEN) {
229 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800230 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700231 filename[n + 1] = L'\0';
232 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100233 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700234 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100235 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700236 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 }
238 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000239}
240
Victor Stinner0327bde2017-11-23 17:03:20 +0100241
Tim Peters8484fbf2004-08-07 19:12:27 +0000242/* Add a path component, by appending stuff to buffer.
243 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
244 NUL-terminated string with no more than MAXPATHLEN characters (not counting
245 the trailing NUL). It's a fatal error if it contains a string longer than
246 that (callers must be careful!). If these requirements are met, it's
247 guaranteed that buffer will still be a NUL-terminated string with no more
248 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
249 stuff as fits will be appended.
250*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700251
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000252static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700253join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000254{
Steve Dower6a65eba2020-01-29 13:46:33 +1100255 if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
256 Py_FatalError("buffer overflow in getpathp.c's join()");
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700257 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000258}
259
Victor Stinner85ce0a72019-09-24 00:55:48 +0200260/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
261 and ".." to produce a direct, well-formed path. */
262static PyStatus
263canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800264{
265 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200266 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800267 }
268
Steve Dower6a65eba2020-01-29 13:46:33 +1100269 if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
270 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800271 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200272 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800273}
274
Victor Stinner0327bde2017-11-23 17:03:20 +0100275
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000276/* gotlandmark only called by search_for_prefix, which ensures
277 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100278 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000279static int
Victor Stinnerdec39712019-09-30 14:49:34 +0200280gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000281{
Victor Stinnerdec39712019-09-30 14:49:34 +0200282 wchar_t filename[MAXPATHLEN+1];
283 memset(filename, 0, sizeof(filename));
284 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
285 join(filename, landmark);
286 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000287}
288
Victor Stinner0327bde2017-11-23 17:03:20 +0100289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200291 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000292static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200293search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700296 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100298 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 reduce(prefix);
302 } while (prefix[0]);
303 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000304}
305
Victor Stinner0327bde2017-11-23 17:03:20 +0100306
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100307static int
308get_dllpath(wchar_t *dllpath)
309{
310#ifdef Py_ENABLE_SHARED
311 extern HANDLE PyWin_DLLhModule;
312 if (PyWin_DLLhModule && GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) {
313 return 0;
314 }
315#endif
316 return -1;
317}
318
319
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000320#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000321
Guido van Rossum88716bb2000-03-30 19:45:39 +0000322/* a string loaded from the DLL at startup.*/
323extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000324
Guido van Rossumeea14491997-08-13 21:30:44 +0000325/* Load a PYTHONPATH value from the registry.
326 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
327
Guido van Rossum88716bb2000-03-30 19:45:39 +0000328 Works in both Unicode and 8bit environments. Only uses the
329 Ex family of functions so it also works with Windows CE.
330
Guido van Rossumeea14491997-08-13 21:30:44 +0000331 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000332
333 XXX - this code is pretty strange, as it used to also
Christian Claussdcfbe4f2021-10-07 16:31:33 +0200334 work on Win16, where the buffer sizes were not available
Mark Hammond5edc6272001-02-23 11:38:38 +0000335 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000336*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000337static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000338getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 HKEY newKey = 0;
341 DWORD dataSize = 0;
342 DWORD numKeys = 0;
343 LONG rc;
344 wchar_t *retval = NULL;
345 WCHAR *dataBuf = NULL;
346 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
347 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700348 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 DWORD index;
350 WCHAR *keyBuf = NULL;
351 WCHAR *keyBufPtr;
352 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 /* Tried to use sysget("winver") but here is too early :-( */
355 versionLen = strlen(PyWin_DLLVersionString);
356 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700357 keyBufLen = sizeof(keyPrefix) +
358 sizeof(WCHAR)*(versionLen-1) +
359 sizeof(keySuffix);
360 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100361 if (keyBuf==NULL) {
362 goto done;
363 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700365 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200366 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
368 keyBufPtr += versionLen;
369 /* NULL comes with this one! */
370 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
371 /* Open the root Python key */
372 rc=RegOpenKeyExW(keyBase,
373 keyBuf, /* subkey */
374 0, /* reserved */
375 KEY_READ,
376 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100377 if (rc!=ERROR_SUCCESS) {
378 goto done;
379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* Find out how big our core buffer is, and how many subkeys we have */
Minmin Gong98e42d12020-05-18 09:50:03 -0700381 rc = RegQueryInfoKeyW(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100383 if (rc!=ERROR_SUCCESS) {
384 goto done;
385 }
386 if (skipcore) {
387 dataSize = 0; /* Only count core ones if we want them! */
388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Allocate a temp array of char buffers, so we only need to loop
390 reading the registry once
391 */
Andy Lester7668a8b2020-03-24 23:26:44 -0500392 ppPaths = PyMem_RawCalloc(numKeys, sizeof(WCHAR *));
Victor Stinner0327bde2017-11-23 17:03:20 +0100393 if (ppPaths==NULL) {
394 goto done;
395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 /* Loop over all subkeys, allocating a temp sub-buffer. */
397 for(index=0;index<numKeys;index++) {
398 WCHAR keyBuf[MAX_PATH+1];
399 HKEY subKey = 0;
400 DWORD reqdSize = MAX_PATH+1;
401 /* Get the sub-key name */
402 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
403 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100404 if (rc!=ERROR_SUCCESS) {
405 goto done;
406 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Open the sub-key */
408 rc=RegOpenKeyExW(newKey,
409 keyBuf, /* subkey */
410 0, /* reserved */
411 KEY_READ,
412 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100413 if (rc!=ERROR_SUCCESS) {
414 goto done;
415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* Find the value of the buffer size, malloc, then read it */
417 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
418 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200419 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (ppPaths[index]) {
421 RegQueryValueExW(subKey, NULL, 0, NULL,
422 (LPBYTE)ppPaths[index],
423 &reqdSize);
424 dataSize += reqdSize + 1; /* 1 for the ";" */
425 }
426 }
427 RegCloseKey(subKey);
428 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100431 if (dataSize == 0) {
432 goto done;
433 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434
435 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200436 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (dataBuf) {
438 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 /* Copy our collected strings */
440 for (index=0;index<numKeys;index++) {
441 if (index > 0) {
442 *(szCur++) = L';';
443 dataSize--;
444 }
445 if (ppPaths[index]) {
446 Py_ssize_t len = wcslen(ppPaths[index]);
447 wcsncpy(szCur, ppPaths[index], len);
448 szCur += len;
449 assert(dataSize > (DWORD)len);
450 dataSize -= (DWORD)len;
451 }
452 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100453 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 else {
luzpaza5293b42017-11-05 07:37:50 -0600457 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (numKeys) {
459 *(szCur++) = L';';
460 dataSize--;
461 }
462 /* Now append the core path entries -
463 this will include the NULL
464 */
465 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
466 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200467 if (rc != ERROR_SUCCESS) {
468 PyMem_RawFree(dataBuf);
469 goto done;
470 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 }
472 /* And set the result - caller must free */
473 retval = dataBuf;
474 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000475done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 /* Loop freeing my temp buffers */
477 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200478 for(index=0; index<numKeys; index++)
479 PyMem_RawFree(ppPaths[index]);
480 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100482 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100484 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200485 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000487}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000488#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000489
Victor Stinner0327bde2017-11-23 17:03:20 +0100490
Victor Stinner331a6a52019-05-27 16:39:22 +0200491static PyStatus
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200492get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100493{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200494 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800495 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100496 wchar_t program_full_path[MAXPATHLEN+1];
497 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100498
Steve Dower9048c492019-06-29 10:34:11 -0700499 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
500 /* GetModuleFileName should never fail when passed NULL */
501 return _PyStatus_ERR("Cannot determine program path");
502 }
503
Steve Dower1c3de542018-12-10 08:11:21 -0800504 /* The launcher may need to force the executable path to a
505 * different environment, so override it here. */
506 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
507 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower9048c492019-06-29 10:34:11 -0700508 /* If overridden, preserve the original full path */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200509 if (pathconfig->base_executable == NULL) {
510 pathconfig->base_executable = PyMem_RawMalloc(
511 sizeof(wchar_t) * (MAXPATHLEN + 1));
512 if (pathconfig->base_executable == NULL) {
513 return _PyStatus_NO_MEMORY();
514 }
515
516 status = canonicalize(pathconfig->base_executable,
517 program_full_path);
518 if (_PyStatus_EXCEPTION(status)) {
519 return status;
520 }
Steve Dower9048c492019-06-29 10:34:11 -0700521 }
522
Steve Dower1c3de542018-12-10 08:11:21 -0800523 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower9048c492019-06-29 10:34:11 -0700524 /* bpo-35873: Clear the environment variable to avoid it being
525 * inherited by child processes. */
526 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100527 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000528
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200529 if (pathconfig->program_full_path == NULL) {
530 pathconfig->program_full_path = PyMem_RawMalloc(
531 sizeof(wchar_t) * (MAXPATHLEN + 1));
532 if (pathconfig->program_full_path == NULL) {
533 return _PyStatus_NO_MEMORY();
534 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000535
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200536 status = canonicalize(pathconfig->program_full_path,
537 program_full_path);
538 if (_PyStatus_EXCEPTION(status)) {
539 return status;
540 }
541 }
542 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000543}
544
Victor Stinner0327bde2017-11-23 17:03:20 +0100545
Victor Stinner85ce0a72019-09-24 00:55:48 +0200546static PyStatus
547read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
548 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700549{
Victor Stinner85ce0a72019-09-24 00:55:48 +0200550 PyStatus status;
551 wchar_t *buf = NULL;
552 wchar_t *wline = NULL;
553 FILE *sp_file;
554
555 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100556 if (sp_file == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200557 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100558 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700559
Steve Dowered51b262016-09-17 12:54:06 -0700560 wcscpy_s(prefix, MAXPATHLEN+1, path);
561 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200562 pathconfig->isolated = 1;
563 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700564
Steve Dower4db86bc2016-09-09 09:17:35 -0700565 size_t bufsiz = MAXPATHLEN;
566 size_t prefixlen = wcslen(prefix);
567
Victor Stinner85ce0a72019-09-24 00:55:48 +0200568 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700569 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200570 status = _PyStatus_NO_MEMORY();
571 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700572 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700573 buf[0] = '\0';
574
575 while (!feof(sp_file)) {
576 char line[MAXPATHLEN + 1];
Victor Stinner85ce0a72019-09-24 00:55:48 +0200577 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100578 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700579 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100580 }
581 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700582 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100583 }
Steve Dowered51b262016-09-17 12:54:06 -0700584 while (*++p) {
585 if (*p == '\r' || *p == '\n') {
586 *p = '\0';
587 break;
588 }
589 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700590
Steve Dowered51b262016-09-17 12:54:06 -0700591 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200592 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700593 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200594 }
595 else if (strncmp(line, "import ", 7) == 0) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200596 status = _PyStatus_ERR("only 'import site' is supported "
597 "in ._pth file");
598 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700599 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700600
Steve Dowered51b262016-09-17 12:54:06 -0700601 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700602 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700603 if (wline == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200604 status = _PyStatus_NO_MEMORY();
605 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700606 }
Steve Dowered51b262016-09-17 12:54:06 -0700607 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700608 wline[wn] = '\0';
609
Steve Dowerc6dd4152016-10-27 14:28:07 -0700610 size_t usedsiz = wcslen(buf);
611 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700612 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700613 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
614 sizeof(wchar_t));
615 if (tmp == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200616 status = _PyStatus_NO_MEMORY();
617 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700618 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700619 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700620 }
621
Steve Dowerc6dd4152016-10-27 14:28:07 -0700622 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700623 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700624 usedsiz += 1;
625 }
Steve Dowered51b262016-09-17 12:54:06 -0700626
Steve Dowerc6dd4152016-10-27 14:28:07 -0700627 errno_t result;
628 _Py_BEGIN_SUPPRESS_IPH
629 result = wcscat_s(buf, bufsiz, prefix);
630 _Py_END_SUPPRESS_IPH
Victor Stinner85ce0a72019-09-24 00:55:48 +0200631
Steve Dowerc6dd4152016-10-27 14:28:07 -0700632 if (result == EINVAL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200633 status = _PyStatus_ERR("invalid argument during ._pth processing");
634 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700635 } else if (result == ERANGE) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200636 status = _PyStatus_ERR("buffer overflow during ._pth processing");
637 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700638 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200639
Steve Dowerc6dd4152016-10-27 14:28:07 -0700640 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700641 join(b, wline);
642
643 PyMem_RawFree(wline);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200644 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700645 }
646
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200647 if (pathconfig->module_search_path == NULL) {
648 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
649 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200650 status = _PyStatus_NO_MEMORY();
651 goto done;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200652 }
653 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700654
Victor Stinner85ce0a72019-09-24 00:55:48 +0200655 *found = 1;
656 status = _PyStatus_OK();
657 goto done;
658
659done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700660 PyMem_RawFree(buf);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200661 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700662 fclose(sp_file);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200663 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100664}
665
666
667static int
Victor Stinnerc4221672019-09-21 01:02:56 +0200668get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
669 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100670{
Steve Dower936a6602020-07-15 22:56:49 +0100671 if (!get_dllpath(filename) &&
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100672 !change_ext(filename, filename, L"._pth") &&
673 exists(filename))
674 {
675 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100676 }
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100677 if (pathconfig->program_full_path[0] &&
678 !change_ext(filename, pathconfig->program_full_path, L"._pth") &&
679 exists(filename))
680 {
681 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100682 }
683 return 0;
684}
685
686
Victor Stinner85ce0a72019-09-24 00:55:48 +0200687static PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200688calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200689 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100690{
Victor Stinnerc4221672019-09-21 01:02:56 +0200691 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100692
Victor Stinnerc4221672019-09-21 01:02:56 +0200693 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200694 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100695 }
696
Victor Stinner85ce0a72019-09-24 00:55:48 +0200697 return read_pth_file(pathconfig, prefix, filename, found);
Victor Stinner0327bde2017-11-23 17:03:20 +0100698}
699
700
701/* Search for an environment configuration file, first in the
702 executable's directory and then in the parent directory.
703 If found, open it for use when searching for prefixes.
704*/
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200705static PyStatus
Victor Stinner85ce0a72019-09-24 00:55:48 +0200706calculate_pyvenv_file(PyCalculatePath *calculate,
707 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100708{
Victor Stinner221fd842019-09-25 02:54:25 +0200709 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100710 const wchar_t *env_cfg = L"pyvenv.cfg";
711
Victor Stinner221fd842019-09-25 02:54:25 +0200712 /* Filename: <argv0_path_len> / "pyvenv.cfg" */
713 wcscpy_s(filename, MAXPATHLEN+1, argv0_path);
714 join(filename, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100715
Victor Stinner221fd842019-09-25 02:54:25 +0200716 FILE *env_file = _Py_wfopen(filename, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100717 if (env_file == NULL) {
718 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100719
Victor Stinner221fd842019-09-25 02:54:25 +0200720 /* Filename: <basename(basename(argv0_path_len))> / "pyvenv.cfg" */
721 reduce(filename);
722 reduce(filename);
723 join(filename, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100724
Victor Stinner221fd842019-09-25 02:54:25 +0200725 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100726 if (env_file == NULL) {
727 errno = 0;
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200728 return _PyStatus_OK();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100729 }
730 }
731
Victor Stinner0327bde2017-11-23 17:03:20 +0100732 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200733 wchar_t *home = NULL;
734 PyStatus status = _Py_FindEnvConfigValue(env_file, L"home", &home);
735 if (_PyStatus_EXCEPTION(status)) {
736 fclose(env_file);
737 return status;
738 }
739 if (home) {
Victor Stinner221fd842019-09-25 02:54:25 +0200740 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200741 PyMem_RawFree(home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100742 }
743 fclose(env_file);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200744 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100745}
746
747
748static void
Victor Stinner85ce0a72019-09-24 00:55:48 +0200749calculate_home_prefix(PyCalculatePath *calculate,
750 const wchar_t *argv0_path,
751 const wchar_t *zip_path,
752 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100753{
Victor Stinner0327bde2017-11-23 17:03:20 +0100754 if (calculate->home == NULL || *calculate->home == '\0') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200755 if (zip_path[0] && exists(zip_path)) {
756 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100757 reduce(prefix);
758 calculate->home = prefix;
759 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200760 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100761 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100762 }
763 else {
764 calculate->home = NULL;
765 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100767 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100768 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100769 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100770}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000771
Victor Stinner9316ee42017-11-25 03:17:57 +0100772
Victor Stinner331a6a52019-05-27 16:39:22 +0200773static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200774calculate_module_search_path(PyCalculatePath *calculate,
775 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200776 const wchar_t *argv0_path,
777 wchar_t *prefix,
778 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100779{
Victor Stinner0327bde2017-11-23 17:03:20 +0100780 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000781#ifdef Py_ENABLE_SHARED
Zackery Spytz676b10512020-03-30 10:04:45 -0600782 if (!Py_IgnoreEnvironmentFlag) {
783 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE,
784 skiphome);
785 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
786 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000787#endif
luzpaza5293b42017-11-05 07:37:50 -0600788 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 anything better to use! */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200790 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100791 calculate->home != NULL ||
792 calculate->machine_path != NULL ||
793 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 /* We need to construct a path from the following parts.
796 (1) the PYTHONPATH environment variable, if set;
797 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100798 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100800 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 (5) the directory containing the executable (argv0_path).
802 The length calculation calculates #4 first.
803 Extra rules:
804 - If PYTHONHOME is set (in any way) item (3) is ignored.
805 - If registry values are used, (4) and (5) are ignored.
806 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100809 size_t bufsz = 0;
810 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200811 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 bufsz = 1;
813 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100814 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100816 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100818 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700820 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200821 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100822 if (calculate->user_path) {
823 bufsz += wcslen(calculate->user_path) + 1;
824 }
825 if (calculate->machine_path) {
826 bufsz += wcslen(calculate->machine_path) + 1;
827 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200828 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200829 if (calculate->pythonpath_env != NULL) {
830 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100831 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000832
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 wchar_t *buf, *start_buf;
834 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200836 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100838 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000839
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200840 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100841 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200842 calculate->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100843 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100844 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 buf = wcschr(buf, L'\0');
846 *buf++ = DELIM;
847 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200848 if (zip_path[0]) {
849 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100850 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100851 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 buf = wcschr(buf, L'\0');
853 *buf++ = DELIM;
854 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100855 if (calculate->user_path) {
856 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100857 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100858 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 buf = wcschr(buf, L'\0');
860 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100862 if (calculate->machine_path) {
863 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100864 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 buf = wcschr(buf, L'\0');
867 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100869 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100871 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100872 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100873 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700875 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700877 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200878 const wchar_t *p = PYTHONPATH;
879 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 size_t n;
881 for (;;) {
882 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100883 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 }
886 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100888 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100890 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100891 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 buf = wcschr(buf, L'\0');
894 p++;
895 n--;
896 }
897 wcsncpy(buf, p, n);
898 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700899 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100900 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 p = q+1;
904 }
905 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200906 if (argv0_path) {
907 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700909 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700911 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 /* Now to pull one last hack/trick. If sys.prefix is
914 empty, then try and find it somewhere on the paths
915 we calculated. We scan backwards, as our general policy
916 is that Python core directories are at the *end* of
917 sys.path. We assume that our "lib" directory is
918 on the path, and that our 'prefix' directory is
919 the parent of that.
920 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100921 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200923 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 while (1) {
925 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200926 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 /* 'look' will end up one character before the
928 start of the path in question - even if this
929 is one character before the start of the buffer
930 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100931 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 look--;
933 nchars = lookEnd-look;
934 wcsncpy(lookBuf, look+1, nchars);
935 lookBuf[nchars] = L'\0';
936 /* Up one level to the parent */
937 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100938 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 break;
940 }
941 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100942 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100944 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 look--;
946 }
947 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100948
Victor Stinner331a6a52019-05-27 16:39:22 +0200949 pathconfig->module_search_path = start_buf;
950 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100951}
952
953
Victor Stinner331a6a52019-05-27 16:39:22 +0200954static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200955calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100956{
Victor Stinner331a6a52019-05-27 16:39:22 +0200957 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100958
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200959 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200960 if (_PyStatus_EXCEPTION(status)) {
961 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100962 }
963
Victor Stinnerb64de462017-12-01 18:27:09 +0100964 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200965 wchar_t argv0_path[MAXPATHLEN+1];
966 memset(argv0_path, 0, sizeof(argv0_path));
967
968 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
969 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100970
971 wchar_t prefix[MAXPATHLEN+1];
972 memset(prefix, 0, sizeof(prefix));
973
974 /* Search for a sys.path file */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200975 int pth_found = 0;
976 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
977 if (_PyStatus_EXCEPTION(status)) {
978 return status;
979 }
980 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100981 goto done;
982 }
983
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200984 status = calculate_pyvenv_file(calculate,
985 argv0_path, Py_ARRAY_LENGTH(argv0_path));
986 if (_PyStatus_EXCEPTION(status)) {
987 return status;
988 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100989
990 /* Calculate zip archive path from DLL or exe path */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200991 wchar_t zip_path[MAXPATHLEN+1];
992 memset(zip_path, 0, sizeof(zip_path));
993
Steve Dowerdcbaa1b2020-07-06 17:32:00 +0100994 if (get_dllpath(zip_path) || change_ext(zip_path, zip_path, L".zip"))
995 {
996 if (change_ext(zip_path, pathconfig->program_full_path, L".zip")) {
997 zip_path[0] = L'\0';
998 }
999 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001000
Victor Stinner85ce0a72019-09-24 00:55:48 +02001001 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001002
Victor Stinnere2677932019-09-21 01:50:16 +02001003 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001004 status = calculate_module_search_path(calculate, pathconfig,
1005 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001006 if (_PyStatus_EXCEPTION(status)) {
1007 return status;
1008 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001009 }
1010
1011done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001012 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001013 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1014 if (pathconfig->prefix == NULL) {
1015 return _PyStatus_NO_MEMORY();
1016 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001017 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001018 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001019 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1020 if (pathconfig->exec_prefix == NULL) {
1021 return _PyStatus_NO_MEMORY();
1022 }
Steve Dower177a41a2018-11-17 20:41:48 -08001023 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001024
Victor Stinner331a6a52019-05-27 16:39:22 +02001025 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001026}
1027
1028
Victor Stinner85ce0a72019-09-24 00:55:48 +02001029static PyStatus
1030calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1031 const PyConfig *config)
1032{
1033 calculate->home = pathconfig->home;
1034 calculate->path_env = _wgetenv(L"PATH");
1035
Victor Stinner85ce0a72019-09-24 00:55:48 +02001036 calculate->pythonpath_env = config->pythonpath_env;
1037
1038 return _PyStatus_OK();
1039}
1040
1041
Victor Stinner0327bde2017-11-23 17:03:20 +01001042static void
1043calculate_free(PyCalculatePath *calculate)
1044{
1045 PyMem_RawFree(calculate->machine_path);
1046 PyMem_RawFree(calculate->user_path);
1047}
1048
Victor Stinner9316ee42017-11-25 03:17:57 +01001049
Victor Stinner85ce0a72019-09-24 00:55:48 +02001050/* Calculate the Python path configuration.
1051
1052 Inputs:
1053
1054 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1055 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
Victor Stinner85ce0a72019-09-24 00:55:48 +02001056 - PATH environment variable
1057 - __PYVENV_LAUNCHER__ environment variable
1058 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1059 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001060 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001061 - pyvenv.cfg configuration file
1062 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001063 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1064 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001065
1066 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001067
1068 - base_executable
1069 - program_full_path
1070 - module_search_path
1071 - prefix
1072 - exec_prefix
1073 - isolated
1074 - site_import
1075
Victor Stinner85ce0a72019-09-24 00:55:48 +02001076 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001077PyStatus
1078_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001079{
Victor Stinnerc4221672019-09-21 01:02:56 +02001080 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001081 PyCalculatePath calculate;
1082 memset(&calculate, 0, sizeof(calculate));
1083
Victor Stinner85ce0a72019-09-24 00:55:48 +02001084 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001085 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001086 goto done;
1087 }
1088
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001089 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001090
Victor Stinner9316ee42017-11-25 03:17:57 +01001091done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001092 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001093 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001094}
1095
1096
Victor Stinner63941882011-09-29 00:42:28 +02001097/* Load python3.dll before loading any extension module that might refer
1098 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001099 to this python DLL is loaded, not a python3.dll that might be on the path
1100 by chance.
1101 Return whether the DLL was found.
1102*/
1103static int python3_checked = 0;
1104static HANDLE hPython3;
1105int
Victor Stinner31a83932017-12-04 13:39:15 +01001106_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001107{
1108 wchar_t py3path[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +01001109 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001110 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001111 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001112 python3_checked = 1;
1113
1114 /* If there is a python3.dll next to the python3y.dll,
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001115 use that DLL */
1116 if (!get_dllpath(py3path)) {
1117 reduce(py3path);
1118 join(py3path, PY3_DLLNAME);
1119 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1120 if (hPython3 != NULL) {
1121 return 1;
1122 }
Victor Stinnerc4221672019-09-21 01:02:56 +02001123 }
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001124
1125 /* If we can locate python3.dll in our application dir,
1126 use that DLL */
1127 hPython3 = LoadLibraryExW(PY3_DLLNAME, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
Victor Stinner0327bde2017-11-23 17:03:20 +01001128 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001129 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001130 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001131
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001132 /* For back-compat, also search {sys.prefix}\DLLs, though
1133 that has not been a normal install layout for a while */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001134 wcscpy(py3path, Py_GetPrefix());
Steve Dowerdcbaa1b2020-07-06 17:32:00 +01001135 if (py3path[0]) {
1136 join(py3path, L"DLLs\\" PY3_DLLNAME);
1137 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1138 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001139 return hPython3 != NULL;
1140}