blob: 888bef1146fd646816019945ee10b892f4977fb0 [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
Victor Stinnerc5c64252019-09-23 15:59:00 +0200535get_program_full_path(_PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100536{
Steve Dower1c3de542018-12-10 08:11:21 -0800537 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100538 wchar_t program_full_path[MAXPATHLEN+1];
539 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100540
Steve Dower323e7432019-06-29 14:28:59 -0700541 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
542 /* GetModuleFileName should never fail when passed NULL */
543 return _PyStatus_ERR("Cannot determine program path");
544 }
545
Steve Dower1c3de542018-12-10 08:11:21 -0800546 /* The launcher may need to force the executable path to a
547 * different environment, so override it here. */
548 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
549 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower323e7432019-06-29 14:28:59 -0700550 /* If overridden, preserve the original full path */
551 pathconfig->base_executable = PyMem_RawMalloc(
552 sizeof(wchar_t) * (MAXPATHLEN + 1));
553 PyStatus status = canonicalize(pathconfig->base_executable,
554 program_full_path);
555 if (_PyStatus_EXCEPTION(status)) {
556 return status;
557 }
558
Steve Dower1c3de542018-12-10 08:11:21 -0800559 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower323e7432019-06-29 14:28:59 -0700560 /* bpo-35873: Clear the environment variable to avoid it being
561 * inherited by child processes. */
562 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100563 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000564
Victor Stinner331a6a52019-05-27 16:39:22 +0200565 pathconfig->program_full_path = PyMem_RawMalloc(
Steve Dower48e8c822018-02-22 10:39:26 -0800566 sizeof(wchar_t) * (MAXPATHLEN + 1));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000567
Victor Stinner331a6a52019-05-27 16:39:22 +0200568 return canonicalize(pathconfig->program_full_path,
Steve Dower48e8c822018-02-22 10:39:26 -0800569 program_full_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000570}
571
Victor Stinner0327bde2017-11-23 17:03:20 +0100572
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100573static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200574read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path)
Steve Dower4db86bc2016-09-09 09:17:35 -0700575{
576 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100577 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100578 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100579 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700580
Steve Dowered51b262016-09-17 12:54:06 -0700581 wcscpy_s(prefix, MAXPATHLEN+1, path);
582 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200583 pathconfig->isolated = 1;
584 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700585
Steve Dower4db86bc2016-09-09 09:17:35 -0700586 size_t bufsiz = MAXPATHLEN;
587 size_t prefixlen = wcslen(prefix);
588
589 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700590 if (buf == NULL) {
591 goto error;
592 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700593 buf[0] = '\0';
594
595 while (!feof(sp_file)) {
596 char line[MAXPATHLEN + 1];
597 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100598 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700599 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100600 }
601 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700602 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100603 }
Steve Dowered51b262016-09-17 12:54:06 -0700604 while (*++p) {
605 if (*p == '\r' || *p == '\n') {
606 *p = '\0';
607 break;
608 }
609 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700610
Steve Dowered51b262016-09-17 12:54:06 -0700611 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200612 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700613 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200614 }
615 else if (strncmp(line, "import ", 7) == 0) {
Steve Dowered51b262016-09-17 12:54:06 -0700616 Py_FatalError("only 'import site' is supported in ._pth file");
617 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700618
Steve Dowered51b262016-09-17 12:54:06 -0700619 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700620 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700621 if (wline == NULL) {
622 goto error;
623 }
Steve Dowered51b262016-09-17 12:54:06 -0700624 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700625 wline[wn] = '\0';
626
Steve Dowerc6dd4152016-10-27 14:28:07 -0700627 size_t usedsiz = wcslen(buf);
628 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700629 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700630 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
631 sizeof(wchar_t));
632 if (tmp == NULL) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700633 PyMem_RawFree(wline);
634 goto error;
635 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700636 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700637 }
638
Steve Dowerc6dd4152016-10-27 14:28:07 -0700639 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700640 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700641 usedsiz += 1;
642 }
Steve Dowered51b262016-09-17 12:54:06 -0700643
Steve Dowerc6dd4152016-10-27 14:28:07 -0700644 errno_t result;
645 _Py_BEGIN_SUPPRESS_IPH
646 result = wcscat_s(buf, bufsiz, prefix);
647 _Py_END_SUPPRESS_IPH
648 if (result == EINVAL) {
649 Py_FatalError("invalid argument during ._pth processing");
650 } else if (result == ERANGE) {
651 Py_FatalError("buffer overflow during ._pth processing");
652 }
653 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700654 join(b, wline);
655
656 PyMem_RawFree(wline);
657 }
658
Steve Dower4db86bc2016-09-09 09:17:35 -0700659 fclose(sp_file);
Victor Stinner331a6a52019-05-27 16:39:22 +0200660 pathconfig->module_search_path = buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100661 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700662
663error:
664 PyMem_RawFree(buf);
665 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100666 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700667}
668
669
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200670static PyStatus
Victor Stinner0327bde2017-11-23 17:03:20 +0100671calculate_init(PyCalculatePath *calculate,
Victor Stinner331a6a52019-05-27 16:39:22 +0200672 const PyConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000673{
Victor Stinner331a6a52019-05-27 16:39:22 +0200674 calculate->home = config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100675 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200676
677 calculate->dll_path = _Py_GetDLLPath();
678 if (calculate->dll_path == NULL) {
679 return _PyStatus_NO_MEMORY();
680 }
681
682 return _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100683}
684
685
686static int
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200687get_pth_filename(PyCalculatePath *calculate, wchar_t *filename,
688 const _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100689{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200690 if (calculate->dll_path[0]) {
691 if (!change_ext(filename, calculate->dll_path, L"._pth") &&
692 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100693 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100694 return 1;
695 }
696 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200697 if (pathconfig->program_full_path[0]) {
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200698 if (!change_ext(filename, pathconfig->program_full_path, L"._pth") &&
699 exists(filename))
Victor Stinner31a83932017-12-04 13:39:15 +0100700 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100701 return 1;
702 }
703 }
704 return 0;
705}
706
707
708static int
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200709calculate_pth_file(PyCalculatePath *calculate, _PyPathConfig *pathconfig,
710 wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100711{
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200712 wchar_t filename[MAXPATHLEN+1];
Victor Stinner0327bde2017-11-23 17:03:20 +0100713
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200714 if (!get_pth_filename(calculate, filename, pathconfig)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100715 return 0;
716 }
717
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200718 return read_pth_file(pathconfig, prefix, filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100719}
720
721
722/* Search for an environment configuration file, first in the
723 executable's directory and then in the parent directory.
724 If found, open it for use when searching for prefixes.
725*/
726static void
727calculate_pyvenv_file(PyCalculatePath *calculate)
728{
729 wchar_t envbuffer[MAXPATHLEN+1];
730 const wchar_t *env_cfg = L"pyvenv.cfg";
731
732 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
733 join(envbuffer, env_cfg);
734
735 FILE *env_file = _Py_wfopen(envbuffer, L"r");
736 if (env_file == NULL) {
737 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100738
Victor Stinner0327bde2017-11-23 17:03:20 +0100739 reduce(envbuffer);
740 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700741 join(envbuffer, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100742
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700743 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100744 if (env_file == NULL) {
745 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100746 }
747 }
748
Victor Stinner0327bde2017-11-23 17:03:20 +0100749 if (env_file == NULL) {
750 return;
751 }
752
753 /* Look for a 'home' variable and set argv0_path to it, if found */
754 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100755 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100756 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
757 }
758 fclose(env_file);
759}
760
761
Victor Stinner331a6a52019-05-27 16:39:22 +0200762#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
Victor Stinner9316ee42017-11-25 03:17:57 +0100763
764
Victor Stinner0327bde2017-11-23 17:03:20 +0100765static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100766calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100767{
Victor Stinner0327bde2017-11-23 17:03:20 +0100768 if (calculate->home == NULL || *calculate->home == '\0') {
769 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100770 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
771 reduce(prefix);
772 calculate->home = prefix;
773 }
774 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
775 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100776 }
777 else {
778 calculate->home = NULL;
779 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100781 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100782 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100783 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100784}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000785
Victor Stinner9316ee42017-11-25 03:17:57 +0100786
Victor Stinner331a6a52019-05-27 16:39:22 +0200787static PyStatus
788calculate_module_search_path(const PyConfig *config,
789 PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner31a83932017-12-04 13:39:15 +0100790 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100791{
Victor Stinner0327bde2017-11-23 17:03:20 +0100792 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000793#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100794 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
795 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000796#endif
luzpaza5293b42017-11-05 07:37:50 -0600797 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 anything better to use! */
Victor Stinner331a6a52019-05-27 16:39:22 +0200799 int skipdefault = (config->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100800 calculate->home != NULL ||
801 calculate->machine_path != NULL ||
802 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 /* We need to construct a path from the following parts.
805 (1) the PYTHONPATH environment variable, if set;
806 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100807 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100809 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 (5) the directory containing the executable (argv0_path).
811 The length calculation calculates #4 first.
812 Extra rules:
813 - If PYTHONHOME is set (in any way) item (3) is ignored.
814 - If registry values are used, (4) and (5) are ignored.
815 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100818 size_t bufsz = 0;
819 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200820 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 bufsz = 1;
822 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100823 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100825 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100827 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700829 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100830 bufsz += wcslen(calculate->argv0_path) + 1;
831 if (calculate->user_path) {
832 bufsz += wcslen(calculate->user_path) + 1;
833 }
834 if (calculate->machine_path) {
835 bufsz += wcslen(calculate->machine_path) + 1;
836 }
837 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200838 if (config->pythonpath_env != NULL) {
839 bufsz += wcslen(config->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100840 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000841
Victor Stinner0327bde2017-11-23 17:03:20 +0100842 wchar_t *buf, *start_buf;
843 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 if (buf == NULL) {
845 /* We can't exit, so print a warning and limp along */
846 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200847 if (config->pythonpath_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200849 pathconfig->module_search_path = config->pythonpath_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 }
851 else {
852 fprintf(stderr, "Using default static path.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200853 pathconfig->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200855 return _PyStatus_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000858
Victor Stinner331a6a52019-05-27 16:39:22 +0200859 if (config->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100860 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner331a6a52019-05-27 16:39:22 +0200861 config->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100862 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 buf = wcschr(buf, L'\0');
865 *buf++ = DELIM;
866 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100867 if (calculate->zip_path[0]) {
868 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100869 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100870 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 buf = wcschr(buf, L'\0');
872 *buf++ = DELIM;
873 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 if (calculate->user_path) {
875 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100876 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 buf = wcschr(buf, L'\0');
879 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100881 if (calculate->machine_path) {
882 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100883 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100884 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 buf = wcschr(buf, L'\0');
886 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100888 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100890 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100891 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700894 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700896 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200897 const wchar_t *p = PYTHONPATH;
898 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 size_t n;
900 for (;;) {
901 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100904 }
905 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100909 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100910 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 buf = wcschr(buf, L'\0');
913 p++;
914 n--;
915 }
916 wcsncpy(buf, p, n);
917 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700918 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100919 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100921 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 p = q+1;
923 }
924 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100925 if (calculate->argv0_path) {
926 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700928 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700930 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* Now to pull one last hack/trick. If sys.prefix is
933 empty, then try and find it somewhere on the paths
934 we calculated. We scan backwards, as our general policy
935 is that Python core directories are at the *end* of
936 sys.path. We assume that our "lib" directory is
937 on the path, and that our 'prefix' directory is
938 the parent of that.
939 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100940 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200942 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 while (1) {
944 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200945 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 /* 'look' will end up one character before the
947 start of the path in question - even if this
948 is one character before the start of the buffer
949 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100950 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 look--;
952 nchars = lookEnd-look;
953 wcsncpy(lookBuf, look+1, nchars);
954 lookBuf[nchars] = L'\0';
955 /* Up one level to the parent */
956 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100957 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 break;
959 }
960 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100961 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100963 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 look--;
965 }
966 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100967
Victor Stinner331a6a52019-05-27 16:39:22 +0200968 pathconfig->module_search_path = start_buf;
969 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100970}
971
972
Victor Stinner331a6a52019-05-27 16:39:22 +0200973static PyStatus
974calculate_path_impl(const PyConfig *config,
975 PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100976{
Victor Stinner331a6a52019-05-27 16:39:22 +0200977 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100978
Victor Stinnerc5c64252019-09-23 15:59:00 +0200979 status = get_program_full_path(pathconfig);
Victor Stinner331a6a52019-05-27 16:39:22 +0200980 if (_PyStatus_EXCEPTION(status)) {
981 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100982 }
983
Victor Stinnerb64de462017-12-01 18:27:09 +0100984 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner331a6a52019-05-27 16:39:22 +0200985 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100986 reduce(calculate->argv0_path);
987
988 wchar_t prefix[MAXPATHLEN+1];
989 memset(prefix, 0, sizeof(prefix));
990
991 /* Search for a sys.path file */
Victor Stinner9f3dcf82019-09-21 02:13:14 +0200992 if (calculate_pth_file(calculate, pathconfig, prefix)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100993 goto done;
994 }
995
996 calculate_pyvenv_file(calculate);
997
998 /* Calculate zip archive path from DLL or exe path */
999 change_ext(calculate->zip_path,
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001000 calculate->dll_path[0] ? calculate->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +01001001 L".zip");
1002
1003 calculate_home_prefix(calculate, prefix);
1004
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001005 if (pathconfig->module_search_path == NULL) {
1006 status = calculate_module_search_path(config, calculate,
1007 pathconfig, prefix);
1008 if (_PyStatus_EXCEPTION(status)) {
1009 return status;
1010 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001011 }
1012
1013done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001014 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1015 if (pathconfig->prefix == NULL) {
1016 return _PyStatus_NO_MEMORY();
Victor Stinner9316ee42017-11-25 03:17:57 +01001017 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001018 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1019 if (pathconfig->exec_prefix == NULL) {
1020 return _PyStatus_NO_MEMORY();
Steve Dower177a41a2018-11-17 20:41:48 -08001021 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001022
Victor Stinner331a6a52019-05-27 16:39:22 +02001023 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001024}
1025
1026
Victor Stinner0327bde2017-11-23 17:03:20 +01001027static void
1028calculate_free(PyCalculatePath *calculate)
1029{
1030 PyMem_RawFree(calculate->machine_path);
1031 PyMem_RawFree(calculate->user_path);
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001032 PyMem_RawFree(calculate->dll_path);
Victor Stinner0327bde2017-11-23 17:03:20 +01001033}
1034
Victor Stinner9316ee42017-11-25 03:17:57 +01001035
Victor Stinner331a6a52019-05-27 16:39:22 +02001036PyStatus
1037_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001038{
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001039 PyStatus status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001040 PyCalculatePath calculate;
1041 memset(&calculate, 0, sizeof(calculate));
1042
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001043 status = calculate_init(&calculate, config);
Victor Stinner331a6a52019-05-27 16:39:22 +02001044 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001045 goto done;
1046 }
1047
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001048 status = calculate_path_impl(config, &calculate, pathconfig);
Victor Stinner0327bde2017-11-23 17:03:20 +01001049
Victor Stinner9316ee42017-11-25 03:17:57 +01001050done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001051 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001052 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001053}
1054
1055
Victor Stinner63941882011-09-29 00:42:28 +02001056/* Load python3.dll before loading any extension module that might refer
1057 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001058 to this python DLL is loaded, not a python3.dll that might be on the path
1059 by chance.
1060 Return whether the DLL was found.
1061*/
1062static int python3_checked = 0;
1063static HANDLE hPython3;
1064int
Victor Stinner31a83932017-12-04 13:39:15 +01001065_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001066{
1067 wchar_t py3path[MAXPATHLEN+1];
1068 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001069 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001070 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001071 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001072 python3_checked = 1;
1073
1074 /* If there is a python3.dll next to the python3y.dll,
1075 assume this is a build tree; use that DLL */
Victor Stinner9f3dcf82019-09-21 02:13:14 +02001076 if (_Py_dll_path != NULL) {
1077 wcscpy(py3path, _Py_dll_path);
1078 }
1079 else {
1080 wcscpy(py3path, L"");
1081 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001082 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001083 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001084 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001085 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001086 wcscpy(s, L"\\python3.dll");
1087 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001088 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001089 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001090 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001091
1092 /* Check sys.prefix\DLLs\python3.dll */
1093 wcscpy(py3path, Py_GetPrefix());
1094 wcscat(py3path, L"\\DLLs\\python3.dll");
1095 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1096 return hPython3 != NULL;
1097}