blob: e142e3650522a6653ebd8167fa3f1aace7d28a9e [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 Stinnerb64de462017-12-01 18:27:09 +010083#include "internal/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>
92#include <Shlwapi.h>
93
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
Victor Stinner0327bde2017-11-23 17:03:20 +0100269
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000270/* gotlandmark only called by search_for_prefix, which ensures
271 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000273static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100274gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700277 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700280 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 prefix[n] = '\0';
282 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000283}
284
Victor Stinner0327bde2017-11-23 17:03:20 +0100285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinnerb64de462017-12-01 18:27:09 +0100287 assumption provided by only caller, calculate_path_impl() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000288static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200289search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700292 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100294 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 reduce(prefix);
298 } while (prefix[0]);
299 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000300}
301
Victor Stinner0327bde2017-11-23 17:03:20 +0100302
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000303#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000304
Guido van Rossum88716bb2000-03-30 19:45:39 +0000305/* a string loaded from the DLL at startup.*/
306extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000307
Guido van Rossumeea14491997-08-13 21:30:44 +0000308/* Load a PYTHONPATH value from the registry.
309 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
310
Guido van Rossum88716bb2000-03-30 19:45:39 +0000311 Works in both Unicode and 8bit environments. Only uses the
312 Ex family of functions so it also works with Windows CE.
313
Guido van Rossumeea14491997-08-13 21:30:44 +0000314 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000315
316 XXX - this code is pretty strange, as it used to also
317 work on Win16, where the buffer sizes werent available
318 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000319*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000320static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000321getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 HKEY newKey = 0;
324 DWORD dataSize = 0;
325 DWORD numKeys = 0;
326 LONG rc;
327 wchar_t *retval = NULL;
328 WCHAR *dataBuf = NULL;
329 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
330 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700331 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 DWORD index;
333 WCHAR *keyBuf = NULL;
334 WCHAR *keyBufPtr;
335 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* Tried to use sysget("winver") but here is too early :-( */
338 versionLen = strlen(PyWin_DLLVersionString);
339 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700340 keyBufLen = sizeof(keyPrefix) +
341 sizeof(WCHAR)*(versionLen-1) +
342 sizeof(keySuffix);
343 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100344 if (keyBuf==NULL) {
345 goto done;
346 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000347
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700348 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200349 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
351 keyBufPtr += versionLen;
352 /* NULL comes with this one! */
353 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
354 /* Open the root Python key */
355 rc=RegOpenKeyExW(keyBase,
356 keyBuf, /* subkey */
357 0, /* reserved */
358 KEY_READ,
359 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100360 if (rc!=ERROR_SUCCESS) {
361 goto done;
362 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 /* Find out how big our core buffer is, and how many subkeys we have */
364 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
365 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100366 if (rc!=ERROR_SUCCESS) {
367 goto done;
368 }
369 if (skipcore) {
370 dataSize = 0; /* Only count core ones if we want them! */
371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* Allocate a temp array of char buffers, so we only need to loop
373 reading the registry once
374 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200375 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100376 if (ppPaths==NULL) {
377 goto done;
378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
380 /* Loop over all subkeys, allocating a temp sub-buffer. */
381 for(index=0;index<numKeys;index++) {
382 WCHAR keyBuf[MAX_PATH+1];
383 HKEY subKey = 0;
384 DWORD reqdSize = MAX_PATH+1;
385 /* Get the sub-key name */
386 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
387 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100388 if (rc!=ERROR_SUCCESS) {
389 goto done;
390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 /* Open the sub-key */
392 rc=RegOpenKeyExW(newKey,
393 keyBuf, /* subkey */
394 0, /* reserved */
395 KEY_READ,
396 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100397 if (rc!=ERROR_SUCCESS) {
398 goto done;
399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 /* Find the value of the buffer size, malloc, then read it */
401 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
402 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200403 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (ppPaths[index]) {
405 RegQueryValueExW(subKey, NULL, 0, NULL,
406 (LPBYTE)ppPaths[index],
407 &reqdSize);
408 dataSize += reqdSize + 1; /* 1 for the ";" */
409 }
410 }
411 RegCloseKey(subKey);
412 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100415 if (dataSize == 0) {
416 goto done;
417 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418
419 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200420 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 if (dataBuf) {
422 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 /* Copy our collected strings */
424 for (index=0;index<numKeys;index++) {
425 if (index > 0) {
426 *(szCur++) = L';';
427 dataSize--;
428 }
429 if (ppPaths[index]) {
430 Py_ssize_t len = wcslen(ppPaths[index]);
431 wcsncpy(szCur, ppPaths[index], len);
432 szCur += len;
433 assert(dataSize > (DWORD)len);
434 dataSize -= (DWORD)len;
435 }
436 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100437 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 else {
luzpaza5293b42017-11-05 07:37:50 -0600441 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (numKeys) {
443 *(szCur++) = L';';
444 dataSize--;
445 }
446 /* Now append the core path entries -
447 this will include the NULL
448 */
449 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
450 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200451 if (rc != ERROR_SUCCESS) {
452 PyMem_RawFree(dataBuf);
453 goto done;
454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 }
456 /* And set the result - caller must free */
457 retval = dataBuf;
458 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000459done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 /* Loop freeing my temp buffers */
461 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200462 for(index=0; index<numKeys; index++)
463 PyMem_RawFree(ppPaths[index]);
464 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100466 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100468 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200469 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000471}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000472#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000473
Victor Stinner0327bde2017-11-23 17:03:20 +0100474
Victor Stinner9316ee42017-11-25 03:17:57 +0100475static _PyInitError
Victor Stinnerb64de462017-12-01 18:27:09 +0100476get_dll_path(PyCalculatePath *calculate, _PyPathConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000477{
Victor Stinner9316ee42017-11-25 03:17:57 +0100478 wchar_t dll_path[MAXPATHLEN+1];
479 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000480
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000481#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100483 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100484 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
485 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100486 }
487 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000488#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100489 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000490#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100491
492 config->dll_path = _PyMem_RawWcsdup(dll_path);
493 if (config->dll_path == NULL) {
494 return _Py_INIT_NO_MEMORY();
495 }
496 return _Py_INIT_OK();
497}
498
499
500static _PyInitError
Victor Stinner31a83932017-12-04 13:39:15 +0100501get_program_full_path(const _PyMainInterpreterConfig *main_config,
502 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +0100503{
Victor Stinnerb64de462017-12-01 18:27:09 +0100504 wchar_t program_full_path[MAXPATHLEN+1];
505 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100506
Victor Stinnerb64de462017-12-01 18:27:09 +0100507 if (GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100508 goto done;
Victor Stinner0327bde2017-11-23 17:03:20 +0100509 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* If there is no slash in the argv0 path, then we have to
512 * assume python is on the user's $PATH, since there's no
513 * other way to find a directory to start the search from. If
514 * $PATH isn't exported, you lose.
515 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000516#ifdef ALTSEP
Victor Stinner31a83932017-12-04 13:39:15 +0100517 if (wcschr(main_config->program_name, SEP) ||
518 wcschr(main_config->program_name, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000519#else
Victor Stinner31a83932017-12-04 13:39:15 +0100520 if (wcschr(main_config->program_name, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000521#endif
Victor Stinner0327bde2017-11-23 17:03:20 +0100522 {
Victor Stinner31a83932017-12-04 13:39:15 +0100523 wcsncpy(program_full_path, main_config->program_name, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100524 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100525 else if (calculate->path_env) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200526 const wchar_t *path = calculate->path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 while (1) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200528 const wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 if (delim) {
531 size_t len = delim - path;
532 /* ensure we can't overwrite buffer */
533 len = min(MAXPATHLEN,len);
Victor Stinnerb64de462017-12-01 18:27:09 +0100534 wcsncpy(program_full_path, path, len);
535 program_full_path[len] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100537 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100538 wcsncpy(program_full_path, path, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100539 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 /* join() is safe for MAXPATHLEN+1 size buffer */
Victor Stinner31a83932017-12-04 13:39:15 +0100542 join(program_full_path, main_config->program_name);
Victor Stinnerb64de462017-12-01 18:27:09 +0100543 if (exists(program_full_path)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100545 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (!delim) {
Victor Stinnerb64de462017-12-01 18:27:09 +0100548 program_full_path[0] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 break;
550 }
551 path = delim + 1;
552 }
553 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100554 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100555 program_full_path[0] = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100556 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100557
558done:
Victor Stinnerb64de462017-12-01 18:27:09 +0100559 config->program_full_path = _PyMem_RawWcsdup(program_full_path);
560 if (config->program_full_path == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100561 return _Py_INIT_NO_MEMORY();
562 }
563 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000564}
565
Victor Stinner0327bde2017-11-23 17:03:20 +0100566
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100567static int
568find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
569{
570 int result = 0; /* meaning not found */
571 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
572
573 fseek(env_file, 0, SEEK_SET);
574 while (!feof(env_file)) {
575 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
576 wchar_t tmpbuffer[MAXPATHLEN*2+1];
577 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200578 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100579
Victor Stinner0327bde2017-11-23 17:03:20 +0100580 if (p == NULL) {
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100581 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100582 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100583 n = strlen(p);
584 if (p[n - 1] != '\n') {
585 /* line has overflowed - bail */
586 break;
587 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100588 if (p[0] == '#') {
589 /* Comment - skip */
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100590 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100591 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100592 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
593 if (decoded != NULL) {
594 Py_ssize_t k;
595 k = PyUnicode_AsWideChar(decoded,
596 tmpbuffer, MAXPATHLEN * 2);
597 Py_DECREF(decoded);
598 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800599 wchar_t * context = NULL;
600 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100601 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800602 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100603 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800604 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100605 if (tok != NULL) {
606 wcsncpy(value, tok, MAXPATHLEN);
607 result = 1;
608 break;
609 }
610 }
611 }
612 }
613 }
614 }
615 return result;
616}
617
Victor Stinner0327bde2017-11-23 17:03:20 +0100618
Victor Stinner9316ee42017-11-25 03:17:57 +0100619static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100620read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
Victor Stinner9316ee42017-11-25 03:17:57 +0100621 int *isolated, int *nosite)
Steve Dower4db86bc2016-09-09 09:17:35 -0700622{
623 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100624 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100625 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100626 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700627
Steve Dowered51b262016-09-17 12:54:06 -0700628 wcscpy_s(prefix, MAXPATHLEN+1, path);
629 reduce(prefix);
630 *isolated = 1;
631 *nosite = 1;
632
Steve Dower4db86bc2016-09-09 09:17:35 -0700633 size_t bufsiz = MAXPATHLEN;
634 size_t prefixlen = wcslen(prefix);
635
636 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
637 buf[0] = '\0';
638
639 while (!feof(sp_file)) {
640 char line[MAXPATHLEN + 1];
641 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100642 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700643 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100644 }
645 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700646 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100647 }
Steve Dowered51b262016-09-17 12:54:06 -0700648 while (*++p) {
649 if (*p == '\r' || *p == '\n') {
650 *p = '\0';
651 break;
652 }
653 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700654
Steve Dowered51b262016-09-17 12:54:06 -0700655 if (strcmp(line, "import site") == 0) {
656 *nosite = 0;
657 continue;
658 } else if (strncmp(line, "import ", 7) == 0) {
659 Py_FatalError("only 'import site' is supported in ._pth file");
660 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700661
Steve Dowered51b262016-09-17 12:54:06 -0700662 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700663 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Steve Dowered51b262016-09-17 12:54:06 -0700664 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700665 wline[wn] = '\0';
666
Steve Dowerc6dd4152016-10-27 14:28:07 -0700667 size_t usedsiz = wcslen(buf);
668 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700669 bufsiz += MAXPATHLEN;
670 buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));
671 if (!buf) {
672 PyMem_RawFree(wline);
673 goto error;
674 }
675 }
676
Steve Dowerc6dd4152016-10-27 14:28:07 -0700677 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700678 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700679 usedsiz += 1;
680 }
Steve Dowered51b262016-09-17 12:54:06 -0700681
Steve Dowerc6dd4152016-10-27 14:28:07 -0700682 errno_t result;
683 _Py_BEGIN_SUPPRESS_IPH
684 result = wcscat_s(buf, bufsiz, prefix);
685 _Py_END_SUPPRESS_IPH
686 if (result == EINVAL) {
687 Py_FatalError("invalid argument during ._pth processing");
688 } else if (result == ERANGE) {
689 Py_FatalError("buffer overflow during ._pth processing");
690 }
691 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700692 join(b, wline);
693
694 PyMem_RawFree(wline);
695 }
696
Steve Dower4db86bc2016-09-09 09:17:35 -0700697 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100698 config->module_search_path = buf;
699 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700700
701error:
702 PyMem_RawFree(buf);
703 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100704 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700705}
706
707
Victor Stinner46972b72017-11-24 22:55:40 +0100708static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100709calculate_init(PyCalculatePath *calculate,
710 const _PyMainInterpreterConfig *main_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000711{
Victor Stinner46972b72017-11-24 22:55:40 +0100712 calculate->home = main_config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100713 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100714}
715
716
717static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100718get_pth_filename(wchar_t *spbuffer, _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100719{
Victor Stinner9316ee42017-11-25 03:17:57 +0100720 if (config->dll_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100721 if (!change_ext(spbuffer, config->dll_path, L"._pth") &&
722 exists(spbuffer))
723 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100724 return 1;
725 }
726 }
Victor Stinnerb64de462017-12-01 18:27:09 +0100727 if (config->program_full_path[0]) {
Victor Stinner31a83932017-12-04 13:39:15 +0100728 if (!change_ext(spbuffer, config->program_full_path, L"._pth") &&
729 exists(spbuffer))
730 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100731 return 1;
732 }
733 }
734 return 0;
735}
736
737
738static int
Victor Stinnerb64de462017-12-01 18:27:09 +0100739calculate_pth_file(_PyPathConfig *config, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100740{
741 wchar_t spbuffer[MAXPATHLEN+1];
742
743 if (!get_pth_filename(spbuffer, config)) {
744 return 0;
745 }
746
Victor Stinner9316ee42017-11-25 03:17:57 +0100747 return read_pth_file(config, prefix, spbuffer,
748 &Py_IsolatedFlag, &Py_NoSiteFlag);
Victor Stinner0327bde2017-11-23 17:03:20 +0100749}
750
751
752/* Search for an environment configuration file, first in the
753 executable's directory and then in the parent directory.
754 If found, open it for use when searching for prefixes.
755*/
756static void
757calculate_pyvenv_file(PyCalculatePath *calculate)
758{
759 wchar_t envbuffer[MAXPATHLEN+1];
760 const wchar_t *env_cfg = L"pyvenv.cfg";
761
762 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
763 join(envbuffer, env_cfg);
764
765 FILE *env_file = _Py_wfopen(envbuffer, L"r");
766 if (env_file == NULL) {
767 errno = 0;
768 reduce(envbuffer);
769 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700770 join(envbuffer, env_cfg);
771 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100772 if (env_file == NULL) {
773 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100774 }
775 }
776
Victor Stinner0327bde2017-11-23 17:03:20 +0100777 if (env_file == NULL) {
778 return;
779 }
780
781 /* Look for a 'home' variable and set argv0_path to it, if found */
782 wchar_t tmpbuffer[MAXPATHLEN+1];
783 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
784 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
785 }
786 fclose(env_file);
787}
788
789
Victor Stinner9316ee42017-11-25 03:17:57 +0100790#define INIT_ERR_BUFFER_OVERFLOW() _Py_INIT_ERR("buffer overflow")
791
792
Victor Stinner0327bde2017-11-23 17:03:20 +0100793static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100794calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100795{
Victor Stinner0327bde2017-11-23 17:03:20 +0100796 if (calculate->home == NULL || *calculate->home == '\0') {
797 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100798 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
799 reduce(prefix);
800 calculate->home = prefix;
801 }
802 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
803 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100804 }
805 else {
806 calculate->home = NULL;
807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100809 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100810 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100811 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100812}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000813
Victor Stinner9316ee42017-11-25 03:17:57 +0100814
815static _PyInitError
Victor Stinner31a83932017-12-04 13:39:15 +0100816calculate_module_search_path(const _PyMainInterpreterConfig *main_config,
817 PyCalculatePath *calculate, _PyPathConfig *config,
818 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100819{
Victor Stinner0327bde2017-11-23 17:03:20 +0100820 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000821#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100822 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
823 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000824#endif
luzpaza5293b42017-11-05 07:37:50 -0600825 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 anything better to use! */
Victor Stinner31a83932017-12-04 13:39:15 +0100827 int skipdefault = (main_config->module_search_path_env != NULL ||
828 calculate->home != NULL ||
829 calculate->machine_path != NULL ||
830 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* We need to construct a path from the following parts.
833 (1) the PYTHONPATH environment variable, if set;
834 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100835 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100837 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 (5) the directory containing the executable (argv0_path).
839 The length calculation calculates #4 first.
840 Extra rules:
841 - If PYTHONHOME is set (in any way) item (3) is ignored.
842 - If registry values are used, (4) and (5) are ignored.
843 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100846 size_t bufsz = 0;
847 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200848 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 bufsz = 1;
850 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100851 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100855 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700857 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100858 bufsz += wcslen(calculate->argv0_path) + 1;
859 if (calculate->user_path) {
860 bufsz += wcslen(calculate->user_path) + 1;
861 }
862 if (calculate->machine_path) {
863 bufsz += wcslen(calculate->machine_path) + 1;
864 }
865 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinner31a83932017-12-04 13:39:15 +0100866 if (main_config->module_search_path_env != NULL) {
867 bufsz += wcslen(main_config->module_search_path_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100868 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000869
Victor Stinner0327bde2017-11-23 17:03:20 +0100870 wchar_t *buf, *start_buf;
871 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (buf == NULL) {
873 /* We can't exit, so print a warning and limp along */
874 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner31a83932017-12-04 13:39:15 +0100875 if (main_config->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner31a83932017-12-04 13:39:15 +0100877 config->module_search_path = main_config->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
879 else {
880 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100881 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100883 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000886
Victor Stinner31a83932017-12-04 13:39:15 +0100887 if (main_config->module_search_path_env) {
888 if (wcscpy_s(buf, bufsz - (buf - start_buf),
889 main_config->module_search_path_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100890 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 buf = wcschr(buf, L'\0');
893 *buf++ = DELIM;
894 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100895 if (calculate->zip_path[0]) {
896 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100897 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100898 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 buf = wcschr(buf, L'\0');
900 *buf++ = DELIM;
901 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 if (calculate->user_path) {
903 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100904 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 buf = wcschr(buf, L'\0');
907 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100909 if (calculate->machine_path) {
910 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100911 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 buf = wcschr(buf, L'\0');
914 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100916 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100918 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100919 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700922 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700924 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200925 const wchar_t *p = PYTHONPATH;
926 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 size_t n;
928 for (;;) {
929 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100930 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100932 }
933 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100935 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100937 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100938 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100939 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 buf = wcschr(buf, L'\0');
941 p++;
942 n--;
943 }
944 wcsncpy(buf, p, n);
945 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700946 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100947 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100949 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 p = q+1;
951 }
952 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100953 if (calculate->argv0_path) {
954 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700956 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700958 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 /* Now to pull one last hack/trick. If sys.prefix is
961 empty, then try and find it somewhere on the paths
962 we calculated. We scan backwards, as our general policy
963 is that Python core directories are at the *end* of
964 sys.path. We assume that our "lib" directory is
965 on the path, and that our 'prefix' directory is
966 the parent of that.
967 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100968 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200970 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 while (1) {
972 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200973 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 /* 'look' will end up one character before the
975 start of the path in question - even if this
976 is one character before the start of the buffer
977 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100978 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 look--;
980 nchars = lookEnd-look;
981 wcsncpy(lookBuf, look+1, nchars);
982 lookBuf[nchars] = L'\0';
983 /* Up one level to the parent */
984 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100985 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 break;
987 }
988 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100989 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100991 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 look--;
993 }
994 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100995
996 config->module_search_path = start_buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100997 return _Py_INIT_OK();
998}
999
1000
1001static _PyInitError
Victor Stinner31a83932017-12-04 13:39:15 +01001002calculate_path_impl(const _PyMainInterpreterConfig *main_config,
1003 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner9316ee42017-11-25 03:17:57 +01001004{
1005 _PyInitError err;
1006
1007 err = get_dll_path(calculate, config);
1008 if (_Py_INIT_FAILED(err)) {
1009 return err;
1010 }
1011
Victor Stinner31a83932017-12-04 13:39:15 +01001012 err = get_program_full_path(main_config, calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +01001013 if (_Py_INIT_FAILED(err)) {
1014 return err;
1015 }
1016
Victor Stinnerb64de462017-12-01 18:27:09 +01001017 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
1018 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +01001019 reduce(calculate->argv0_path);
1020
1021 wchar_t prefix[MAXPATHLEN+1];
1022 memset(prefix, 0, sizeof(prefix));
1023
1024 /* Search for a sys.path file */
1025 if (calculate_pth_file(config, prefix)) {
1026 goto done;
1027 }
1028
1029 calculate_pyvenv_file(calculate);
1030
1031 /* Calculate zip archive path from DLL or exe path */
1032 change_ext(calculate->zip_path,
Victor Stinnerb64de462017-12-01 18:27:09 +01001033 config->dll_path[0] ? config->dll_path : config->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001034 L".zip");
1035
1036 calculate_home_prefix(calculate, prefix);
1037
Victor Stinner31a83932017-12-04 13:39:15 +01001038 err = calculate_module_search_path(main_config, calculate, config, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +01001039 if (_Py_INIT_FAILED(err)) {
1040 return err;
1041 }
1042
1043done:
1044 config->prefix = _PyMem_RawWcsdup(prefix);
1045 if (config->prefix == NULL) {
1046 return _Py_INIT_NO_MEMORY();
1047 }
1048
1049 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001050}
1051
1052
Victor Stinner0327bde2017-11-23 17:03:20 +01001053static void
1054calculate_free(PyCalculatePath *calculate)
1055{
1056 PyMem_RawFree(calculate->machine_path);
1057 PyMem_RawFree(calculate->user_path);
1058}
1059
Victor Stinner9316ee42017-11-25 03:17:57 +01001060
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001061_PyInitError
Victor Stinner31a83932017-12-04 13:39:15 +01001062_PyPathConfig_Calculate(_PyPathConfig *config,
1063 const _PyMainInterpreterConfig *main_config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001064{
Victor Stinner0327bde2017-11-23 17:03:20 +01001065 PyCalculatePath calculate;
1066 memset(&calculate, 0, sizeof(calculate));
1067
Victor Stinner46972b72017-11-24 22:55:40 +01001068 calculate_init(&calculate, main_config);
1069
Victor Stinner31a83932017-12-04 13:39:15 +01001070 _PyInitError err = calculate_path_impl(main_config, &calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +01001071 if (_Py_INIT_FAILED(err)) {
1072 goto done;
1073 }
1074
Victor Stinner9316ee42017-11-25 03:17:57 +01001075 err = _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001076
Victor Stinner9316ee42017-11-25 03:17:57 +01001077done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001078 calculate_free(&calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +01001079 return err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001080}
1081
1082
Victor Stinner63941882011-09-29 00:42:28 +02001083/* Load python3.dll before loading any extension module that might refer
1084 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001085 to this python DLL is loaded, not a python3.dll that might be on the path
1086 by chance.
1087 Return whether the DLL was found.
1088*/
1089static int python3_checked = 0;
1090static HANDLE hPython3;
1091int
Victor Stinner31a83932017-12-04 13:39:15 +01001092_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001093{
1094 wchar_t py3path[MAXPATHLEN+1];
1095 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001096 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001097 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001098 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001099 python3_checked = 1;
1100
1101 /* If there is a python3.dll next to the python3y.dll,
1102 assume this is a build tree; use that DLL */
Victor Stinnerb64de462017-12-01 18:27:09 +01001103 wcscpy(py3path, _Py_path_config.dll_path);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001104 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001105 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001106 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001107 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001108 wcscpy(s, L"\\python3.dll");
1109 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001110 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001111 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001112 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001113
1114 /* Check sys.prefix\DLLs\python3.dll */
1115 wcscpy(py3path, Py_GetPrefix());
1116 wcscat(py3path, L"\\DLLs\\python3.dll");
1117 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1118 return hPython3 != NULL;
1119}