blob: 38e433b6dcd05aa0fbe2e456d5aa653d00b88faa [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];
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100121 wchar_t program_name[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100122 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
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100135 wchar_t *program_name; /* Program name */
Victor Stinner0327bde2017-11-23 17:03:20 +0100136 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 Stinnerf04ebe22017-11-25 00:01:23 +0100487get_progpath(PyCalculatePath *calculate, PyPathConfig *config)
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;
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100493 /* static init of program_name ensures final char remains \0 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100494 if (PyWin_DLLhModule) {
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100495 if (!GetModuleFileNameW(PyWin_DLLhModule, config->dllpath, MAXPATHLEN)) {
496 config->dllpath[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100497 }
498 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000499#else
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100500 config->dllpath[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000501#endif
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100502 if (GetModuleFileNameW(NULL, config->program_name, 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 Stinnerf04ebe22017-11-25 00:01:23 +0100512 if (wcschr(calculate->program_name, SEP) || wcschr(calculate->program_name, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000513#else
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100514 if (wcschr(calculate->program_name, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000515#endif
Victor Stinner0327bde2017-11-23 17:03:20 +0100516 {
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100517 wcsncpy(config->program_name, calculate->program_name, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100518 }
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);
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100527 wcsncpy(config->program_name, path, len);
528 *(config->program_name + len) = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100530 else {
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100531 wcsncpy(config->program_name, 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 Stinnerf04ebe22017-11-25 00:01:23 +0100535 join(config->program_name, calculate->program_name);
536 if (exists(config->program_name)) {
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) {
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100541 config->program_name[0] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 break;
543 }
544 path = delim + 1;
545 }
546 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100547 else {
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100548 config->program_name[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;
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100698 calculate->program_name = main_config->program_name;
Steve Dower4db86bc2016-09-09 09:17:35 -0700699
Victor Stinner0327bde2017-11-23 17:03:20 +0100700 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100701}
702
703
704static int
705get_pth_filename(wchar_t *spbuffer, PyPathConfig *config)
706{
707 if (config->dllpath[0]) {
708 if (!change_ext(spbuffer, config->dllpath, L"._pth") && exists(spbuffer)) {
709 return 1;
710 }
711 }
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100712 if (config->program_name[0]) {
713 if (!change_ext(spbuffer, config->program_name, L"._pth") && exists(spbuffer)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100714 return 1;
715 }
716 }
717 return 0;
718}
719
720
721static int
722calculate_pth_file(PyPathConfig *config)
723{
724 wchar_t spbuffer[MAXPATHLEN+1];
725
726 if (!get_pth_filename(spbuffer, config)) {
727 return 0;
728 }
729
730 config->module_search_path = read_pth_file(spbuffer, config->prefix,
731 &Py_IsolatedFlag,
732 &Py_NoSiteFlag);
733 if (!config->module_search_path) {
734 return 0;
735 }
736 return 1;
737}
738
739
740/* Search for an environment configuration file, first in the
741 executable's directory and then in the parent directory.
742 If found, open it for use when searching for prefixes.
743*/
744static void
745calculate_pyvenv_file(PyCalculatePath *calculate)
746{
747 wchar_t envbuffer[MAXPATHLEN+1];
748 const wchar_t *env_cfg = L"pyvenv.cfg";
749
750 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
751 join(envbuffer, env_cfg);
752
753 FILE *env_file = _Py_wfopen(envbuffer, L"r");
754 if (env_file == NULL) {
755 errno = 0;
756 reduce(envbuffer);
757 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700758 join(envbuffer, env_cfg);
759 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100760 if (env_file == NULL) {
761 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100762 }
763 }
764
Victor Stinner0327bde2017-11-23 17:03:20 +0100765 if (env_file == NULL) {
766 return;
767 }
768
769 /* Look for a 'home' variable and set argv0_path to it, if found */
770 wchar_t tmpbuffer[MAXPATHLEN+1];
771 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
772 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
773 }
774 fclose(env_file);
775}
776
777
778static void
779calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config,
780 const _PyMainInterpreterConfig *main_config)
781{
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100782 get_progpath(calculate, config);
783 /* program_name guaranteed \0 terminated in MAXPATH+1 bytes. */
784 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_name);
Victor Stinner0327bde2017-11-23 17:03:20 +0100785 reduce(calculate->argv0_path);
786
787 /* Search for a sys.path file */
788 if (calculate_pth_file(config)) {
789 return;
790 }
791
792 calculate_pyvenv_file(calculate);
793
Steve Dower50289382016-09-09 14:22:43 -0700794 /* Calculate zip archive path from DLL or exe path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100795 change_ext(calculate->zip_path,
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100796 config->dllpath[0] ? config->dllpath : config->program_name,
Victor Stinner0327bde2017-11-23 17:03:20 +0100797 L".zip");
Steve Dower50289382016-09-09 14:22:43 -0700798
Victor Stinner0327bde2017-11-23 17:03:20 +0100799 if (calculate->home == NULL || *calculate->home == '\0') {
800 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
801 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->zip_path);
802 reduce(config->prefix);
803 calculate->home = config->prefix;
804 } else if (search_for_prefix(config->prefix, calculate->argv0_path, LANDMARK)) {
805 calculate->home = config->prefix;
806 }
807 else {
808 calculate->home = NULL;
809 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100811 else {
812 wcscpy_s(config->prefix, MAXPATHLEN+1, calculate->home);
813 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000814
Victor Stinner0327bde2017-11-23 17:03:20 +0100815 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000816#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100817 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
818 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000819#endif
luzpaza5293b42017-11-05 07:37:50 -0600820 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 anything better to use! */
Victor Stinner0327bde2017-11-23 17:03:20 +0100822 int skipdefault = (calculate->module_search_path_env!=NULL || calculate->home!=NULL || \
823 calculate->machine_path!=NULL || calculate->user_path!=NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 /* We need to construct a path from the following parts.
826 (1) the PYTHONPATH environment variable, if set;
827 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100828 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100830 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 (5) the directory containing the executable (argv0_path).
832 The length calculation calculates #4 first.
833 Extra rules:
834 - If PYTHONHOME is set (in any way) item (3) is ignored.
835 - If registry values are used, (4) and (5) are ignored.
836 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100839 size_t bufsz = 0;
840 if (calculate->home != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 wchar_t *p;
842 bufsz = 1;
843 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100844 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100848 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700850 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100851 bufsz += wcslen(calculate->argv0_path) + 1;
852 if (calculate->user_path) {
853 bufsz += wcslen(calculate->user_path) + 1;
854 }
855 if (calculate->machine_path) {
856 bufsz += wcslen(calculate->machine_path) + 1;
857 }
858 bufsz += wcslen(calculate->zip_path) + 1;
859 if (calculate->module_search_path_env != NULL) {
860 bufsz += wcslen(calculate->module_search_path_env) + 1;
861 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000862
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 wchar_t *buf, *start_buf;
864 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 if (buf == NULL) {
866 /* We can't exit, so print a warning and limp along */
867 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100868 if (calculate->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100870 config->module_search_path = calculate->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 }
872 else {
873 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return;
877 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100878 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000879
Victor Stinner0327bde2017-11-23 17:03:20 +0100880 if (calculate->module_search_path_env) {
881 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->module_search_path_env)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700882 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100883 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 buf = wcschr(buf, L'\0');
885 *buf++ = DELIM;
886 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100887 if (calculate->zip_path[0]) {
888 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700889 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100890 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 buf = wcschr(buf, L'\0');
892 *buf++ = DELIM;
893 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100894 if (calculate->user_path) {
895 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700896 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 buf = wcschr(buf, L'\0');
899 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100901 if (calculate->machine_path) {
902 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100908 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100910 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700911 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700914 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700916 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 wchar_t *p = PYTHONPATH;
918 wchar_t *q;
919 size_t n;
920 for (;;) {
921 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100922 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100924 }
925 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100927 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100929 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700930 Py_FatalError("buffer overflow in getpathp.c's calculate_path()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100931 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 buf = wcschr(buf, L'\0');
933 p++;
934 n--;
935 }
936 wcsncpy(buf, p, n);
937 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700938 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100939 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 p = q+1;
943 }
944 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100945 if (calculate->argv0_path) {
946 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700948 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700950 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 /* Now to pull one last hack/trick. If sys.prefix is
953 empty, then try and find it somewhere on the paths
954 we calculated. We scan backwards, as our general policy
955 is that Python core directories are at the *end* of
956 sys.path. We assume that our "lib" directory is
957 on the path, and that our 'prefix' directory is
958 the parent of that.
959 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100960 if (config->prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 wchar_t lookBuf[MAXPATHLEN+1];
962 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
963 while (1) {
964 Py_ssize_t nchars;
965 wchar_t *lookEnd = look;
966 /* 'look' will end up one character before the
967 start of the path in question - even if this
968 is one character before the start of the buffer
969 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100970 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 look--;
972 nchars = lookEnd-look;
973 wcsncpy(lookBuf, look+1, nchars);
974 lookBuf[nchars] = L'\0';
975 /* Up one level to the parent */
976 reduce(lookBuf);
Victor Stinner0327bde2017-11-23 17:03:20 +0100977 if (search_for_prefix(config->prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 break;
979 }
980 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100981 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100983 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 look--;
985 }
986 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100987
988 config->module_search_path = start_buf;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000989}
990
991
Victor Stinner0327bde2017-11-23 17:03:20 +0100992static void
993calculate_free(PyCalculatePath *calculate)
994{
995 PyMem_RawFree(calculate->machine_path);
996 PyMem_RawFree(calculate->user_path);
997}
998
999static void
1000calculate_path(const _PyMainInterpreterConfig *main_config)
1001{
Victor Stinner46972b72017-11-24 22:55:40 +01001002 _PyInitError err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001003 PyCalculatePath calculate;
1004 memset(&calculate, 0, sizeof(calculate));
1005
Victor Stinner46972b72017-11-24 22:55:40 +01001006 _PyMainInterpreterConfig tmp_config;
1007 int use_tmp = (main_config == NULL);
1008 if (use_tmp) {
1009 tmp_config = _PyMainInterpreterConfig_INIT;
1010 err = _PyMainInterpreterConfig_ReadEnv(&tmp_config);
1011 if (_Py_INIT_FAILED(err)) {
1012 goto fatal_error;
1013 }
1014 main_config = &tmp_config;
Victor Stinner0327bde2017-11-23 17:03:20 +01001015 }
1016
Victor Stinner46972b72017-11-24 22:55:40 +01001017 calculate_init(&calculate, main_config);
1018
Victor Stinner0327bde2017-11-23 17:03:20 +01001019 PyPathConfig new_path_config;
1020 memset(&new_path_config, 0, sizeof(new_path_config));
1021
1022 calculate_path_impl(&calculate, &new_path_config, main_config);
1023 path_config = new_path_config;
1024
Victor Stinner46972b72017-11-24 22:55:40 +01001025 if (use_tmp) {
1026 _PyMainInterpreterConfig_Clear(&tmp_config);
1027 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001028 calculate_free(&calculate);
Victor Stinner46972b72017-11-24 22:55:40 +01001029 return;
1030
1031fatal_error:
1032 if (use_tmp) {
1033 _PyMainInterpreterConfig_Clear(&tmp_config);
1034 }
1035 calculate_free(&calculate);
1036 _Py_FatalInitError(err);
Victor Stinner0327bde2017-11-23 17:03:20 +01001037}
1038
1039
1040
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001041/* External interface */
1042
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001043void
1044Py_SetPath(const wchar_t *path)
1045{
Victor Stinner0327bde2017-11-23 17:03:20 +01001046 if (path_config.module_search_path != NULL) {
1047 PyMem_RawFree(path_config.module_search_path);
1048 path_config.module_search_path = NULL;
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001049 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001050
1051 if (path == NULL) {
1052 return;
1053 }
1054
Victor Stinnerf04ebe22017-11-25 00:01:23 +01001055 wchar_t *program_name = Py_GetProgramName();
1056 wcsncpy(path_config.program_name, program_name, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +01001057 path_config.prefix[0] = L'\0';
1058 path_config.module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
1059 if (path_config.module_search_path != NULL) {
1060 wcscpy(path_config.module_search_path, path);
Victor Stinner1a7425f2013-07-07 16:25:15 +02001061 }
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001062}
1063
Victor Stinner0327bde2017-11-23 17:03:20 +01001064
Martin v. Löwis790465f2008-04-05 20:41:37 +00001065wchar_t *
Victor Stinner0327bde2017-11-23 17:03:20 +01001066_Py_GetPathWithConfig(const _PyMainInterpreterConfig *main_config)
Victor Stinnerd4341102017-11-23 00:12:09 +01001067{
Victor Stinner0327bde2017-11-23 17:03:20 +01001068 if (!path_config.module_search_path) {
1069 calculate_path(main_config);
Victor Stinnerd4341102017-11-23 00:12:09 +01001070 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001071 return path_config.module_search_path;
Victor Stinnerd4341102017-11-23 00:12:09 +01001072}
1073
Victor Stinner0327bde2017-11-23 17:03:20 +01001074
Victor Stinnerd4341102017-11-23 00:12:09 +01001075wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001076Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001077{
Victor Stinner0327bde2017-11-23 17:03:20 +01001078 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001079 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001080 }
1081 return path_config.module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001082}
1083
Victor Stinner0327bde2017-11-23 17:03:20 +01001084
Martin v. Löwis790465f2008-04-05 20:41:37 +00001085wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001086Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001087{
Victor Stinner0327bde2017-11-23 17:03:20 +01001088 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001089 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001090 }
1091 return path_config.prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001092}
1093
Victor Stinner0327bde2017-11-23 17:03:20 +01001094
Martin v. Löwis790465f2008-04-05 20:41:37 +00001095wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001096Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001099}
1100
Victor Stinner0327bde2017-11-23 17:03:20 +01001101
Martin v. Löwis790465f2008-04-05 20:41:37 +00001102wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001103Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001104{
Victor Stinner0327bde2017-11-23 17:03:20 +01001105 if (!path_config.module_search_path) {
Victor Stinnerd4341102017-11-23 00:12:09 +01001106 calculate_path(NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +01001107 }
Victor Stinnerf04ebe22017-11-25 00:01:23 +01001108 return path_config.program_name;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001109}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001110
Victor Stinner0327bde2017-11-23 17:03:20 +01001111
Victor Stinner63941882011-09-29 00:42:28 +02001112/* Load python3.dll before loading any extension module that might refer
1113 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001114 to this python DLL is loaded, not a python3.dll that might be on the path
1115 by chance.
1116 Return whether the DLL was found.
1117*/
1118static int python3_checked = 0;
1119static HANDLE hPython3;
1120int
1121_Py_CheckPython3()
1122{
1123 wchar_t py3path[MAXPATHLEN+1];
1124 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001125 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001126 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001127 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001128 python3_checked = 1;
1129
1130 /* If there is a python3.dll next to the python3y.dll,
1131 assume this is a build tree; use that DLL */
Victor Stinner0327bde2017-11-23 17:03:20 +01001132 wcscpy(py3path, path_config.dllpath);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001133 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001134 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001135 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001136 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001137 wcscpy(s, L"\\python3.dll");
1138 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001139 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001140 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001141 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001142
1143 /* Check sys.prefix\DLLs\python3.dll */
1144 wcscpy(py3path, Py_GetPrefix());
1145 wcscat(py3path, L"\\DLLs\\python3.dll");
1146 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1147 return hPython3 != NULL;
1148}