blob: 085caf195a992055b6b266c31298cd27b4e04200 [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 Stinner61691d82019-10-02 23:51:20 +020083#include "pycore_initconfig.h" /* PyStatus */
84#include "pycore_pathconfig.h" /* _PyPathConfig */
Victor Stinner621cebe2018-11-12 16:53:38 +010085#include "pycore_pystate.h"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000086#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000087#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000088
Steve Dower4db86bc2016-09-09 09:17:35 -070089#ifndef MS_WINDOWS
90#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000091#endif
92
Steve Dower4db86bc2016-09-09 09:17:35 -070093#include <windows.h>
erikjanss6cf82552018-07-25 02:41:46 +020094#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070095
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000097#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098#endif /* HAVE_SYS_TYPES_H */
99
100#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000102#endif /* HAVE_SYS_STAT_H */
103
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104#include <string.h>
105
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106/* Search in some common locations for the associated Python libraries.
107 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000108 * Py_GetPath() tries to return a sensible Python module search path.
109 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000110 * The approach is an adaptation for Windows of the strategy used in
111 * ../Modules/getpath.c; it uses the Windows Registry as one of its
112 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000113 *
114 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
115 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000116 */
117
118#ifndef LANDMARK
Victor Stinner85ce0a72019-09-24 00:55:48 +0200119# define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000120#endif
121
Victor Stinner85ce0a72019-09-24 00:55:48 +0200122#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
123
124
Victor Stinner0327bde2017-11-23 17:03:20 +0100125typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200126 const wchar_t *path_env; /* PATH environment variable */
127 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100128
Victor Stinner85ce0a72019-09-24 00:55:48 +0200129 /* Registry key "Software\Python\PythonCore\X.Y\PythonPath"
130 where X.Y is the Python version (major.minor) */
Victor Stinner0327bde2017-11-23 17:03:20 +0100131 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
132 wchar_t *user_path; /* from HKEY_CURRENT_USER */
133
Victor Stinnerc4221672019-09-21 01:02:56 +0200134 wchar_t *dll_path;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200135
136 const wchar_t *pythonpath_env;
Victor Stinner0327bde2017-11-23 17:03:20 +0100137} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000138
Guido van Rossumeea14491997-08-13 21:30:44 +0000139
Victor Stinner0327bde2017-11-23 17:03:20 +0100140/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000141static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100142is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000143{
144#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000146#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000148#endif
149}
150
Victor Stinner0327bde2017-11-23 17:03:20 +0100151
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000152/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100153 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000154static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000155reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000156{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700157 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100158 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700159 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100160 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 while (i > 0 && !is_sep(dir[i]))
163 --i;
164 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000165}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166
Victor Stinner0327bde2017-11-23 17:03:20 +0100167
Steve Dowered51b262016-09-17 12:54:06 -0700168static int
169change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
170{
171 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
172 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100173 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700174 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100175 }
Steve Dowered51b262016-09-17 12:54:06 -0700176
177 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
178 --i;
179
180 if (i == 0) {
181 dest[0] = '\0';
182 return -1;
183 }
184
Victor Stinner0327bde2017-11-23 17:03:20 +0100185 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700186 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100187 }
Steve Dowered51b262016-09-17 12:54:06 -0700188
189 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100190 wcscat_s(dest, MAXPATHLEN+1, ext))
191 {
Steve Dowered51b262016-09-17 12:54:06 -0700192 dest[0] = '\0';
193 return -1;
194 }
195
196 return 0;
197}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000198
Victor Stinner0327bde2017-11-23 17:03:20 +0100199
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000200static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200201exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000204}
205
Victor Stinner0327bde2017-11-23 17:03:20 +0100206
207/* Is module -- check for .pyc too.
208 Assumes 'filename' MAXPATHLEN+1 bytes long -
209 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000210static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100211ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000212{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100213 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700214
Victor Stinner0327bde2017-11-23 17:03:20 +0100215 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100217 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700220 n = wcsnlen_s(filename, MAXPATHLEN+1);
221 if (n < MAXPATHLEN) {
222 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800223 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700224 filename[n + 1] = L'\0';
225 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100226 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700227 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100228 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700229 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 }
231 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000232}
233
Victor Stinner0327bde2017-11-23 17:03:20 +0100234
Tim Peters8484fbf2004-08-07 19:12:27 +0000235/* Add a path component, by appending stuff to buffer.
236 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
237 NUL-terminated string with no more than MAXPATHLEN characters (not counting
238 the trailing NUL). It's a fatal error if it contains a string longer than
239 that (callers must be careful!). If these requirements are met, it's
240 guaranteed that buffer will still be a NUL-terminated string with no more
241 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
242 stuff as fits will be appended.
243*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700244
245static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100246typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
247 PCWSTR pszPathIn, PCWSTR pszMore,
248 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700249static PPathCchCombineEx _PathCchCombineEx;
250
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000251static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700252join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000253{
Steve Dower4db86bc2016-09-09 09:17:35 -0700254 if (_PathCchCombineEx_Initialized == 0) {
255 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100256 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700257 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100258 }
259 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700260 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100261 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700262 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700264
Steve Dower4db86bc2016-09-09 09:17:35 -0700265 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100266 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700267 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100268 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700269 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100270 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700271 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700273 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000274}
275
Steve Dower48e8c822018-02-22 10:39:26 -0800276static int _PathCchCanonicalizeEx_Initialized = 0;
277typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
278 PCWSTR pszPathIn, unsigned long dwFlags);
279static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
280
Victor Stinner85ce0a72019-09-24 00:55:48 +0200281/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
282 and ".." to produce a direct, well-formed path. */
283static PyStatus
284canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800285{
286 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200287 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800288 }
289
290 if (_PathCchCanonicalizeEx_Initialized == 0) {
291 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
292 if (pathapi) {
293 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
294 }
295 else {
296 _PathCchCanonicalizeEx = NULL;
297 }
298 _PathCchCanonicalizeEx_Initialized = 1;
299 }
300
301 if (_PathCchCanonicalizeEx) {
302 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200303 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800304 }
305 }
306 else {
307 if (!PathCanonicalizeW(buffer, path)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200308 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800309 }
310 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200311 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800312}
313
Victor Stinner0327bde2017-11-23 17:03:20 +0100314
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000315/* gotlandmark only called by search_for_prefix, which ensures
316 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100317 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000318static int
Victor Stinnerdec39712019-09-30 14:49:34 +0200319gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000320{
Victor Stinnerdec39712019-09-30 14:49:34 +0200321 wchar_t filename[MAXPATHLEN+1];
322 memset(filename, 0, sizeof(filename));
323 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
324 join(filename, landmark);
325 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000326}
327
Victor Stinner0327bde2017-11-23 17:03:20 +0100328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200330 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000331static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200332search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700335 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100337 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 reduce(prefix);
341 } while (prefix[0]);
342 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000343}
344
Victor Stinner0327bde2017-11-23 17:03:20 +0100345
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000346#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000347
Guido van Rossum88716bb2000-03-30 19:45:39 +0000348/* a string loaded from the DLL at startup.*/
349extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000350
Guido van Rossumeea14491997-08-13 21:30:44 +0000351/* Load a PYTHONPATH value from the registry.
352 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
353
Guido van Rossum88716bb2000-03-30 19:45:39 +0000354 Works in both Unicode and 8bit environments. Only uses the
355 Ex family of functions so it also works with Windows CE.
356
Guido van Rossumeea14491997-08-13 21:30:44 +0000357 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000358
359 XXX - this code is pretty strange, as it used to also
360 work on Win16, where the buffer sizes werent available
361 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000362*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000363static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000364getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 HKEY newKey = 0;
367 DWORD dataSize = 0;
368 DWORD numKeys = 0;
369 LONG rc;
370 wchar_t *retval = NULL;
371 WCHAR *dataBuf = NULL;
372 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
373 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700374 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 DWORD index;
376 WCHAR *keyBuf = NULL;
377 WCHAR *keyBufPtr;
378 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 /* Tried to use sysget("winver") but here is too early :-( */
381 versionLen = strlen(PyWin_DLLVersionString);
382 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700383 keyBufLen = sizeof(keyPrefix) +
384 sizeof(WCHAR)*(versionLen-1) +
385 sizeof(keySuffix);
386 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100387 if (keyBuf==NULL) {
388 goto done;
389 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000390
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700391 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200392 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
394 keyBufPtr += versionLen;
395 /* NULL comes with this one! */
396 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
397 /* Open the root Python key */
398 rc=RegOpenKeyExW(keyBase,
399 keyBuf, /* subkey */
400 0, /* reserved */
401 KEY_READ,
402 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100403 if (rc!=ERROR_SUCCESS) {
404 goto done;
405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 /* Find out how big our core buffer is, and how many subkeys we have */
407 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
408 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100409 if (rc!=ERROR_SUCCESS) {
410 goto done;
411 }
412 if (skipcore) {
413 dataSize = 0; /* Only count core ones if we want them! */
414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 /* Allocate a temp array of char buffers, so we only need to loop
416 reading the registry once
417 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200418 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100419 if (ppPaths==NULL) {
420 goto done;
421 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
423 /* Loop over all subkeys, allocating a temp sub-buffer. */
424 for(index=0;index<numKeys;index++) {
425 WCHAR keyBuf[MAX_PATH+1];
426 HKEY subKey = 0;
427 DWORD reqdSize = MAX_PATH+1;
428 /* Get the sub-key name */
429 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
430 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100431 if (rc!=ERROR_SUCCESS) {
432 goto done;
433 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* Open the sub-key */
435 rc=RegOpenKeyExW(newKey,
436 keyBuf, /* subkey */
437 0, /* reserved */
438 KEY_READ,
439 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100440 if (rc!=ERROR_SUCCESS) {
441 goto done;
442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* Find the value of the buffer size, malloc, then read it */
444 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
445 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200446 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (ppPaths[index]) {
448 RegQueryValueExW(subKey, NULL, 0, NULL,
449 (LPBYTE)ppPaths[index],
450 &reqdSize);
451 dataSize += reqdSize + 1; /* 1 for the ";" */
452 }
453 }
454 RegCloseKey(subKey);
455 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100458 if (dataSize == 0) {
459 goto done;
460 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461
462 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200463 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (dataBuf) {
465 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 /* Copy our collected strings */
467 for (index=0;index<numKeys;index++) {
468 if (index > 0) {
469 *(szCur++) = L';';
470 dataSize--;
471 }
472 if (ppPaths[index]) {
473 Py_ssize_t len = wcslen(ppPaths[index]);
474 wcsncpy(szCur, ppPaths[index], len);
475 szCur += len;
476 assert(dataSize > (DWORD)len);
477 dataSize -= (DWORD)len;
478 }
479 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100480 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100482 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 else {
luzpaza5293b42017-11-05 07:37:50 -0600484 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (numKeys) {
486 *(szCur++) = L';';
487 dataSize--;
488 }
489 /* Now append the core path entries -
490 this will include the NULL
491 */
492 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
493 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200494 if (rc != ERROR_SUCCESS) {
495 PyMem_RawFree(dataBuf);
496 goto done;
497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 /* And set the result - caller must free */
500 retval = dataBuf;
501 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000502done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* Loop freeing my temp buffers */
504 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200505 for(index=0; index<numKeys; index++)
506 PyMem_RawFree(ppPaths[index]);
507 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100509 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100511 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200512 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000514}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000515#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000516
Victor Stinner0327bde2017-11-23 17:03:20 +0100517
Victor Stinner410759f2019-05-18 04:17:01 +0200518wchar_t*
519_Py_GetDLLPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000520{
Victor Stinner9316ee42017-11-25 03:17:57 +0100521 wchar_t dll_path[MAXPATHLEN+1];
522 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000523
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000524#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100526 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100527 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
528 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100529 }
530 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000531#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100532 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000533#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100534
Victor Stinner410759f2019-05-18 04:17:01 +0200535 return _PyMem_RawWcsdup(dll_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100536}
537
538
Victor Stinner331a6a52019-05-27 16:39:22 +0200539static PyStatus
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200540get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100541{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200542 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800543 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100544 wchar_t program_full_path[MAXPATHLEN+1];
545 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100546
Steve Dower9048c492019-06-29 10:34:11 -0700547 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
548 /* GetModuleFileName should never fail when passed NULL */
549 return _PyStatus_ERR("Cannot determine program path");
550 }
551
Steve Dower1c3de542018-12-10 08:11:21 -0800552 /* The launcher may need to force the executable path to a
553 * different environment, so override it here. */
554 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
555 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower9048c492019-06-29 10:34:11 -0700556 /* If overridden, preserve the original full path */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200557 if (pathconfig->base_executable == NULL) {
558 pathconfig->base_executable = PyMem_RawMalloc(
559 sizeof(wchar_t) * (MAXPATHLEN + 1));
560 if (pathconfig->base_executable == NULL) {
561 return _PyStatus_NO_MEMORY();
562 }
563
564 status = canonicalize(pathconfig->base_executable,
565 program_full_path);
566 if (_PyStatus_EXCEPTION(status)) {
567 return status;
568 }
Steve Dower9048c492019-06-29 10:34:11 -0700569 }
570
Steve Dower1c3de542018-12-10 08:11:21 -0800571 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower9048c492019-06-29 10:34:11 -0700572 /* bpo-35873: Clear the environment variable to avoid it being
573 * inherited by child processes. */
574 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100575 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000576
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200577 if (pathconfig->program_full_path == NULL) {
578 pathconfig->program_full_path = PyMem_RawMalloc(
579 sizeof(wchar_t) * (MAXPATHLEN + 1));
580 if (pathconfig->program_full_path == NULL) {
581 return _PyStatus_NO_MEMORY();
582 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000583
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200584 status = canonicalize(pathconfig->program_full_path,
585 program_full_path);
586 if (_PyStatus_EXCEPTION(status)) {
587 return status;
588 }
589 }
590 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000591}
592
Victor Stinner0327bde2017-11-23 17:03:20 +0100593
Victor Stinner85ce0a72019-09-24 00:55:48 +0200594static PyStatus
595read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
596 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700597{
Victor Stinner85ce0a72019-09-24 00:55:48 +0200598 PyStatus status;
599 wchar_t *buf = NULL;
600 wchar_t *wline = NULL;
601 FILE *sp_file;
602
603 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100604 if (sp_file == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200605 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100606 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700607
Steve Dowered51b262016-09-17 12:54:06 -0700608 wcscpy_s(prefix, MAXPATHLEN+1, path);
609 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200610 pathconfig->isolated = 1;
611 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700612
Steve Dower4db86bc2016-09-09 09:17:35 -0700613 size_t bufsiz = MAXPATHLEN;
614 size_t prefixlen = wcslen(prefix);
615
Victor Stinner85ce0a72019-09-24 00:55:48 +0200616 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700617 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200618 status = _PyStatus_NO_MEMORY();
619 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700620 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700621 buf[0] = '\0';
622
623 while (!feof(sp_file)) {
624 char line[MAXPATHLEN + 1];
Victor Stinner85ce0a72019-09-24 00:55:48 +0200625 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100626 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700627 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100628 }
629 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700630 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100631 }
Steve Dowered51b262016-09-17 12:54:06 -0700632 while (*++p) {
633 if (*p == '\r' || *p == '\n') {
634 *p = '\0';
635 break;
636 }
637 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700638
Steve Dowered51b262016-09-17 12:54:06 -0700639 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200640 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700641 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200642 }
643 else if (strncmp(line, "import ", 7) == 0) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200644 status = _PyStatus_ERR("only 'import site' is supported "
645 "in ._pth file");
646 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700647 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700648
Steve Dowered51b262016-09-17 12:54:06 -0700649 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700650 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700651 if (wline == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200652 status = _PyStatus_NO_MEMORY();
653 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700654 }
Steve Dowered51b262016-09-17 12:54:06 -0700655 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700656 wline[wn] = '\0';
657
Steve Dowerc6dd4152016-10-27 14:28:07 -0700658 size_t usedsiz = wcslen(buf);
659 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700660 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700661 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
662 sizeof(wchar_t));
663 if (tmp == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200664 status = _PyStatus_NO_MEMORY();
665 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700666 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700667 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700668 }
669
Steve Dowerc6dd4152016-10-27 14:28:07 -0700670 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700671 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700672 usedsiz += 1;
673 }
Steve Dowered51b262016-09-17 12:54:06 -0700674
Steve Dowerc6dd4152016-10-27 14:28:07 -0700675 errno_t result;
676 _Py_BEGIN_SUPPRESS_IPH
677 result = wcscat_s(buf, bufsiz, prefix);
678 _Py_END_SUPPRESS_IPH
Victor Stinner85ce0a72019-09-24 00:55:48 +0200679
Steve Dowerc6dd4152016-10-27 14:28:07 -0700680 if (result == EINVAL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200681 status = _PyStatus_ERR("invalid argument during ._pth processing");
682 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700683 } else if (result == ERANGE) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200684 status = _PyStatus_ERR("buffer overflow during ._pth processing");
685 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700686 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200687
Steve Dowerc6dd4152016-10-27 14:28:07 -0700688 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700689 join(b, wline);
690
691 PyMem_RawFree(wline);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200692 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700693 }
694
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200695 if (pathconfig->module_search_path == NULL) {
696 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
697 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200698 status = _PyStatus_NO_MEMORY();
699 goto done;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200700 }
701 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700702
Victor Stinner85ce0a72019-09-24 00:55:48 +0200703 *found = 1;
704 status = _PyStatus_OK();
705 goto done;
706
707done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700708 PyMem_RawFree(buf);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200709 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700710 fclose(sp_file);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200711 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100712}
713
714
715static int
Victor Stinnerc4221672019-09-21 01:02:56 +0200716get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
717 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100718{
Victor Stinnerc4221672019-09-21 01:02:56 +0200719 if (calculate->dll_path[0]) {
720 if (!change_ext(filename, calculate->dll_path, L"._pth") &&
721 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100722 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100723 return 1;
724 }
725 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200726 if (pathconfig->program_full_path[0]) {
Victor Stinnerc4221672019-09-21 01:02:56 +0200727 if (!change_ext(filename, pathconfig->program_full_path, L"._pth") &&
728 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100729 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100730 return 1;
731 }
732 }
733 return 0;
734}
735
736
Victor Stinner85ce0a72019-09-24 00:55:48 +0200737static PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200738calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200739 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100740{
Victor Stinnerc4221672019-09-21 01:02:56 +0200741 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100742
Victor Stinnerc4221672019-09-21 01:02:56 +0200743 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200744 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100745 }
746
Victor Stinner85ce0a72019-09-24 00:55:48 +0200747 return read_pth_file(pathconfig, prefix, filename, found);
Victor Stinner0327bde2017-11-23 17:03:20 +0100748}
749
750
751/* Search for an environment configuration file, first in the
752 executable's directory and then in the parent directory.
753 If found, open it for use when searching for prefixes.
754*/
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200755static PyStatus
Victor Stinner85ce0a72019-09-24 00:55:48 +0200756calculate_pyvenv_file(PyCalculatePath *calculate,
757 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100758{
Victor Stinner221fd842019-09-25 02:54:25 +0200759 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100760 const wchar_t *env_cfg = L"pyvenv.cfg";
761
Victor Stinner221fd842019-09-25 02:54:25 +0200762 /* Filename: <argv0_path_len> / "pyvenv.cfg" */
763 wcscpy_s(filename, MAXPATHLEN+1, argv0_path);
764 join(filename, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100765
Victor Stinner221fd842019-09-25 02:54:25 +0200766 FILE *env_file = _Py_wfopen(filename, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100767 if (env_file == NULL) {
768 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100769
Victor Stinner221fd842019-09-25 02:54:25 +0200770 /* Filename: <basename(basename(argv0_path_len))> / "pyvenv.cfg" */
771 reduce(filename);
772 reduce(filename);
773 join(filename, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100774
Victor Stinner221fd842019-09-25 02:54:25 +0200775 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100776 if (env_file == NULL) {
777 errno = 0;
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200778 return _PyStatus_OK();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100779 }
780 }
781
Victor Stinner0327bde2017-11-23 17:03:20 +0100782 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200783 wchar_t *home = NULL;
784 PyStatus status = _Py_FindEnvConfigValue(env_file, L"home", &home);
785 if (_PyStatus_EXCEPTION(status)) {
786 fclose(env_file);
787 return status;
788 }
789 if (home) {
Victor Stinner221fd842019-09-25 02:54:25 +0200790 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200791 PyMem_RawFree(home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100792 }
793 fclose(env_file);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200794 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100795}
796
797
798static void
Victor Stinner85ce0a72019-09-24 00:55:48 +0200799calculate_home_prefix(PyCalculatePath *calculate,
800 const wchar_t *argv0_path,
801 const wchar_t *zip_path,
802 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100803{
Victor Stinner0327bde2017-11-23 17:03:20 +0100804 if (calculate->home == NULL || *calculate->home == '\0') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200805 if (zip_path[0] && exists(zip_path)) {
806 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100807 reduce(prefix);
808 calculate->home = prefix;
809 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200810 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100811 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100812 }
813 else {
814 calculate->home = NULL;
815 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100817 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100818 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100819 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100820}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000821
Victor Stinner9316ee42017-11-25 03:17:57 +0100822
Victor Stinner331a6a52019-05-27 16:39:22 +0200823static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200824calculate_module_search_path(PyCalculatePath *calculate,
825 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200826 const wchar_t *argv0_path,
827 wchar_t *prefix,
828 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100829{
Victor Stinner0327bde2017-11-23 17:03:20 +0100830 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000831#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100832 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
833 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000834#endif
luzpaza5293b42017-11-05 07:37:50 -0600835 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 anything better to use! */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200837 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100838 calculate->home != NULL ||
839 calculate->machine_path != NULL ||
840 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 /* We need to construct a path from the following parts.
843 (1) the PYTHONPATH environment variable, if set;
844 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100845 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100847 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 (5) the directory containing the executable (argv0_path).
849 The length calculation calculates #4 first.
850 Extra rules:
851 - If PYTHONHOME is set (in any way) item (3) is ignored.
852 - If registry values are used, (4) and (5) are ignored.
853 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100856 size_t bufsz = 0;
857 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200858 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 bufsz = 1;
860 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100861 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100865 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700867 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200868 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100869 if (calculate->user_path) {
870 bufsz += wcslen(calculate->user_path) + 1;
871 }
872 if (calculate->machine_path) {
873 bufsz += wcslen(calculate->machine_path) + 1;
874 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200875 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200876 if (calculate->pythonpath_env != NULL) {
877 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100878 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000879
Victor Stinner0327bde2017-11-23 17:03:20 +0100880 wchar_t *buf, *start_buf;
881 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200883 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000886
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200887 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100888 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200889 calculate->pythonpath_env)) {
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 Stinner85ce0a72019-09-24 00:55:48 +0200895 if (zip_path[0]) {
896 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_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;
901 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 if (calculate->user_path) {
903 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_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->machine_path) {
910 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100911 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 buf = wcschr(buf, L'\0');
914 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100916 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100918 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100919 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700922 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700924 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200925 const wchar_t *p = PYTHONPATH;
926 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 size_t n;
928 for (;;) {
929 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100930 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100932 }
933 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100937 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100938 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 buf = wcschr(buf, L'\0');
941 p++;
942 n--;
943 }
944 wcsncpy(buf, p, n);
945 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700946 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100947 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 p = q+1;
951 }
952 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200953 if (argv0_path) {
954 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700956 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700958 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* Now to pull one last hack/trick. If sys.prefix is
961 empty, then try and find it somewhere on the paths
962 we calculated. We scan backwards, as our general policy
963 is that Python core directories are at the *end* of
964 sys.path. We assume that our "lib" directory is
965 on the path, and that our 'prefix' directory is
966 the parent of that.
967 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100968 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200970 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 while (1) {
972 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200973 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 /* 'look' will end up one character before the
975 start of the path in question - even if this
976 is one character before the start of the buffer
977 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100978 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 look--;
980 nchars = lookEnd-look;
981 wcsncpy(lookBuf, look+1, nchars);
982 lookBuf[nchars] = L'\0';
983 /* Up one level to the parent */
984 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100985 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 break;
987 }
988 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100989 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 look--;
993 }
994 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100995
Victor Stinner331a6a52019-05-27 16:39:22 +0200996 pathconfig->module_search_path = start_buf;
997 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100998}
999
1000
Victor Stinner331a6a52019-05-27 16:39:22 +02001001static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001002calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +01001003{
Victor Stinner331a6a52019-05-27 16:39:22 +02001004 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +01001005
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001006 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +02001007 if (_PyStatus_EXCEPTION(status)) {
1008 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +01001009 }
1010
Victor Stinnerb64de462017-12-01 18:27:09 +01001011 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001012 wchar_t argv0_path[MAXPATHLEN+1];
1013 memset(argv0_path, 0, sizeof(argv0_path));
1014
1015 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
1016 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +01001017
1018 wchar_t prefix[MAXPATHLEN+1];
1019 memset(prefix, 0, sizeof(prefix));
1020
1021 /* Search for a sys.path file */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001022 int pth_found = 0;
1023 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
1024 if (_PyStatus_EXCEPTION(status)) {
1025 return status;
1026 }
1027 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001028 goto done;
1029 }
1030
Victor Stinnerc02b41b2019-10-04 19:53:43 +02001031 status = calculate_pyvenv_file(calculate,
1032 argv0_path, Py_ARRAY_LENGTH(argv0_path));
1033 if (_PyStatus_EXCEPTION(status)) {
1034 return status;
1035 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001036
1037 /* Calculate zip archive path from DLL or exe path */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001038 wchar_t zip_path[MAXPATHLEN+1];
1039 memset(zip_path, 0, sizeof(zip_path));
1040
1041 change_ext(zip_path,
Victor Stinnerc4221672019-09-21 01:02:56 +02001042 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001043 L".zip");
1044
Victor Stinner85ce0a72019-09-24 00:55:48 +02001045 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001046
Victor Stinnere2677932019-09-21 01:50:16 +02001047 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001048 status = calculate_module_search_path(calculate, pathconfig,
1049 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001050 if (_PyStatus_EXCEPTION(status)) {
1051 return status;
1052 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001053 }
1054
1055done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001056 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001057 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1058 if (pathconfig->prefix == NULL) {
1059 return _PyStatus_NO_MEMORY();
1060 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001061 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001062 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001063 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1064 if (pathconfig->exec_prefix == NULL) {
1065 return _PyStatus_NO_MEMORY();
1066 }
Steve Dower177a41a2018-11-17 20:41:48 -08001067 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001068
Victor Stinner331a6a52019-05-27 16:39:22 +02001069 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001070}
1071
1072
Victor Stinner85ce0a72019-09-24 00:55:48 +02001073static PyStatus
1074calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1075 const PyConfig *config)
1076{
1077 calculate->home = pathconfig->home;
1078 calculate->path_env = _wgetenv(L"PATH");
1079
1080 calculate->dll_path = _Py_GetDLLPath();
1081 if (calculate->dll_path == NULL) {
1082 return _PyStatus_NO_MEMORY();
1083 }
1084
1085 calculate->pythonpath_env = config->pythonpath_env;
1086
1087 return _PyStatus_OK();
1088}
1089
1090
Victor Stinner0327bde2017-11-23 17:03:20 +01001091static void
1092calculate_free(PyCalculatePath *calculate)
1093{
1094 PyMem_RawFree(calculate->machine_path);
1095 PyMem_RawFree(calculate->user_path);
Victor Stinnerc4221672019-09-21 01:02:56 +02001096 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001097}
1098
Victor Stinner9316ee42017-11-25 03:17:57 +01001099
Victor Stinner85ce0a72019-09-24 00:55:48 +02001100/* Calculate the Python path configuration.
1101
1102 Inputs:
1103
1104 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1105 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
1106 - DLL path: _Py_GetDLLPath()
1107 - PATH environment variable
1108 - __PYVENV_LAUNCHER__ environment variable
1109 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1110 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001111 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001112 - pyvenv.cfg configuration file
1113 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001114 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1115 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001116
1117 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001118
1119 - base_executable
1120 - program_full_path
1121 - module_search_path
1122 - prefix
1123 - exec_prefix
1124 - isolated
1125 - site_import
1126
Victor Stinner85ce0a72019-09-24 00:55:48 +02001127 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001128PyStatus
1129_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001130{
Victor Stinnerc4221672019-09-21 01:02:56 +02001131 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001132 PyCalculatePath calculate;
1133 memset(&calculate, 0, sizeof(calculate));
1134
Victor Stinner85ce0a72019-09-24 00:55:48 +02001135 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001136 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001137 goto done;
1138 }
1139
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001140 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001141
Victor Stinner9316ee42017-11-25 03:17:57 +01001142done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001143 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001144 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001145}
1146
1147
Victor Stinner63941882011-09-29 00:42:28 +02001148/* Load python3.dll before loading any extension module that might refer
1149 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001150 to this python DLL is loaded, not a python3.dll that might be on the path
1151 by chance.
1152 Return whether the DLL was found.
1153*/
1154static int python3_checked = 0;
1155static HANDLE hPython3;
1156int
Victor Stinner31a83932017-12-04 13:39:15 +01001157_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001158{
1159 wchar_t py3path[MAXPATHLEN+1];
1160 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001161 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001162 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001163 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001164 python3_checked = 1;
1165
1166 /* If there is a python3.dll next to the python3y.dll,
1167 assume this is a build tree; use that DLL */
Victor Stinnerc4221672019-09-21 01:02:56 +02001168 if (_Py_dll_path != NULL) {
1169 wcscpy(py3path, _Py_dll_path);
1170 }
1171 else {
1172 wcscpy(py3path, L"");
1173 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001174 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001175 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001176 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001177 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001178 wcscpy(s, L"\\python3.dll");
1179 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001180 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001181 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001182 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001183
1184 /* Check sys.prefix\DLLs\python3.dll */
1185 wcscpy(py3path, Py_GetPrefix());
1186 wcscat(py3path, L"\\DLLs\\python3.dll");
1187 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1188 return hPython3 != NULL;
1189}