blob: 64aa1e0d141f0556ddd4a01699645a69ce2d882b [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 Stinner871ff772019-05-17 23:54:00 +020083#include "pycore_coreconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010084#include "pycore_pystate.h"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000086#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087
Steve Dower4db86bc2016-09-09 09:17:35 -070088#ifndef MS_WINDOWS
89#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000090#endif
91
Steve Dower4db86bc2016-09-09 09:17:35 -070092#include <windows.h>
erikjanss6cf82552018-07-25 02:41:46 +020093#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070094
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000096#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097#endif /* HAVE_SYS_TYPES_H */
98
99#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101#endif /* HAVE_SYS_STAT_H */
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103#include <string.h>
104
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105/* Search in some common locations for the associated Python libraries.
106 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107 * Py_GetPath() tries to return a sensible Python module search path.
108 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000109 * The approach is an adaptation for Windows of the strategy used in
110 * ../Modules/getpath.c; it uses the Windows Registry as one of its
111 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000112 *
113 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
114 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115 */
116
117#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000118#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119#endif
120
Victor Stinner0327bde2017-11-23 17:03:20 +0100121typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200122 const wchar_t *path_env; /* PATH environment variable */
123 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100124
125 /* Registry key "Software\Python\PythonCore\PythonPath" */
126 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
127 wchar_t *user_path; /* from HKEY_CURRENT_USER */
128
Victor Stinner0327bde2017-11-23 17:03:20 +0100129 wchar_t argv0_path[MAXPATHLEN+1];
130 wchar_t zip_path[MAXPATHLEN+1];
131} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000132
Guido van Rossumeea14491997-08-13 21:30:44 +0000133
Victor Stinner0327bde2017-11-23 17:03:20 +0100134/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000135static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100136is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000137{
138#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142#endif
143}
144
Victor Stinner0327bde2017-11-23 17:03:20 +0100145
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000146/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100147 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000148static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000149reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700151 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100152 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700153 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100154 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 while (i > 0 && !is_sep(dir[i]))
157 --i;
158 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000159}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160
Victor Stinner0327bde2017-11-23 17:03:20 +0100161
Steve Dowered51b262016-09-17 12:54:06 -0700162static int
163change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
164{
165 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
166 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100167 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700168 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100169 }
Steve Dowered51b262016-09-17 12:54:06 -0700170
171 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
172 --i;
173
174 if (i == 0) {
175 dest[0] = '\0';
176 return -1;
177 }
178
Victor Stinner0327bde2017-11-23 17:03:20 +0100179 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700180 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100181 }
Steve Dowered51b262016-09-17 12:54:06 -0700182
183 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100184 wcscat_s(dest, MAXPATHLEN+1, ext))
185 {
Steve Dowered51b262016-09-17 12:54:06 -0700186 dest[0] = '\0';
187 return -1;
188 }
189
190 return 0;
191}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000192
Victor Stinner0327bde2017-11-23 17:03:20 +0100193
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000194static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200195exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000198}
199
Victor Stinner0327bde2017-11-23 17:03:20 +0100200
201/* Is module -- check for .pyc too.
202 Assumes 'filename' MAXPATHLEN+1 bytes long -
203 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000204static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100205ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000206{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100207 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700208
Victor Stinner0327bde2017-11-23 17:03:20 +0100209 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100211 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700214 n = wcsnlen_s(filename, MAXPATHLEN+1);
215 if (n < MAXPATHLEN) {
216 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800217 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700218 filename[n + 1] = L'\0';
219 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100220 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700221 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100222 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700223 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 }
225 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000226}
227
Victor Stinner0327bde2017-11-23 17:03:20 +0100228
Tim Peters8484fbf2004-08-07 19:12:27 +0000229/* Add a path component, by appending stuff to buffer.
230 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
231 NUL-terminated string with no more than MAXPATHLEN characters (not counting
232 the trailing NUL). It's a fatal error if it contains a string longer than
233 that (callers must be careful!). If these requirements are met, it's
234 guaranteed that buffer will still be a NUL-terminated string with no more
235 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
236 stuff as fits will be appended.
237*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700238
239static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100240typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
241 PCWSTR pszPathIn, PCWSTR pszMore,
242 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700243static PPathCchCombineEx _PathCchCombineEx;
244
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000245static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700246join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000247{
Steve Dower4db86bc2016-09-09 09:17:35 -0700248 if (_PathCchCombineEx_Initialized == 0) {
249 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100250 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700251 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100252 }
253 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700254 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100255 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700256 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700258
Steve Dower4db86bc2016-09-09 09:17:35 -0700259 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100260 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100262 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700263 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100264 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700265 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100266 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700267 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000268}
269
Steve Dower48e8c822018-02-22 10:39:26 -0800270static int _PathCchCanonicalizeEx_Initialized = 0;
271typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
272 PCWSTR pszPathIn, unsigned long dwFlags);
273static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
274
275static _PyInitError canonicalize(wchar_t *buffer, const wchar_t *path)
276{
277 if (buffer == NULL) {
278 return _Py_INIT_NO_MEMORY();
279 }
280
281 if (_PathCchCanonicalizeEx_Initialized == 0) {
282 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
283 if (pathapi) {
284 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
285 }
286 else {
287 _PathCchCanonicalizeEx = NULL;
288 }
289 _PathCchCanonicalizeEx_Initialized = 1;
290 }
291
292 if (_PathCchCanonicalizeEx) {
293 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
294 return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()");
295 }
296 }
297 else {
298 if (!PathCanonicalizeW(buffer, path)) {
299 return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()");
300 }
301 }
302 return _Py_INIT_OK();
303}
304
Victor Stinner0327bde2017-11-23 17:03:20 +0100305
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000306/* gotlandmark only called by search_for_prefix, which ensures
307 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100308 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000309static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100310gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700313 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700316 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 prefix[n] = '\0';
318 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000319}
320
Victor Stinner0327bde2017-11-23 17:03:20 +0100321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinnerb64de462017-12-01 18:27:09 +0100323 assumption provided by only caller, calculate_path_impl() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000324static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200325search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700328 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100330 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 reduce(prefix);
334 } while (prefix[0]);
335 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000336}
337
Victor Stinner0327bde2017-11-23 17:03:20 +0100338
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000339#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000340
Guido van Rossum88716bb2000-03-30 19:45:39 +0000341/* a string loaded from the DLL at startup.*/
342extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000343
Guido van Rossumeea14491997-08-13 21:30:44 +0000344/* Load a PYTHONPATH value from the registry.
345 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
346
Guido van Rossum88716bb2000-03-30 19:45:39 +0000347 Works in both Unicode and 8bit environments. Only uses the
348 Ex family of functions so it also works with Windows CE.
349
Guido van Rossumeea14491997-08-13 21:30:44 +0000350 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000351
352 XXX - this code is pretty strange, as it used to also
353 work on Win16, where the buffer sizes werent available
354 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000355*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000356static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000357getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 HKEY newKey = 0;
360 DWORD dataSize = 0;
361 DWORD numKeys = 0;
362 LONG rc;
363 wchar_t *retval = NULL;
364 WCHAR *dataBuf = NULL;
365 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
366 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700367 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 DWORD index;
369 WCHAR *keyBuf = NULL;
370 WCHAR *keyBufPtr;
371 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* Tried to use sysget("winver") but here is too early :-( */
374 versionLen = strlen(PyWin_DLLVersionString);
375 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700376 keyBufLen = sizeof(keyPrefix) +
377 sizeof(WCHAR)*(versionLen-1) +
378 sizeof(keySuffix);
379 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100380 if (keyBuf==NULL) {
381 goto done;
382 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000383
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700384 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200385 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
387 keyBufPtr += versionLen;
388 /* NULL comes with this one! */
389 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
390 /* Open the root Python key */
391 rc=RegOpenKeyExW(keyBase,
392 keyBuf, /* subkey */
393 0, /* reserved */
394 KEY_READ,
395 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100396 if (rc!=ERROR_SUCCESS) {
397 goto done;
398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* Find out how big our core buffer is, and how many subkeys we have */
400 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
401 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100402 if (rc!=ERROR_SUCCESS) {
403 goto done;
404 }
405 if (skipcore) {
406 dataSize = 0; /* Only count core ones if we want them! */
407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 /* Allocate a temp array of char buffers, so we only need to loop
409 reading the registry once
410 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200411 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100412 if (ppPaths==NULL) {
413 goto done;
414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
416 /* Loop over all subkeys, allocating a temp sub-buffer. */
417 for(index=0;index<numKeys;index++) {
418 WCHAR keyBuf[MAX_PATH+1];
419 HKEY subKey = 0;
420 DWORD reqdSize = MAX_PATH+1;
421 /* Get the sub-key name */
422 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
423 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100424 if (rc!=ERROR_SUCCESS) {
425 goto done;
426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* Open the sub-key */
428 rc=RegOpenKeyExW(newKey,
429 keyBuf, /* subkey */
430 0, /* reserved */
431 KEY_READ,
432 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100433 if (rc!=ERROR_SUCCESS) {
434 goto done;
435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 /* Find the value of the buffer size, malloc, then read it */
437 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
438 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200439 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (ppPaths[index]) {
441 RegQueryValueExW(subKey, NULL, 0, NULL,
442 (LPBYTE)ppPaths[index],
443 &reqdSize);
444 dataSize += reqdSize + 1; /* 1 for the ";" */
445 }
446 }
447 RegCloseKey(subKey);
448 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100451 if (dataSize == 0) {
452 goto done;
453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454
455 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200456 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (dataBuf) {
458 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* Copy our collected strings */
460 for (index=0;index<numKeys;index++) {
461 if (index > 0) {
462 *(szCur++) = L';';
463 dataSize--;
464 }
465 if (ppPaths[index]) {
466 Py_ssize_t len = wcslen(ppPaths[index]);
467 wcsncpy(szCur, ppPaths[index], len);
468 szCur += len;
469 assert(dataSize > (DWORD)len);
470 dataSize -= (DWORD)len;
471 }
472 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100473 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 else {
luzpaza5293b42017-11-05 07:37:50 -0600477 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (numKeys) {
479 *(szCur++) = L';';
480 dataSize--;
481 }
482 /* Now append the core path entries -
483 this will include the NULL
484 */
485 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
486 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200487 if (rc != ERROR_SUCCESS) {
488 PyMem_RawFree(dataBuf);
489 goto done;
490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 }
492 /* And set the result - caller must free */
493 retval = dataBuf;
494 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000495done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Loop freeing my temp buffers */
497 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200498 for(index=0; index<numKeys; index++)
499 PyMem_RawFree(ppPaths[index]);
500 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100502 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100504 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200505 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000507}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000508#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000509
Victor Stinner0327bde2017-11-23 17:03:20 +0100510
Victor Stinner9316ee42017-11-25 03:17:57 +0100511static _PyInitError
Victor Stinnerb64de462017-12-01 18:27:09 +0100512get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000513{
Victor Stinner9316ee42017-11-25 03:17:57 +0100514 wchar_t dll_path[MAXPATHLEN+1];
515 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000516
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000517#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100519 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100520 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
521 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100522 }
523 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000524#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100525 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000526#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100527
528 config->dll_path = _PyMem_RawWcsdup(dll_path);
529 if (config->dll_path == NULL) {
530 return _Py_INIT_NO_MEMORY();
531 }
532 return _Py_INIT_OK();
533}
534
535
536static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100537get_program_full_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100538 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +0100539{
Steve Dower1c3de542018-12-10 08:11:21 -0800540 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100541 wchar_t program_full_path[MAXPATHLEN+1];
542 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100543
Steve Dower1c3de542018-12-10 08:11:21 -0800544 /* The launcher may need to force the executable path to a
545 * different environment, so override it here. */
546 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
547 if (pyvenv_launcher && pyvenv_launcher[0]) {
548 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
549 } else if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
Steve Dower48e8c822018-02-22 10:39:26 -0800550 /* GetModuleFileName should never fail when passed NULL */
551 return _Py_INIT_ERR("Cannot determine program path");
Victor Stinner0327bde2017-11-23 17:03:20 +0100552 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000553
Steve Dower48e8c822018-02-22 10:39:26 -0800554 config->program_full_path = PyMem_RawMalloc(
555 sizeof(wchar_t) * (MAXPATHLEN + 1));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000556
Steve Dower48e8c822018-02-22 10:39:26 -0800557 return canonicalize(config->program_full_path,
558 program_full_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000559}
560
Victor Stinner0327bde2017-11-23 17:03:20 +0100561
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100562static int
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200563read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
Steve Dower4db86bc2016-09-09 09:17:35 -0700564{
565 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100566 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100567 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100568 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700569
Steve Dowered51b262016-09-17 12:54:06 -0700570 wcscpy_s(prefix, MAXPATHLEN+1, path);
571 reduce(prefix);
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200572 config->isolated = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200573 config->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700574
Steve Dower4db86bc2016-09-09 09:17:35 -0700575 size_t bufsiz = MAXPATHLEN;
576 size_t prefixlen = wcslen(prefix);
577
578 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700579 if (buf == NULL) {
580 goto error;
581 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700582 buf[0] = '\0';
583
584 while (!feof(sp_file)) {
585 char line[MAXPATHLEN + 1];
586 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100587 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700588 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100589 }
590 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700591 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100592 }
Steve Dowered51b262016-09-17 12:54:06 -0700593 while (*++p) {
594 if (*p == '\r' || *p == '\n') {
595 *p = '\0';
596 break;
597 }
598 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700599
Steve Dowered51b262016-09-17 12:54:06 -0700600 if (strcmp(line, "import site") == 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200601 config->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700602 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200603 }
604 else if (strncmp(line, "import ", 7) == 0) {
Steve Dowered51b262016-09-17 12:54:06 -0700605 Py_FatalError("only 'import site' is supported in ._pth file");
606 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700607
Steve Dowered51b262016-09-17 12:54:06 -0700608 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700609 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700610 if (wline == NULL) {
611 goto error;
612 }
Steve Dowered51b262016-09-17 12:54:06 -0700613 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700614 wline[wn] = '\0';
615
Steve Dowerc6dd4152016-10-27 14:28:07 -0700616 size_t usedsiz = wcslen(buf);
617 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700618 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700619 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
620 sizeof(wchar_t));
621 if (tmp == NULL) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700622 PyMem_RawFree(wline);
623 goto error;
624 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700625 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700626 }
627
Steve Dowerc6dd4152016-10-27 14:28:07 -0700628 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700629 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700630 usedsiz += 1;
631 }
Steve Dowered51b262016-09-17 12:54:06 -0700632
Steve Dowerc6dd4152016-10-27 14:28:07 -0700633 errno_t result;
634 _Py_BEGIN_SUPPRESS_IPH
635 result = wcscat_s(buf, bufsiz, prefix);
636 _Py_END_SUPPRESS_IPH
637 if (result == EINVAL) {
638 Py_FatalError("invalid argument during ._pth processing");
639 } else if (result == ERANGE) {
640 Py_FatalError("buffer overflow during ._pth processing");
641 }
642 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700643 join(b, wline);
644
645 PyMem_RawFree(wline);
646 }
647
Steve Dower4db86bc2016-09-09 09:17:35 -0700648 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100649 config->module_search_path = buf;
650 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700651
652error:
653 PyMem_RawFree(buf);
654 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100655 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700656}
657
658
Victor Stinner46972b72017-11-24 22:55:40 +0100659static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100660calculate_init(PyCalculatePath *calculate,
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100661 const _PyCoreConfig *core_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000662{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100663 calculate->home = core_config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100664 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100665}
666
667
668static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100669get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100670{
Victor Stinner9316ee42017-11-25 03:17:57 +0100671 if (config->dll_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100672 if (!change_ext(spbuffer, config->dll_path, L"._pth") &&
673 exists(spbuffer))
674 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100675 return 1;
676 }
677 }
Victor Stinnerb64de462017-12-01 18:27:09 +0100678 if (config->program_full_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100679 if (!change_ext(spbuffer, config->program_full_path, L"._pth") &&
680 exists(spbuffer))
681 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100682 return 1;
683 }
684 }
685 return 0;
686}
687
688
689static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100690calculate_pth_file(_PyPathConfig *config, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100691{
692 wchar_t spbuffer[MAXPATHLEN+1];
693
694 if (!get_pth_filename(spbuffer, config)) {
695 return 0;
696 }
697
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200698 return read_pth_file(config, prefix, spbuffer);
Victor Stinner0327bde2017-11-23 17:03:20 +0100699}
700
701
702/* Search for an environment configuration file, first in the
703 executable's directory and then in the parent directory.
704 If found, open it for use when searching for prefixes.
705*/
706static void
707calculate_pyvenv_file(PyCalculatePath *calculate)
708{
709 wchar_t envbuffer[MAXPATHLEN+1];
710 const wchar_t *env_cfg = L"pyvenv.cfg";
711
712 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
713 join(envbuffer, env_cfg);
714
715 FILE *env_file = _Py_wfopen(envbuffer, L"r");
716 if (env_file == NULL) {
717 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100718
Victor Stinner0327bde2017-11-23 17:03:20 +0100719 reduce(envbuffer);
720 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700721 join(envbuffer, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100722
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700723 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100724 if (env_file == NULL) {
725 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100726 }
727 }
728
Victor Stinner0327bde2017-11-23 17:03:20 +0100729 if (env_file == NULL) {
730 return;
731 }
732
733 /* Look for a 'home' variable and set argv0_path to it, if found */
734 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100735 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100736 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
737 }
738 fclose(env_file);
739}
740
741
Victor Stinner9316ee42017-11-25 03:17:57 +0100742#define INIT_ERR_BUFFER_OVERFLOW() _Py_INIT_ERR("buffer overflow")
743
744
Victor Stinner0327bde2017-11-23 17:03:20 +0100745static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100746calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100747{
Victor Stinner0327bde2017-11-23 17:03:20 +0100748 if (calculate->home == NULL || *calculate->home == '\0') {
749 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100750 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
751 reduce(prefix);
752 calculate->home = prefix;
753 }
754 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
755 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100756 }
757 else {
758 calculate->home = NULL;
759 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100761 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100762 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100763 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100764}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000765
Victor Stinner9316ee42017-11-25 03:17:57 +0100766
767static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100768calculate_module_search_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100769 PyCalculatePath *calculate, _PyPathConfig *config,
770 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100771{
Victor Stinner0327bde2017-11-23 17:03:20 +0100772 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000773#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100774 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
775 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000776#endif
luzpaza5293b42017-11-05 07:37:50 -0600777 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 anything better to use! */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100779 int skipdefault = (core_config->module_search_path_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100780 calculate->home != NULL ||
781 calculate->machine_path != NULL ||
782 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* We need to construct a path from the following parts.
785 (1) the PYTHONPATH environment variable, if set;
786 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100787 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100789 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 (5) the directory containing the executable (argv0_path).
791 The length calculation calculates #4 first.
792 Extra rules:
793 - If PYTHONHOME is set (in any way) item (3) is ignored.
794 - If registry values are used, (4) and (5) are ignored.
795 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100798 size_t bufsz = 0;
799 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200800 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 bufsz = 1;
802 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100803 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100805 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100807 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700809 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100810 bufsz += wcslen(calculate->argv0_path) + 1;
811 if (calculate->user_path) {
812 bufsz += wcslen(calculate->user_path) + 1;
813 }
814 if (calculate->machine_path) {
815 bufsz += wcslen(calculate->machine_path) + 1;
816 }
817 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100818 if (core_config->module_search_path_env != NULL) {
819 bufsz += wcslen(core_config->module_search_path_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100820 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000821
Victor Stinner0327bde2017-11-23 17:03:20 +0100822 wchar_t *buf, *start_buf;
823 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 if (buf == NULL) {
825 /* We can't exit, so print a warning and limp along */
826 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100827 if (core_config->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100829 config->module_search_path = core_config->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 }
831 else {
832 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100835 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100837 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000838
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100839 if (core_config->module_search_path_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100840 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100841 core_config->module_search_path_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100842 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100843 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 buf = wcschr(buf, L'\0');
845 *buf++ = DELIM;
846 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100847 if (calculate->zip_path[0]) {
848 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100849 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100850 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 buf = wcschr(buf, L'\0');
852 *buf++ = DELIM;
853 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100854 if (calculate->user_path) {
855 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100856 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 buf = wcschr(buf, L'\0');
859 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100861 if (calculate->machine_path) {
862 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100863 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 buf = wcschr(buf, L'\0');
866 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100868 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100870 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100871 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100872 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700874 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700876 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200877 const wchar_t *p = PYTHONPATH;
878 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 size_t n;
880 for (;;) {
881 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100882 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 }
885 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100889 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100890 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 buf = wcschr(buf, L'\0');
893 p++;
894 n--;
895 }
896 wcsncpy(buf, p, n);
897 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700898 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100899 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 p = q+1;
903 }
904 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 if (calculate->argv0_path) {
906 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700908 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700910 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 /* Now to pull one last hack/trick. If sys.prefix is
913 empty, then try and find it somewhere on the paths
914 we calculated. We scan backwards, as our general policy
915 is that Python core directories are at the *end* of
916 sys.path. We assume that our "lib" directory is
917 on the path, and that our 'prefix' directory is
918 the parent of that.
919 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100920 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200922 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 while (1) {
924 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200925 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 /* 'look' will end up one character before the
927 start of the path in question - even if this
928 is one character before the start of the buffer
929 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100930 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 look--;
932 nchars = lookEnd-look;
933 wcsncpy(lookBuf, look+1, nchars);
934 lookBuf[nchars] = L'\0';
935 /* Up one level to the parent */
936 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100937 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 break;
939 }
940 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100941 if (look < start_buf) {
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 look--;
945 }
946 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100947
948 config->module_search_path = start_buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100949 return _Py_INIT_OK();
950}
951
952
953static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100954calculate_path_impl(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100955 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +0100956{
957 _PyInitError err;
958
959 err = get_dll_path(calculate, config);
960 if (_Py_INIT_FAILED(err)) {
961 return err;
962 }
963
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100964 err = get_program_full_path(core_config, calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +0100965 if (_Py_INIT_FAILED(err)) {
966 return err;
967 }
968
Victor Stinnerb64de462017-12-01 18:27:09 +0100969 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
970 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100971 reduce(calculate->argv0_path);
972
973 wchar_t prefix[MAXPATHLEN+1];
974 memset(prefix, 0, sizeof(prefix));
975
976 /* Search for a sys.path file */
977 if (calculate_pth_file(config, prefix)) {
978 goto done;
979 }
980
981 calculate_pyvenv_file(calculate);
982
983 /* Calculate zip archive path from DLL or exe path */
984 change_ext(calculate->zip_path,
Victor Stinnerb64de462017-12-01 18:27:09 +0100985 config->dll_path[0] ? config->dll_path : config->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +0100986 L".zip");
987
988 calculate_home_prefix(calculate, prefix);
989
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100990 err = calculate_module_search_path(core_config, calculate, config, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +0100991 if (_Py_INIT_FAILED(err)) {
992 return err;
993 }
994
995done:
996 config->prefix = _PyMem_RawWcsdup(prefix);
997 if (config->prefix == NULL) {
998 return _Py_INIT_NO_MEMORY();
999 }
Steve Dower177a41a2018-11-17 20:41:48 -08001000 config->exec_prefix = _PyMem_RawWcsdup(prefix);
1001 if (config->exec_prefix == NULL) {
1002 return _Py_INIT_NO_MEMORY();
1003 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001004
1005 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001006}
1007
1008
Victor Stinner0327bde2017-11-23 17:03:20 +01001009static void
1010calculate_free(PyCalculatePath *calculate)
1011{
1012 PyMem_RawFree(calculate->machine_path);
1013 PyMem_RawFree(calculate->user_path);
1014}
1015
Victor Stinner9316ee42017-11-25 03:17:57 +01001016
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001017_PyInitError
Victor Stinnerb1147e42018-07-21 02:06:16 +02001018_PyPathConfig_Calculate_impl(_PyPathConfig *config, const _PyCoreConfig *core_config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001019{
Victor Stinner0327bde2017-11-23 17:03:20 +01001020 PyCalculatePath calculate;
1021 memset(&calculate, 0, sizeof(calculate));
1022
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001023 calculate_init(&calculate, core_config);
Victor Stinner46972b72017-11-24 22:55:40 +01001024
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001025 _PyInitError err = calculate_path_impl(core_config, &calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +01001026 if (_Py_INIT_FAILED(err)) {
1027 goto done;
1028 }
1029
Victor Stinner9316ee42017-11-25 03:17:57 +01001030 err = _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001031
Victor Stinner9316ee42017-11-25 03:17:57 +01001032done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001033 calculate_free(&calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +01001034 return err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001035}
1036
1037
Victor Stinner63941882011-09-29 00:42:28 +02001038/* Load python3.dll before loading any extension module that might refer
1039 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001040 to this python DLL is loaded, not a python3.dll that might be on the path
1041 by chance.
1042 Return whether the DLL was found.
1043*/
1044static int python3_checked = 0;
1045static HANDLE hPython3;
1046int
Victor Stinner31a83932017-12-04 13:39:15 +01001047_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001048{
1049 wchar_t py3path[MAXPATHLEN+1];
1050 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001051 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001052 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001053 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001054 python3_checked = 1;
1055
1056 /* If there is a python3.dll next to the python3y.dll,
1057 assume this is a build tree; use that DLL */
Victor Stinnerb64de462017-12-01 18:27:09 +01001058 wcscpy(py3path, _Py_path_config.dll_path);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001059 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001060 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001061 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001062 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001063 wcscpy(s, L"\\python3.dll");
1064 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001065 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001066 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001067 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001068
1069 /* Check sys.prefix\DLLs\python3.dll */
1070 wcscpy(py3path, Py_GetPrefix());
1071 wcscat(py3path, L"\\DLLs\\python3.dll");
1072 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1073 return hPython3 != NULL;
1074}