blob: 3b65b35ce6146ed1a5ea867e7c479ae5d52e4ddd [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>
Steve Dower6a65eba2020-01-29 13:46:33 +110094#include <pathcch.h>
erikjanss6cf82552018-07-25 02:41:46 +020095#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070096
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000098#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099#endif /* HAVE_SYS_TYPES_H */
100
101#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000103#endif /* HAVE_SYS_STAT_H */
104
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105#include <string.h>
106
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107/* Search in some common locations for the associated Python libraries.
108 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000109 * Py_GetPath() tries to return a sensible Python module search path.
110 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000111 * The approach is an adaptation for Windows of the strategy used in
112 * ../Modules/getpath.c; it uses the Windows Registry as one of its
113 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000114 *
115 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
116 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000117 */
118
119#ifndef LANDMARK
Victor Stinner85ce0a72019-09-24 00:55:48 +0200120# define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000121#endif
122
Victor Stinner85ce0a72019-09-24 00:55:48 +0200123#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
124
125
Victor Stinner0327bde2017-11-23 17:03:20 +0100126typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200127 const wchar_t *path_env; /* PATH environment variable */
128 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100129
Victor Stinner85ce0a72019-09-24 00:55:48 +0200130 /* Registry key "Software\Python\PythonCore\X.Y\PythonPath"
131 where X.Y is the Python version (major.minor) */
Victor Stinner0327bde2017-11-23 17:03:20 +0100132 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
133 wchar_t *user_path; /* from HKEY_CURRENT_USER */
134
Victor Stinnerc4221672019-09-21 01:02:56 +0200135 wchar_t *dll_path;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200136
137 const wchar_t *pythonpath_env;
Victor Stinner0327bde2017-11-23 17:03:20 +0100138} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139
Guido van Rossumeea14491997-08-13 21:30:44 +0000140
Victor Stinner0327bde2017-11-23 17:03:20 +0100141/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100143is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000144{
145#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000149#endif
150}
151
Victor Stinner0327bde2017-11-23 17:03:20 +0100152
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000153/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100154 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000155static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000156reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000157{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700158 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100159 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700160 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100161 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 while (i > 0 && !is_sep(dir[i]))
164 --i;
165 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000166}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167
Victor Stinner0327bde2017-11-23 17:03:20 +0100168
Steve Dowered51b262016-09-17 12:54:06 -0700169static int
170change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
171{
172 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
173 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100174 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700175 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100176 }
Steve Dowered51b262016-09-17 12:54:06 -0700177
178 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
179 --i;
180
181 if (i == 0) {
182 dest[0] = '\0';
183 return -1;
184 }
185
Victor Stinner0327bde2017-11-23 17:03:20 +0100186 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700187 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100188 }
Steve Dowered51b262016-09-17 12:54:06 -0700189
190 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100191 wcscat_s(dest, MAXPATHLEN+1, ext))
192 {
Steve Dowered51b262016-09-17 12:54:06 -0700193 dest[0] = '\0';
194 return -1;
195 }
196
197 return 0;
198}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000199
Victor Stinner0327bde2017-11-23 17:03:20 +0100200
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000201static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200202exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000205}
206
Victor Stinner0327bde2017-11-23 17:03:20 +0100207
208/* Is module -- check for .pyc too.
209 Assumes 'filename' MAXPATHLEN+1 bytes long -
210 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000211static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100212ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000213{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100214 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700215
Victor Stinner0327bde2017-11-23 17:03:20 +0100216 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100218 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700221 n = wcsnlen_s(filename, MAXPATHLEN+1);
222 if (n < MAXPATHLEN) {
223 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800224 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700225 filename[n + 1] = L'\0';
226 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100227 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700228 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100229 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700230 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 }
232 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000233}
234
Victor Stinner0327bde2017-11-23 17:03:20 +0100235
Tim Peters8484fbf2004-08-07 19:12:27 +0000236/* Add a path component, by appending stuff to buffer.
237 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
238 NUL-terminated string with no more than MAXPATHLEN characters (not counting
239 the trailing NUL). It's a fatal error if it contains a string longer than
240 that (callers must be careful!). If these requirements are met, it's
241 guaranteed that buffer will still be a NUL-terminated string with no more
242 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
243 stuff as fits will be appended.
244*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700245
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000246static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700247join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000248{
Steve Dower6a65eba2020-01-29 13:46:33 +1100249 if (FAILED(PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
250 Py_FatalError("buffer overflow in getpathp.c's join()");
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700251 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000252}
253
Victor Stinner85ce0a72019-09-24 00:55:48 +0200254/* Call PathCchCanonicalizeEx(path): remove navigation elements such as "."
255 and ".." to produce a direct, well-formed path. */
256static PyStatus
257canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800258{
259 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200260 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800261 }
262
Steve Dower6a65eba2020-01-29 13:46:33 +1100263 if (FAILED(PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
264 return INIT_ERR_BUFFER_OVERFLOW();
Steve Dower48e8c822018-02-22 10:39:26 -0800265 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200266 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800267}
268
Victor Stinner0327bde2017-11-23 17:03:20 +0100269
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000270/* gotlandmark only called by search_for_prefix, which ensures
271 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000273static int
Victor Stinnerdec39712019-09-30 14:49:34 +0200274gotlandmark(const wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000275{
Victor Stinnerdec39712019-09-30 14:49:34 +0200276 wchar_t filename[MAXPATHLEN+1];
277 memset(filename, 0, sizeof(filename));
278 wcscpy_s(filename, Py_ARRAY_LENGTH(filename), prefix);
279 join(filename, landmark);
280 return ismodule(filename, FALSE);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000281}
282
Victor Stinner0327bde2017-11-23 17:03:20 +0100283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200285 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000286static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200287search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000288{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700290 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100292 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 reduce(prefix);
296 } while (prefix[0]);
297 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000298}
299
Victor Stinner0327bde2017-11-23 17:03:20 +0100300
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000301#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000302
Guido van Rossum88716bb2000-03-30 19:45:39 +0000303/* a string loaded from the DLL at startup.*/
304extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000305
Guido van Rossumeea14491997-08-13 21:30:44 +0000306/* Load a PYTHONPATH value from the registry.
307 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
308
Guido van Rossum88716bb2000-03-30 19:45:39 +0000309 Works in both Unicode and 8bit environments. Only uses the
310 Ex family of functions so it also works with Windows CE.
311
Guido van Rossumeea14491997-08-13 21:30:44 +0000312 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000313
314 XXX - this code is pretty strange, as it used to also
315 work on Win16, where the buffer sizes werent available
316 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000317*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000318static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000319getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 HKEY newKey = 0;
322 DWORD dataSize = 0;
323 DWORD numKeys = 0;
324 LONG rc;
325 wchar_t *retval = NULL;
326 WCHAR *dataBuf = NULL;
327 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
328 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700329 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 DWORD index;
331 WCHAR *keyBuf = NULL;
332 WCHAR *keyBufPtr;
333 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* Tried to use sysget("winver") but here is too early :-( */
336 versionLen = strlen(PyWin_DLLVersionString);
337 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700338 keyBufLen = sizeof(keyPrefix) +
339 sizeof(WCHAR)*(versionLen-1) +
340 sizeof(keySuffix);
341 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100342 if (keyBuf==NULL) {
343 goto done;
344 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000345
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700346 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200347 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
349 keyBufPtr += versionLen;
350 /* NULL comes with this one! */
351 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
352 /* Open the root Python key */
353 rc=RegOpenKeyExW(keyBase,
354 keyBuf, /* subkey */
355 0, /* reserved */
356 KEY_READ,
357 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100358 if (rc!=ERROR_SUCCESS) {
359 goto done;
360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 /* Find out how big our core buffer is, and how many subkeys we have */
362 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
363 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100364 if (rc!=ERROR_SUCCESS) {
365 goto done;
366 }
367 if (skipcore) {
368 dataSize = 0; /* Only count core ones if we want them! */
369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* Allocate a temp array of char buffers, so we only need to loop
371 reading the registry once
372 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200373 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100374 if (ppPaths==NULL) {
375 goto done;
376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
378 /* Loop over all subkeys, allocating a temp sub-buffer. */
379 for(index=0;index<numKeys;index++) {
380 WCHAR keyBuf[MAX_PATH+1];
381 HKEY subKey = 0;
382 DWORD reqdSize = MAX_PATH+1;
383 /* Get the sub-key name */
384 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
385 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100386 if (rc!=ERROR_SUCCESS) {
387 goto done;
388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Open the sub-key */
390 rc=RegOpenKeyExW(newKey,
391 keyBuf, /* subkey */
392 0, /* reserved */
393 KEY_READ,
394 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100395 if (rc!=ERROR_SUCCESS) {
396 goto done;
397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 /* Find the value of the buffer size, malloc, then read it */
399 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
400 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200401 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (ppPaths[index]) {
403 RegQueryValueExW(subKey, NULL, 0, NULL,
404 (LPBYTE)ppPaths[index],
405 &reqdSize);
406 dataSize += reqdSize + 1; /* 1 for the ";" */
407 }
408 }
409 RegCloseKey(subKey);
410 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100413 if (dataSize == 0) {
414 goto done;
415 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416
417 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200418 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (dataBuf) {
420 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* Copy our collected strings */
422 for (index=0;index<numKeys;index++) {
423 if (index > 0) {
424 *(szCur++) = L';';
425 dataSize--;
426 }
427 if (ppPaths[index]) {
428 Py_ssize_t len = wcslen(ppPaths[index]);
429 wcsncpy(szCur, ppPaths[index], len);
430 szCur += len;
431 assert(dataSize > (DWORD)len);
432 dataSize -= (DWORD)len;
433 }
434 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100435 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100437 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 else {
luzpaza5293b42017-11-05 07:37:50 -0600439 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (numKeys) {
441 *(szCur++) = L';';
442 dataSize--;
443 }
444 /* Now append the core path entries -
445 this will include the NULL
446 */
447 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
448 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200449 if (rc != ERROR_SUCCESS) {
450 PyMem_RawFree(dataBuf);
451 goto done;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 }
454 /* And set the result - caller must free */
455 retval = dataBuf;
456 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000457done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Loop freeing my temp buffers */
459 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200460 for(index=0; index<numKeys; index++)
461 PyMem_RawFree(ppPaths[index]);
462 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100464 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100466 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200467 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000469}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000470#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000471
Victor Stinner0327bde2017-11-23 17:03:20 +0100472
Victor Stinner410759f2019-05-18 04:17:01 +0200473wchar_t*
474_Py_GetDLLPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000475{
Victor Stinner9316ee42017-11-25 03:17:57 +0100476 wchar_t dll_path[MAXPATHLEN+1];
477 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000478
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000479#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100481 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100482 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
483 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100484 }
485 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000486#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100487 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000488#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100489
Victor Stinner410759f2019-05-18 04:17:01 +0200490 return _PyMem_RawWcsdup(dll_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100491}
492
493
Victor Stinner331a6a52019-05-27 16:39:22 +0200494static PyStatus
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200495get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100496{
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200497 PyStatus status;
Steve Dower1c3de542018-12-10 08:11:21 -0800498 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100499 wchar_t program_full_path[MAXPATHLEN+1];
500 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100501
Steve Dower9048c492019-06-29 10:34:11 -0700502 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
503 /* GetModuleFileName should never fail when passed NULL */
504 return _PyStatus_ERR("Cannot determine program path");
505 }
506
Steve Dower1c3de542018-12-10 08:11:21 -0800507 /* The launcher may need to force the executable path to a
508 * different environment, so override it here. */
509 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
510 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower9048c492019-06-29 10:34:11 -0700511 /* If overridden, preserve the original full path */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200512 if (pathconfig->base_executable == NULL) {
513 pathconfig->base_executable = PyMem_RawMalloc(
514 sizeof(wchar_t) * (MAXPATHLEN + 1));
515 if (pathconfig->base_executable == NULL) {
516 return _PyStatus_NO_MEMORY();
517 }
518
519 status = canonicalize(pathconfig->base_executable,
520 program_full_path);
521 if (_PyStatus_EXCEPTION(status)) {
522 return status;
523 }
Steve Dower9048c492019-06-29 10:34:11 -0700524 }
525
Steve Dower1c3de542018-12-10 08:11:21 -0800526 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower9048c492019-06-29 10:34:11 -0700527 /* bpo-35873: Clear the environment variable to avoid it being
528 * inherited by child processes. */
529 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100530 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000531
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200532 if (pathconfig->program_full_path == NULL) {
533 pathconfig->program_full_path = PyMem_RawMalloc(
534 sizeof(wchar_t) * (MAXPATHLEN + 1));
535 if (pathconfig->program_full_path == NULL) {
536 return _PyStatus_NO_MEMORY();
537 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000538
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200539 status = canonicalize(pathconfig->program_full_path,
540 program_full_path);
541 if (_PyStatus_EXCEPTION(status)) {
542 return status;
543 }
544 }
545 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000546}
547
Victor Stinner0327bde2017-11-23 17:03:20 +0100548
Victor Stinner85ce0a72019-09-24 00:55:48 +0200549static PyStatus
550read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path,
551 int *found)
Steve Dower4db86bc2016-09-09 09:17:35 -0700552{
Victor Stinner85ce0a72019-09-24 00:55:48 +0200553 PyStatus status;
554 wchar_t *buf = NULL;
555 wchar_t *wline = NULL;
556 FILE *sp_file;
557
558 sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100559 if (sp_file == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200560 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100561 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700562
Steve Dowered51b262016-09-17 12:54:06 -0700563 wcscpy_s(prefix, MAXPATHLEN+1, path);
564 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200565 pathconfig->isolated = 1;
566 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700567
Steve Dower4db86bc2016-09-09 09:17:35 -0700568 size_t bufsiz = MAXPATHLEN;
569 size_t prefixlen = wcslen(prefix);
570
Victor Stinner85ce0a72019-09-24 00:55:48 +0200571 buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700572 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200573 status = _PyStatus_NO_MEMORY();
574 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700575 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700576 buf[0] = '\0';
577
578 while (!feof(sp_file)) {
579 char line[MAXPATHLEN + 1];
Victor Stinner85ce0a72019-09-24 00:55:48 +0200580 char *p = fgets(line, Py_ARRAY_LENGTH(line), sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100581 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700582 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100583 }
584 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700585 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100586 }
Steve Dowered51b262016-09-17 12:54:06 -0700587 while (*++p) {
588 if (*p == '\r' || *p == '\n') {
589 *p = '\0';
590 break;
591 }
592 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700593
Steve Dowered51b262016-09-17 12:54:06 -0700594 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200595 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700596 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200597 }
598 else if (strncmp(line, "import ", 7) == 0) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200599 status = _PyStatus_ERR("only 'import site' is supported "
600 "in ._pth file");
601 goto done;
Steve Dowered51b262016-09-17 12:54:06 -0700602 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700603
Steve Dowered51b262016-09-17 12:54:06 -0700604 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700605 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700606 if (wline == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200607 status = _PyStatus_NO_MEMORY();
608 goto done;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700609 }
Steve Dowered51b262016-09-17 12:54:06 -0700610 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700611 wline[wn] = '\0';
612
Steve Dowerc6dd4152016-10-27 14:28:07 -0700613 size_t usedsiz = wcslen(buf);
614 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700615 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700616 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
617 sizeof(wchar_t));
618 if (tmp == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200619 status = _PyStatus_NO_MEMORY();
620 goto done;
Steve Dower4db86bc2016-09-09 09:17:35 -0700621 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700622 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700623 }
624
Steve Dowerc6dd4152016-10-27 14:28:07 -0700625 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700626 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700627 usedsiz += 1;
628 }
Steve Dowered51b262016-09-17 12:54:06 -0700629
Steve Dowerc6dd4152016-10-27 14:28:07 -0700630 errno_t result;
631 _Py_BEGIN_SUPPRESS_IPH
632 result = wcscat_s(buf, bufsiz, prefix);
633 _Py_END_SUPPRESS_IPH
Victor Stinner85ce0a72019-09-24 00:55:48 +0200634
Steve Dowerc6dd4152016-10-27 14:28:07 -0700635 if (result == EINVAL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200636 status = _PyStatus_ERR("invalid argument during ._pth processing");
637 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700638 } else if (result == ERANGE) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200639 status = _PyStatus_ERR("buffer overflow during ._pth processing");
640 goto done;
Steve Dowerc6dd4152016-10-27 14:28:07 -0700641 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200642
Steve Dowerc6dd4152016-10-27 14:28:07 -0700643 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700644 join(b, wline);
645
646 PyMem_RawFree(wline);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200647 wline = NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700648 }
649
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200650 if (pathconfig->module_search_path == NULL) {
651 pathconfig->module_search_path = _PyMem_RawWcsdup(buf);
652 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200653 status = _PyStatus_NO_MEMORY();
654 goto done;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200655 }
656 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700657
Victor Stinner85ce0a72019-09-24 00:55:48 +0200658 *found = 1;
659 status = _PyStatus_OK();
660 goto done;
661
662done:
Steve Dower4db86bc2016-09-09 09:17:35 -0700663 PyMem_RawFree(buf);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200664 PyMem_RawFree(wline);
Steve Dower4db86bc2016-09-09 09:17:35 -0700665 fclose(sp_file);
Victor Stinner85ce0a72019-09-24 00:55:48 +0200666 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +0100667}
668
669
670static int
Victor Stinnerc4221672019-09-21 01:02:56 +0200671get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
672 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100673{
Victor Stinnerc4221672019-09-21 01:02:56 +0200674 if (calculate->dll_path[0]) {
675 if (!change_ext(filename, calculate->dll_path, L"._pth") &&
676 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100677 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100678 return 1;
679 }
680 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200681 if (pathconfig->program_full_path[0]) {
Victor Stinnerc4221672019-09-21 01:02:56 +0200682 if (!change_ext(filename, pathconfig->program_full_path, L"._pth") &&
683 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100684 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100685 return 1;
686 }
687 }
688 return 0;
689}
690
691
Victor Stinner85ce0a72019-09-24 00:55:48 +0200692static PyStatus
Victor Stinnerc4221672019-09-21 01:02:56 +0200693calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200694 wchar_t *prefix, int *found)
Victor Stinner0327bde2017-11-23 17:03:20 +0100695{
Victor Stinnerc4221672019-09-21 01:02:56 +0200696 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100697
Victor Stinnerc4221672019-09-21 01:02:56 +0200698 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200699 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100700 }
701
Victor Stinner85ce0a72019-09-24 00:55:48 +0200702 return read_pth_file(pathconfig, prefix, filename, found);
Victor Stinner0327bde2017-11-23 17:03:20 +0100703}
704
705
706/* Search for an environment configuration file, first in the
707 executable's directory and then in the parent directory.
708 If found, open it for use when searching for prefixes.
709*/
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200710static PyStatus
Victor Stinner85ce0a72019-09-24 00:55:48 +0200711calculate_pyvenv_file(PyCalculatePath *calculate,
712 wchar_t *argv0_path, size_t argv0_path_len)
Victor Stinner0327bde2017-11-23 17:03:20 +0100713{
Victor Stinner221fd842019-09-25 02:54:25 +0200714 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100715 const wchar_t *env_cfg = L"pyvenv.cfg";
716
Victor Stinner221fd842019-09-25 02:54:25 +0200717 /* Filename: <argv0_path_len> / "pyvenv.cfg" */
718 wcscpy_s(filename, MAXPATHLEN+1, argv0_path);
719 join(filename, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100720
Victor Stinner221fd842019-09-25 02:54:25 +0200721 FILE *env_file = _Py_wfopen(filename, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100722 if (env_file == NULL) {
723 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100724
Victor Stinner221fd842019-09-25 02:54:25 +0200725 /* Filename: <basename(basename(argv0_path_len))> / "pyvenv.cfg" */
726 reduce(filename);
727 reduce(filename);
728 join(filename, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100729
Victor Stinner221fd842019-09-25 02:54:25 +0200730 env_file = _Py_wfopen(filename, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100731 if (env_file == NULL) {
732 errno = 0;
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200733 return _PyStatus_OK();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100734 }
735 }
736
Victor Stinner0327bde2017-11-23 17:03:20 +0100737 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200738 wchar_t *home = NULL;
739 PyStatus status = _Py_FindEnvConfigValue(env_file, L"home", &home);
740 if (_PyStatus_EXCEPTION(status)) {
741 fclose(env_file);
742 return status;
743 }
744 if (home) {
Victor Stinner221fd842019-09-25 02:54:25 +0200745 wcscpy_s(argv0_path, argv0_path_len, home);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200746 PyMem_RawFree(home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100747 }
748 fclose(env_file);
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200749 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100750}
751
752
753static void
Victor Stinner85ce0a72019-09-24 00:55:48 +0200754calculate_home_prefix(PyCalculatePath *calculate,
755 const wchar_t *argv0_path,
756 const wchar_t *zip_path,
757 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100758{
Victor Stinner0327bde2017-11-23 17:03:20 +0100759 if (calculate->home == NULL || *calculate->home == '\0') {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200760 if (zip_path[0] && exists(zip_path)) {
761 wcscpy_s(prefix, MAXPATHLEN+1, zip_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100762 reduce(prefix);
763 calculate->home = prefix;
764 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200765 else if (search_for_prefix(prefix, argv0_path, LANDMARK)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100766 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100767 }
768 else {
769 calculate->home = NULL;
770 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100772 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100773 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100774 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100775}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000776
Victor Stinner9316ee42017-11-25 03:17:57 +0100777
Victor Stinner331a6a52019-05-27 16:39:22 +0200778static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200779calculate_module_search_path(PyCalculatePath *calculate,
780 _PyPathConfig *pathconfig,
Victor Stinner85ce0a72019-09-24 00:55:48 +0200781 const wchar_t *argv0_path,
782 wchar_t *prefix,
783 const wchar_t *zip_path)
Victor Stinner9316ee42017-11-25 03:17:57 +0100784{
Victor Stinner0327bde2017-11-23 17:03:20 +0100785 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000786#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100787 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
788 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000789#endif
luzpaza5293b42017-11-05 07:37:50 -0600790 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 anything better to use! */
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200792 int skipdefault = (calculate->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100793 calculate->home != NULL ||
794 calculate->machine_path != NULL ||
795 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* We need to construct a path from the following parts.
798 (1) the PYTHONPATH environment variable, if set;
799 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100800 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100802 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 (5) the directory containing the executable (argv0_path).
804 The length calculation calculates #4 first.
805 Extra rules:
806 - If PYTHONHOME is set (in any way) item (3) is ignored.
807 - If registry values are used, (4) and (5) are ignored.
808 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100811 size_t bufsz = 0;
812 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200813 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 bufsz = 1;
815 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100816 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100820 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700822 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner85ce0a72019-09-24 00:55:48 +0200823 bufsz += wcslen(argv0_path) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100824 if (calculate->user_path) {
825 bufsz += wcslen(calculate->user_path) + 1;
826 }
827 if (calculate->machine_path) {
828 bufsz += wcslen(calculate->machine_path) + 1;
829 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200830 bufsz += wcslen(zip_path) + 1;
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200831 if (calculate->pythonpath_env != NULL) {
832 bufsz += wcslen(calculate->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000834
Victor Stinner0327bde2017-11-23 17:03:20 +0100835 wchar_t *buf, *start_buf;
836 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (buf == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +0200838 return _PyStatus_NO_MEMORY();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100840 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000841
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200842 if (calculate->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100843 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200844 calculate->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100845 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 buf = wcschr(buf, L'\0');
848 *buf++ = DELIM;
849 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200850 if (zip_path[0]) {
851 if (wcscpy_s(buf, bufsz - (buf - start_buf), zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100852 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 buf = wcschr(buf, L'\0');
855 *buf++ = DELIM;
856 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 if (calculate->user_path) {
858 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100859 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 buf = wcschr(buf, L'\0');
862 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100864 if (calculate->machine_path) {
865 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100866 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 buf = wcschr(buf, L'\0');
869 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100871 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100873 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100874 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700877 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700879 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200880 const wchar_t *p = PYTHONPATH;
881 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 size_t n;
883 for (;;) {
884 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100887 }
888 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100893 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 buf = wcschr(buf, L'\0');
896 p++;
897 n--;
898 }
899 wcsncpy(buf, p, n);
900 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700901 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 p = q+1;
906 }
907 }
Victor Stinner85ce0a72019-09-24 00:55:48 +0200908 if (argv0_path) {
909 wcscpy(buf, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700911 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700913 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 /* Now to pull one last hack/trick. If sys.prefix is
916 empty, then try and find it somewhere on the paths
917 we calculated. We scan backwards, as our general policy
918 is that Python core directories are at the *end* of
919 sys.path. We assume that our "lib" directory is
920 on the path, and that our 'prefix' directory is
921 the parent of that.
922 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100923 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200925 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 while (1) {
927 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200928 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 /* 'look' will end up one character before the
930 start of the path in question - even if this
931 is one character before the start of the buffer
932 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100933 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 look--;
935 nchars = lookEnd-look;
936 wcsncpy(lookBuf, look+1, nchars);
937 lookBuf[nchars] = L'\0';
938 /* Up one level to the parent */
939 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100940 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 break;
942 }
943 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100944 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 look--;
948 }
949 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100950
Victor Stinner331a6a52019-05-27 16:39:22 +0200951 pathconfig->module_search_path = start_buf;
952 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100953}
954
955
Victor Stinner331a6a52019-05-27 16:39:22 +0200956static PyStatus
Victor Stinner9c42f8c2019-09-23 18:47:29 +0200957calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100958{
Victor Stinner331a6a52019-05-27 16:39:22 +0200959 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100960
Victor Stinnerfcdb0272019-09-23 14:45:47 +0200961 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200962 if (_PyStatus_EXCEPTION(status)) {
963 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100964 }
965
Victor Stinnerb64de462017-12-01 18:27:09 +0100966 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200967 wchar_t argv0_path[MAXPATHLEN+1];
968 memset(argv0_path, 0, sizeof(argv0_path));
969
970 wcscpy_s(argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
971 reduce(argv0_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100972
973 wchar_t prefix[MAXPATHLEN+1];
974 memset(prefix, 0, sizeof(prefix));
975
976 /* Search for a sys.path file */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200977 int pth_found = 0;
978 status = calculate_pth_file(calculate, pathconfig, prefix, &pth_found);
979 if (_PyStatus_EXCEPTION(status)) {
980 return status;
981 }
982 if (pth_found) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100983 goto done;
984 }
985
Victor Stinnerc02b41b2019-10-04 19:53:43 +0200986 status = calculate_pyvenv_file(calculate,
987 argv0_path, Py_ARRAY_LENGTH(argv0_path));
988 if (_PyStatus_EXCEPTION(status)) {
989 return status;
990 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100991
992 /* Calculate zip archive path from DLL or exe path */
Victor Stinner85ce0a72019-09-24 00:55:48 +0200993 wchar_t zip_path[MAXPATHLEN+1];
994 memset(zip_path, 0, sizeof(zip_path));
995
996 change_ext(zip_path,
Victor Stinnerc4221672019-09-21 01:02:56 +0200997 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +0100998 L".zip");
999
Victor Stinner85ce0a72019-09-24 00:55:48 +02001000 calculate_home_prefix(calculate, argv0_path, zip_path, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001001
Victor Stinnere2677932019-09-21 01:50:16 +02001002 if (pathconfig->module_search_path == NULL) {
Victor Stinner85ce0a72019-09-24 00:55:48 +02001003 status = calculate_module_search_path(calculate, pathconfig,
1004 argv0_path, prefix, zip_path);
Victor Stinnere2677932019-09-21 01:50:16 +02001005 if (_PyStatus_EXCEPTION(status)) {
1006 return status;
1007 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001008 }
1009
1010done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001011 if (pathconfig->prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001012 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1013 if (pathconfig->prefix == NULL) {
1014 return _PyStatus_NO_MEMORY();
1015 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001016 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001017 if (pathconfig->exec_prefix == NULL) {
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001018 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1019 if (pathconfig->exec_prefix == NULL) {
1020 return _PyStatus_NO_MEMORY();
1021 }
Steve Dower177a41a2018-11-17 20:41:48 -08001022 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001023
Victor Stinner331a6a52019-05-27 16:39:22 +02001024 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001025}
1026
1027
Victor Stinner85ce0a72019-09-24 00:55:48 +02001028static PyStatus
1029calculate_init(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
1030 const PyConfig *config)
1031{
1032 calculate->home = pathconfig->home;
1033 calculate->path_env = _wgetenv(L"PATH");
1034
1035 calculate->dll_path = _Py_GetDLLPath();
1036 if (calculate->dll_path == NULL) {
1037 return _PyStatus_NO_MEMORY();
1038 }
1039
1040 calculate->pythonpath_env = config->pythonpath_env;
1041
1042 return _PyStatus_OK();
1043}
1044
1045
Victor Stinner0327bde2017-11-23 17:03:20 +01001046static void
1047calculate_free(PyCalculatePath *calculate)
1048{
1049 PyMem_RawFree(calculate->machine_path);
1050 PyMem_RawFree(calculate->user_path);
Victor Stinnerc4221672019-09-21 01:02:56 +02001051 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001052}
1053
Victor Stinner9316ee42017-11-25 03:17:57 +01001054
Victor Stinner85ce0a72019-09-24 00:55:48 +02001055/* Calculate the Python path configuration.
1056
1057 Inputs:
1058
1059 - PyConfig.pythonpath_env: PYTHONPATH environment variable
1060 - _PyPathConfig.home: Py_SetPythonHome() or PYTHONHOME environment variable
1061 - DLL path: _Py_GetDLLPath()
1062 - PATH environment variable
1063 - __PYVENV_LAUNCHER__ environment variable
1064 - GetModuleFileNameW(NULL): fully qualified path of the executable file of
1065 the current process
Victor Stinner8bf39b62019-09-26 02:22:35 +02001066 - ._pth configuration file
Victor Stinner85ce0a72019-09-24 00:55:48 +02001067 - pyvenv.cfg configuration file
1068 - Registry key "Software\Python\PythonCore\X.Y\PythonPath"
Victor Stinner8bf39b62019-09-26 02:22:35 +02001069 of HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE where X.Y is the Python
1070 version.
Victor Stinner85ce0a72019-09-24 00:55:48 +02001071
1072 Outputs, 'pathconfig' fields:
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001073
1074 - base_executable
1075 - program_full_path
1076 - module_search_path
1077 - prefix
1078 - exec_prefix
1079 - isolated
1080 - site_import
1081
Victor Stinner85ce0a72019-09-24 00:55:48 +02001082 If a field is already set (non NULL), it is left unchanged. */
Victor Stinner331a6a52019-05-27 16:39:22 +02001083PyStatus
1084_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001085{
Victor Stinnerc4221672019-09-21 01:02:56 +02001086 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001087 PyCalculatePath calculate;
1088 memset(&calculate, 0, sizeof(calculate));
1089
Victor Stinner85ce0a72019-09-24 00:55:48 +02001090 status = calculate_init(&calculate, pathconfig, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001091 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001092 goto done;
1093 }
1094
Victor Stinner9c42f8c2019-09-23 18:47:29 +02001095 status = calculate_path(&calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001096
Victor Stinner9316ee42017-11-25 03:17:57 +01001097done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001098 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001099 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001100}
1101
1102
Victor Stinner63941882011-09-29 00:42:28 +02001103/* Load python3.dll before loading any extension module that might refer
1104 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001105 to this python DLL is loaded, not a python3.dll that might be on the path
1106 by chance.
1107 Return whether the DLL was found.
1108*/
1109static int python3_checked = 0;
1110static HANDLE hPython3;
1111int
Victor Stinner31a83932017-12-04 13:39:15 +01001112_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001113{
1114 wchar_t py3path[MAXPATHLEN+1];
1115 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001116 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001117 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001118 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001119 python3_checked = 1;
1120
1121 /* If there is a python3.dll next to the python3y.dll,
1122 assume this is a build tree; use that DLL */
Victor Stinnerc4221672019-09-21 01:02:56 +02001123 if (_Py_dll_path != NULL) {
1124 wcscpy(py3path, _Py_dll_path);
1125 }
1126 else {
1127 wcscpy(py3path, L"");
1128 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001129 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001130 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001131 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001132 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001133 wcscpy(s, L"\\python3.dll");
1134 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001135 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001136 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001137 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001138
1139 /* Check sys.prefix\DLLs\python3.dll */
1140 wcscpy(py3path, Py_GetPrefix());
1141 wcscat(py3path, L"\\DLLs\\python3.dll");
1142 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1143 return hPython3 != NULL;
1144}