blob: cf20b9f613c87b7f49eb3d6afbd297d3821247b9 [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 Stinner331a6a52019-05-27 16:39:22 +020083#include "pycore_initconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010084#include "pycore_pystate.h"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085#include "osdefs.h"
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>
erikjanss6cf82552018-07-25 02:41:46 +020093#include <shlwapi.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
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700118# define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119#endif
120
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700121#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
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700128 /* 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 Stinner3f5409a2019-09-23 19:50:27 +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{
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -0700168 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
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -0700198 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
252static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100253typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
254 PCWSTR pszPathIn, PCWSTR pszMore,
255 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700256static PPathCchCombineEx _PathCchCombineEx;
257
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000258static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700259join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000260{
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 if (_PathCchCombineEx_Initialized == 0) {
Steve Dowerad4a20b2020-01-30 17:18:25 +1100262 HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
263 LOAD_LIBRARY_SEARCH_SYSTEM32);
Victor Stinner0327bde2017-11-23 17:03:20 +0100264 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700265 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100266 }
267 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700268 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100269 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700270 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700272
Steve Dower4db86bc2016-09-09 09:17:35 -0700273 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100274 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700275 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100276 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700277 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100278 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700279 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100280 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700281 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000282}
283
Steve Dower48e8c822018-02-22 10:39:26 -0800284static int _PathCchCanonicalizeEx_Initialized = 0;
285typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
286 PCWSTR pszPathIn, unsigned long dwFlags);
287static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
288
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700289/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
290 and ".." to produce a direct, well-formed path. */
291static PyStatus
292canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800293{
294 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200295 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800296 }
297
298 if (_PathCchCanonicalizeEx_Initialized == 0) {
Steve Dowerad4a20b2020-01-30 17:18:25 +1100299 HMODULE pathapi = LoadLibraryExW(L"api-ms-win-core-path-l1-1-0.dll", NULL,
300 LOAD_LIBRARY_SEARCH_SYSTEM32);
Steve Dower48e8c822018-02-22 10:39:26 -0800301 if (pathapi) {
302 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
303 }
304 else {
305 _PathCchCanonicalizeEx = NULL;
306 }
307 _PathCchCanonicalizeEx_Initialized = 1;
308 }
309
310 if (_PathCchCanonicalizeEx) {
311 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700312 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800313 }
314 }
315 else {
316 if (!PathCanonicalizeW(buffer, path)) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700317 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800318 }
319 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200320 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800321}
322
Victor Stinner0327bde2017-11-23 17:03:20 +0100323
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000324/* gotlandmark only called by search_for_prefix, which ensures
325 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100326 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000327static int
Victor Stinner18c4ba92019-09-30 14:49:42 +0200328gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000329{
Victor Stinner18c4ba92019-09-30 14:49:42 +0200330 wchar_t filename[MAXPATHLEN+1];
331 memset(filename, 0, sizeof(filename));
332 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
333 join(filename, landmark);
334 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000335}
336
Victor Stinner0327bde2017-11-23 17:03:20 +0100337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner3f5409a2019-09-23 19:50:27 +0200339 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000340static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200341search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700344 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100346 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 reduce(prefix);
350 } while (prefix[0]);
351 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000352}
353
Victor Stinner0327bde2017-11-23 17:03:20 +0100354
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -0700355static int
356get_dllpath(wchar_t *dllpath)
357{
358#ifdef Py_ENABLE_SHARED
359 extern HANDLE PyWin_DLLhModule;
360 if (PyWin_DLLhModule && GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) {
361 return 0;
362 }
363#endif
364 return -1;
365}
366
367
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000368#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000369
Guido van Rossum88716bb2000-03-30 19:45:39 +0000370/* a string loaded from the DLL at startup.*/
371extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000372
Guido van Rossumeea14491997-08-13 21:30:44 +0000373/* Load a PYTHONPATH value from the registry.
374 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
375
Guido van Rossum88716bb2000-03-30 19:45:39 +0000376 Works in both Unicode and 8bit environments. Only uses the
377 Ex family of functions so it also works with Windows CE.
378
Guido van Rossumeea14491997-08-13 21:30:44 +0000379 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000380
381 XXX - this code is pretty strange, as it used to also
382 work on Win16, where the buffer sizes werent available
383 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000384*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000385static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000386getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 HKEY newKey = 0;
389 DWORD dataSize = 0;
390 DWORD numKeys = 0;
391 LONG rc;
392 wchar_t *retval = NULL;
393 WCHAR *dataBuf = NULL;
394 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
395 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700396 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 DWORD index;
398 WCHAR *keyBuf = NULL;
399 WCHAR *keyBufPtr;
400 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 /* Tried to use sysget("winver") but here is too early :-( */
403 versionLen = strlen(PyWin_DLLVersionString);
404 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700405 keyBufLen = sizeof(keyPrefix) +
406 sizeof(WCHAR)*(versionLen-1) +
407 sizeof(keySuffix);
408 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100409 if (keyBuf==NULL) {
410 goto done;
411 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000412
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700413 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200414 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
416 keyBufPtr += versionLen;
417 /* NULL comes with this one! */
418 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
419 /* Open the root Python key */
420 rc=RegOpenKeyExW(keyBase,
421 keyBuf, /* subkey */
422 0, /* reserved */
423 KEY_READ,
424 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100425 if (rc!=ERROR_SUCCESS) {
426 goto done;
427 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* Find out how big our core buffer is, and how many subkeys we have */
Miss Islington (bot)460eac22020-05-18 10:09:59 -0700429 rc = RegQueryInfoKeyW(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100431 if (rc!=ERROR_SUCCESS) {
432 goto done;
433 }
434 if (skipcore) {
435 dataSize = 0; /* Only count core ones if we want them! */
436 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 /* Allocate a temp array of char buffers, so we only need to loop
438 reading the registry once
439 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200440 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100441 if (ppPaths==NULL) {
442 goto done;
443 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
445 /* Loop over all subkeys, allocating a temp sub-buffer. */
446 for(index=0;index<numKeys;index++) {
447 WCHAR keyBuf[MAX_PATH+1];
448 HKEY subKey = 0;
449 DWORD reqdSize = MAX_PATH+1;
450 /* Get the sub-key name */
451 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
452 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100453 if (rc!=ERROR_SUCCESS) {
454 goto done;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 /* Open the sub-key */
457 rc=RegOpenKeyExW(newKey,
458 keyBuf, /* subkey */
459 0, /* reserved */
460 KEY_READ,
461 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100462 if (rc!=ERROR_SUCCESS) {
463 goto done;
464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Find the value of the buffer size, malloc, then read it */
466 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
467 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200468 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (ppPaths[index]) {
470 RegQueryValueExW(subKey, NULL, 0, NULL,
471 (LPBYTE)ppPaths[index],
472 &reqdSize);
473 dataSize += reqdSize + 1; /* 1 for the ";" */
474 }
475 }
476 RegCloseKey(subKey);
477 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100480 if (dataSize == 0) {
481 goto done;
482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483
484 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200485 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if (dataBuf) {
487 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 /* Copy our collected strings */
489 for (index=0;index<numKeys;index++) {
490 if (index > 0) {
491 *(szCur++) = L';';
492 dataSize--;
493 }
494 if (ppPaths[index]) {
495 Py_ssize_t len = wcslen(ppPaths[index]);
496 wcsncpy(szCur, ppPaths[index], len);
497 szCur += len;
498 assert(dataSize > (DWORD)len);
499 dataSize -= (DWORD)len;
500 }
501 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100502 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 else {
luzpaza5293b42017-11-05 07:37:50 -0600506 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (numKeys) {
508 *(szCur++) = L';';
509 dataSize--;
510 }
511 /* Now append the core path entries -
512 this will include the NULL
513 */
514 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
515 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200516 if (rc != ERROR_SUCCESS) {
517 PyMem_RawFree(dataBuf);
518 goto done;
519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 }
521 /* And set the result - caller must free */
522 retval = dataBuf;
523 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000524done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 /* Loop freeing my temp buffers */
526 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200527 for(index=0; index<numKeys; index++)
528 PyMem_RawFree(ppPaths[index]);
529 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100531 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100533 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200534 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000536}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000537#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000538
Victor Stinner0327bde2017-11-23 17:03:20 +0100539
Victor Stinner331a6a52019-05-27 16:39:22 +0200540static PyStatus
Victor Stinnerc5c64252019-09-23 15:59:00 +0200541get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100542{
Victor Stinner3f5409a2019-09-23 19:50:27 +0200543 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800544 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100545 wchar_t program_full_path[MAXPATHLEN+1];
546 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100547
Steve Dower323e7432019-06-29 14:28:59 -0700548 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
549 /* GetModuleFileName should never fail when passed NULL */
550 return _PyStatus_ERR("Cannot determine program path");
551 }
552
Steve Dower1c3de542018-12-10 08:11:21 -0800553 /* The launcher may need to force the executable path to a
554 * different environment, so override it here. */
555 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
556 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower323e7432019-06-29 14:28:59 -0700557 /* If overridden, preserve the original full path */
Victor Stinner3f5409a2019-09-23 19:50:27 +0200558 if (pathconfig->base_executable == NULL) {
559 pathconfig->base_executable = PyMem_RawMalloc(
560 sizeof(wchar_t) * (MAXPATHLEN + 1));
561 if (pathconfig->base_executable == NULL) {
562 return _PyStatus_NO_MEMORY();
563 }
564
565 status = canonicalize(pathconfig->base_executable,
566 program_full_path);
567 if (_PyStatus_EXCEPTION(status)) {
568 return status;
569 }
Steve Dower323e7432019-06-29 14:28:59 -0700570 }
571
Steve Dower1c3de542018-12-10 08:11:21 -0800572 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower323e7432019-06-29 14:28:59 -0700573 /* bpo-35873: Clear the environment variable to avoid it being
574 * inherited by child processes. */
575 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100576 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000577
Victor Stinner3f5409a2019-09-23 19:50:27 +0200578 if (pathconfig->program_full_path == NULL) {
579 pathconfig->program_full_path = PyMem_RawMalloc(
580 sizeof(wchar_t) * (MAXPATHLEN + 1));
581 if (pathconfig->program_full_path == NULL) {
582 return _PyStatus_NO_MEMORY();
583 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000584
Victor Stinner3f5409a2019-09-23 19:50:27 +0200585 status = canonicalize(pathconfig->program_full_path,
586 program_full_path);
587 if (_PyStatus_EXCEPTION(status)) {
588 return status;
589 }
590 }
591 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000592}
593
Victor Stinner0327bde2017-11-23 17:03:20 +0100594
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700595static PyStatus
596read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
597 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700598{
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700599 PyStatus status;
600 wchar_t *buf = NULL;
601 wchar_t *wline = NULL;
602 FILE *sp_file;
603
604 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100605 if (sp_file == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700606 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100607 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700608
Steve Dowered51b262016-09-17 12:54:06 -0700609 wcscpy_s(prefix, MAXPATHLEN+1, path);
610 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200611 pathconfig->isolated = 1;
612 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700613
Steve Dower4db86bc2016-09-09 09:17:35 -0700614 size_t bufsiz = MAXPATHLEN;
615 size_t prefixlen = wcslen(prefix);
616
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700617 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700618 if (buf == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700619 status = _PyStatus_NO_MEMORY();
620 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700621 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700622 buf[0] = '\0';
623
624 while (!feof(sp_file)) {
625 char line[MAXPATHLEN + 1];
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700626 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100627 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700628 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100629 }
630 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700631 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100632 }
Steve Dowered51b262016-09-17 12:54:06 -0700633 while (*++p) {
634 if (*p == '\r' || *p == '\n') {
635 *p = '\0';
636 break;
637 }
638 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700639
Steve Dowered51b262016-09-17 12:54:06 -0700640 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200641 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700642 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200643 }
644 else if (strncmp(line, "import ", 7) == 0) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700645 status = _PyStatus_ERR("only 'import site' is supported "
646 "in ._pth file");
647 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700648 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700649
Steve Dowered51b262016-09-17 12:54:06 -0700650 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700651 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700652 if (wline == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700653 status = _PyStatus_NO_MEMORY();
654 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700655 }
Steve Dowered51b262016-09-17 12:54:06 -0700656 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700657 wline[wn] = '\0';
658
Steve Dowerc6dd4152016-10-27 14:28:07 -0700659 size_t usedsiz = wcslen(buf);
660 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700661 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700662 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
663 sizeof(wchar_t));
664 if (tmp == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700665 status = _PyStatus_NO_MEMORY();
666 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700667 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700668 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700669 }
670
Steve Dowerc6dd4152016-10-27 14:28:07 -0700671 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700672 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700673 usedsiz += 1;
674 }
Steve Dowered51b262016-09-17 12:54:06 -0700675
Steve Dowerc6dd4152016-10-27 14:28:07 -0700676 errno_t result;
677 _Py_BEGIN_SUPPRESS_IPH
678 result = wcscat_s(buf, bufsiz, prefix);
679 _Py_END_SUPPRESS_IPH
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700680
Steve Dowerc6dd4152016-10-27 14:28:07 -0700681 if (result == EINVAL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700682 status = _PyStatus_ERR("invalid argument during ._pth processing");
683 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700684 } else if (result == ERANGE) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700685 status = _PyStatus_ERR("buffer overflow during ._pth processing");
686 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700687 }
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700688
Steve Dowerc6dd4152016-10-27 14:28:07 -0700689 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700690 join(b, wline);
691
692 PyMem_RawFree(wline);
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700693 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700694 }
695
Victor Stinner3f5409a2019-09-23 19:50:27 +0200696 if (pathconfig->module_search_path == NULL) {
697 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
698 if (pathconfig->module_search_path == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700699 status = _PyStatus_NO_MEMORY();
700 goto done;
Victor Stinner3f5409a2019-09-23 19:50:27 +0200701 }
702 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700703
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700704 *found = 1;
705 status = _PyStatus_OK();
706 goto done;
707
708done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700709 PyMem_RawFree(buf);
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700710 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700711 fclose(sp_file);
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700712 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100713}
714
715
716static int
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200717get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
718 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100719{
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -0700720 if (get_dllpath(filename) &&
721 !change_ext(filename, filename, L"._pth") &&
722 exists(filename))
723 {
724 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100725 }
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -0700726 if (pathconfig->program_full_path[0] &&
727 !change_ext(filename, pathconfig->program_full_path, L"._pth") &&
728 exists(filename))
729 {
730 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100731 }
732 return 0;
733}
734
735
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700736static PyStatus
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200737calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700738 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100739{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200740 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100741
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200742 if (!get_pth_filename(calculate, filename, pathconfig)) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700743 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100744 }
745
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700746 return read_pth_file(pathconfig, prefix, filename, found);
Victor Stinner0327bde2017-11-23 17:03:20 +0100747}
748
749
750/* Search for an environment configuration file, first in the
751 executable's directory and then in the parent directory.
752 If found, open it for use when searching for prefixes.
753*/
754static void
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700755calculate_pyvenv_file(PyCalculatePath *calculate,
756 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100757{
Victor Stinner96c84752019-09-26 16:17:34 +0200758 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100759 const wchar_t *env_cfg = L"pyvenv.cfg";
760
Victor Stinner96c84752019-09-26 16:17:34 +0200761 /* Filename: <argv0_path_len> / "pyvenv.cfg" */
762 wcscpy_s(filename, MAXPATHLEN+1, argv0_path);
763 join(filename, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100764
Victor Stinner96c84752019-09-26 16:17:34 +0200765 FILE *env_file = _Py_wfopen(filename, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100766 if (env_file == NULL) {
767 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100768
Victor Stinner96c84752019-09-26 16:17:34 +0200769 /* Filename: <basename(basename(argv0_path_len))> / "pyvenv.cfg" */
770 reduce(filename);
771 reduce(filename);
772 join(filename, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100773
Victor Stinner96c84752019-09-26 16:17:34 +0200774 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100775 if (env_file == NULL) {
776 errno = 0;
Victor Stinner96c84752019-09-26 16:17:34 +0200777 return;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100778 }
779 }
780
Victor Stinner0327bde2017-11-23 17:03:20 +0100781 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinner96c84752019-09-26 16:17:34 +0200782 wchar_t home[MAXPATHLEN+1];
783 if (_Py_FindEnvConfigValue(env_file, L"home",
784 home, Py_ARRAY_LENGTH(home))) {
785 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100786 }
787 fclose(env_file);
788}
789
790
791static void
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700792calculate_home_prefix(PyCalculatePath *calculate,
793 const wchar_t *argv0_path,
794 const wchar_t *zip_path,
795 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100796{
Victor Stinner0327bde2017-11-23 17:03:20 +0100797 if (calculate->home == NULL || *calculate->home == '\0') {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700798 if (zip_path[0] && exists(zip_path)) {
799 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100800 reduce(prefix);
801 calculate->home = prefix;
802 }
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700803 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100804 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100805 }
806 else {
807 calculate->home = NULL;
808 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100810 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100811 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100812 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100813}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000814
Victor Stinner9316ee42017-11-25 03:17:57 +0100815
Victor Stinner331a6a52019-05-27 16:39:22 +0200816static PyStatus
Victor Stinner3f5409a2019-09-23 19:50:27 +0200817calculate_module_search_path(PyCalculatePath *calculate,
818 _PyPathConfig *pathconfig,
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700819 const wchar_t *argv0_path,
820 wchar_t *prefix,
821 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100822{
Victor Stinner0327bde2017-11-23 17:03:20 +0100823 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000824#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100825 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
826 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000827#endif
luzpaza5293b42017-11-05 07:37:50 -0600828 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 anything better to use! */
Victor Stinner3f5409a2019-09-23 19:50:27 +0200830 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100831 calculate->home != NULL ||
832 calculate->machine_path != NULL ||
833 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 /* We need to construct a path from the following parts.
836 (1) the PYTHONPATH environment variable, if set;
837 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100838 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100840 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 (5) the directory containing the executable (argv0_path).
842 The length calculation calculates #4 first.
843 Extra rules:
844 - If PYTHONHOME is set (in any way) item (3) is ignored.
845 - If registry values are used, (4) and (5) are ignored.
846 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100849 size_t bufsz = 0;
850 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200851 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 bufsz = 1;
853 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100854 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100856 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100858 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700860 bufsz += wcslen(PYTHONPATH) + 1;
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700861 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100862 if (calculate->user_path) {
863 bufsz += wcslen(calculate->user_path) + 1;
864 }
865 if (calculate->machine_path) {
866 bufsz += wcslen(calculate->machine_path) + 1;
867 }
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700868 bufsz += wcslen(zip_path) + 1;
Victor Stinner3f5409a2019-09-23 19:50:27 +0200869 if (calculate->pythonpath_env != NULL) {
870 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100871 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000872
Victor Stinner0327bde2017-11-23 17:03:20 +0100873 wchar_t *buf, *start_buf;
874 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (buf == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700876 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100878 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000879
Victor Stinner3f5409a2019-09-23 19:50:27 +0200880 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100881 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner3f5409a2019-09-23 19:50:27 +0200882 calculate->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100883 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 buf = wcschr(buf, L'\0');
886 *buf++ = DELIM;
887 }
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700888 if (zip_path[0]) {
889 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100890 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 buf = wcschr(buf, L'\0');
893 *buf++ = DELIM;
894 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100895 if (calculate->user_path) {
896 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100897 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 buf = wcschr(buf, L'\0');
900 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 if (calculate->machine_path) {
903 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100904 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 buf = wcschr(buf, L'\0');
907 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100909 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100911 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100912 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700915 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700917 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200918 const wchar_t *p = PYTHONPATH;
919 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 size_t n;
921 for (;;) {
922 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100923 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100925 }
926 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100928 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100930 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100931 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 buf = wcschr(buf, L'\0');
934 p++;
935 n--;
936 }
937 wcsncpy(buf, p, n);
938 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700939 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100940 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 p = q+1;
944 }
945 }
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -0700946 if (argv0_path) {
947 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700949 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700951 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 /* Now to pull one last hack/trick. If sys.prefix is
954 empty, then try and find it somewhere on the paths
955 we calculated. We scan backwards, as our general policy
956 is that Python core directories are at the *end* of
957 sys.path. We assume that our "lib" directory is
958 on the path, and that our 'prefix' directory is
959 the parent of that.
960 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100961 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200963 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 while (1) {
965 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200966 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* 'look' will end up one character before the
968 start of the path in question - even if this
969 is one character before the start of the buffer
970 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100971 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 look--;
973 nchars = lookEnd-look;
974 wcsncpy(lookBuf, look+1, nchars);
975 lookBuf[nchars] = L'\0';
976 /* Up one level to the parent */
977 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100978 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 break;
980 }
981 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100982 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100984 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 look--;
986 }
987 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100988
Victor Stinner331a6a52019-05-27 16:39:22 +0200989 pathconfig->module_search_path = start_buf;
990 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100991}
992
993
Victor Stinner331a6a52019-05-27 16:39:22 +0200994static PyStatus
Victor Stinner3f5409a2019-09-23 19:50:27 +0200995calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100996{
Victor Stinner331a6a52019-05-27 16:39:22 +0200997 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100998
Victor Stinnerc5c64252019-09-23 15:59:00 +0200999 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +02001000 if (_PyStatus_EXCEPTION(status)) {
1001 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +01001002 }
1003
Victor Stinnerb64de462017-12-01 18:27:09 +01001004 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001005 wchar_t argv0_path[MAXPATHLEN+1];
1006 memset(argv0_path, 0, sizeof(argv0_path));
1007
1008 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
1009 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +01001010
1011 wchar_t prefix[MAXPATHLEN+1];
1012 memset(prefix, 0, sizeof(prefix));
1013
1014 /* Search for a sys.path file */
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001015 int pth_found = 0;
1016 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
1017 if (_PyStatus_EXCEPTION(status)) {
1018 return status;
1019 }
1020 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001021 goto done;
1022 }
1023
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001024 calculate_pyvenv_file(calculate, argv0_path, Py_ARRAY_LENGTH(argv0_path));
Victor Stinner9316ee42017-11-25 03:17:57 +01001025
1026 /* Calculate zip archive path from DLL or exe path */
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001027 wchar_t zip_path[MAXPATHLEN+1];
1028 memset(zip_path, 0, sizeof(zip_path));
1029
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -07001030 if (get_dllpath(zip_path) || change_ext(zip_path, zip_path, L".zip"))
1031 {
1032 if (change_ext(zip_path, pathconfig->program_full_path, L".zip")) {
1033 zip_path[0] = L'\0';
1034 }
1035 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001036
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001037 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001038
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001039 if (pathconfig->module_search_path == NULL) {
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001040 status = calculate_module_search_path(calculate, pathconfig,
1041 argv0_path, prefix, zip_path);
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001042 if (_PyStatus_EXCEPTION(status)) {
1043 return status;
1044 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001045 }
1046
1047done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001048 if (pathconfig->prefix == NULL) {
Victor Stinner3f5409a2019-09-23 19:50:27 +02001049 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1050 if (pathconfig->prefix == NULL) {
1051 return _PyStatus_NO_MEMORY();
1052 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001053 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001054 if (pathconfig->exec_prefix == NULL) {
Victor Stinner3f5409a2019-09-23 19:50:27 +02001055 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1056 if (pathconfig->exec_prefix == NULL) {
1057 return _PyStatus_NO_MEMORY();
1058 }
Steve Dower177a41a2018-11-17 20:41:48 -08001059 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001060
Victor Stinner331a6a52019-05-27 16:39:22 +02001061 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001062}
1063
1064
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001065static PyStatus
1066calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1067 const PyConfig *config)
1068{
1069 calculate->home = pathconfig->home;
1070 calculate->path_env = _wgetenv(L"PATH");
1071
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001072 calculate->pythonpath_env = config->pythonpath_env;
1073
1074 return _PyStatus_OK();
1075}
1076
1077
Victor Stinner0327bde2017-11-23 17:03:20 +01001078static void
1079calculate_free(PyCalculatePath *calculate)
1080{
1081 PyMem_RawFree(calculate->machine_path);
1082 PyMem_RawFree(calculate->user_path);
1083}
1084
Victor Stinner9316ee42017-11-25 03:17:57 +01001085
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001086/* Calculate the Python path configuration.
1087
1088 Inputs:
1089
1090 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1091 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001092 - PATH environment variable
1093 - __PYVENV_LAUNCHER__ environment variable
1094 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1095 the current process
Victor Stinner96c84752019-09-26 16:17:34 +02001096 - ._pth configuration file
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001097 - pyvenv.cfg configuration file
1098 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner96c84752019-09-26 16:17:34 +02001099 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1100 version.
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001101
1102 Outputs, 'pathconfig' fields:
Victor Stinner3f5409a2019-09-23 19:50:27 +02001103
1104 - base_executable
1105 - program_full_path
1106 - module_search_path
1107 - prefix
1108 - exec_prefix
1109 - isolated
1110 - site_import
1111
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001112 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001113PyStatus
1114_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001115{
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001116 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001117 PyCalculatePath calculate;
1118 memset(&calculate, 0, sizeof(calculate));
1119
Miss Islington (bot)7f7cd892019-09-23 16:16:53 -07001120 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001121 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001122 goto done;
1123 }
1124
Victor Stinner3f5409a2019-09-23 19:50:27 +02001125 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001126
Victor Stinner9316ee42017-11-25 03:17:57 +01001127done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001128 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001129 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001130}
1131
1132
Victor Stinner63941882011-09-29 00:42:28 +02001133/* Load python3.dll before loading any extension module that might refer
1134 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001135 to this python DLL is loaded, not a python3.dll that might be on the path
1136 by chance.
1137 Return whether the DLL was found.
1138*/
1139static int python3_checked = 0;
1140static HANDLE hPython3;
1141int
Victor Stinner31a83932017-12-04 13:39:15 +01001142_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001143{
1144 wchar_t py3path[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +01001145 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001146 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001147 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001148 python3_checked = 1;
1149
1150 /* If there is a python3.dll next to the python3y.dll,
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -07001151 use that DLL */
1152 if (!get_dllpath(py3path)) {
1153 reduce(py3path);
1154 join(py3path, PY3_DLLNAME);
1155 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1156 if (hPython3 != NULL) {
1157 return 1;
1158 }
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001159 }
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -07001160
1161 /* If we can locate python3.dll in our application dir,
1162 use that DLL */
1163 hPython3 = LoadLibraryExW(PY3_DLLNAME, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
Victor Stinner0327bde2017-11-23 17:03:20 +01001164 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001165 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001166 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001167
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -07001168 /* For back-compat, also search {sys.prefix}\DLLs, though
1169 that has not been a normal install layout for a while */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001170 wcscpy(py3path, Py_GetPrefix());
Miss Islington (bot)aa7f7752020-07-06 10:12:16 -07001171 if (py3path[0]) {
1172 join(py3path, L"DLLs\\" PY3_DLLNAME);
1173 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
1174 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001175 return hPython3 != NULL;
1176}