blob: 0ee53080bf3108834a640e8aee901dcaade56678 [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001
2/* Return the initial module search path. */
Jesus Ceaf1af7052012-10-05 02:48:46 +02003/* Used by DOS, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00004
Guido van Rossum88716bb2000-03-30 19:45:39 +00005/* ----------------------------------------------------------------
6 PATH RULES FOR WINDOWS:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these
Steve Dowered51b262016-09-17 12:54:06 -07009 are actually fetched is different). The presence of a python._pth or
10 pythonXY._pth file alongside the program overrides these rules - see
11 below.
Guido van Rossum88716bb2000-03-30 19:45:39 +000012
13 * Python always adds an empty entry at the start, which corresponds
14 to the current directory.
15
Georg Brandl7eb4b7d2005-07-22 21:49:32 +000016 * If the PYTHONPATH env. var. exists, its entries are added next.
Guido van Rossum88716bb2000-03-30 19:45:39 +000017
18 * We look in the registry for "application paths" - that is, sub-keys
19 under the main PythonPath registry key. These are added next (the
20 order of sub-key processing is undefined).
21 HKEY_CURRENT_USER is searched and added first.
22 HKEY_LOCAL_MACHINE is searched and added next.
23 (Note that all known installers only use HKLM, so HKCU is typically
24 empty)
25
26 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
27 is set, we believe it. Otherwise, we use the path of our host .EXE's
Martin Panterfd13c0f2016-09-10 10:45:28 +000028 to try and locate one of our "landmarks" and deduce our home.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 - If we DO have a Python Home: The relevant sub-directories (Lib,
Zachary Warec4b53af2016-09-09 17:59:49 -070030 DLLs, etc) are based on the Python Home
Guido van Rossum88716bb2000-03-30 19:45:39 +000031 - If we DO NOT have a Python Home, the core Python Path is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 loaded from the registry. This is the main PythonPath key,
Guido van Rossum88716bb2000-03-30 19:45:39 +000033 and both HKLM and HKCU are combined to form the path)
34
35 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
36 specified, and can't locate any Registry entries (ie, we have _nothing_
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 we can assume is a good path), a default path with relative entries is
Zachary Warec4b53af2016-09-09 17:59:49 -070038 used (eg. .\Lib;.\DLLs, etc)
Guido van Rossum88716bb2000-03-30 19:45:39 +000039
40
Steve Dowered51b262016-09-17 12:54:06 -070041 If a '._pth' file exists adjacent to the executable with the same base name
42 (e.g. python._pth adjacent to python.exe) or adjacent to the shared library
43 (e.g. python36._pth adjacent to python36.dll), it is used in preference to
44 the above process. The shared library file takes precedence over the
45 executable. The path file must contain a list of paths to add to sys.path,
46 one per line. Each path is relative to the directory containing the file.
47 Blank lines and comments beginning with '#' are permitted.
48
49 In the presence of this ._pth file, no other paths are added to the search
50 path, the registry finder is not enabled, site.py is not imported and
51 isolated mode is enabled. The site package can be enabled by including a
52 line reading "import site"; no other imports are recognized. Any invalid
53 entry (other than directories that do not exist) will result in immediate
54 termination of the program.
55
Steve Dower4db86bc2016-09-09 09:17:35 -070056
Guido van Rossum88716bb2000-03-30 19:45:39 +000057 The end result of all this is:
58 * When running python.exe, or any other .exe in the main Python directory
59 (either an installed version, or directly from the PCbuild directory),
60 the core path is deduced, and the core paths in the registry are
61 ignored. Other "application paths" in the registry are always read.
62
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 * When Python is hosted in another exe (different directory, embedded via
Guido van Rossum88716bb2000-03-30 19:45:39 +000064 COM, etc), the Python Home will not be deduced, so the core path from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 the registry is used. Other "application paths" in the registry are
Guido van Rossum88716bb2000-03-30 19:45:39 +000066 always read.
67
68 * If Python can't find its home and there is no registry (eg, frozen
69 exe, some very strange installation setup) you get a path with
70 some default, but relative, paths.
71
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000072 * An embedding application can use Py_SetPath() to override all of
Steve Dower4db86bc2016-09-09 09:17:35 -070073 these automatic path computations.
74
Steve Dowered51b262016-09-17 12:54:06 -070075 * An install of Python can fully specify the contents of sys.path using
76 either a 'EXENAME._pth' or 'DLLNAME._pth' file, optionally including
77 "import site" to enable the site module.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000078
Guido van Rossum88716bb2000-03-30 19:45:39 +000079 ---------------------------------------------------------------- */
80
81
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000082#include "Python.h"
Victor Stinner331a6a52019-05-27 16:39:22 +020083#include "pycore_initconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010084#include "pycore_pystate.h"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000086#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087
Steve Dower4db86bc2016-09-09 09:17:35 -070088#ifndef MS_WINDOWS
89#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000090#endif
91
Steve Dower4db86bc2016-09-09 09:17:35 -070092#include <windows.h>
erikjanss6cf82552018-07-25 02:41:46 +020093#include <shlwapi.h>
Steve Dower4db86bc2016-09-09 09:17:35 -070094
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000096#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097#endif /* HAVE_SYS_TYPES_H */
98
99#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000100#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000101#endif /* HAVE_SYS_STAT_H */
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103#include <string.h>
104
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105/* Search in some common locations for the associated Python libraries.
106 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000107 * Py_GetPath() tries to return a sensible Python module search path.
108 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000109 * The approach is an adaptation for Windows of the strategy used in
110 * ../Modules/getpath.c; it uses the Windows Registry as one of its
111 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000112 *
113 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
114 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000115 */
116
117#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000118#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000119#endif
120
Victor Stinner0327bde2017-11-23 17:03:20 +0100121typedef struct {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200122 const wchar_t *path_env; /* PATH environment variable */
123 const wchar_t *home; /* PYTHONHOME environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100124
125 /* Registry key "Software\Python\PythonCore\PythonPath" */
126 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
127 wchar_t *user_path; /* from HKEY_CURRENT_USER */
128
Victor Stinner0327bde2017-11-23 17:03:20 +0100129 wchar_t argv0_path[MAXPATHLEN+1];
130 wchar_t zip_path[MAXPATHLEN+1];
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200131
132 wchar_t *dll_path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100133} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000134
Guido van Rossumeea14491997-08-13 21:30:44 +0000135
Victor Stinner0327bde2017-11-23 17:03:20 +0100136/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000137static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100138is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139{
140#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000144#endif
145}
146
Victor Stinner0327bde2017-11-23 17:03:20 +0100147
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000148/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100149 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000151reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000152{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700153 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100154 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700155 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100156 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 while (i > 0 && !is_sep(dir[i]))
159 --i;
160 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000161}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162
Victor Stinner0327bde2017-11-23 17:03:20 +0100163
Steve Dowered51b262016-09-17 12:54:06 -0700164static int
165change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
166{
167 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
168 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100169 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700170 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100171 }
Steve Dowered51b262016-09-17 12:54:06 -0700172
173 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
174 --i;
175
176 if (i == 0) {
177 dest[0] = '\0';
178 return -1;
179 }
180
Victor Stinner0327bde2017-11-23 17:03:20 +0100181 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700182 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100183 }
Steve Dowered51b262016-09-17 12:54:06 -0700184
185 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100186 wcscat_s(dest, MAXPATHLEN+1, ext))
187 {
Steve Dowered51b262016-09-17 12:54:06 -0700188 dest[0] = '\0';
189 return -1;
190 }
191
192 return 0;
193}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000194
Victor Stinner0327bde2017-11-23 17:03:20 +0100195
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000196static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200197exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000200}
201
Victor Stinner0327bde2017-11-23 17:03:20 +0100202
203/* Is module -- check for .pyc too.
204 Assumes 'filename' MAXPATHLEN+1 bytes long -
205 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000206static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100207ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000208{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100209 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700210
Victor Stinner0327bde2017-11-23 17:03:20 +0100211 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100213 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700216 n = wcsnlen_s(filename, MAXPATHLEN+1);
217 if (n < MAXPATHLEN) {
218 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800219 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700220 filename[n + 1] = L'\0';
221 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100222 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700223 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100224 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700225 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 }
227 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000228}
229
Victor Stinner0327bde2017-11-23 17:03:20 +0100230
Tim Peters8484fbf2004-08-07 19:12:27 +0000231/* Add a path component, by appending stuff to buffer.
232 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
233 NUL-terminated string with no more than MAXPATHLEN characters (not counting
234 the trailing NUL). It's a fatal error if it contains a string longer than
235 that (callers must be careful!). If these requirements are met, it's
236 guaranteed that buffer will still be a NUL-terminated string with no more
237 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
238 stuff as fits will be appended.
239*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700240
241static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100242typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
243 PCWSTR pszPathIn, PCWSTR pszMore,
244 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700245static PPathCchCombineEx _PathCchCombineEx;
246
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000247static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700248join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000249{
Steve Dower4db86bc2016-09-09 09:17:35 -0700250 if (_PathCchCombineEx_Initialized == 0) {
251 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100252 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700253 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100254 }
255 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700256 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100257 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700258 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700260
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100262 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700263 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100264 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700265 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100266 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700267 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100268 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700269 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000270}
271
Steve Dower48e8c822018-02-22 10:39:26 -0800272static int _PathCchCanonicalizeEx_Initialized = 0;
273typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
274 PCWSTR pszPathIn, unsigned long dwFlags);
275static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
276
Victor Stinner331a6a52019-05-27 16:39:22 +0200277static PyStatus canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800278{
279 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200280 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800281 }
282
283 if (_PathCchCanonicalizeEx_Initialized == 0) {
284 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
285 if (pathapi) {
286 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
287 }
288 else {
289 _PathCchCanonicalizeEx = NULL;
290 }
291 _PathCchCanonicalizeEx_Initialized = 1;
292 }
293
294 if (_PathCchCanonicalizeEx) {
295 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200296 return _PyStatus_ERR("buffer overflow in getpathp.c's canonicalize()");
Steve Dower48e8c822018-02-22 10:39:26 -0800297 }
298 }
299 else {
300 if (!PathCanonicalizeW(buffer, path)) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200301 return _PyStatus_ERR("buffer overflow in getpathp.c's canonicalize()");
Steve Dower48e8c822018-02-22 10:39:26 -0800302 }
303 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200304 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800305}
306
Victor Stinner0327bde2017-11-23 17:03:20 +0100307
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000308/* gotlandmark only called by search_for_prefix, which ensures
309 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100310 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000311static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100312gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700315 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700318 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 prefix[n] = '\0';
320 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000321}
322
Victor Stinner0327bde2017-11-23 17:03:20 +0100323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinnerb64de462017-12-01 18:27:09 +0100325 assumption provided by only caller, calculate_path_impl() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000326static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200327search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700330 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100332 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 reduce(prefix);
336 } while (prefix[0]);
337 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000338}
339
Victor Stinner0327bde2017-11-23 17:03:20 +0100340
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000341#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000342
Guido van Rossum88716bb2000-03-30 19:45:39 +0000343/* a string loaded from the DLL at startup.*/
344extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000345
Guido van Rossumeea14491997-08-13 21:30:44 +0000346/* Load a PYTHONPATH value from the registry.
347 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
348
Guido van Rossum88716bb2000-03-30 19:45:39 +0000349 Works in both Unicode and 8bit environments. Only uses the
350 Ex family of functions so it also works with Windows CE.
351
Guido van Rossumeea14491997-08-13 21:30:44 +0000352 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000353
354 XXX - this code is pretty strange, as it used to also
355 work on Win16, where the buffer sizes werent available
356 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000357*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000358static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000359getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 HKEY newKey = 0;
362 DWORD dataSize = 0;
363 DWORD numKeys = 0;
364 LONG rc;
365 wchar_t *retval = NULL;
366 WCHAR *dataBuf = NULL;
367 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
368 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700369 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 DWORD index;
371 WCHAR *keyBuf = NULL;
372 WCHAR *keyBufPtr;
373 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 /* Tried to use sysget("winver") but here is too early :-( */
376 versionLen = strlen(PyWin_DLLVersionString);
377 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700378 keyBufLen = sizeof(keyPrefix) +
379 sizeof(WCHAR)*(versionLen-1) +
380 sizeof(keySuffix);
381 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100382 if (keyBuf==NULL) {
383 goto done;
384 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000385
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700386 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200387 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
389 keyBufPtr += versionLen;
390 /* NULL comes with this one! */
391 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
392 /* Open the root Python key */
393 rc=RegOpenKeyExW(keyBase,
394 keyBuf, /* subkey */
395 0, /* reserved */
396 KEY_READ,
397 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100398 if (rc!=ERROR_SUCCESS) {
399 goto done;
400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 /* Find out how big our core buffer is, and how many subkeys we have */
402 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
403 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100404 if (rc!=ERROR_SUCCESS) {
405 goto done;
406 }
407 if (skipcore) {
408 dataSize = 0; /* Only count core ones if we want them! */
409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* Allocate a temp array of char buffers, so we only need to loop
411 reading the registry once
412 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200413 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100414 if (ppPaths==NULL) {
415 goto done;
416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
418 /* Loop over all subkeys, allocating a temp sub-buffer. */
419 for(index=0;index<numKeys;index++) {
420 WCHAR keyBuf[MAX_PATH+1];
421 HKEY subKey = 0;
422 DWORD reqdSize = MAX_PATH+1;
423 /* Get the sub-key name */
424 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
425 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100426 if (rc!=ERROR_SUCCESS) {
427 goto done;
428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 /* Open the sub-key */
430 rc=RegOpenKeyExW(newKey,
431 keyBuf, /* subkey */
432 0, /* reserved */
433 KEY_READ,
434 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100435 if (rc!=ERROR_SUCCESS) {
436 goto done;
437 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 /* Find the value of the buffer size, malloc, then read it */
439 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
440 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200441 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (ppPaths[index]) {
443 RegQueryValueExW(subKey, NULL, 0, NULL,
444 (LPBYTE)ppPaths[index],
445 &reqdSize);
446 dataSize += reqdSize + 1; /* 1 for the ";" */
447 }
448 }
449 RegCloseKey(subKey);
450 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100453 if (dataSize == 0) {
454 goto done;
455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456
457 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200458 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (dataBuf) {
460 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* Copy our collected strings */
462 for (index=0;index<numKeys;index++) {
463 if (index > 0) {
464 *(szCur++) = L';';
465 dataSize--;
466 }
467 if (ppPaths[index]) {
468 Py_ssize_t len = wcslen(ppPaths[index]);
469 wcsncpy(szCur, ppPaths[index], len);
470 szCur += len;
471 assert(dataSize > (DWORD)len);
472 dataSize -= (DWORD)len;
473 }
474 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100475 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100477 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 else {
luzpaza5293b42017-11-05 07:37:50 -0600479 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (numKeys) {
481 *(szCur++) = L';';
482 dataSize--;
483 }
484 /* Now append the core path entries -
485 this will include the NULL
486 */
487 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
488 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200489 if (rc != ERROR_SUCCESS) {
490 PyMem_RawFree(dataBuf);
491 goto done;
492 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 /* And set the result - caller must free */
495 retval = dataBuf;
496 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000497done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 /* Loop freeing my temp buffers */
499 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200500 for(index=0; index<numKeys; index++)
501 PyMem_RawFree(ppPaths[index]);
502 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100504 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100506 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200507 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000509}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000510#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000511
Victor Stinner0327bde2017-11-23 17:03:20 +0100512
Victor Stinner410759f2019-05-18 04:17:01 +0200513wchar_t*
514_Py_GetDLLPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000515{
Victor Stinner9316ee42017-11-25 03:17:57 +0100516 wchar_t dll_path[MAXPATHLEN+1];
517 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000518
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000519#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100521 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100522 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
523 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100524 }
525 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000526#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100527 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000528#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100529
Victor Stinner410759f2019-05-18 04:17:01 +0200530 return _PyMem_RawWcsdup(dll_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100531}
532
533
Victor Stinner331a6a52019-05-27 16:39:22 +0200534static PyStatus
535get_program_full_path(const PyConfig *config,
536 PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100537{
Steve Dower1c3de542018-12-10 08:11:21 -0800538 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100539 wchar_t program_full_path[MAXPATHLEN+1];
540 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100541
Steve Dower323e7432019-06-29 14:28:59 -0700542 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
543 /* GetModuleFileName should never fail when passed NULL */
544 return _PyStatus_ERR("Cannot determine program path");
545 }
546
Steve Dower1c3de542018-12-10 08:11:21 -0800547 /* The launcher may need to force the executable path to a
548 * different environment, so override it here. */
549 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
550 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower323e7432019-06-29 14:28:59 -0700551 /* If overridden, preserve the original full path */
552 pathconfig->base_executable = PyMem_RawMalloc(
553 sizeof(wchar_t) * (MAXPATHLEN + 1));
554 PyStatus status = canonicalize(pathconfig->base_executable,
555 program_full_path);
556 if (_PyStatus_EXCEPTION(status)) {
557 return status;
558 }
559
Steve Dower1c3de542018-12-10 08:11:21 -0800560 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower323e7432019-06-29 14:28:59 -0700561 /* bpo-35873: Clear the environment variable to avoid it being
562 * inherited by child processes. */
563 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100564 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000565
Victor Stinner331a6a52019-05-27 16:39:22 +0200566 pathconfig->program_full_path = PyMem_RawMalloc(
Steve Dower48e8c822018-02-22 10:39:26 -0800567 sizeof(wchar_t) * (MAXPATHLEN + 1));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000568
Victor Stinner331a6a52019-05-27 16:39:22 +0200569 return canonicalize(pathconfig->program_full_path,
Steve Dower48e8c822018-02-22 10:39:26 -0800570 program_full_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000571}
572
Victor Stinner0327bde2017-11-23 17:03:20 +0100573
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100574static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200575read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path)
Steve Dower4db86bc2016-09-09 09:17:35 -0700576{
577 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100578 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100579 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100580 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700581
Steve Dowered51b262016-09-17 12:54:06 -0700582 wcscpy_s(prefix, MAXPATHLEN+1, path);
583 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200584 pathconfig->isolated = 1;
585 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700586
Steve Dower4db86bc2016-09-09 09:17:35 -0700587 size_t bufsiz = MAXPATHLEN;
588 size_t prefixlen = wcslen(prefix);
589
590 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700591 if (buf == NULL) {
592 goto error;
593 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700594 buf[0] = '\0';
595
596 while (!feof(sp_file)) {
597 char line[MAXPATHLEN + 1];
598 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100599 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700600 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100601 }
602 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700603 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100604 }
Steve Dowered51b262016-09-17 12:54:06 -0700605 while (*++p) {
606 if (*p == '\r' || *p == '\n') {
607 *p = '\0';
608 break;
609 }
610 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700611
Steve Dowered51b262016-09-17 12:54:06 -0700612 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200613 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700614 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200615 }
616 else if (strncmp(line, "import ", 7) == 0) {
Steve Dowered51b262016-09-17 12:54:06 -0700617 Py_FatalError("only 'import site' is supported in ._pth file");
618 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700619
Steve Dowered51b262016-09-17 12:54:06 -0700620 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700621 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700622 if (wline == NULL) {
623 goto error;
624 }
Steve Dowered51b262016-09-17 12:54:06 -0700625 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700626 wline[wn] = '\0';
627
Steve Dowerc6dd4152016-10-27 14:28:07 -0700628 size_t usedsiz = wcslen(buf);
629 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700630 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700631 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
632 sizeof(wchar_t));
633 if (tmp == NULL) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700634 PyMem_RawFree(wline);
635 goto error;
636 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700637 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700638 }
639
Steve Dowerc6dd4152016-10-27 14:28:07 -0700640 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700641 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700642 usedsiz += 1;
643 }
Steve Dowered51b262016-09-17 12:54:06 -0700644
Steve Dowerc6dd4152016-10-27 14:28:07 -0700645 errno_t result;
646 _Py_BEGIN_SUPPRESS_IPH
647 result = wcscat_s(buf, bufsiz, prefix);
648 _Py_END_SUPPRESS_IPH
649 if (result == EINVAL) {
650 Py_FatalError("invalid argument during ._pth processing");
651 } else if (result == ERANGE) {
652 Py_FatalError("buffer overflow during ._pth processing");
653 }
654 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700655 join(b, wline);
656
657 PyMem_RawFree(wline);
658 }
659
Steve Dower4db86bc2016-09-09 09:17:35 -0700660 fclose(sp_file);
Victor Stinner331a6a52019-05-27 16:39:22 +0200661 pathconfig->module_search_path = buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100662 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700663
664error:
665 PyMem_RawFree(buf);
666 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100667 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700668}
669
670
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200671static PyStatus
Victor Stinner0327bde2017-11-23 17:03:20 +0100672calculate_init(PyCalculatePath *calculate,
Victor Stinner331a6a52019-05-27 16:39:22 +0200673 const PyConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000674{
Victor Stinner331a6a52019-05-27 16:39:22 +0200675 calculate->home = config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100676 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200677
678 calculate->dll_path = _Py_GetDLLPath();
679 if (calculate->dll_path == NULL) {
680 return _PyStatus_NO_MEMORY();
681 }
682
683 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100684}
685
686
687static int
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200688get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
689 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100690{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200691 if (calculate->dll_path[0]) {
692 if (!change_ext(filename, calculate->dll_path, L"._pth") &&
693 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100694 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100695 return 1;
696 }
697 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200698 if (pathconfig->program_full_path[0]) {
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200699 if (!change_ext(filename, pathconfig->program_full_path, L"._pth") &&
700 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100701 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100702 return 1;
703 }
704 }
705 return 0;
706}
707
708
709static int
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200710calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
711 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100712{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200713 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100714
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200715 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100716 return 0;
717 }
718
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200719 return read_pth_file(pathconfig, prefix, filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100720}
721
722
723/* Search for an environment configuration file, first in the
724 executable's directory and then in the parent directory.
725 If found, open it for use when searching for prefixes.
726*/
727static void
728calculate_pyvenv_file(PyCalculatePath *calculate)
729{
730 wchar_t envbuffer[MAXPATHLEN+1];
731 const wchar_t *env_cfg = L"pyvenv.cfg";
732
733 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
734 join(envbuffer, env_cfg);
735
736 FILE *env_file = _Py_wfopen(envbuffer, L"r");
737 if (env_file == NULL) {
738 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100739
Victor Stinner0327bde2017-11-23 17:03:20 +0100740 reduce(envbuffer);
741 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700742 join(envbuffer, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100743
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700744 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100745 if (env_file == NULL) {
746 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100747 }
748 }
749
Victor Stinner0327bde2017-11-23 17:03:20 +0100750 if (env_file == NULL) {
751 return;
752 }
753
754 /* Look for a 'home' variable and set argv0_path to it, if found */
755 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100756 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100757 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
758 }
759 fclose(env_file);
760}
761
762
Victor Stinner331a6a52019-05-27 16:39:22 +0200763#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
Victor Stinner9316ee42017-11-25 03:17:57 +0100764
765
Victor Stinner0327bde2017-11-23 17:03:20 +0100766static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100767calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100768{
Victor Stinner0327bde2017-11-23 17:03:20 +0100769 if (calculate->home == NULL || *calculate->home == '\0') {
770 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100771 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
772 reduce(prefix);
773 calculate->home = prefix;
774 }
775 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
776 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100777 }
778 else {
779 calculate->home = NULL;
780 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100782 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100783 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100784 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100785}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000786
Victor Stinner9316ee42017-11-25 03:17:57 +0100787
Victor Stinner331a6a52019-05-27 16:39:22 +0200788static PyStatus
789calculate_module_search_path(const PyConfig *config,
790 PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner31a83932017-12-04 13:39:15 +0100791 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100792{
Victor Stinner0327bde2017-11-23 17:03:20 +0100793 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000794#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100795 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
796 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000797#endif
luzpaza5293b42017-11-05 07:37:50 -0600798 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 anything better to use! */
Victor Stinner331a6a52019-05-27 16:39:22 +0200800 int skipdefault = (config->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100801 calculate->home != NULL ||
802 calculate->machine_path != NULL ||
803 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* We need to construct a path from the following parts.
806 (1) the PYTHONPATH environment variable, if set;
807 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100808 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100810 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 (5) the directory containing the executable (argv0_path).
812 The length calculation calculates #4 first.
813 Extra rules:
814 - If PYTHONHOME is set (in any way) item (3) is ignored.
815 - If registry values are used, (4) and (5) are ignored.
816 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100819 size_t bufsz = 0;
820 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200821 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 bufsz = 1;
823 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100824 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100826 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100828 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700830 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100831 bufsz += wcslen(calculate->argv0_path) + 1;
832 if (calculate->user_path) {
833 bufsz += wcslen(calculate->user_path) + 1;
834 }
835 if (calculate->machine_path) {
836 bufsz += wcslen(calculate->machine_path) + 1;
837 }
838 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200839 if (config->pythonpath_env != NULL) {
840 bufsz += wcslen(config->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100841 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000842
Victor Stinner0327bde2017-11-23 17:03:20 +0100843 wchar_t *buf, *start_buf;
844 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 if (buf == NULL) {
846 /* We can't exit, so print a warning and limp along */
847 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200848 if (config->pythonpath_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200850 pathconfig->module_search_path = config->pythonpath_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 }
852 else {
853 fprintf(stderr, "Using default static path.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200854 pathconfig->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200856 return _PyStatus_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100858 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000859
Victor Stinner331a6a52019-05-27 16:39:22 +0200860 if (config->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100861 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner331a6a52019-05-27 16:39:22 +0200862 config->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100863 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 buf = wcschr(buf, L'\0');
866 *buf++ = DELIM;
867 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100868 if (calculate->zip_path[0]) {
869 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100870 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100871 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 buf = wcschr(buf, L'\0');
873 *buf++ = DELIM;
874 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100875 if (calculate->user_path) {
876 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100877 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 buf = wcschr(buf, L'\0');
880 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100882 if (calculate->machine_path) {
883 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100884 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 buf = wcschr(buf, L'\0');
887 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100889 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100892 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100893 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700895 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700897 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200898 const wchar_t *p = PYTHONPATH;
899 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 size_t n;
901 for (;;) {
902 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100903 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 }
906 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100910 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100911 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 buf = wcschr(buf, L'\0');
914 p++;
915 n--;
916 }
917 wcsncpy(buf, p, n);
918 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700919 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100920 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100922 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 p = q+1;
924 }
925 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100926 if (calculate->argv0_path) {
927 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700929 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700931 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 /* Now to pull one last hack/trick. If sys.prefix is
934 empty, then try and find it somewhere on the paths
935 we calculated. We scan backwards, as our general policy
936 is that Python core directories are at the *end* of
937 sys.path. We assume that our "lib" directory is
938 on the path, and that our 'prefix' directory is
939 the parent of that.
940 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100941 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200943 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 while (1) {
945 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200946 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 /* 'look' will end up one character before the
948 start of the path in question - even if this
949 is one character before the start of the buffer
950 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100951 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 look--;
953 nchars = lookEnd-look;
954 wcsncpy(lookBuf, look+1, nchars);
955 lookBuf[nchars] = L'\0';
956 /* Up one level to the parent */
957 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100958 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 break;
960 }
961 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100962 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 look--;
966 }
967 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100968
Victor Stinner331a6a52019-05-27 16:39:22 +0200969 pathconfig->module_search_path = start_buf;
970 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100971}
972
973
Victor Stinner331a6a52019-05-27 16:39:22 +0200974static PyStatus
975calculate_path_impl(const PyConfig *config,
976 PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100977{
Victor Stinner331a6a52019-05-27 16:39:22 +0200978 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100979
Victor Stinner331a6a52019-05-27 16:39:22 +0200980 status = get_program_full_path(config, calculate, pathconfig);
981 if (_PyStatus_EXCEPTION(status)) {
982 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100983 }
984
Victor Stinnerb64de462017-12-01 18:27:09 +0100985 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner331a6a52019-05-27 16:39:22 +0200986 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100987 reduce(calculate->argv0_path);
988
989 wchar_t prefix[MAXPATHLEN+1];
990 memset(prefix, 0, sizeof(prefix));
991
992 /* Search for a sys.path file */
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200993 if (calculate_pth_file(calculate, pathconfig, prefix)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100994 goto done;
995 }
996
997 calculate_pyvenv_file(calculate);
998
999 /* Calculate zip archive path from DLL or exe path */
1000 change_ext(calculate->zip_path,
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001001 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001002 L".zip");
1003
1004 calculate_home_prefix(calculate, prefix);
1005
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001006 if (pathconfig->module_search_path == NULL) {
1007 status = calculate_module_search_path(config, calculate,
1008 pathconfig, prefix);
1009 if (_PyStatus_EXCEPTION(status)) {
1010 return status;
1011 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001012 }
1013
1014done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001015 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1016 if (pathconfig->prefix == NULL) {
1017 return _PyStatus_NO_MEMORY();
Victor Stinner9316ee42017-11-25 03:17:57 +01001018 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001019 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1020 if (pathconfig->exec_prefix == NULL) {
1021 return _PyStatus_NO_MEMORY();
Steve Dower177a41a2018-11-17 20:41:48 -08001022 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001023
Victor Stinner331a6a52019-05-27 16:39:22 +02001024 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001025}
1026
1027
Victor Stinner0327bde2017-11-23 17:03:20 +01001028static void
1029calculate_free(PyCalculatePath *calculate)
1030{
1031 PyMem_RawFree(calculate->machine_path);
1032 PyMem_RawFree(calculate->user_path);
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001033 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001034}
1035
Victor Stinner9316ee42017-11-25 03:17:57 +01001036
Victor Stinner331a6a52019-05-27 16:39:22 +02001037PyStatus
1038_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001039{
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001040 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001041 PyCalculatePath calculate;
1042 memset(&calculate, 0, sizeof(calculate));
1043
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001044 status = calculate_init(&calculate, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001045 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001046 goto done;
1047 }
1048
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001049 status = calculate_path_impl(config, &calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001050
Victor Stinner9316ee42017-11-25 03:17:57 +01001051done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001052 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001053 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001054}
1055
1056
Victor Stinner63941882011-09-29 00:42:28 +02001057/* Load python3.dll before loading any extension module that might refer
1058 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001059 to this python DLL is loaded, not a python3.dll that might be on the path
1060 by chance.
1061 Return whether the DLL was found.
1062*/
1063static int python3_checked = 0;
1064static HANDLE hPython3;
1065int
Victor Stinner31a83932017-12-04 13:39:15 +01001066_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001067{
1068 wchar_t py3path[MAXPATHLEN+1];
1069 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001070 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001071 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001072 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001073 python3_checked = 1;
1074
1075 /* If there is a python3.dll next to the python3y.dll,
1076 assume this is a build tree; use that DLL */
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001077 if (_Py_dll_path != NULL) {
1078 wcscpy(py3path, _Py_dll_path);
1079 }
1080 else {
1081 wcscpy(py3path, L"");
1082 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001083 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001084 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001085 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001086 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001087 wcscpy(s, L"\\python3.dll");
1088 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001089 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001090 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001091 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001092
1093 /* Check sys.prefix\DLLs\python3.dll */
1094 wcscpy(py3path, Py_GetPrefix());
1095 wcscat(py3path, L"\\DLLs\\python3.dll");
1096 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1097 return hPython3 != NULL;
1098}