blob: 5adf16d6f27384d76684b7a7038e1d3e5b482705 [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 Stinner46972b72017-11-24 22:55:40 +0100692static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100693calculate_init(PyCalculatePath *calculate,
694 const _PyMainInterpreterConfig *main_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000695{
Victor Stinner46972b72017-11-24 22:55:40 +0100696 calculate->home = main_config->home;
697 calculate->module_search_path_env = main_config->module_search_path_env;
Steve Dower4db86bc2016-09-09 09:17:35 -0700698
Victor Stinner0327bde2017-11-23 17:03:20 +0100699 calculate->path_env = _wgetenv(L"PATH");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100700
Victor Stinner0327bde2017-11-23 17:03:20 +0100701 wchar_t *prog = Py_GetProgramName();
702 if (prog == NULL || *prog == '\0') {
703 prog = L"python";
704 }
705 calculate->prog = prog;
Victor Stinner0327bde2017-11-23 17:03:20 +0100706}
707
708
709static int
710get_pth_filename(wchar_t *spbuffer, PyPathConfig *config)
711{
712 if (config->dllpath[0]) {
713 if (!change_ext(spbuffer, config->dllpath, L"._pth") && exists(spbuffer)) {
714 return 1;
715 }
716 }
717 if (config->progpath[0]) {
718 if (!change_ext(spbuffer, config->progpath, L"._pth") && exists(spbuffer)) {
719 return 1;
720 }
721 }
722 return 0;
723}
724
725
726static int
727calculate_pth_file(PyPathConfig *config)
728{
729 wchar_t spbuffer[MAXPATHLEN+1];
730
731 if (!get_pth_filename(spbuffer, config)) {
732 return 0;
733 }
734
735 config->module_search_path = read_pth_file(spbuffer, config->prefix,
736 &Py_IsolatedFlag,
737 &Py_NoSiteFlag);
738 if (!config->module_search_path) {
739 return 0;
740 }
741 return 1;
742}
743
744
745/* Search for an environment configuration file, first in the
746 executable's directory and then in the parent directory.
747 If found, open it for use when searching for prefixes.
748*/
749static void
750calculate_pyvenv_file(PyCalculatePath *calculate)
751{
752 wchar_t envbuffer[MAXPATHLEN+1];
753 const wchar_t *env_cfg = L"pyvenv.cfg";
754
755 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
756 join(envbuffer, env_cfg);
757
758 FILE *env_file = _Py_wfopen(envbuffer, L"r");
759 if (env_file == NULL) {
760 errno = 0;
761 reduce(envbuffer);
762 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700763 join(envbuffer, env_cfg);
764 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100765 if (env_file == NULL) {
766 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100767 }
768 }
769
Victor Stinner0327bde2017-11-23 17:03:20 +0100770 if (env_file == NULL) {
771 return;
772 }
773
774 /* Look for a 'home' variable and set argv0_path to it, if found */
775 wchar_t tmpbuffer[MAXPATHLEN+1];
776 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
777 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
778 }
779 fclose(env_file);
780}
781
782
783static void
784calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config,
785 const _PyMainInterpreterConfig *main_config)
786{
787 get_progpath(calculate, config->progpath, config->dllpath);
788 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
789 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->progpath);
790 reduce(calculate->argv0_path);
791
792 /* Search for a sys.path file */
793 if (calculate_pth_file(config)) {
794 return;
795 }
796
797 calculate_pyvenv_file(calculate);
798
Steve Dower50289382016-09-09 14:22:43 -0700799 /* Calculate zip archive path from DLL or exe path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100800 change_ext(calculate->zip_path,
801 config->dllpath[0] ? config->dllpath : config->progpath,
802 L".zip");
Steve Dower50289382016-09-09 14:22:43 -0700803
Victor Stinner0327bde2017-11-23 17:03:20 +0100804 if (calculate->home == NULL || *calculate->home == '\0') {
805 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
806 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->zip_path);
807 reduce(config->prefix);
808 calculate->home = config->prefix;
809 } else if (search_for_prefix(config->prefix, calculate->argv0_path, LANDMARK)) {
810 calculate->home = config->prefix;
811 }
812 else {
813 calculate->home = NULL;
814 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100816 else {
817 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->home);
818 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000819
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 Stinner0327bde2017-11-23 17:03:20 +0100827 int skipdefault = (calculate->module_search_path_env!=NULL || calculate->home!=NULL || \
828 calculate->machine_path!=NULL || calculate->user_path!=NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 /* We need to construct a path from the following parts.
831 (1) the PYTHONPATH environment variable, if set;
832 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100835 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 (5) the directory containing the executable (argv0_path).
837 The length calculation calculates #4 first.
838 Extra rules:
839 - If PYTHONHOME is set (in any way) item (3) is ignored.
840 - If registry values are used, (4) and (5) are ignored.
841 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100844 size_t bufsz = 0;
845 if (calculate->home != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 wchar_t *p;
847 bufsz = 1;
848 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100849 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100851 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100853 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700855 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100856 bufsz += wcslen(calculate->argv0_path) + 1;
857 if (calculate->user_path) {
858 bufsz += wcslen(calculate->user_path) + 1;
859 }
860 if (calculate->machine_path) {
861 bufsz += wcslen(calculate->machine_path) + 1;
862 }
863 bufsz += wcslen(calculate->zip_path) + 1;
864 if (calculate->module_search_path_env != NULL) {
865 bufsz += wcslen(calculate->module_search_path_env) + 1;
866 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000867
Victor Stinner0327bde2017-11-23 17:03:20 +0100868 wchar_t *buf, *start_buf;
869 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (buf == NULL) {
871 /* We can't exit, so print a warning and limp along */
872 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100873 if (calculate->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100875 config->module_search_path = calculate->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 }
877 else {
878 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100879 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 return;
882 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100883 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000884
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 if (calculate->module_search_path_env) {
886 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->module_search_path_env)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700887 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100888 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 buf = wcschr(buf, L'\0');
890 *buf++ = DELIM;
891 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 if (calculate->zip_path[0]) {
893 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700894 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100895 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 buf = wcschr(buf, L'\0');
897 *buf++ = DELIM;
898 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100899 if (calculate->user_path) {
900 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700901 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 buf = wcschr(buf, L'\0');
904 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100906 if (calculate->machine_path) {
907 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700908 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 buf = wcschr(buf, L'\0');
911 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100913 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100915 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700916 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100917 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700919 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700921 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 wchar_t *p = PYTHONPATH;
923 wchar_t *q;
924 size_t n;
925 for (;;) {
926 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100927 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100929 }
930 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100934 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700935 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 buf = wcschr(buf, L'\0');
938 p++;
939 n--;
940 }
941 wcsncpy(buf, p, n);
942 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700943 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100944 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 p = q+1;
948 }
949 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100950 if (calculate->argv0_path) {
951 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700953 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700955 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 /* Now to pull one last hack/trick. If sys.prefix is
958 empty, then try and find it somewhere on the paths
959 we calculated. We scan backwards, as our general policy
960 is that Python core directories are at the *end* of
961 sys.path. We assume that our "lib" directory is
962 on the path, and that our 'prefix' directory is
963 the parent of that.
964 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100965 if (config->prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 wchar_t lookBuf[MAXPATHLEN+1];
967 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
968 while (1) {
969 Py_ssize_t nchars;
970 wchar_t *lookEnd = look;
971 /* 'look' will end up one character before the
972 start of the path in question - even if this
973 is one character before the start of the buffer
974 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100975 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 look--;
977 nchars = lookEnd-look;
978 wcsncpy(lookBuf, look+1, nchars);
979 lookBuf[nchars] = L'\0';
980 /* Up one level to the parent */
981 reduce(lookBuf);
Victor Stinner0327bde2017-11-23 17:03:20 +0100982 if (search_for_prefix(config->prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 break;
984 }
985 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100986 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100988 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 look--;
990 }
991 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100992
993 config->module_search_path = start_buf;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000994}
995
996
Victor Stinner0327bde2017-11-23 17:03:20 +0100997static void
998calculate_free(PyCalculatePath *calculate)
999{
1000 PyMem_RawFree(calculate->machine_path);
1001 PyMem_RawFree(calculate->user_path);
1002}
1003
1004static void
1005calculate_path(const _PyMainInterpreterConfig *main_config)
1006{
Victor Stinner46972b72017-11-24 22:55:40 +01001007 _PyInitError err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001008 PyCalculatePath calculate;
1009 memset(&calculate, 0, sizeof(calculate));
1010
Victor Stinner46972b72017-11-24 22:55:40 +01001011 _PyMainInterpreterConfig tmp_config;
1012 int use_tmp = (main_config == NULL);
1013 if (use_tmp) {
1014 tmp_config = _PyMainInterpreterConfig_INIT;
1015 err = _PyMainInterpreterConfig_ReadEnv(&tmp_config);
1016 if (_Py_INIT_FAILED(err)) {
1017 goto fatal_error;
1018 }
1019 main_config = &tmp_config;
Victor Stinner0327bde2017-11-23 17:03:20 +01001020 }
1021
Victor Stinner46972b72017-11-24 22:55:40 +01001022 calculate_init(&calculate, main_config);
1023
Victor Stinner0327bde2017-11-23 17:03:20 +01001024 PyPathConfig new_path_config;
1025 memset(&new_path_config, 0, sizeof(new_path_config));
1026
1027 calculate_path_impl(&calculate, &new_path_config, main_config);
1028 path_config = new_path_config;
1029
Victor Stinner46972b72017-11-24 22:55:40 +01001030 if (use_tmp) {
1031 _PyMainInterpreterConfig_Clear(&tmp_config);
1032 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001033 calculate_free(&calculate);
Victor Stinner46972b72017-11-24 22:55:40 +01001034 return;
1035
1036fatal_error:
1037 if (use_tmp) {
1038 _PyMainInterpreterConfig_Clear(&tmp_config);
1039 }
1040 calculate_free(&calculate);
1041 _Py_FatalInitError(err);
Victor Stinner0327bde2017-11-23 17:03:20 +01001042}
1043
1044
1045
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001046/* External interface */
1047
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001048void
1049Py_SetPath(const wchar_t *path)
1050{
Victor Stinner0327bde2017-11-23 17:03:20 +01001051 if (path_config.module_search_path != NULL) {
1052 PyMem_RawFree(path_config.module_search_path);
1053 path_config.module_search_path = NULL;
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001054 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001055
1056 if (path == NULL) {
1057 return;
1058 }
1059
1060 wchar_t *prog = Py_GetProgramName();
1061 wcsncpy(path_config.progpath, prog, MAXPATHLEN);
1062 path_config.prefix[0] = L'\0';
1063 path_config.module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
1064 if (path_config.module_search_path != NULL) {
1065 wcscpy(path_config.module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001066 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001067}
1068
Victor Stinner0327bde2017-11-23 17:03:20 +01001069
Martin v. Löwis790465f2008-04-05 20:41:37 +00001070wchar_t *
Victor Stinner0327bde2017-11-23 17:03:20 +01001071_Py_GetPathWithConfig(const _PyMainInterpreterConfig *main_config)
Victor Stinnerd4341102017-11-23 00:12:09 +01001072{
Victor Stinner0327bde2017-11-23 17:03:20 +01001073 if (!path_config.module_search_path) {
1074 calculate_path(main_config);
Victor Stinnerd4341102017-11-23 00:12:09 +01001075 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001076 return path_config.module_search_path;
Victor Stinnerd4341102017-11-23 00:12:09 +01001077}
1078
Victor Stinner0327bde2017-11-23 17:03:20 +01001079
Victor Stinnerd4341102017-11-23 00:12:09 +01001080wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001081Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001082{
Victor Stinner0327bde2017-11-23 17:03:20 +01001083 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001084 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001085 }
1086 return path_config.module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001087}
1088
Victor Stinner0327bde2017-11-23 17:03:20 +01001089
Martin v. Löwis790465f2008-04-05 20:41:37 +00001090wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001091Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001092{
Victor Stinner0327bde2017-11-23 17:03:20 +01001093 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001094 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001095 }
1096 return path_config.prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001097}
1098
Victor Stinner0327bde2017-11-23 17:03:20 +01001099
Martin v. Löwis790465f2008-04-05 20:41:37 +00001100wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001101Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001104}
1105
Victor Stinner0327bde2017-11-23 17:03:20 +01001106
Martin v. Löwis790465f2008-04-05 20:41:37 +00001107wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001108Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001109{
Victor Stinner0327bde2017-11-23 17:03:20 +01001110 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001111 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001112 }
1113 return path_config.progpath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001114}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001115
Victor Stinner0327bde2017-11-23 17:03:20 +01001116
Victor Stinner63941882011-09-29 00:42:28 +02001117/* Load python3.dll before loading any extension module that might refer
1118 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001119 to this python DLL is loaded, not a python3.dll that might be on the path
1120 by chance.
1121 Return whether the DLL was found.
1122*/
1123static int python3_checked = 0;
1124static HANDLE hPython3;
1125int
1126_Py_CheckPython3()
1127{
1128 wchar_t py3path[MAXPATHLEN+1];
1129 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001130 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001131 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001132 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001133 python3_checked = 1;
1134
1135 /* If there is a python3.dll next to the python3y.dll,
1136 assume this is a build tree; use that DLL */
Victor Stinner0327bde2017-11-23 17:03:20 +01001137 wcscpy(py3path, path_config.dllpath);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001138 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001139 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001140 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001141 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001142 wcscpy(s, L"\\python3.dll");
1143 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001144 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001145 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001146 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001147
1148 /* Check sys.prefix\DLLs\python3.dll */
1149 wcscpy(py3path, Py_GetPrefix());
1150 wcscat(py3path, L"\\DLLs\\python3.dll");
1151 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1152 return hPython3 != NULL;
1153}