blob: b17c002f25c8efca46e026f856860cbc72e0b2a6 [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*/
755static void
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 Stinner221fd842019-09-25 02:54:25 +0200778 return;
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 Stinner221fd842019-09-25 02:54:25 +0200783 wchar_t home[MAXPATHLEN+1];
784 if (_Py_FindEnvConfigValue(env_file, L"home",
785 home, Py_ARRAY_LENGTH(home))) {
786 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100787 }
788 fclose(env_file);
789}
790
791
792static void
Victor Stinner85ce0a72019-09-24 00:55:48 +0200793calculate_home_prefix(PyCalculatePath *calculate,
794 const wchar_t *argv0_path,
795 const wchar_t *zip_path,
796 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100797{
Victor Stinner0327bde2017-11-23 17:03:20 +0100798 if (calculate->home == NULL || *calculate->home == '\0') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200799 if (zip_path[0] && exists(zip_path)) {
800 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100801 reduce(prefix);
802 calculate->home = prefix;
803 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200804 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100805 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100806 }
807 else {
808 calculate->home = NULL;
809 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100811 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100812 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100813 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100814}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000815
Victor Stinner9316ee42017-11-25 03:17:57 +0100816
Victor Stinner331a6a52019-05-27 16:39:22 +0200817static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200818calculate_module_search_path(PyCalculatePath *calculate,
819 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200820 const wchar_t *argv0_path,
821 wchar_t *prefix,
822 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100823{
Victor Stinner0327bde2017-11-23 17:03:20 +0100824 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000825#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100826 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
827 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000828#endif
luzpaza5293b42017-11-05 07:37:50 -0600829 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 anything better to use! */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200831 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100832 calculate->home != NULL ||
833 calculate->machine_path != NULL ||
834 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 /* We need to construct a path from the following parts.
837 (1) the PYTHONPATH environment variable, if set;
838 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100839 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100841 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 (5) the directory containing the executable (argv0_path).
843 The length calculation calculates #4 first.
844 Extra rules:
845 - If PYTHONHOME is set (in any way) item (3) is ignored.
846 - If registry values are used, (4) and (5) are ignored.
847 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100850 size_t bufsz = 0;
851 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200852 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 bufsz = 1;
854 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100855 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100859 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700861 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200862 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 if (calculate->user_path) {
864 bufsz += wcslen(calculate->user_path) + 1;
865 }
866 if (calculate->machine_path) {
867 bufsz += wcslen(calculate->machine_path) + 1;
868 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200869 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200870 if (calculate->pythonpath_env != NULL) {
871 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100872 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000873
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 wchar_t *buf, *start_buf;
875 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200877 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100879 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000880
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200881 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100882 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200883 calculate->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100884 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 buf = wcschr(buf, L'\0');
887 *buf++ = DELIM;
888 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200889 if (zip_path[0]) {
890 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100891 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 buf = wcschr(buf, L'\0');
894 *buf++ = DELIM;
895 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100896 if (calculate->user_path) {
897 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100898 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 buf = wcschr(buf, L'\0');
901 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100903 if (calculate->machine_path) {
904 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100905 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100906 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 buf = wcschr(buf, L'\0');
908 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100910 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100913 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700916 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700918 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200919 const wchar_t *p = PYTHONPATH;
920 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 size_t n;
922 for (;;) {
923 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100924 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100926 }
927 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100929 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100931 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100932 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 buf = wcschr(buf, L'\0');
935 p++;
936 n--;
937 }
938 wcsncpy(buf, p, n);
939 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700940 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100941 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100943 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 p = q+1;
945 }
946 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200947 if (argv0_path) {
948 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700950 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700952 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 /* Now to pull one last hack/trick. If sys.prefix is
955 empty, then try and find it somewhere on the paths
956 we calculated. We scan backwards, as our general policy
957 is that Python core directories are at the *end* of
958 sys.path. We assume that our "lib" directory is
959 on the path, and that our 'prefix' directory is
960 the parent of that.
961 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100962 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200964 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 while (1) {
966 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200967 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* 'look' will end up one character before the
969 start of the path in question - even if this
970 is one character before the start of the buffer
971 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100972 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 look--;
974 nchars = lookEnd-look;
975 wcsncpy(lookBuf, look+1, nchars);
976 lookBuf[nchars] = L'\0';
977 /* Up one level to the parent */
978 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100979 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 break;
981 }
982 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100983 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100985 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 look--;
987 }
988 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100989
Victor Stinner331a6a52019-05-27 16:39:22 +0200990 pathconfig->module_search_path = start_buf;
991 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100992}
993
994
Victor Stinner331a6a52019-05-27 16:39:22 +0200995static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200996calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100997{
Victor Stinner331a6a52019-05-27 16:39:22 +0200998 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100999
Victor Stinnerfcdb0272019-09-23 14:45:47 +02001000 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +02001001 if (_PyStatus_EXCEPTION(status)) {
1002 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +01001003 }
1004
Victor Stinnerb64de462017-12-01 18:27:09 +01001005 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001006 wchar_t argv0_path[MAXPATHLEN+1];
1007 memset(argv0_path, 0, sizeof(argv0_path));
1008
1009 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
1010 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +01001011
1012 wchar_t prefix[MAXPATHLEN+1];
1013 memset(prefix, 0, sizeof(prefix));
1014
1015 /* Search for a sys.path file */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001016 int pth_found = 0;
1017 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
1018 if (_PyStatus_EXCEPTION(status)) {
1019 return status;
1020 }
1021 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001022 goto done;
1023 }
1024
Victor Stinner85ce0a72019-09-24 00:55:48 +02001025 calculate_pyvenv_file(calculate, argv0_path, Py_ARRAY_LENGTH(argv0_path));
Victor Stinner9316ee42017-11-25 03:17:57 +01001026
1027 /* Calculate zip archive path from DLL or exe path */
Victor Stinner85ce0a72019-09-24 00:55:48 +02001028 wchar_t zip_path[MAXPATHLEN+1];
1029 memset(zip_path, 0, sizeof(zip_path));
1030
1031 change_ext(zip_path,
Victor Stinnerc4221672019-09-21 01:02:56 +02001032 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001033 L".zip");
1034
Victor Stinner85ce0a72019-09-24 00:55:48 +02001035 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001036
Victor Stinnere2677932019-09-21 01:50:16 +02001037 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001038 status = calculate_module_search_path(calculate, pathconfig,
1039 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001040 if (_PyStatus_EXCEPTION(status)) {
1041 return status;
1042 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001043 }
1044
1045done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001046 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001047 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1048 if (pathconfig->prefix == NULL) {
1049 return _PyStatus_NO_MEMORY();
1050 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001051 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001052 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001053 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1054 if (pathconfig->exec_prefix == NULL) {
1055 return _PyStatus_NO_MEMORY();
1056 }
Steve Dower177a41a2018-11-17 20:41:48 -08001057 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001058
Victor Stinner331a6a52019-05-27 16:39:22 +02001059 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001060}
1061
1062
Victor Stinner85ce0a72019-09-24 00:55:48 +02001063static PyStatus
1064calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1065 const PyConfig *config)
1066{
1067 calculate->home = pathconfig->home;
1068 calculate->path_env = _wgetenv(L"PATH");
1069
1070 calculate->dll_path = _Py_GetDLLPath();
1071 if (calculate->dll_path == NULL) {
1072 return _PyStatus_NO_MEMORY();
1073 }
1074
1075 calculate->pythonpath_env = config->pythonpath_env;
1076
1077 return _PyStatus_OK();
1078}
1079
1080
Victor Stinner0327bde2017-11-23 17:03:20 +01001081static void
1082calculate_free(PyCalculatePath *calculate)
1083{
1084 PyMem_RawFree(calculate->machine_path);
1085 PyMem_RawFree(calculate->user_path);
Victor Stinnerc4221672019-09-21 01:02:56 +02001086 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001087}
1088
Victor Stinner9316ee42017-11-25 03:17:57 +01001089
Victor Stinner85ce0a72019-09-24 00:55:48 +02001090/* Calculate the Python path configuration.
1091
1092 Inputs:
1093
1094 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1095 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
1096 - DLL path: _Py_GetDLLPath()
1097 - PATH environment variable
1098 - __PYVENV_LAUNCHER__ environment variable
1099 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1100 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001101 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001102 - pyvenv.cfg configuration file
1103 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001104 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1105 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001106
1107 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001108
1109 - base_executable
1110 - program_full_path
1111 - module_search_path
1112 - prefix
1113 - exec_prefix
1114 - isolated
1115 - site_import
1116
Victor Stinner85ce0a72019-09-24 00:55:48 +02001117 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001118PyStatus
1119_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001120{
Victor Stinnerc4221672019-09-21 01:02:56 +02001121 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001122 PyCalculatePath calculate;
1123 memset(&calculate, 0, sizeof(calculate));
1124
Victor Stinner85ce0a72019-09-24 00:55:48 +02001125 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001126 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001127 goto done;
1128 }
1129
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001130 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001131
Victor Stinner9316ee42017-11-25 03:17:57 +01001132done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001133 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001134 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001135}
1136
1137
Victor Stinner63941882011-09-29 00:42:28 +02001138/* Load python3.dll before loading any extension module that might refer
1139 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001140 to this python DLL is loaded, not a python3.dll that might be on the path
1141 by chance.
1142 Return whether the DLL was found.
1143*/
1144static int python3_checked = 0;
1145static HANDLE hPython3;
1146int
Victor Stinner31a83932017-12-04 13:39:15 +01001147_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001148{
1149 wchar_t py3path[MAXPATHLEN+1];
1150 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001151 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001152 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001153 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001154 python3_checked = 1;
1155
1156 /* If there is a python3.dll next to the python3y.dll,
1157 assume this is a build tree; use that DLL */
Victor Stinnerc4221672019-09-21 01:02:56 +02001158 if (_Py_dll_path != NULL) {
1159 wcscpy(py3path, _Py_dll_path);
1160 }
1161 else {
1162 wcscpy(py3path, L"");
1163 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001164 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001165 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001166 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001167 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001168 wcscpy(s, L"\\python3.dll");
1169 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001170 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001171 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001172 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001173
1174 /* Check sys.prefix\DLLs\python3.dll */
1175 wcscpy(py3path, Py_GetPrefix());
1176 wcscat(py3path, L"\\DLLs\\python3.dll");
1177 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1178 return hPython3 != NULL;
1179}