blob: 35c31bf29bdfd7fd358a815e9635b0f9692134db [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 Stinner621cebe2018-11-12 16:53:38 +010083#include "pycore_pystate.h"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000084#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000085#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000086
Steve Dower4db86bc2016-09-09 09:17:35 -070087#ifndef MS_WINDOWS
88#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000089#endif
90
Steve Dower4db86bc2016-09-09 09:17:35 -070091#include <windows.h>
erikjanss6cf82552018-07-25 02:41:46 +020092#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070093
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000095#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000096#endif /* HAVE_SYS_TYPES_H */
97
98#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000099#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000100#endif /* HAVE_SYS_STAT_H */
101
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000102#include <string.h>
103
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000104/* Search in some common locations for the associated Python libraries.
105 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106 * Py_GetPath() tries to return a sensible Python module search path.
107 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000108 * The approach is an adaptation for Windows of the strategy used in
109 * ../Modules/getpath.c; it uses the Windows Registry as one of its
110 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000111 *
112 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
113 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000114 */
115
116#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000117#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000118#endif
119
Victor Stinner0327bde2017-11-23 17:03:20 +0100120typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200121 const wchar_t *path_env; /* PATH environment variable */
122 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100123
124 /* Registry key "Software\Python\PythonCore\PythonPath" */
125 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
126 wchar_t *user_path; /* from HKEY_CURRENT_USER */
127
Victor Stinner0327bde2017-11-23 17:03:20 +0100128 wchar_t argv0_path[MAXPATHLEN+1];
129 wchar_t zip_path[MAXPATHLEN+1];
130} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000131
Guido van Rossumeea14491997-08-13 21:30:44 +0000132
Victor Stinner0327bde2017-11-23 17:03:20 +0100133/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000134static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100135is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000136{
137#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000141#endif
142}
143
Victor Stinner0327bde2017-11-23 17:03:20 +0100144
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000145/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100146 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000148reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000149{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700150 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100151 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700152 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100153 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 while (i > 0 && !is_sep(dir[i]))
156 --i;
157 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159
Victor Stinner0327bde2017-11-23 17:03:20 +0100160
Steve Dowered51b262016-09-17 12:54:06 -0700161static int
162change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
163{
164 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
165 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100166 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700167 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100168 }
Steve Dowered51b262016-09-17 12:54:06 -0700169
170 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
171 --i;
172
173 if (i == 0) {
174 dest[0] = '\0';
175 return -1;
176 }
177
Victor Stinner0327bde2017-11-23 17:03:20 +0100178 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700179 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100180 }
Steve Dowered51b262016-09-17 12:54:06 -0700181
182 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100183 wcscat_s(dest, MAXPATHLEN+1, ext))
184 {
Steve Dowered51b262016-09-17 12:54:06 -0700185 dest[0] = '\0';
186 return -1;
187 }
188
189 return 0;
190}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000191
Victor Stinner0327bde2017-11-23 17:03:20 +0100192
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000193static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200194exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000197}
198
Victor Stinner0327bde2017-11-23 17:03:20 +0100199
200/* Is module -- check for .pyc too.
201 Assumes 'filename' MAXPATHLEN+1 bytes long -
202 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000203static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100204ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000205{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100206 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700207
Victor Stinner0327bde2017-11-23 17:03:20 +0100208 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100210 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700213 n = wcsnlen_s(filename, MAXPATHLEN+1);
214 if (n < MAXPATHLEN) {
215 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800216 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700217 filename[n + 1] = L'\0';
218 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100219 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700220 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100221 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700222 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 }
224 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000225}
226
Victor Stinner0327bde2017-11-23 17:03:20 +0100227
Tim Peters8484fbf2004-08-07 19:12:27 +0000228/* Add a path component, by appending stuff to buffer.
229 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
230 NUL-terminated string with no more than MAXPATHLEN characters (not counting
231 the trailing NUL). It's a fatal error if it contains a string longer than
232 that (callers must be careful!). If these requirements are met, it's
233 guaranteed that buffer will still be a NUL-terminated string with no more
234 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
235 stuff as fits will be appended.
236*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700237
238static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100239typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
240 PCWSTR pszPathIn, PCWSTR pszMore,
241 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700242static PPathCchCombineEx _PathCchCombineEx;
243
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000244static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700245join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000246{
Steve Dower4db86bc2016-09-09 09:17:35 -0700247 if (_PathCchCombineEx_Initialized == 0) {
248 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100249 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700250 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100251 }
252 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700253 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100254 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700255 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700257
Steve Dower4db86bc2016-09-09 09:17:35 -0700258 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100259 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700260 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100261 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700262 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100263 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700264 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100265 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700266 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000267}
268
Steve Dower48e8c822018-02-22 10:39:26 -0800269static int _PathCchCanonicalizeEx_Initialized = 0;
270typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
271 PCWSTR pszPathIn, unsigned long dwFlags);
272static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
273
274static _PyInitError canonicalize(wchar_t *buffer, const wchar_t *path)
275{
276 if (buffer == NULL) {
277 return _Py_INIT_NO_MEMORY();
278 }
279
280 if (_PathCchCanonicalizeEx_Initialized == 0) {
281 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
282 if (pathapi) {
283 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
284 }
285 else {
286 _PathCchCanonicalizeEx = NULL;
287 }
288 _PathCchCanonicalizeEx_Initialized = 1;
289 }
290
291 if (_PathCchCanonicalizeEx) {
292 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
293 return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()");
294 }
295 }
296 else {
297 if (!PathCanonicalizeW(buffer, path)) {
298 return _Py_INIT_ERR("buffer overflow in getpathp.c's canonicalize()");
299 }
300 }
301 return _Py_INIT_OK();
302}
303
Victor Stinner0327bde2017-11-23 17:03:20 +0100304
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000305/* gotlandmark only called by search_for_prefix, which ensures
306 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100307 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000308static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100309gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700312 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700315 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 prefix[n] = '\0';
317 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000318}
319
Victor Stinner0327bde2017-11-23 17:03:20 +0100320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinnerb64de462017-12-01 18:27:09 +0100322 assumption provided by only caller, calculate_path_impl() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000323static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200324search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700327 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100329 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100331 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 reduce(prefix);
333 } while (prefix[0]);
334 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000335}
336
Victor Stinner0327bde2017-11-23 17:03:20 +0100337
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000338#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000339
Guido van Rossum88716bb2000-03-30 19:45:39 +0000340/* a string loaded from the DLL at startup.*/
341extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000342
Guido van Rossumeea14491997-08-13 21:30:44 +0000343/* Load a PYTHONPATH value from the registry.
344 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
345
Guido van Rossum88716bb2000-03-30 19:45:39 +0000346 Works in both Unicode and 8bit environments. Only uses the
347 Ex family of functions so it also works with Windows CE.
348
Guido van Rossumeea14491997-08-13 21:30:44 +0000349 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000350
351 XXX - this code is pretty strange, as it used to also
352 work on Win16, where the buffer sizes werent available
353 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000354*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000355static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000356getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 HKEY newKey = 0;
359 DWORD dataSize = 0;
360 DWORD numKeys = 0;
361 LONG rc;
362 wchar_t *retval = NULL;
363 WCHAR *dataBuf = NULL;
364 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
365 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700366 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 DWORD index;
368 WCHAR *keyBuf = NULL;
369 WCHAR *keyBufPtr;
370 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* Tried to use sysget("winver") but here is too early :-( */
373 versionLen = strlen(PyWin_DLLVersionString);
374 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700375 keyBufLen = sizeof(keyPrefix) +
376 sizeof(WCHAR)*(versionLen-1) +
377 sizeof(keySuffix);
378 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100379 if (keyBuf==NULL) {
380 goto done;
381 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700383 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200384 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
386 keyBufPtr += versionLen;
387 /* NULL comes with this one! */
388 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
389 /* Open the root Python key */
390 rc=RegOpenKeyExW(keyBase,
391 keyBuf, /* subkey */
392 0, /* reserved */
393 KEY_READ,
394 &newKey);
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 out how big our core buffer is, and how many subkeys we have */
399 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
400 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100401 if (rc!=ERROR_SUCCESS) {
402 goto done;
403 }
404 if (skipcore) {
405 dataSize = 0; /* Only count core ones if we want them! */
406 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 /* Allocate a temp array of char buffers, so we only need to loop
408 reading the registry once
409 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200410 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100411 if (ppPaths==NULL) {
412 goto done;
413 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
415 /* Loop over all subkeys, allocating a temp sub-buffer. */
416 for(index=0;index<numKeys;index++) {
417 WCHAR keyBuf[MAX_PATH+1];
418 HKEY subKey = 0;
419 DWORD reqdSize = MAX_PATH+1;
420 /* Get the sub-key name */
421 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
422 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100423 if (rc!=ERROR_SUCCESS) {
424 goto done;
425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 /* Open the sub-key */
427 rc=RegOpenKeyExW(newKey,
428 keyBuf, /* subkey */
429 0, /* reserved */
430 KEY_READ,
431 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100432 if (rc!=ERROR_SUCCESS) {
433 goto done;
434 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 /* Find the value of the buffer size, malloc, then read it */
436 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
437 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200438 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (ppPaths[index]) {
440 RegQueryValueExW(subKey, NULL, 0, NULL,
441 (LPBYTE)ppPaths[index],
442 &reqdSize);
443 dataSize += reqdSize + 1; /* 1 for the ";" */
444 }
445 }
446 RegCloseKey(subKey);
447 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100450 if (dataSize == 0) {
451 goto done;
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453
454 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200455 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 if (dataBuf) {
457 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 /* Copy our collected strings */
459 for (index=0;index<numKeys;index++) {
460 if (index > 0) {
461 *(szCur++) = L';';
462 dataSize--;
463 }
464 if (ppPaths[index]) {
465 Py_ssize_t len = wcslen(ppPaths[index]);
466 wcsncpy(szCur, ppPaths[index], len);
467 szCur += len;
468 assert(dataSize > (DWORD)len);
469 dataSize -= (DWORD)len;
470 }
471 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100472 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100474 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 else {
luzpaza5293b42017-11-05 07:37:50 -0600476 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (numKeys) {
478 *(szCur++) = L';';
479 dataSize--;
480 }
481 /* Now append the core path entries -
482 this will include the NULL
483 */
484 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
485 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200486 if (rc != ERROR_SUCCESS) {
487 PyMem_RawFree(dataBuf);
488 goto done;
489 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
491 /* And set the result - caller must free */
492 retval = dataBuf;
493 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000494done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* Loop freeing my temp buffers */
496 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200497 for(index=0; index<numKeys; index++)
498 PyMem_RawFree(ppPaths[index]);
499 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100501 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100503 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200504 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000506}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000507#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000508
Victor Stinner0327bde2017-11-23 17:03:20 +0100509
Victor Stinner9316ee42017-11-25 03:17:57 +0100510static _PyInitError
Victor Stinnerb64de462017-12-01 18:27:09 +0100511get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000512{
Victor Stinner9316ee42017-11-25 03:17:57 +0100513 wchar_t dll_path[MAXPATHLEN+1];
514 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000515
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000516#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100518 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100519 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
520 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100521 }
522 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000523#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100524 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000525#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100526
527 config->dll_path = _PyMem_RawWcsdup(dll_path);
528 if (config->dll_path == NULL) {
529 return _Py_INIT_NO_MEMORY();
530 }
531 return _Py_INIT_OK();
532}
533
534
535static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100536get_program_full_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100537 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +0100538{
Victor Stinnerb64de462017-12-01 18:27:09 +0100539 wchar_t program_full_path[MAXPATHLEN+1];
540 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100541
Victor Stinnercb0b78a2018-12-07 12:57:43 +0100542 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
Steve Dower48e8c822018-02-22 10:39:26 -0800543 /* GetModuleFileName should never fail when passed NULL */
544 return _Py_INIT_ERR("Cannot determine program path");
Victor Stinner0327bde2017-11-23 17:03:20 +0100545 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000546
Steve Dower48e8c822018-02-22 10:39:26 -0800547 config->program_full_path = PyMem_RawMalloc(
548 sizeof(wchar_t) * (MAXPATHLEN + 1));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000549
Steve Dower48e8c822018-02-22 10:39:26 -0800550 return canonicalize(config->program_full_path,
551 program_full_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000552}
553
Victor Stinner0327bde2017-11-23 17:03:20 +0100554
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100555static int
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200556read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path)
Steve Dower4db86bc2016-09-09 09:17:35 -0700557{
558 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100559 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100560 return 0;
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 Stinnerf2626ce2018-07-21 03:54:20 +0200565 config->isolated = 1;
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200566 config->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
571 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700572 if (buf == NULL) {
573 goto error;
574 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700575 buf[0] = '\0';
576
577 while (!feof(sp_file)) {
578 char line[MAXPATHLEN + 1];
579 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100580 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700581 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100582 }
583 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700584 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100585 }
Steve Dowered51b262016-09-17 12:54:06 -0700586 while (*++p) {
587 if (*p == '\r' || *p == '\n') {
588 *p = '\0';
589 break;
590 }
591 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700592
Steve Dowered51b262016-09-17 12:54:06 -0700593 if (strcmp(line, "import site") == 0) {
Victor Stinnerd19d8d52018-07-24 13:55:48 +0200594 config->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700595 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200596 }
597 else if (strncmp(line, "import ", 7) == 0) {
Steve Dowered51b262016-09-17 12:54:06 -0700598 Py_FatalError("only 'import site' is supported in ._pth file");
599 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700600
Steve Dowered51b262016-09-17 12:54:06 -0700601 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700602 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700603 if (wline == NULL) {
604 goto error;
605 }
Steve Dowered51b262016-09-17 12:54:06 -0700606 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700607 wline[wn] = '\0';
608
Steve Dowerc6dd4152016-10-27 14:28:07 -0700609 size_t usedsiz = wcslen(buf);
610 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700611 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700612 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
613 sizeof(wchar_t));
614 if (tmp == NULL) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700615 PyMem_RawFree(wline);
616 goto error;
617 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700618 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700619 }
620
Steve Dowerc6dd4152016-10-27 14:28:07 -0700621 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700622 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700623 usedsiz += 1;
624 }
Steve Dowered51b262016-09-17 12:54:06 -0700625
Steve Dowerc6dd4152016-10-27 14:28:07 -0700626 errno_t result;
627 _Py_BEGIN_SUPPRESS_IPH
628 result = wcscat_s(buf, bufsiz, prefix);
629 _Py_END_SUPPRESS_IPH
630 if (result == EINVAL) {
631 Py_FatalError("invalid argument during ._pth processing");
632 } else if (result == ERANGE) {
633 Py_FatalError("buffer overflow during ._pth processing");
634 }
635 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700636 join(b, wline);
637
638 PyMem_RawFree(wline);
639 }
640
Steve Dower4db86bc2016-09-09 09:17:35 -0700641 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100642 config->module_search_path = buf;
643 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700644
645error:
646 PyMem_RawFree(buf);
647 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100648 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700649}
650
651
Victor Stinner46972b72017-11-24 22:55:40 +0100652static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100653calculate_init(PyCalculatePath *calculate,
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100654 const _PyCoreConfig *core_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000655{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100656 calculate->home = core_config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100657 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100658}
659
660
661static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100662get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100663{
Victor Stinner9316ee42017-11-25 03:17:57 +0100664 if (config->dll_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100665 if (!change_ext(spbuffer, config->dll_path, L"._pth") &&
666 exists(spbuffer))
667 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100668 return 1;
669 }
670 }
Victor Stinnerb64de462017-12-01 18:27:09 +0100671 if (config->program_full_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100672 if (!change_ext(spbuffer, config->program_full_path, L"._pth") &&
673 exists(spbuffer))
674 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100675 return 1;
676 }
677 }
678 return 0;
679}
680
681
682static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100683calculate_pth_file(_PyPathConfig *config, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100684{
685 wchar_t spbuffer[MAXPATHLEN+1];
686
687 if (!get_pth_filename(spbuffer, config)) {
688 return 0;
689 }
690
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200691 return read_pth_file(config, prefix, spbuffer);
Victor Stinner0327bde2017-11-23 17:03:20 +0100692}
693
694
695/* Search for an environment configuration file, first in the
696 executable's directory and then in the parent directory.
697 If found, open it for use when searching for prefixes.
698*/
699static void
700calculate_pyvenv_file(PyCalculatePath *calculate)
701{
702 wchar_t envbuffer[MAXPATHLEN+1];
703 const wchar_t *env_cfg = L"pyvenv.cfg";
704
705 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
706 join(envbuffer, env_cfg);
707
708 FILE *env_file = _Py_wfopen(envbuffer, L"r");
709 if (env_file == NULL) {
710 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100711
Victor Stinner0327bde2017-11-23 17:03:20 +0100712 reduce(envbuffer);
713 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700714 join(envbuffer, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100715
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700716 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100717 if (env_file == NULL) {
718 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100719 }
720 }
721
Victor Stinner0327bde2017-11-23 17:03:20 +0100722 if (env_file == NULL) {
723 return;
724 }
725
726 /* Look for a 'home' variable and set argv0_path to it, if found */
727 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100728 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100729 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
730 }
731 fclose(env_file);
732}
733
734
Victor Stinner9316ee42017-11-25 03:17:57 +0100735#define INIT_ERR_BUFFER_OVERFLOW() _Py_INIT_ERR("buffer overflow")
736
737
Victor Stinner0327bde2017-11-23 17:03:20 +0100738static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100739calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100740{
Victor Stinner0327bde2017-11-23 17:03:20 +0100741 if (calculate->home == NULL || *calculate->home == '\0') {
742 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100743 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
744 reduce(prefix);
745 calculate->home = prefix;
746 }
747 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
748 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100749 }
750 else {
751 calculate->home = NULL;
752 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100754 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100755 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100756 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100757}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000758
Victor Stinner9316ee42017-11-25 03:17:57 +0100759
760static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100761calculate_module_search_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100762 PyCalculatePath *calculate, _PyPathConfig *config,
763 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100764{
Victor Stinner0327bde2017-11-23 17:03:20 +0100765 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000766#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100767 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
768 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000769#endif
luzpaza5293b42017-11-05 07:37:50 -0600770 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 anything better to use! */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100772 int skipdefault = (core_config->module_search_path_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100773 calculate->home != NULL ||
774 calculate->machine_path != NULL ||
775 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* We need to construct a path from the following parts.
778 (1) the PYTHONPATH environment variable, if set;
779 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100780 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100782 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 (5) the directory containing the executable (argv0_path).
784 The length calculation calculates #4 first.
785 Extra rules:
786 - If PYTHONHOME is set (in any way) item (3) is ignored.
787 - If registry values are used, (4) and (5) are ignored.
788 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100791 size_t bufsz = 0;
792 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200793 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 bufsz = 1;
795 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100796 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100798 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100800 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700802 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100803 bufsz += wcslen(calculate->argv0_path) + 1;
804 if (calculate->user_path) {
805 bufsz += wcslen(calculate->user_path) + 1;
806 }
807 if (calculate->machine_path) {
808 bufsz += wcslen(calculate->machine_path) + 1;
809 }
810 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100811 if (core_config->module_search_path_env != NULL) {
812 bufsz += wcslen(core_config->module_search_path_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100813 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000814
Victor Stinner0327bde2017-11-23 17:03:20 +0100815 wchar_t *buf, *start_buf;
816 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (buf == NULL) {
818 /* We can't exit, so print a warning and limp along */
819 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100820 if (core_config->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100822 config->module_search_path = core_config->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 }
824 else {
825 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100826 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100828 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100830 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000831
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100832 if (core_config->module_search_path_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100833 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100834 core_config->module_search_path_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100835 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100836 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 buf = wcschr(buf, L'\0');
838 *buf++ = DELIM;
839 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100840 if (calculate->zip_path[0]) {
841 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
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->user_path) {
848 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100854 if (calculate->machine_path) {
855 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_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->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100864 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700867 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700869 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200870 const wchar_t *p = PYTHONPATH;
871 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 size_t n;
873 for (;;) {
874 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100875 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100877 }
878 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100880 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100882 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100883 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 buf = wcschr(buf, L'\0');
886 p++;
887 n--;
888 }
889 wcsncpy(buf, p, n);
890 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700891 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 p = q+1;
896 }
897 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100898 if (calculate->argv0_path) {
899 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700901 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700903 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 /* Now to pull one last hack/trick. If sys.prefix is
906 empty, then try and find it somewhere on the paths
907 we calculated. We scan backwards, as our general policy
908 is that Python core directories are at the *end* of
909 sys.path. We assume that our "lib" directory is
910 on the path, and that our 'prefix' directory is
911 the parent of that.
912 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100913 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200915 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 while (1) {
917 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200918 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 /* 'look' will end up one character before the
920 start of the path in question - even if this
921 is one character before the start of the buffer
922 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100923 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 look--;
925 nchars = lookEnd-look;
926 wcsncpy(lookBuf, look+1, nchars);
927 lookBuf[nchars] = L'\0';
928 /* Up one level to the parent */
929 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100930 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 break;
932 }
933 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100934 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 look--;
938 }
939 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100940
941 config->module_search_path = start_buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100942 return _Py_INIT_OK();
943}
944
945
946static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100947calculate_path_impl(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100948 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +0100949{
950 _PyInitError err;
951
952 err = get_dll_path(calculate, config);
953 if (_Py_INIT_FAILED(err)) {
954 return err;
955 }
956
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100957 err = get_program_full_path(core_config, calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +0100958 if (_Py_INIT_FAILED(err)) {
959 return err;
960 }
961
Victor Stinnerb64de462017-12-01 18:27:09 +0100962 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
963 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100964 reduce(calculate->argv0_path);
965
966 wchar_t prefix[MAXPATHLEN+1];
967 memset(prefix, 0, sizeof(prefix));
968
969 /* Search for a sys.path file */
970 if (calculate_pth_file(config, prefix)) {
971 goto done;
972 }
973
974 calculate_pyvenv_file(calculate);
975
976 /* Calculate zip archive path from DLL or exe path */
977 change_ext(calculate->zip_path,
Victor Stinnerb64de462017-12-01 18:27:09 +0100978 config->dll_path[0] ? config->dll_path : config->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +0100979 L".zip");
980
981 calculate_home_prefix(calculate, prefix);
982
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100983 err = calculate_module_search_path(core_config, calculate, config, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +0100984 if (_Py_INIT_FAILED(err)) {
985 return err;
986 }
987
988done:
989 config->prefix = _PyMem_RawWcsdup(prefix);
990 if (config->prefix == NULL) {
991 return _Py_INIT_NO_MEMORY();
992 }
Steve Dower177a41a2018-11-17 20:41:48 -0800993 config->exec_prefix = _PyMem_RawWcsdup(prefix);
994 if (config->exec_prefix == NULL) {
995 return _Py_INIT_NO_MEMORY();
996 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100997
998 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000999}
1000
1001
Victor Stinner0327bde2017-11-23 17:03:20 +01001002static void
1003calculate_free(PyCalculatePath *calculate)
1004{
1005 PyMem_RawFree(calculate->machine_path);
1006 PyMem_RawFree(calculate->user_path);
1007}
1008
Victor Stinner9316ee42017-11-25 03:17:57 +01001009
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001010_PyInitError
Victor Stinnerb1147e42018-07-21 02:06:16 +02001011_PyPathConfig_Calculate_impl(_PyPathConfig *config, const _PyCoreConfig *core_config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001012{
Victor Stinner0327bde2017-11-23 17:03:20 +01001013 PyCalculatePath calculate;
1014 memset(&calculate, 0, sizeof(calculate));
1015
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001016 calculate_init(&calculate, core_config);
Victor Stinner46972b72017-11-24 22:55:40 +01001017
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001018 _PyInitError err = calculate_path_impl(core_config, &calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +01001019 if (_Py_INIT_FAILED(err)) {
1020 goto done;
1021 }
1022
Victor Stinner9316ee42017-11-25 03:17:57 +01001023 err = _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001024
Victor Stinner9316ee42017-11-25 03:17:57 +01001025done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001026 calculate_free(&calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +01001027 return err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001028}
1029
1030
Victor Stinner63941882011-09-29 00:42:28 +02001031/* Load python3.dll before loading any extension module that might refer
1032 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001033 to this python DLL is loaded, not a python3.dll that might be on the path
1034 by chance.
1035 Return whether the DLL was found.
1036*/
1037static int python3_checked = 0;
1038static HANDLE hPython3;
1039int
Victor Stinner31a83932017-12-04 13:39:15 +01001040_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001041{
1042 wchar_t py3path[MAXPATHLEN+1];
1043 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001044 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001045 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001046 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001047 python3_checked = 1;
1048
1049 /* If there is a python3.dll next to the python3y.dll,
1050 assume this is a build tree; use that DLL */
Victor Stinnerb64de462017-12-01 18:27:09 +01001051 wcscpy(py3path, _Py_path_config.dll_path);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001052 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001053 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001054 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001055 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001056 wcscpy(s, L"\\python3.dll");
1057 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001058 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001059 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001060 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001061
1062 /* Check sys.prefix\DLLs\python3.dll */
1063 wcscpy(py3path, Py_GetPrefix());
1064 wcscat(py3path, L"\\DLLs\\python3.dll");
1065 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1066 return hPython3 != NULL;
1067}