blob: e0cb9a257584960ed70a8fd7d1b073a02134f30f [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"
83#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000084#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085
Steve Dower4db86bc2016-09-09 09:17:35 -070086#ifndef MS_WINDOWS
87#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000088#endif
89
Steve Dower4db86bc2016-09-09 09:17:35 -070090#include <windows.h>
91#include <Shlwapi.h>
92
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000094#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#endif /* HAVE_SYS_TYPES_H */
96
97#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000098#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099#endif /* HAVE_SYS_STAT_H */
100
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#include <string.h>
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103/* Search in some common locations for the associated Python libraries.
104 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 * Py_GetPath() tries to return a sensible Python module search path.
106 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000107 * The approach is an adaptation for Windows of the strategy used in
108 * ../Modules/getpath.c; it uses the Windows Registry as one of its
109 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000110 *
111 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
112 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113 */
114
115#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000116#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000117#endif
118
Victor Stinner0327bde2017-11-23 17:03:20 +0100119typedef struct {
120 wchar_t prefix[MAXPATHLEN+1];
121 wchar_t progpath[MAXPATHLEN+1];
122 wchar_t dllpath[MAXPATHLEN+1];
123 wchar_t *module_search_path;
124} PyPathConfig;
125
126typedef struct {
127 wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
128 wchar_t *path_env; /* PATH environment variable */
129 wchar_t *home; /* PYTHONHOME environment variable */
130
131 /* Registry key "Software\Python\PythonCore\PythonPath" */
132 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
133 wchar_t *user_path; /* from HKEY_CURRENT_USER */
134
135 wchar_t *prog; /* Program name */
136 wchar_t argv0_path[MAXPATHLEN+1];
137 wchar_t zip_path[MAXPATHLEN+1];
138} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139
Guido van Rossumeea14491997-08-13 21:30:44 +0000140
Victor Stinner0327bde2017-11-23 17:03:20 +0100141static PyPathConfig path_config = {.module_search_path = NULL};
142
143
144/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000145static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100146is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147{
148#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000152#endif
153}
154
Victor Stinner0327bde2017-11-23 17:03:20 +0100155
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000156/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100157 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000159reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000160{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700161 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100162 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700163 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100164 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 while (i > 0 && !is_sep(dir[i]))
167 --i;
168 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000169}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170
Victor Stinner0327bde2017-11-23 17:03:20 +0100171
Steve Dowered51b262016-09-17 12:54:06 -0700172static int
173change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
174{
175 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
176 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100177 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700178 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100179 }
Steve Dowered51b262016-09-17 12:54:06 -0700180
181 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
182 --i;
183
184 if (i == 0) {
185 dest[0] = '\0';
186 return -1;
187 }
188
Victor Stinner0327bde2017-11-23 17:03:20 +0100189 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700190 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100191 }
Steve Dowered51b262016-09-17 12:54:06 -0700192
193 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100194 wcscat_s(dest, MAXPATHLEN+1, ext))
195 {
Steve Dowered51b262016-09-17 12:54:06 -0700196 dest[0] = '\0';
197 return -1;
198 }
199
200 return 0;
201}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000202
Victor Stinner0327bde2017-11-23 17:03:20 +0100203
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000204static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000205exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000208}
209
Victor Stinner0327bde2017-11-23 17:03:20 +0100210
211/* Is module -- check for .pyc too.
212 Assumes 'filename' MAXPATHLEN+1 bytes long -
213 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000214static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100215ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000216{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100217 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700218
Victor Stinner0327bde2017-11-23 17:03:20 +0100219 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100221 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700224 n = wcsnlen_s(filename, MAXPATHLEN+1);
225 if (n < MAXPATHLEN) {
226 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800227 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700228 filename[n + 1] = L'\0';
229 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100230 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700231 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100232 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700233 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 }
235 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000236}
237
Victor Stinner0327bde2017-11-23 17:03:20 +0100238
Tim Peters8484fbf2004-08-07 19:12:27 +0000239/* Add a path component, by appending stuff to buffer.
240 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
241 NUL-terminated string with no more than MAXPATHLEN characters (not counting
242 the trailing NUL). It's a fatal error if it contains a string longer than
243 that (callers must be careful!). If these requirements are met, it's
244 guaranteed that buffer will still be a NUL-terminated string with no more
245 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
246 stuff as fits will be appended.
247*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700248
249static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100250typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
251 PCWSTR pszPathIn, PCWSTR pszMore,
252 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700253static PPathCchCombineEx _PathCchCombineEx;
254
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000255static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700256join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000257{
Steve Dower4db86bc2016-09-09 09:17:35 -0700258 if (_PathCchCombineEx_Initialized == 0) {
259 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100260 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100262 }
263 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700264 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100265 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700266 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700268
Steve Dower4db86bc2016-09-09 09:17:35 -0700269 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100270 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700271 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700273 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100274 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700275 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100276 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700277 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000278}
279
Victor Stinner0327bde2017-11-23 17:03:20 +0100280
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000281/* gotlandmark only called by search_for_prefix, which ensures
282 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100283 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000284static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100285gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700288 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700291 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 prefix[n] = '\0';
293 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000294}
295
Victor Stinner0327bde2017-11-23 17:03:20 +0100296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000298 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000299static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100300search_for_prefix(wchar_t *prefix, wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700303 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100305 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 reduce(prefix);
309 } while (prefix[0]);
310 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000311}
312
Victor Stinner0327bde2017-11-23 17:03:20 +0100313
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000314#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000315
Guido van Rossum88716bb2000-03-30 19:45:39 +0000316/* a string loaded from the DLL at startup.*/
317extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000318
Guido van Rossumeea14491997-08-13 21:30:44 +0000319/* Load a PYTHONPATH value from the registry.
320 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
321
Guido van Rossum88716bb2000-03-30 19:45:39 +0000322 Works in both Unicode and 8bit environments. Only uses the
323 Ex family of functions so it also works with Windows CE.
324
Guido van Rossumeea14491997-08-13 21:30:44 +0000325 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000326
327 XXX - this code is pretty strange, as it used to also
328 work on Win16, where the buffer sizes werent available
329 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000330*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000331static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000332getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 HKEY newKey = 0;
335 DWORD dataSize = 0;
336 DWORD numKeys = 0;
337 LONG rc;
338 wchar_t *retval = NULL;
339 WCHAR *dataBuf = NULL;
340 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
341 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700342 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 DWORD index;
344 WCHAR *keyBuf = NULL;
345 WCHAR *keyBufPtr;
346 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Tried to use sysget("winver") but here is too early :-( */
349 versionLen = strlen(PyWin_DLLVersionString);
350 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700351 keyBufLen = sizeof(keyPrefix) +
352 sizeof(WCHAR)*(versionLen-1) +
353 sizeof(keySuffix);
354 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100355 if (keyBuf==NULL) {
356 goto done;
357 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700359 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200360 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
362 keyBufPtr += versionLen;
363 /* NULL comes with this one! */
364 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
365 /* Open the root Python key */
366 rc=RegOpenKeyExW(keyBase,
367 keyBuf, /* subkey */
368 0, /* reserved */
369 KEY_READ,
370 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100371 if (rc!=ERROR_SUCCESS) {
372 goto done;
373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* Find out how big our core buffer is, and how many subkeys we have */
375 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
376 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100377 if (rc!=ERROR_SUCCESS) {
378 goto done;
379 }
380 if (skipcore) {
381 dataSize = 0; /* Only count core ones if we want them! */
382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* Allocate a temp array of char buffers, so we only need to loop
384 reading the registry once
385 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200386 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100387 if (ppPaths==NULL) {
388 goto done;
389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
391 /* Loop over all subkeys, allocating a temp sub-buffer. */
392 for(index=0;index<numKeys;index++) {
393 WCHAR keyBuf[MAX_PATH+1];
394 HKEY subKey = 0;
395 DWORD reqdSize = MAX_PATH+1;
396 /* Get the sub-key name */
397 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
398 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100399 if (rc!=ERROR_SUCCESS) {
400 goto done;
401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 /* Open the sub-key */
403 rc=RegOpenKeyExW(newKey,
404 keyBuf, /* subkey */
405 0, /* reserved */
406 KEY_READ,
407 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100408 if (rc!=ERROR_SUCCESS) {
409 goto done;
410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Find the value of the buffer size, malloc, then read it */
412 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
413 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200414 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (ppPaths[index]) {
416 RegQueryValueExW(subKey, NULL, 0, NULL,
417 (LPBYTE)ppPaths[index],
418 &reqdSize);
419 dataSize += reqdSize + 1; /* 1 for the ";" */
420 }
421 }
422 RegCloseKey(subKey);
423 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100426 if (dataSize == 0) {
427 goto done;
428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429
430 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200431 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (dataBuf) {
433 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* Copy our collected strings */
435 for (index=0;index<numKeys;index++) {
436 if (index > 0) {
437 *(szCur++) = L';';
438 dataSize--;
439 }
440 if (ppPaths[index]) {
441 Py_ssize_t len = wcslen(ppPaths[index]);
442 wcsncpy(szCur, ppPaths[index], len);
443 szCur += len;
444 assert(dataSize > (DWORD)len);
445 dataSize -= (DWORD)len;
446 }
447 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100448 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 else {
luzpaza5293b42017-11-05 07:37:50 -0600452 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (numKeys) {
454 *(szCur++) = L';';
455 dataSize--;
456 }
457 /* Now append the core path entries -
458 this will include the NULL
459 */
460 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
461 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200462 if (rc != ERROR_SUCCESS) {
463 PyMem_RawFree(dataBuf);
464 goto done;
465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 }
467 /* And set the result - caller must free */
468 retval = dataBuf;
469 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000470done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 /* Loop freeing my temp buffers */
472 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200473 for(index=0; index<numKeys; index++)
474 PyMem_RawFree(ppPaths[index]);
475 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100477 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100479 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200480 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000482}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000483#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000484
Victor Stinner0327bde2017-11-23 17:03:20 +0100485
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000486static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100487get_progpath(PyCalculatePath *calculate, wchar_t *progpath, wchar_t *dllpath)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488{
Victor Stinner0327bde2017-11-23 17:03:20 +0100489 wchar_t *path = calculate->path_env;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000490
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000491#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 extern HANDLE PyWin_DLLhModule;
493 /* static init of progpath ensures final char remains \0 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100494 if (PyWin_DLLhModule) {
495 if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 dllpath[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100497 }
498 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000499#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000501#endif
Victor Stinner0327bde2017-11-23 17:03:20 +0100502 if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 return;
Victor Stinner0327bde2017-11-23 17:03:20 +0100504 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 /* If there is no slash in the argv0 path, then we have to
507 * assume python is on the user's $PATH, since there's no
508 * other way to find a directory to start the search from. If
509 * $PATH isn't exported, you lose.
510 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000511#ifdef ALTSEP
Victor Stinner0327bde2017-11-23 17:03:20 +0100512 if (wcschr(calculate->prog, SEP) || wcschr(calculate->prog, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000513#else
Victor Stinner0327bde2017-11-23 17:03:20 +0100514 if (wcschr(calculate->prog, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000515#endif
Victor Stinner0327bde2017-11-23 17:03:20 +0100516 {
517 wcsncpy(progpath, calculate->prog, MAXPATHLEN);
518 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 else if (path) {
520 while (1) {
521 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (delim) {
524 size_t len = delim - path;
525 /* ensure we can't overwrite buffer */
526 len = min(MAXPATHLEN,len);
527 wcsncpy(progpath, path, len);
528 *(progpath + len) = '\0';
529 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100530 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 wcsncpy(progpath, path, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100532 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* join() is safe for MAXPATHLEN+1 size buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100535 join(progpath, calculate->prog);
536 if (exists(progpath)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100538 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (!delim) {
541 progpath[0] = '\0';
542 break;
543 }
544 path = delim + 1;
545 }
546 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100547 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 progpath[0] = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100549 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000550}
551
Victor Stinner0327bde2017-11-23 17:03:20 +0100552
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100553static int
554find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
555{
556 int result = 0; /* meaning not found */
557 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
558
559 fseek(env_file, 0, SEEK_SET);
560 while (!feof(env_file)) {
561 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
562 wchar_t tmpbuffer[MAXPATHLEN*2+1];
563 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200564 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100565
Victor Stinner0327bde2017-11-23 17:03:20 +0100566 if (p == NULL) {
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100567 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100568 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100569 n = strlen(p);
570 if (p[n - 1] != '\n') {
571 /* line has overflowed - bail */
572 break;
573 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100574 if (p[0] == '#') {
575 /* Comment - skip */
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100576 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100577 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100578 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
579 if (decoded != NULL) {
580 Py_ssize_t k;
581 k = PyUnicode_AsWideChar(decoded,
582 tmpbuffer, MAXPATHLEN * 2);
583 Py_DECREF(decoded);
584 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800585 wchar_t * context = NULL;
586 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100587 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800588 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100589 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800590 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100591 if (tok != NULL) {
592 wcsncpy(value, tok, MAXPATHLEN);
593 result = 1;
594 break;
595 }
596 }
597 }
598 }
599 }
600 }
601 return result;
602}
603
Victor Stinner0327bde2017-11-23 17:03:20 +0100604
605static wchar_t*
Steve Dowered51b262016-09-17 12:54:06 -0700606read_pth_file(const wchar_t *path, wchar_t *prefix, int *isolated, int *nosite)
Steve Dower4db86bc2016-09-09 09:17:35 -0700607{
608 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100609 if (sp_file == NULL) {
610 return NULL;
611 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700612
Steve Dowered51b262016-09-17 12:54:06 -0700613 wcscpy_s(prefix, MAXPATHLEN+1, path);
614 reduce(prefix);
615 *isolated = 1;
616 *nosite = 1;
617
Steve Dower4db86bc2016-09-09 09:17:35 -0700618 size_t bufsiz = MAXPATHLEN;
619 size_t prefixlen = wcslen(prefix);
620
621 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
622 buf[0] = '\0';
623
624 while (!feof(sp_file)) {
625 char line[MAXPATHLEN + 1];
626 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100627 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700628 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100629 }
630 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700631 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100632 }
Steve Dowered51b262016-09-17 12:54:06 -0700633 while (*++p) {
634 if (*p == '\r' || *p == '\n') {
635 *p = '\0';
636 break;
637 }
638 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700639
Steve Dowered51b262016-09-17 12:54:06 -0700640 if (strcmp(line, "import site") == 0) {
641 *nosite = 0;
642 continue;
643 } else if (strncmp(line, "import ", 7) == 0) {
644 Py_FatalError("only 'import site' is supported in ._pth file");
645 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700646
Steve Dowered51b262016-09-17 12:54:06 -0700647 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700648 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Steve Dowered51b262016-09-17 12:54:06 -0700649 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700650 wline[wn] = '\0';
651
Steve Dowerc6dd4152016-10-27 14:28:07 -0700652 size_t usedsiz = wcslen(buf);
653 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700654 bufsiz += MAXPATHLEN;
655 buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));
656 if (!buf) {
657 PyMem_RawFree(wline);
658 goto error;
659 }
660 }
661
Steve Dowerc6dd4152016-10-27 14:28:07 -0700662 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700663 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700664 usedsiz += 1;
665 }
Steve Dowered51b262016-09-17 12:54:06 -0700666
Steve Dowerc6dd4152016-10-27 14:28:07 -0700667 errno_t result;
668 _Py_BEGIN_SUPPRESS_IPH
669 result = wcscat_s(buf, bufsiz, prefix);
670 _Py_END_SUPPRESS_IPH
671 if (result == EINVAL) {
672 Py_FatalError("invalid argument during ._pth processing");
673 } else if (result == ERANGE) {
674 Py_FatalError("buffer overflow during ._pth processing");
675 }
676 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700677 join(b, wline);
678
679 PyMem_RawFree(wline);
680 }
681
Steve Dower4db86bc2016-09-09 09:17:35 -0700682 fclose(sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100683 return buf;
Steve Dower4db86bc2016-09-09 09:17:35 -0700684
685error:
686 PyMem_RawFree(buf);
687 fclose(sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100688 return NULL;
Steve Dower4db86bc2016-09-09 09:17:35 -0700689}
690
691
Victor Stinner0327bde2017-11-23 17:03:20 +0100692static _PyInitError
693calculate_init(PyCalculatePath *calculate,
694 const _PyMainInterpreterConfig *main_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000695{
Victor Stinner0327bde2017-11-23 17:03:20 +0100696 _PyInitError err;
Guido van Rossumeea14491997-08-13 21:30:44 +0000697
Victor Stinner0327bde2017-11-23 17:03:20 +0100698 err = _Py_GetPythonHomeWithConfig(main_config, &calculate->home);
699 if (_Py_INIT_FAILED(err)) {
700 return err;
701 }
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000702
Victor Stinner0327bde2017-11-23 17:03:20 +0100703 if (main_config) {
704 calculate->module_search_path_env = main_config->module_search_path_env;
Victor Stinnerd4341102017-11-23 00:12:09 +0100705 }
706 else if (!Py_IgnoreEnvironmentFlag) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100707 wchar_t *path = _wgetenv(L"PYTHONPATH");
708 if (path && *path != '\0') {
709 calculate->module_search_path_env = path;
Steve Dower4db86bc2016-09-09 09:17:35 -0700710 }
711 }
712
Victor Stinner0327bde2017-11-23 17:03:20 +0100713 calculate->path_env = _wgetenv(L"PATH");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100714
Victor Stinner0327bde2017-11-23 17:03:20 +0100715 wchar_t *prog = Py_GetProgramName();
716 if (prog == NULL || *prog == '\0') {
717 prog = L"python";
718 }
719 calculate->prog = prog;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100720
Victor Stinner0327bde2017-11-23 17:03:20 +0100721 return _Py_INIT_OK();
722}
723
724
725static int
726get_pth_filename(wchar_t *spbuffer, PyPathConfig *config)
727{
728 if (config->dllpath[0]) {
729 if (!change_ext(spbuffer, config->dllpath, L"._pth") && exists(spbuffer)) {
730 return 1;
731 }
732 }
733 if (config->progpath[0]) {
734 if (!change_ext(spbuffer, config->progpath, L"._pth") && exists(spbuffer)) {
735 return 1;
736 }
737 }
738 return 0;
739}
740
741
742static int
743calculate_pth_file(PyPathConfig *config)
744{
745 wchar_t spbuffer[MAXPATHLEN+1];
746
747 if (!get_pth_filename(spbuffer, config)) {
748 return 0;
749 }
750
751 config->module_search_path = read_pth_file(spbuffer, config->prefix,
752 &Py_IsolatedFlag,
753 &Py_NoSiteFlag);
754 if (!config->module_search_path) {
755 return 0;
756 }
757 return 1;
758}
759
760
761/* Search for an environment configuration file, first in the
762 executable's directory and then in the parent directory.
763 If found, open it for use when searching for prefixes.
764*/
765static void
766calculate_pyvenv_file(PyCalculatePath *calculate)
767{
768 wchar_t envbuffer[MAXPATHLEN+1];
769 const wchar_t *env_cfg = L"pyvenv.cfg";
770
771 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
772 join(envbuffer, env_cfg);
773
774 FILE *env_file = _Py_wfopen(envbuffer, L"r");
775 if (env_file == NULL) {
776 errno = 0;
777 reduce(envbuffer);
778 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700779 join(envbuffer, env_cfg);
780 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100781 if (env_file == NULL) {
782 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100783 }
784 }
785
Victor Stinner0327bde2017-11-23 17:03:20 +0100786 if (env_file == NULL) {
787 return;
788 }
789
790 /* Look for a 'home' variable and set argv0_path to it, if found */
791 wchar_t tmpbuffer[MAXPATHLEN+1];
792 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
793 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
794 }
795 fclose(env_file);
796}
797
798
799static void
800calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config,
801 const _PyMainInterpreterConfig *main_config)
802{
803 get_progpath(calculate, config->progpath, config->dllpath);
804 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
805 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->progpath);
806 reduce(calculate->argv0_path);
807
808 /* Search for a sys.path file */
809 if (calculate_pth_file(config)) {
810 return;
811 }
812
813 calculate_pyvenv_file(calculate);
814
Steve Dower50289382016-09-09 14:22:43 -0700815 /* Calculate zip archive path from DLL or exe path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100816 change_ext(calculate->zip_path,
817 config->dllpath[0] ? config->dllpath : config->progpath,
818 L".zip");
Steve Dower50289382016-09-09 14:22:43 -0700819
Victor Stinner0327bde2017-11-23 17:03:20 +0100820 if (calculate->home == NULL || *calculate->home == '\0') {
821 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
822 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->zip_path);
823 reduce(config->prefix);
824 calculate->home = config->prefix;
825 } else if (search_for_prefix(config->prefix, calculate->argv0_path, LANDMARK)) {
826 calculate->home = config->prefix;
827 }
828 else {
829 calculate->home = NULL;
830 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100832 else {
833 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->home);
834 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000835
Victor Stinner0327bde2017-11-23 17:03:20 +0100836 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000837#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100838 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
839 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000840#endif
luzpaza5293b42017-11-05 07:37:50 -0600841 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 anything better to use! */
Victor Stinner0327bde2017-11-23 17:03:20 +0100843 int skipdefault = (calculate->module_search_path_env!=NULL || calculate->home!=NULL || \
844 calculate->machine_path!=NULL || calculate->user_path!=NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 /* We need to construct a path from the following parts.
847 (1) the PYTHONPATH environment variable, if set;
848 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100849 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100851 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 (5) the directory containing the executable (argv0_path).
853 The length calculation calculates #4 first.
854 Extra rules:
855 - If PYTHONHOME is set (in any way) item (3) is ignored.
856 - If registry values are used, (4) and (5) are ignored.
857 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100860 size_t bufsz = 0;
861 if (calculate->home != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 wchar_t *p;
863 bufsz = 1;
864 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100865 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100869 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700871 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100872 bufsz += wcslen(calculate->argv0_path) + 1;
873 if (calculate->user_path) {
874 bufsz += wcslen(calculate->user_path) + 1;
875 }
876 if (calculate->machine_path) {
877 bufsz += wcslen(calculate->machine_path) + 1;
878 }
879 bufsz += wcslen(calculate->zip_path) + 1;
880 if (calculate->module_search_path_env != NULL) {
881 bufsz += wcslen(calculate->module_search_path_env) + 1;
882 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000883
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 wchar_t *buf, *start_buf;
885 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 if (buf == NULL) {
887 /* We can't exit, so print a warning and limp along */
888 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100889 if (calculate->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 config->module_search_path = calculate->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 }
893 else {
894 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100895 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 return;
898 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100899 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000900
Victor Stinner0327bde2017-11-23 17:03:20 +0100901 if (calculate->module_search_path_env) {
902 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->module_search_path_env)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700903 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 buf = wcschr(buf, L'\0');
906 *buf++ = DELIM;
907 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100908 if (calculate->zip_path[0]) {
909 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700910 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 buf = wcschr(buf, L'\0');
913 *buf++ = DELIM;
914 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100915 if (calculate->user_path) {
916 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700917 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 buf = wcschr(buf, L'\0');
920 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100922 if (calculate->machine_path) {
923 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700924 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 buf = wcschr(buf, L'\0');
927 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100929 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100931 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700932 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700935 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700937 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 wchar_t *p = PYTHONPATH;
939 wchar_t *q;
940 size_t n;
941 for (;;) {
942 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100943 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100945 }
946 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100948 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100950 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700951 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 buf = wcschr(buf, L'\0');
954 p++;
955 n--;
956 }
957 wcsncpy(buf, p, n);
958 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700959 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100960 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100962 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 p = q+1;
964 }
965 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100966 if (calculate->argv0_path) {
967 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700969 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700971 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 /* Now to pull one last hack/trick. If sys.prefix is
974 empty, then try and find it somewhere on the paths
975 we calculated. We scan backwards, as our general policy
976 is that Python core directories are at the *end* of
977 sys.path. We assume that our "lib" directory is
978 on the path, and that our 'prefix' directory is
979 the parent of that.
980 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100981 if (config->prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 wchar_t lookBuf[MAXPATHLEN+1];
983 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
984 while (1) {
985 Py_ssize_t nchars;
986 wchar_t *lookEnd = look;
987 /* 'look' will end up one character before the
988 start of the path in question - even if this
989 is one character before the start of the buffer
990 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100991 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 look--;
993 nchars = lookEnd-look;
994 wcsncpy(lookBuf, look+1, nchars);
995 lookBuf[nchars] = L'\0';
996 /* Up one level to the parent */
997 reduce(lookBuf);
Victor Stinner0327bde2017-11-23 17:03:20 +0100998 if (search_for_prefix(config->prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 break;
1000 }
1001 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +01001002 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 break;
Victor Stinner0327bde2017-11-23 17:03:20 +01001004 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 look--;
1006 }
1007 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001008
1009 config->module_search_path = start_buf;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001010}
1011
1012
Victor Stinner0327bde2017-11-23 17:03:20 +01001013static void
1014calculate_free(PyCalculatePath *calculate)
1015{
1016 PyMem_RawFree(calculate->machine_path);
1017 PyMem_RawFree(calculate->user_path);
1018}
1019
1020static void
1021calculate_path(const _PyMainInterpreterConfig *main_config)
1022{
1023 PyCalculatePath calculate;
1024 memset(&calculate, 0, sizeof(calculate));
1025
1026 _PyInitError err = calculate_init(&calculate, main_config);
1027 if (_Py_INIT_FAILED(err)) {
1028 calculate_free(&calculate);
1029 _Py_FatalInitError(err);
1030 }
1031
1032 PyPathConfig new_path_config;
1033 memset(&new_path_config, 0, sizeof(new_path_config));
1034
1035 calculate_path_impl(&calculate, &new_path_config, main_config);
1036 path_config = new_path_config;
1037
1038 calculate_free(&calculate);
1039}
1040
1041
1042
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001043/* External interface */
1044
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001045void
1046Py_SetPath(const wchar_t *path)
1047{
Victor Stinner0327bde2017-11-23 17:03:20 +01001048 if (path_config.module_search_path != NULL) {
1049 PyMem_RawFree(path_config.module_search_path);
1050 path_config.module_search_path = NULL;
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001051 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001052
1053 if (path == NULL) {
1054 return;
1055 }
1056
1057 wchar_t *prog = Py_GetProgramName();
1058 wcsncpy(path_config.progpath, prog, MAXPATHLEN);
1059 path_config.prefix[0] = L'\0';
1060 path_config.module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
1061 if (path_config.module_search_path != NULL) {
1062 wcscpy(path_config.module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001063 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001064}
1065
Victor Stinner0327bde2017-11-23 17:03:20 +01001066
Martin v. Löwis790465f2008-04-05 20:41:37 +00001067wchar_t *
Victor Stinner0327bde2017-11-23 17:03:20 +01001068_Py_GetPathWithConfig(const _PyMainInterpreterConfig *main_config)
Victor Stinnerd4341102017-11-23 00:12:09 +01001069{
Victor Stinner0327bde2017-11-23 17:03:20 +01001070 if (!path_config.module_search_path) {
1071 calculate_path(main_config);
Victor Stinnerd4341102017-11-23 00:12:09 +01001072 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001073 return path_config.module_search_path;
Victor Stinnerd4341102017-11-23 00:12:09 +01001074}
1075
Victor Stinner0327bde2017-11-23 17:03:20 +01001076
Victor Stinnerd4341102017-11-23 00:12:09 +01001077wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001078Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001079{
Victor Stinner0327bde2017-11-23 17:03:20 +01001080 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001081 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001082 }
1083 return path_config.module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001084}
1085
Victor Stinner0327bde2017-11-23 17:03:20 +01001086
Martin v. Löwis790465f2008-04-05 20:41:37 +00001087wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001088Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001089{
Victor Stinner0327bde2017-11-23 17:03:20 +01001090 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001091 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001092 }
1093 return path_config.prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001094}
1095
Victor Stinner0327bde2017-11-23 17:03:20 +01001096
Martin v. Löwis790465f2008-04-05 20:41:37 +00001097wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001098Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001101}
1102
Victor Stinner0327bde2017-11-23 17:03:20 +01001103
Martin v. Löwis790465f2008-04-05 20:41:37 +00001104wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001105Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001106{
Victor Stinner0327bde2017-11-23 17:03:20 +01001107 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001108 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001109 }
1110 return path_config.progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001111}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001112
Victor Stinner0327bde2017-11-23 17:03:20 +01001113
Victor Stinner63941882011-09-29 00:42:28 +02001114/* Load python3.dll before loading any extension module that might refer
1115 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001116 to this python DLL is loaded, not a python3.dll that might be on the path
1117 by chance.
1118 Return whether the DLL was found.
1119*/
1120static int python3_checked = 0;
1121static HANDLE hPython3;
1122int
1123_Py_CheckPython3()
1124{
1125 wchar_t py3path[MAXPATHLEN+1];
1126 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001127 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001128 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001129 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001130 python3_checked = 1;
1131
1132 /* If there is a python3.dll next to the python3y.dll,
1133 assume this is a build tree; use that DLL */
Victor Stinner0327bde2017-11-23 17:03:20 +01001134 wcscpy(py3path, path_config.dllpath);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001135 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001136 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001137 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001138 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001139 wcscpy(s, L"\\python3.dll");
1140 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001141 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001142 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001143 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001144
1145 /* Check sys.prefix\DLLs\python3.dll */
1146 wcscpy(py3path, Py_GetPrefix());
1147 wcscat(py3path, L"\\DLLs\\python3.dll");
1148 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1149 return hPython3 != NULL;
1150}