blob: 04f24d986f667c0c7d73d5d8b13110dcadc5a295 [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
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 Stinnerc4221672019-09-21 01:02:56 +0200133 wchar_t *dll_path;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200134
135 const wchar_t *pythonpath_env;
Victor Stinner0327bde2017-11-23 17:03:20 +0100136} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000137
Guido van Rossumeea14491997-08-13 21:30:44 +0000138
Victor Stinner0327bde2017-11-23 17:03:20 +0100139/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100141is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142{
143#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000145#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147#endif
148}
149
Victor Stinner0327bde2017-11-23 17:03:20 +0100150
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000151/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100152 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000153static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000154reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000155{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700156 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100157 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700158 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100159 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 while (i > 0 && !is_sep(dir[i]))
162 --i;
163 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000164}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165
Victor Stinner0327bde2017-11-23 17:03:20 +0100166
Steve Dowered51b262016-09-17 12:54:06 -0700167static int
168change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
169{
170 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
171 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100172 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700173 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100174 }
Steve Dowered51b262016-09-17 12:54:06 -0700175
176 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
177 --i;
178
179 if (i == 0) {
180 dest[0] = '\0';
181 return -1;
182 }
183
Victor Stinner0327bde2017-11-23 17:03:20 +0100184 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700185 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100186 }
Steve Dowered51b262016-09-17 12:54:06 -0700187
188 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100189 wcscat_s(dest, MAXPATHLEN+1, ext))
190 {
Steve Dowered51b262016-09-17 12:54:06 -0700191 dest[0] = '\0';
192 return -1;
193 }
194
195 return 0;
196}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000197
Victor Stinner0327bde2017-11-23 17:03:20 +0100198
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000199static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200200exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000203}
204
Victor Stinner0327bde2017-11-23 17:03:20 +0100205
206/* Is module -- check for .pyc too.
207 Assumes 'filename' MAXPATHLEN+1 bytes long -
208 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000209static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100210ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000211{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100212 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700213
Victor Stinner0327bde2017-11-23 17:03:20 +0100214 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100216 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700219 n = wcsnlen_s(filename, MAXPATHLEN+1);
220 if (n < MAXPATHLEN) {
221 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800222 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700223 filename[n + 1] = L'\0';
224 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100225 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700226 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100227 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700228 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 }
230 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000231}
232
Victor Stinner0327bde2017-11-23 17:03:20 +0100233
Tim Peters8484fbf2004-08-07 19:12:27 +0000234/* Add a path component, by appending stuff to buffer.
235 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
236 NUL-terminated string with no more than MAXPATHLEN characters (not counting
237 the trailing NUL). It's a fatal error if it contains a string longer than
238 that (callers must be careful!). If these requirements are met, it's
239 guaranteed that buffer will still be a NUL-terminated string with no more
240 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
241 stuff as fits will be appended.
242*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700243
244static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100245typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
246 PCWSTR pszPathIn, PCWSTR pszMore,
247 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700248static PPathCchCombineEx _PathCchCombineEx;
249
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000250static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700251join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000252{
Steve Dower4db86bc2016-09-09 09:17:35 -0700253 if (_PathCchCombineEx_Initialized == 0) {
254 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100255 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700256 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100257 }
258 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700259 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100260 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700263
Steve Dower4db86bc2016-09-09 09:17:35 -0700264 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100265 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700266 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100267 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700268 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100269 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700270 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100271 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700272 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000273}
274
Steve Dower48e8c822018-02-22 10:39:26 -0800275static int _PathCchCanonicalizeEx_Initialized = 0;
276typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
277 PCWSTR pszPathIn, unsigned long dwFlags);
278static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
279
Victor Stinner85ce0a72019-09-24 00:55:48 +0200280/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
281 and ".." to produce a direct, well-formed path. */
282static PyStatus
283canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800284{
285 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200286 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800287 }
288
289 if (_PathCchCanonicalizeEx_Initialized == 0) {
290 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
291 if (pathapi) {
292 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
293 }
294 else {
295 _PathCchCanonicalizeEx = NULL;
296 }
297 _PathCchCanonicalizeEx_Initialized = 1;
298 }
299
300 if (_PathCchCanonicalizeEx) {
301 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200302 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800303 }
304 }
305 else {
306 if (!PathCanonicalizeW(buffer, path)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200307 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800308 }
309 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200310 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800311}
312
Victor Stinner0327bde2017-11-23 17:03:20 +0100313
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000314/* gotlandmark only called by search_for_prefix, which ensures
315 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100316 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000317static int
Victor Stinnerdec39712019-09-30 14:49:34 +0200318gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000319{
Victor Stinnerdec39712019-09-30 14:49:34 +0200320 wchar_t filename[MAXPATHLEN+1];
321 memset(filename, 0, sizeof(filename));
322 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
323 join(filename, landmark);
324 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000325}
326
Victor Stinner0327bde2017-11-23 17:03:20 +0100327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200329 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000330static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200331search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700334 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100336 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 reduce(prefix);
340 } while (prefix[0]);
341 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000342}
343
Victor Stinner0327bde2017-11-23 17:03:20 +0100344
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000345#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000346
Guido van Rossum88716bb2000-03-30 19:45:39 +0000347/* a string loaded from the DLL at startup.*/
348extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000349
Guido van Rossumeea14491997-08-13 21:30:44 +0000350/* Load a PYTHONPATH value from the registry.
351 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
352
Guido van Rossum88716bb2000-03-30 19:45:39 +0000353 Works in both Unicode and 8bit environments. Only uses the
354 Ex family of functions so it also works with Windows CE.
355
Guido van Rossumeea14491997-08-13 21:30:44 +0000356 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000357
358 XXX - this code is pretty strange, as it used to also
359 work on Win16, where the buffer sizes werent available
360 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000361*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000362static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000363getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 HKEY newKey = 0;
366 DWORD dataSize = 0;
367 DWORD numKeys = 0;
368 LONG rc;
369 wchar_t *retval = NULL;
370 WCHAR *dataBuf = NULL;
371 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
372 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700373 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 DWORD index;
375 WCHAR *keyBuf = NULL;
376 WCHAR *keyBufPtr;
377 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* Tried to use sysget("winver") but here is too early :-( */
380 versionLen = strlen(PyWin_DLLVersionString);
381 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700382 keyBufLen = sizeof(keyPrefix) +
383 sizeof(WCHAR)*(versionLen-1) +
384 sizeof(keySuffix);
385 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100386 if (keyBuf==NULL) {
387 goto done;
388 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000389
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700390 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200391 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
393 keyBufPtr += versionLen;
394 /* NULL comes with this one! */
395 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
396 /* Open the root Python key */
397 rc=RegOpenKeyExW(keyBase,
398 keyBuf, /* subkey */
399 0, /* reserved */
400 KEY_READ,
401 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100402 if (rc!=ERROR_SUCCESS) {
403 goto done;
404 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 /* Find out how big our core buffer is, and how many subkeys we have */
406 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
407 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100408 if (rc!=ERROR_SUCCESS) {
409 goto done;
410 }
411 if (skipcore) {
412 dataSize = 0; /* Only count core ones if we want them! */
413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* Allocate a temp array of char buffers, so we only need to loop
415 reading the registry once
416 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200417 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100418 if (ppPaths==NULL) {
419 goto done;
420 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
422 /* Loop over all subkeys, allocating a temp sub-buffer. */
423 for(index=0;index<numKeys;index++) {
424 WCHAR keyBuf[MAX_PATH+1];
425 HKEY subKey = 0;
426 DWORD reqdSize = MAX_PATH+1;
427 /* Get the sub-key name */
428 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
429 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100430 if (rc!=ERROR_SUCCESS) {
431 goto done;
432 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 /* Open the sub-key */
434 rc=RegOpenKeyExW(newKey,
435 keyBuf, /* subkey */
436 0, /* reserved */
437 KEY_READ,
438 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100439 if (rc!=ERROR_SUCCESS) {
440 goto done;
441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 /* Find the value of the buffer size, malloc, then read it */
443 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
444 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200445 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (ppPaths[index]) {
447 RegQueryValueExW(subKey, NULL, 0, NULL,
448 (LPBYTE)ppPaths[index],
449 &reqdSize);
450 dataSize += reqdSize + 1; /* 1 for the ";" */
451 }
452 }
453 RegCloseKey(subKey);
454 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100457 if (dataSize == 0) {
458 goto done;
459 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460
461 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200462 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (dataBuf) {
464 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 /* Copy our collected strings */
466 for (index=0;index<numKeys;index++) {
467 if (index > 0) {
468 *(szCur++) = L';';
469 dataSize--;
470 }
471 if (ppPaths[index]) {
472 Py_ssize_t len = wcslen(ppPaths[index]);
473 wcsncpy(szCur, ppPaths[index], len);
474 szCur += len;
475 assert(dataSize > (DWORD)len);
476 dataSize -= (DWORD)len;
477 }
478 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100479 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 else {
luzpaza5293b42017-11-05 07:37:50 -0600483 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (numKeys) {
485 *(szCur++) = L';';
486 dataSize--;
487 }
488 /* Now append the core path entries -
489 this will include the NULL
490 */
491 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
492 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200493 if (rc != ERROR_SUCCESS) {
494 PyMem_RawFree(dataBuf);
495 goto done;
496 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
498 /* And set the result - caller must free */
499 retval = dataBuf;
500 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000501done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* Loop freeing my temp buffers */
503 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200504 for(index=0; index<numKeys; index++)
505 PyMem_RawFree(ppPaths[index]);
506 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100508 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100510 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200511 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000513}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000514#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000515
Victor Stinner0327bde2017-11-23 17:03:20 +0100516
Victor Stinner410759f2019-05-18 04:17:01 +0200517wchar_t*
518_Py_GetDLLPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000519{
Victor Stinner9316ee42017-11-25 03:17:57 +0100520 wchar_t dll_path[MAXPATHLEN+1];
521 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000522
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000523#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100525 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100526 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
527 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100528 }
529 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000530#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100531 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000532#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100533
Victor Stinner410759f2019-05-18 04:17:01 +0200534 return _PyMem_RawWcsdup(dll_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100535}
536
537
Victor Stinner331a6a52019-05-27 16:39:22 +0200538static PyStatus
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200539get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100540{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200541 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800542 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100543 wchar_t program_full_path[MAXPATHLEN+1];
544 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100545
Steve Dower9048c492019-06-29 10:34:11 -0700546 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
547 /* GetModuleFileName should never fail when passed NULL */
548 return _PyStatus_ERR("Cannot determine program path");
549 }
550
Steve Dower1c3de542018-12-10 08:11:21 -0800551 /* The launcher may need to force the executable path to a
552 * different environment, so override it here. */
553 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
554 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower9048c492019-06-29 10:34:11 -0700555 /* If overridden, preserve the original full path */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200556 if (pathconfig->base_executable == NULL) {
557 pathconfig->base_executable = PyMem_RawMalloc(
558 sizeof(wchar_t) * (MAXPATHLEN + 1));
559 if (pathconfig->base_executable == NULL) {
560 return _PyStatus_NO_MEMORY();
561 }
562
563 status = canonicalize(pathconfig->base_executable,
564 program_full_path);
565 if (_PyStatus_EXCEPTION(status)) {
566 return status;
567 }
Steve Dower9048c492019-06-29 10:34:11 -0700568 }
569
Steve Dower1c3de542018-12-10 08:11:21 -0800570 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower9048c492019-06-29 10:34:11 -0700571 /* bpo-35873: Clear the environment variable to avoid it being
572 * inherited by child processes. */
573 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100574 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000575
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200576 if (pathconfig->program_full_path == NULL) {
577 pathconfig->program_full_path = PyMem_RawMalloc(
578 sizeof(wchar_t) * (MAXPATHLEN + 1));
579 if (pathconfig->program_full_path == NULL) {
580 return _PyStatus_NO_MEMORY();
581 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000582
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200583 status = canonicalize(pathconfig->program_full_path,
584 program_full_path);
585 if (_PyStatus_EXCEPTION(status)) {
586 return status;
587 }
588 }
589 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000590}
591
Victor Stinner0327bde2017-11-23 17:03:20 +0100592
Victor Stinner85ce0a72019-09-24 00:55:48 +0200593static PyStatus
594read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
595 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700596{
Victor Stinner85ce0a72019-09-24 00:55:48 +0200597 PyStatus status;
598 wchar_t *buf = NULL;
599 wchar_t *wline = NULL;
600 FILE *sp_file;
601
602 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100603 if (sp_file == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200604 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100605 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700606
Steve Dowered51b262016-09-17 12:54:06 -0700607 wcscpy_s(prefix, MAXPATHLEN+1, path);
608 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200609 pathconfig->isolated = 1;
610 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700611
Steve Dower4db86bc2016-09-09 09:17:35 -0700612 size_t bufsiz = MAXPATHLEN;
613 size_t prefixlen = wcslen(prefix);
614
Victor Stinner85ce0a72019-09-24 00:55:48 +0200615 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700616 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200617 status = _PyStatus_NO_MEMORY();
618 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700619 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700620 buf[0] = '\0';
621
622 while (!feof(sp_file)) {
623 char line[MAXPATHLEN + 1];
Victor Stinner85ce0a72019-09-24 00:55:48 +0200624 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100625 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700626 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100627 }
628 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700629 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100630 }
Steve Dowered51b262016-09-17 12:54:06 -0700631 while (*++p) {
632 if (*p == '\r' || *p == '\n') {
633 *p = '\0';
634 break;
635 }
636 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700637
Steve Dowered51b262016-09-17 12:54:06 -0700638 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200639 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700640 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200641 }
642 else if (strncmp(line, "import ", 7) == 0) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200643 status = _PyStatus_ERR("only 'import site' is supported "
644 "in ._pth file");
645 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700646 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700647
Steve Dowered51b262016-09-17 12:54:06 -0700648 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700649 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700650 if (wline == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200651 status = _PyStatus_NO_MEMORY();
652 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700653 }
Steve Dowered51b262016-09-17 12:54:06 -0700654 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700655 wline[wn] = '\0';
656
Steve Dowerc6dd4152016-10-27 14:28:07 -0700657 size_t usedsiz = wcslen(buf);
658 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700659 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700660 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
661 sizeof(wchar_t));
662 if (tmp == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200663 status = _PyStatus_NO_MEMORY();
664 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700665 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700666 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700667 }
668
Steve Dowerc6dd4152016-10-27 14:28:07 -0700669 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700670 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700671 usedsiz += 1;
672 }
Steve Dowered51b262016-09-17 12:54:06 -0700673
Steve Dowerc6dd4152016-10-27 14:28:07 -0700674 errno_t result;
675 _Py_BEGIN_SUPPRESS_IPH
676 result = wcscat_s(buf, bufsiz, prefix);
677 _Py_END_SUPPRESS_IPH
Victor Stinner85ce0a72019-09-24 00:55:48 +0200678
Steve Dowerc6dd4152016-10-27 14:28:07 -0700679 if (result == EINVAL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200680 status = _PyStatus_ERR("invalid argument during ._pth processing");
681 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700682 } else if (result == ERANGE) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200683 status = _PyStatus_ERR("buffer overflow during ._pth processing");
684 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700685 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200686
Steve Dowerc6dd4152016-10-27 14:28:07 -0700687 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700688 join(b, wline);
689
690 PyMem_RawFree(wline);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200691 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700692 }
693
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200694 if (pathconfig->module_search_path == NULL) {
695 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
696 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200697 status = _PyStatus_NO_MEMORY();
698 goto done;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200699 }
700 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700701
Victor Stinner85ce0a72019-09-24 00:55:48 +0200702 *found = 1;
703 status = _PyStatus_OK();
704 goto done;
705
706done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700707 PyMem_RawFree(buf);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200708 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700709 fclose(sp_file);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200710 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100711}
712
713
714static int
Victor Stinnerc4221672019-09-21 01:02:56 +0200715get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
716 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100717{
Victor Stinnerc4221672019-09-21 01:02:56 +0200718 if (calculate->dll_path[0]) {
719 if (!change_ext(filename, calculate->dll_path, L"._pth") &&
720 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100721 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100722 return 1;
723 }
724 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200725 if (pathconfig->program_full_path[0]) {
Victor Stinnerc4221672019-09-21 01:02:56 +0200726 if (!change_ext(filename, pathconfig->program_full_path, L"._pth") &&
727 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100728 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100729 return 1;
730 }
731 }
732 return 0;
733}
734
735
Victor Stinner85ce0a72019-09-24 00:55:48 +0200736static PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200737calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200738 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100739{
Victor Stinnerc4221672019-09-21 01:02:56 +0200740 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100741
Victor Stinnerc4221672019-09-21 01:02:56 +0200742 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200743 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100744 }
745
Victor Stinner85ce0a72019-09-24 00:55:48 +0200746 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
Victor Stinner85ce0a72019-09-24 00:55:48 +0200755calculate_pyvenv_file(PyCalculatePath *calculate,
756 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100757{
Victor Stinner221fd842019-09-25 02:54:25 +0200758 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100759 const wchar_t *env_cfg = L"pyvenv.cfg";
760
Victor Stinner221fd842019-09-25 02:54:25 +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 Stinner221fd842019-09-25 02:54:25 +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 Stinner221fd842019-09-25 02:54:25 +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 Stinner221fd842019-09-25 02:54:25 +0200774 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100775 if (env_file == NULL) {
776 errno = 0;
Victor Stinner221fd842019-09-25 02:54:25 +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 Stinner221fd842019-09-25 02:54:25 +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
Victor Stinner85ce0a72019-09-24 00:55:48 +0200792calculate_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') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200798 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 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200803 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 Stinner9c42f8c2019-09-23 18:47:29 +0200817calculate_module_search_path(PyCalculatePath *calculate,
818 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200819 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 Stinner9c42f8c2019-09-23 18:47:29 +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;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200861 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 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200868 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +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) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200876 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 Stinner9c42f8c2019-09-23 18:47:29 +0200880 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100881 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +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 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200888 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 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200946 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 Stinner9c42f8c2019-09-23 18:47:29 +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 Stinnerfcdb0272019-09-23 14:45:47 +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. */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001005 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 */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001015 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
Victor Stinner85ce0a72019-09-24 00:55:48 +02001024 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 */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001027 wchar_t zip_path[MAXPATHLEN+1];
1028 memset(zip_path, 0, sizeof(zip_path));
1029
1030 change_ext(zip_path,
Victor Stinnerc4221672019-09-21 01:02:56 +02001031 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001032 L".zip");
1033
Victor Stinner85ce0a72019-09-24 00:55:48 +02001034 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001035
Victor Stinnere2677932019-09-21 01:50:16 +02001036 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001037 status = calculate_module_search_path(calculate, pathconfig,
1038 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001039 if (_PyStatus_EXCEPTION(status)) {
1040 return status;
1041 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001042 }
1043
1044done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001045 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001046 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1047 if (pathconfig->prefix == NULL) {
1048 return _PyStatus_NO_MEMORY();
1049 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001050 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001051 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001052 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1053 if (pathconfig->exec_prefix == NULL) {
1054 return _PyStatus_NO_MEMORY();
1055 }
Steve Dower177a41a2018-11-17 20:41:48 -08001056 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001057
Victor Stinner331a6a52019-05-27 16:39:22 +02001058 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001059}
1060
1061
Victor Stinner85ce0a72019-09-24 00:55:48 +02001062static PyStatus
1063calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1064 const PyConfig *config)
1065{
1066 calculate->home = pathconfig->home;
1067 calculate->path_env = _wgetenv(L"PATH");
1068
1069 calculate->dll_path = _Py_GetDLLPath();
1070 if (calculate->dll_path == NULL) {
1071 return _PyStatus_NO_MEMORY();
1072 }
1073
1074 calculate->pythonpath_env = config->pythonpath_env;
1075
1076 return _PyStatus_OK();
1077}
1078
1079
Victor Stinner0327bde2017-11-23 17:03:20 +01001080static void
1081calculate_free(PyCalculatePath *calculate)
1082{
1083 PyMem_RawFree(calculate->machine_path);
1084 PyMem_RawFree(calculate->user_path);
Victor Stinnerc4221672019-09-21 01:02:56 +02001085 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001086}
1087
Victor Stinner9316ee42017-11-25 03:17:57 +01001088
Victor Stinner85ce0a72019-09-24 00:55:48 +02001089/* Calculate the Python path configuration.
1090
1091 Inputs:
1092
1093 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1094 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
1095 - DLL path: _Py_GetDLLPath()
1096 - PATH environment variable
1097 - __PYVENV_LAUNCHER__ environment variable
1098 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1099 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001100 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001101 - pyvenv.cfg configuration file
1102 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001103 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1104 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001105
1106 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001107
1108 - base_executable
1109 - program_full_path
1110 - module_search_path
1111 - prefix
1112 - exec_prefix
1113 - isolated
1114 - site_import
1115
Victor Stinner85ce0a72019-09-24 00:55:48 +02001116 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001117PyStatus
1118_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001119{
Victor Stinnerc4221672019-09-21 01:02:56 +02001120 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001121 PyCalculatePath calculate;
1122 memset(&calculate, 0, sizeof(calculate));
1123
Victor Stinner85ce0a72019-09-24 00:55:48 +02001124 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001125 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001126 goto done;
1127 }
1128
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001129 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001130
Victor Stinner9316ee42017-11-25 03:17:57 +01001131done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001132 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001133 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001134}
1135
1136
Victor Stinner63941882011-09-29 00:42:28 +02001137/* Load python3.dll before loading any extension module that might refer
1138 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001139 to this python DLL is loaded, not a python3.dll that might be on the path
1140 by chance.
1141 Return whether the DLL was found.
1142*/
1143static int python3_checked = 0;
1144static HANDLE hPython3;
1145int
Victor Stinner31a83932017-12-04 13:39:15 +01001146_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001147{
1148 wchar_t py3path[MAXPATHLEN+1];
1149 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001150 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001151 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001152 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001153 python3_checked = 1;
1154
1155 /* If there is a python3.dll next to the python3y.dll,
1156 assume this is a build tree; use that DLL */
Victor Stinnerc4221672019-09-21 01:02:56 +02001157 if (_Py_dll_path != NULL) {
1158 wcscpy(py3path, _Py_dll_path);
1159 }
1160 else {
1161 wcscpy(py3path, L"");
1162 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001163 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001164 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001165 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001166 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001167 wcscpy(s, L"\\python3.dll");
1168 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001169 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001170 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001171 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001172
1173 /* Check sys.prefix\DLLs\python3.dll */
1174 wcscpy(py3path, Py_GetPrefix());
1175 wcscat(py3path, L"\\DLLs\\python3.dll");
1176 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1177 return hPython3 != NULL;
1178}