blob: 7465d314b04398fcc7c9ceabbf4155313f67aa9b [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];
131} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000132
Guido van Rossumeea14491997-08-13 21:30:44 +0000133
Victor Stinner0327bde2017-11-23 17:03:20 +0100134/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000135static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100136is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000137{
138#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000140#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000142#endif
143}
144
Victor Stinner0327bde2017-11-23 17:03:20 +0100145
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000146/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100147 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000148static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000149reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700151 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100152 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700153 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100154 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 while (i > 0 && !is_sep(dir[i]))
157 --i;
158 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000159}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160
Victor Stinner0327bde2017-11-23 17:03:20 +0100161
Steve Dowered51b262016-09-17 12:54:06 -0700162static int
163change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
164{
165 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
166 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100167 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700168 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100169 }
Steve Dowered51b262016-09-17 12:54:06 -0700170
171 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
172 --i;
173
174 if (i == 0) {
175 dest[0] = '\0';
176 return -1;
177 }
178
Victor Stinner0327bde2017-11-23 17:03:20 +0100179 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700180 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100181 }
Steve Dowered51b262016-09-17 12:54:06 -0700182
183 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100184 wcscat_s(dest, MAXPATHLEN+1, ext))
185 {
Steve Dowered51b262016-09-17 12:54:06 -0700186 dest[0] = '\0';
187 return -1;
188 }
189
190 return 0;
191}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000192
Victor Stinner0327bde2017-11-23 17:03:20 +0100193
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000194static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200195exists(const wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000198}
199
Victor Stinner0327bde2017-11-23 17:03:20 +0100200
201/* Is module -- check for .pyc too.
202 Assumes 'filename' MAXPATHLEN+1 bytes long -
203 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000204static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100205ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000206{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100207 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700208
Victor Stinner0327bde2017-11-23 17:03:20 +0100209 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100211 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700214 n = wcsnlen_s(filename, MAXPATHLEN+1);
215 if (n < MAXPATHLEN) {
216 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800217 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700218 filename[n + 1] = L'\0';
219 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100220 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700221 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100222 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700223 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 }
225 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000226}
227
Victor Stinner0327bde2017-11-23 17:03:20 +0100228
Tim Peters8484fbf2004-08-07 19:12:27 +0000229/* Add a path component, by appending stuff to buffer.
230 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
231 NUL-terminated string with no more than MAXPATHLEN characters (not counting
232 the trailing NUL). It's a fatal error if it contains a string longer than
233 that (callers must be careful!). If these requirements are met, it's
234 guaranteed that buffer will still be a NUL-terminated string with no more
235 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
236 stuff as fits will be appended.
237*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700238
239static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100240typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
241 PCWSTR pszPathIn, PCWSTR pszMore,
242 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700243static PPathCchCombineEx _PathCchCombineEx;
244
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000245static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700246join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000247{
Steve Dower4db86bc2016-09-09 09:17:35 -0700248 if (_PathCchCombineEx_Initialized == 0) {
249 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100250 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700251 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100252 }
253 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700254 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100255 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700256 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700258
Steve Dower4db86bc2016-09-09 09:17:35 -0700259 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100260 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100262 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700263 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100264 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700265 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100266 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700267 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000268}
269
Steve Dower48e8c822018-02-22 10:39:26 -0800270static int _PathCchCanonicalizeEx_Initialized = 0;
271typedef HRESULT(__stdcall *PPathCchCanonicalizeEx) (PWSTR pszPathOut, size_t cchPathOut,
272 PCWSTR pszPathIn, unsigned long dwFlags);
273static PPathCchCanonicalizeEx _PathCchCanonicalizeEx;
274
Victor Stinner331a6a52019-05-27 16:39:22 +0200275static PyStatus canonicalize(wchar_t *buffer, const wchar_t *path)
Steve Dower48e8c822018-02-22 10:39:26 -0800276{
277 if (buffer == NULL) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200278 return _PyStatus_NO_MEMORY();
Steve Dower48e8c822018-02-22 10:39:26 -0800279 }
280
281 if (_PathCchCanonicalizeEx_Initialized == 0) {
282 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
283 if (pathapi) {
284 _PathCchCanonicalizeEx = (PPathCchCanonicalizeEx)GetProcAddress(pathapi, "PathCchCanonicalizeEx");
285 }
286 else {
287 _PathCchCanonicalizeEx = NULL;
288 }
289 _PathCchCanonicalizeEx_Initialized = 1;
290 }
291
292 if (_PathCchCanonicalizeEx) {
293 if (FAILED(_PathCchCanonicalizeEx(buffer, MAXPATHLEN + 1, path, 0))) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200294 return _PyStatus_ERR("buffer overflow in getpathp.c's canonicalize()");
Steve Dower48e8c822018-02-22 10:39:26 -0800295 }
296 }
297 else {
298 if (!PathCanonicalizeW(buffer, path)) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200299 return _PyStatus_ERR("buffer overflow in getpathp.c's canonicalize()");
Steve Dower48e8c822018-02-22 10:39:26 -0800300 }
301 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200302 return _PyStatus_OK();
Steve Dower48e8c822018-02-22 10:39:26 -0800303}
304
Victor Stinner0327bde2017-11-23 17:03:20 +0100305
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000306/* gotlandmark only called by search_for_prefix, which ensures
307 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100308 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000309static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100310gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700313 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700316 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 prefix[n] = '\0';
318 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000319}
320
Victor Stinner0327bde2017-11-23 17:03:20 +0100321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Victor Stinnerb64de462017-12-01 18:27:09 +0100323 assumption provided by only caller, calculate_path_impl() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000324static int
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200325search_for_prefix(wchar_t *prefix, const wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700328 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100330 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 reduce(prefix);
334 } while (prefix[0]);
335 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000336}
337
Victor Stinner0327bde2017-11-23 17:03:20 +0100338
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000339#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000340
Guido van Rossum88716bb2000-03-30 19:45:39 +0000341/* a string loaded from the DLL at startup.*/
342extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000343
Guido van Rossumeea14491997-08-13 21:30:44 +0000344/* Load a PYTHONPATH value from the registry.
345 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
346
Guido van Rossum88716bb2000-03-30 19:45:39 +0000347 Works in both Unicode and 8bit environments. Only uses the
348 Ex family of functions so it also works with Windows CE.
349
Guido van Rossumeea14491997-08-13 21:30:44 +0000350 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000351
352 XXX - this code is pretty strange, as it used to also
353 work on Win16, where the buffer sizes werent available
354 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000355*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000356static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000357getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 HKEY newKey = 0;
360 DWORD dataSize = 0;
361 DWORD numKeys = 0;
362 LONG rc;
363 wchar_t *retval = NULL;
364 WCHAR *dataBuf = NULL;
365 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
366 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700367 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 DWORD index;
369 WCHAR *keyBuf = NULL;
370 WCHAR *keyBufPtr;
371 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* Tried to use sysget("winver") but here is too early :-( */
374 versionLen = strlen(PyWin_DLLVersionString);
375 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700376 keyBufLen = sizeof(keyPrefix) +
377 sizeof(WCHAR)*(versionLen-1) +
378 sizeof(keySuffix);
379 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100380 if (keyBuf==NULL) {
381 goto done;
382 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000383
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700384 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200385 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
387 keyBufPtr += versionLen;
388 /* NULL comes with this one! */
389 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
390 /* Open the root Python key */
391 rc=RegOpenKeyExW(keyBase,
392 keyBuf, /* subkey */
393 0, /* reserved */
394 KEY_READ,
395 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100396 if (rc!=ERROR_SUCCESS) {
397 goto done;
398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* Find out how big our core buffer is, and how many subkeys we have */
400 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
401 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100402 if (rc!=ERROR_SUCCESS) {
403 goto done;
404 }
405 if (skipcore) {
406 dataSize = 0; /* Only count core ones if we want them! */
407 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 /* Allocate a temp array of char buffers, so we only need to loop
409 reading the registry once
410 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200411 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100412 if (ppPaths==NULL) {
413 goto done;
414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
416 /* Loop over all subkeys, allocating a temp sub-buffer. */
417 for(index=0;index<numKeys;index++) {
418 WCHAR keyBuf[MAX_PATH+1];
419 HKEY subKey = 0;
420 DWORD reqdSize = MAX_PATH+1;
421 /* Get the sub-key name */
422 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
423 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100424 if (rc!=ERROR_SUCCESS) {
425 goto done;
426 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 /* Open the sub-key */
428 rc=RegOpenKeyExW(newKey,
429 keyBuf, /* subkey */
430 0, /* reserved */
431 KEY_READ,
432 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100433 if (rc!=ERROR_SUCCESS) {
434 goto done;
435 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 /* Find the value of the buffer size, malloc, then read it */
437 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
438 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200439 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (ppPaths[index]) {
441 RegQueryValueExW(subKey, NULL, 0, NULL,
442 (LPBYTE)ppPaths[index],
443 &reqdSize);
444 dataSize += reqdSize + 1; /* 1 for the ";" */
445 }
446 }
447 RegCloseKey(subKey);
448 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100451 if (dataSize == 0) {
452 goto done;
453 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454
455 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200456 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (dataBuf) {
458 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 /* Copy our collected strings */
460 for (index=0;index<numKeys;index++) {
461 if (index > 0) {
462 *(szCur++) = L';';
463 dataSize--;
464 }
465 if (ppPaths[index]) {
466 Py_ssize_t len = wcslen(ppPaths[index]);
467 wcsncpy(szCur, ppPaths[index], len);
468 szCur += len;
469 assert(dataSize > (DWORD)len);
470 dataSize -= (DWORD)len;
471 }
472 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100473 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 else {
luzpaza5293b42017-11-05 07:37:50 -0600477 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (numKeys) {
479 *(szCur++) = L';';
480 dataSize--;
481 }
482 /* Now append the core path entries -
483 this will include the NULL
484 */
485 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
486 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200487 if (rc != ERROR_SUCCESS) {
488 PyMem_RawFree(dataBuf);
489 goto done;
490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 }
492 /* And set the result - caller must free */
493 retval = dataBuf;
494 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000495done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 /* Loop freeing my temp buffers */
497 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200498 for(index=0; index<numKeys; index++)
499 PyMem_RawFree(ppPaths[index]);
500 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100502 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100504 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200505 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000507}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000508#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000509
Victor Stinner0327bde2017-11-23 17:03:20 +0100510
Victor Stinner410759f2019-05-18 04:17:01 +0200511wchar_t*
512_Py_GetDLLPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000513{
Victor Stinner9316ee42017-11-25 03:17:57 +0100514 wchar_t dll_path[MAXPATHLEN+1];
515 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000516
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000517#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100519 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100520 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
521 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100522 }
523 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000524#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100525 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000526#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100527
Victor Stinner410759f2019-05-18 04:17:01 +0200528 return _PyMem_RawWcsdup(dll_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100529}
530
531
Victor Stinner331a6a52019-05-27 16:39:22 +0200532static PyStatus
533get_program_full_path(const PyConfig *config,
534 PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100535{
Steve Dower1c3de542018-12-10 08:11:21 -0800536 const wchar_t *pyvenv_launcher;
Victor Stinnerb64de462017-12-01 18:27:09 +0100537 wchar_t program_full_path[MAXPATHLEN+1];
538 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100539
Steve Dower323e7432019-06-29 14:28:59 -0700540 if (!GetModuleFileNameW(NULL, program_full_path, MAXPATHLEN)) {
541 /* GetModuleFileName should never fail when passed NULL */
542 return _PyStatus_ERR("Cannot determine program path");
543 }
544
Steve Dower1c3de542018-12-10 08:11:21 -0800545 /* The launcher may need to force the executable path to a
546 * different environment, so override it here. */
547 pyvenv_launcher = _wgetenv(L"__PYVENV_LAUNCHER__");
548 if (pyvenv_launcher && pyvenv_launcher[0]) {
Steve Dower323e7432019-06-29 14:28:59 -0700549 /* If overridden, preserve the original full path */
550 pathconfig->base_executable = PyMem_RawMalloc(
551 sizeof(wchar_t) * (MAXPATHLEN + 1));
552 PyStatus status = canonicalize(pathconfig->base_executable,
553 program_full_path);
554 if (_PyStatus_EXCEPTION(status)) {
555 return status;
556 }
557
Steve Dower1c3de542018-12-10 08:11:21 -0800558 wcscpy_s(program_full_path, MAXPATHLEN+1, pyvenv_launcher);
Steve Dower323e7432019-06-29 14:28:59 -0700559 /* bpo-35873: Clear the environment variable to avoid it being
560 * inherited by child processes. */
561 _wputenv_s(L"__PYVENV_LAUNCHER__", L"");
Victor Stinner0327bde2017-11-23 17:03:20 +0100562 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000563
Victor Stinner331a6a52019-05-27 16:39:22 +0200564 pathconfig->program_full_path = PyMem_RawMalloc(
Steve Dower48e8c822018-02-22 10:39:26 -0800565 sizeof(wchar_t) * (MAXPATHLEN + 1));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000566
Victor Stinner331a6a52019-05-27 16:39:22 +0200567 return canonicalize(pathconfig->program_full_path,
Steve Dower48e8c822018-02-22 10:39:26 -0800568 program_full_path);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000569}
570
Victor Stinner0327bde2017-11-23 17:03:20 +0100571
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100572static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200573read_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix, const wchar_t *path)
Steve Dower4db86bc2016-09-09 09:17:35 -0700574{
575 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100576 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100577 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100578 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700579
Steve Dowered51b262016-09-17 12:54:06 -0700580 wcscpy_s(prefix, MAXPATHLEN+1, path);
581 reduce(prefix);
Victor Stinner331a6a52019-05-27 16:39:22 +0200582 pathconfig->isolated = 1;
583 pathconfig->site_import = 0;
Steve Dowered51b262016-09-17 12:54:06 -0700584
Steve Dower4db86bc2016-09-09 09:17:35 -0700585 size_t bufsiz = MAXPATHLEN;
586 size_t prefixlen = wcslen(prefix);
587
588 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700589 if (buf == NULL) {
590 goto error;
591 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700592 buf[0] = '\0';
593
594 while (!feof(sp_file)) {
595 char line[MAXPATHLEN + 1];
596 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100597 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700598 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100599 }
600 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700601 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100602 }
Steve Dowered51b262016-09-17 12:54:06 -0700603 while (*++p) {
604 if (*p == '\r' || *p == '\n') {
605 *p = '\0';
606 break;
607 }
608 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700609
Steve Dowered51b262016-09-17 12:54:06 -0700610 if (strcmp(line, "import site") == 0) {
Victor Stinner331a6a52019-05-27 16:39:22 +0200611 pathconfig->site_import = 1;
Steve Dowered51b262016-09-17 12:54:06 -0700612 continue;
Victor Stinnerf2626ce2018-07-21 03:54:20 +0200613 }
614 else if (strncmp(line, "import ", 7) == 0) {
Steve Dowered51b262016-09-17 12:54:06 -0700615 Py_FatalError("only 'import site' is supported in ._pth file");
616 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700617
Steve Dowered51b262016-09-17 12:54:06 -0700618 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700619 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Zackery Spytz4c49da02018-12-07 03:11:30 -0700620 if (wline == NULL) {
621 goto error;
622 }
Steve Dowered51b262016-09-17 12:54:06 -0700623 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700624 wline[wn] = '\0';
625
Steve Dowerc6dd4152016-10-27 14:28:07 -0700626 size_t usedsiz = wcslen(buf);
627 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700628 bufsiz += MAXPATHLEN;
Zackery Spytz4c49da02018-12-07 03:11:30 -0700629 wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *
630 sizeof(wchar_t));
631 if (tmp == NULL) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700632 PyMem_RawFree(wline);
633 goto error;
634 }
Zackery Spytz4c49da02018-12-07 03:11:30 -0700635 buf = tmp;
Steve Dower4db86bc2016-09-09 09:17:35 -0700636 }
637
Steve Dowerc6dd4152016-10-27 14:28:07 -0700638 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700639 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700640 usedsiz += 1;
641 }
Steve Dowered51b262016-09-17 12:54:06 -0700642
Steve Dowerc6dd4152016-10-27 14:28:07 -0700643 errno_t result;
644 _Py_BEGIN_SUPPRESS_IPH
645 result = wcscat_s(buf, bufsiz, prefix);
646 _Py_END_SUPPRESS_IPH
647 if (result == EINVAL) {
648 Py_FatalError("invalid argument during ._pth processing");
649 } else if (result == ERANGE) {
650 Py_FatalError("buffer overflow during ._pth processing");
651 }
652 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700653 join(b, wline);
654
655 PyMem_RawFree(wline);
656 }
657
Steve Dower4db86bc2016-09-09 09:17:35 -0700658 fclose(sp_file);
Victor Stinner331a6a52019-05-27 16:39:22 +0200659 pathconfig->module_search_path = buf;
Victor Stinner9316ee42017-11-25 03:17:57 +0100660 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700661
662error:
663 PyMem_RawFree(buf);
664 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100665 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700666}
667
668
Victor Stinner46972b72017-11-24 22:55:40 +0100669static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100670calculate_init(PyCalculatePath *calculate,
Victor Stinner331a6a52019-05-27 16:39:22 +0200671 const PyConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000672{
Victor Stinner331a6a52019-05-27 16:39:22 +0200673 calculate->home = config->home;
Victor Stinner0327bde2017-11-23 17:03:20 +0100674 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100675}
676
677
678static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200679get_pth_filename(wchar_t *spbuffer, _PyPathConfig *pathconfig)
Victor Stinner0327bde2017-11-23 17:03:20 +0100680{
Victor Stinner331a6a52019-05-27 16:39:22 +0200681 if (pathconfig->dll_path[0]) {
682 if (!change_ext(spbuffer, pathconfig->dll_path, L"._pth") &&
Victor Stinner31a83932017-12-04 13:39:15 +0100683 exists(spbuffer))
684 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100685 return 1;
686 }
687 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200688 if (pathconfig->program_full_path[0]) {
689 if (!change_ext(spbuffer, pathconfig->program_full_path, L"._pth") &&
Victor Stinner31a83932017-12-04 13:39:15 +0100690 exists(spbuffer))
691 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100692 return 1;
693 }
694 }
695 return 0;
696}
697
698
699static int
Victor Stinner331a6a52019-05-27 16:39:22 +0200700calculate_pth_file(_PyPathConfig *pathconfig, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100701{
702 wchar_t spbuffer[MAXPATHLEN+1];
703
Victor Stinner331a6a52019-05-27 16:39:22 +0200704 if (!get_pth_filename(spbuffer, pathconfig)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100705 return 0;
706 }
707
Victor Stinner331a6a52019-05-27 16:39:22 +0200708 return read_pth_file(pathconfig, prefix, spbuffer);
Victor Stinner0327bde2017-11-23 17:03:20 +0100709}
710
711
712/* Search for an environment configuration file, first in the
713 executable's directory and then in the parent directory.
714 If found, open it for use when searching for prefixes.
715*/
716static void
717calculate_pyvenv_file(PyCalculatePath *calculate)
718{
719 wchar_t envbuffer[MAXPATHLEN+1];
720 const wchar_t *env_cfg = L"pyvenv.cfg";
721
722 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
723 join(envbuffer, env_cfg);
724
725 FILE *env_file = _Py_wfopen(envbuffer, L"r");
726 if (env_file == NULL) {
727 errno = 0;
Victor Stinner9bee3292017-12-21 16:49:13 +0100728
Victor Stinner0327bde2017-11-23 17:03:20 +0100729 reduce(envbuffer);
730 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700731 join(envbuffer, env_cfg);
Victor Stinner9bee3292017-12-21 16:49:13 +0100732
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700733 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100734 if (env_file == NULL) {
735 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100736 }
737 }
738
Victor Stinner0327bde2017-11-23 17:03:20 +0100739 if (env_file == NULL) {
740 return;
741 }
742
743 /* Look for a 'home' variable and set argv0_path to it, if found */
744 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100745 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100746 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
747 }
748 fclose(env_file);
749}
750
751
Victor Stinner331a6a52019-05-27 16:39:22 +0200752#define INIT_ERR_BUFFER_OVERFLOW() _PyStatus_ERR("buffer overflow")
Victor Stinner9316ee42017-11-25 03:17:57 +0100753
754
Victor Stinner0327bde2017-11-23 17:03:20 +0100755static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100756calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100757{
Victor Stinner0327bde2017-11-23 17:03:20 +0100758 if (calculate->home == NULL || *calculate->home == '\0') {
759 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100760 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
761 reduce(prefix);
762 calculate->home = prefix;
763 }
764 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
765 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100766 }
767 else {
768 calculate->home = NULL;
769 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100771 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100772 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100773 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100774}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000775
Victor Stinner9316ee42017-11-25 03:17:57 +0100776
Victor Stinner331a6a52019-05-27 16:39:22 +0200777static PyStatus
778calculate_module_search_path(const PyConfig *config,
779 PyCalculatePath *calculate, _PyPathConfig *pathconfig,
Victor Stinner31a83932017-12-04 13:39:15 +0100780 wchar_t *prefix)
Victor Stinner9316ee42017-11-25 03:17:57 +0100781{
Victor Stinner0327bde2017-11-23 17:03:20 +0100782 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000783#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100784 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
785 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000786#endif
luzpaza5293b42017-11-05 07:37:50 -0600787 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 anything better to use! */
Victor Stinner331a6a52019-05-27 16:39:22 +0200789 int skipdefault = (config->pythonpath_env != NULL ||
Victor Stinner31a83932017-12-04 13:39:15 +0100790 calculate->home != NULL ||
791 calculate->machine_path != NULL ||
792 calculate->user_path != NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 /* We need to construct a path from the following parts.
795 (1) the PYTHONPATH environment variable, if set;
796 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100797 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100799 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 (5) the directory containing the executable (argv0_path).
801 The length calculation calculates #4 first.
802 Extra rules:
803 - If PYTHONHOME is set (in any way) item (3) is ignored.
804 - If registry values are used, (4) and (5) are ignored.
805 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100808 size_t bufsz = 0;
809 if (calculate->home != NULL) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200810 const wchar_t *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 bufsz = 1;
812 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100813 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100815 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100817 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700819 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100820 bufsz += wcslen(calculate->argv0_path) + 1;
821 if (calculate->user_path) {
822 bufsz += wcslen(calculate->user_path) + 1;
823 }
824 if (calculate->machine_path) {
825 bufsz += wcslen(calculate->machine_path) + 1;
826 }
827 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinner331a6a52019-05-27 16:39:22 +0200828 if (config->pythonpath_env != NULL) {
829 bufsz += wcslen(config->pythonpath_env) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100830 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000831
Victor Stinner0327bde2017-11-23 17:03:20 +0100832 wchar_t *buf, *start_buf;
833 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (buf == NULL) {
835 /* We can't exit, so print a warning and limp along */
836 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200837 if (config->pythonpath_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200839 pathconfig->module_search_path = config->pythonpath_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 }
841 else {
842 fprintf(stderr, "Using default static path.\n");
Victor Stinner331a6a52019-05-27 16:39:22 +0200843 pathconfig->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 }
Victor Stinner331a6a52019-05-27 16:39:22 +0200845 return _PyStatus_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100847 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000848
Victor Stinner331a6a52019-05-27 16:39:22 +0200849 if (config->pythonpath_env) {
Victor Stinner31a83932017-12-04 13:39:15 +0100850 if (wcscpy_s(buf, bufsz - (buf - start_buf),
Victor Stinner331a6a52019-05-27 16:39:22 +0200851 config->pythonpath_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100852 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100853 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 buf = wcschr(buf, L'\0');
855 *buf++ = DELIM;
856 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 if (calculate->zip_path[0]) {
858 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100859 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100860 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 buf = wcschr(buf, L'\0');
862 *buf++ = DELIM;
863 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100864 if (calculate->user_path) {
865 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100866 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100867 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 buf = wcschr(buf, L'\0');
869 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100871 if (calculate->machine_path) {
872 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100873 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 buf = wcschr(buf, L'\0');
876 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100878 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100880 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100881 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100882 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700884 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700886 } else {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200887 const wchar_t *p = PYTHONPATH;
888 const wchar_t *q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 size_t n;
890 for (;;) {
891 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100892 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100894 }
895 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100899 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100900 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 buf = wcschr(buf, L'\0');
903 p++;
904 n--;
905 }
906 wcsncpy(buf, p, n);
907 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700908 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100909 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 p = q+1;
913 }
914 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100915 if (calculate->argv0_path) {
916 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700918 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700920 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 /* Now to pull one last hack/trick. If sys.prefix is
923 empty, then try and find it somewhere on the paths
924 we calculated. We scan backwards, as our general policy
925 is that Python core directories are at the *end* of
926 sys.path. We assume that our "lib" directory is
927 on the path, and that our 'prefix' directory is
928 the parent of that.
929 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100930 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 wchar_t lookBuf[MAXPATHLEN+1];
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200932 const wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 while (1) {
934 Py_ssize_t nchars;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200935 const wchar_t *lookEnd = look;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 /* 'look' will end up one character before the
937 start of the path in question - even if this
938 is one character before the start of the buffer
939 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100940 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 look--;
942 nchars = lookEnd-look;
943 wcsncpy(lookBuf, look+1, nchars);
944 lookBuf[nchars] = L'\0';
945 /* Up one level to the parent */
946 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100947 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 break;
949 }
950 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100951 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100953 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 look--;
955 }
956 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100957
Victor Stinner331a6a52019-05-27 16:39:22 +0200958 pathconfig->module_search_path = start_buf;
959 return _PyStatus_OK();
Victor Stinner9316ee42017-11-25 03:17:57 +0100960}
961
962
Victor Stinner331a6a52019-05-27 16:39:22 +0200963static PyStatus
964calculate_path_impl(const PyConfig *config,
965 PyCalculatePath *calculate, _PyPathConfig *pathconfig)
Victor Stinner9316ee42017-11-25 03:17:57 +0100966{
Victor Stinner331a6a52019-05-27 16:39:22 +0200967 PyStatus status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100968
Victor Stinner331a6a52019-05-27 16:39:22 +0200969 assert(pathconfig->dll_path == NULL);
Victor Stinner410759f2019-05-18 04:17:01 +0200970
Victor Stinner331a6a52019-05-27 16:39:22 +0200971 pathconfig->dll_path = _Py_GetDLLPath();
972 if (pathconfig->dll_path == NULL) {
973 return _PyStatus_NO_MEMORY();
Victor Stinner9316ee42017-11-25 03:17:57 +0100974 }
975
Victor Stinner331a6a52019-05-27 16:39:22 +0200976 status = get_program_full_path(config, calculate, pathconfig);
977 if (_PyStatus_EXCEPTION(status)) {
978 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +0100979 }
980
Victor Stinnerb64de462017-12-01 18:27:09 +0100981 /* program_full_path guaranteed \0 terminated in MAXPATH+1 bytes. */
Victor Stinner331a6a52019-05-27 16:39:22 +0200982 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, pathconfig->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100983 reduce(calculate->argv0_path);
984
985 wchar_t prefix[MAXPATHLEN+1];
986 memset(prefix, 0, sizeof(prefix));
987
988 /* Search for a sys.path file */
Victor Stinner331a6a52019-05-27 16:39:22 +0200989 if (calculate_pth_file(pathconfig, prefix)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100990 goto done;
991 }
992
993 calculate_pyvenv_file(calculate);
994
995 /* Calculate zip archive path from DLL or exe path */
996 change_ext(calculate->zip_path,
Victor Stinner331a6a52019-05-27 16:39:22 +0200997 pathconfig->dll_path[0] ? pathconfig->dll_path : pathconfig->program_full_path,
Victor Stinner9316ee42017-11-25 03:17:57 +0100998 L".zip");
999
1000 calculate_home_prefix(calculate, prefix);
1001
Victor Stinner331a6a52019-05-27 16:39:22 +02001002 status = calculate_module_search_path(config, calculate, pathconfig, prefix);
1003 if (_PyStatus_EXCEPTION(status)) {
1004 return status;
Victor Stinner9316ee42017-11-25 03:17:57 +01001005 }
1006
1007done:
Victor Stinner331a6a52019-05-27 16:39:22 +02001008 pathconfig->prefix = _PyMem_RawWcsdup(prefix);
1009 if (pathconfig->prefix == NULL) {
1010 return _PyStatus_NO_MEMORY();
Victor Stinner9316ee42017-11-25 03:17:57 +01001011 }
Victor Stinner331a6a52019-05-27 16:39:22 +02001012 pathconfig->exec_prefix = _PyMem_RawWcsdup(prefix);
1013 if (pathconfig->exec_prefix == NULL) {
1014 return _PyStatus_NO_MEMORY();
Steve Dower177a41a2018-11-17 20:41:48 -08001015 }
Victor Stinner9316ee42017-11-25 03:17:57 +01001016
Victor Stinner331a6a52019-05-27 16:39:22 +02001017 return _PyStatus_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001018}
1019
1020
Victor Stinner0327bde2017-11-23 17:03:20 +01001021static void
1022calculate_free(PyCalculatePath *calculate)
1023{
1024 PyMem_RawFree(calculate->machine_path);
1025 PyMem_RawFree(calculate->user_path);
1026}
1027
Victor Stinner9316ee42017-11-25 03:17:57 +01001028
Victor Stinner331a6a52019-05-27 16:39:22 +02001029PyStatus
1030_PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +02001031{
Victor Stinner0327bde2017-11-23 17:03:20 +01001032 PyCalculatePath calculate;
1033 memset(&calculate, 0, sizeof(calculate));
1034
Victor Stinner331a6a52019-05-27 16:39:22 +02001035 calculate_init(&calculate, config);
Victor Stinner46972b72017-11-24 22:55:40 +01001036
Victor Stinner331a6a52019-05-27 16:39:22 +02001037 PyStatus status = calculate_path_impl(config, &calculate, pathconfig);
1038 if (_PyStatus_EXCEPTION(status)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001039 goto done;
1040 }
1041
Victor Stinner331a6a52019-05-27 16:39:22 +02001042 status = _PyStatus_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001043
Victor Stinner9316ee42017-11-25 03:17:57 +01001044done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001045 calculate_free(&calculate);
Victor Stinner331a6a52019-05-27 16:39:22 +02001046 return status;
Victor Stinner0327bde2017-11-23 17:03:20 +01001047}
1048
1049
Victor Stinner63941882011-09-29 00:42:28 +02001050/* Load python3.dll before loading any extension module that might refer
1051 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001052 to this python DLL is loaded, not a python3.dll that might be on the path
1053 by chance.
1054 Return whether the DLL was found.
1055*/
1056static int python3_checked = 0;
1057static HANDLE hPython3;
1058int
Victor Stinner31a83932017-12-04 13:39:15 +01001059_Py_CheckPython3(void)
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001060{
1061 wchar_t py3path[MAXPATHLEN+1];
1062 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001063 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001064 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001065 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001066 python3_checked = 1;
1067
1068 /* If there is a python3.dll next to the python3y.dll,
1069 assume this is a build tree; use that DLL */
Victor Stinnerb64de462017-12-01 18:27:09 +01001070 wcscpy(py3path, _Py_path_config.dll_path);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001071 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001072 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001073 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001074 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001075 wcscpy(s, L"\\python3.dll");
1076 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001077 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001078 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001079 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001080
1081 /* Check sys.prefix\DLLs\python3.dll */
1082 wcscpy(py3path, Py_GetPrefix());
1083 wcscat(py3path, L"\\DLLs\\python3.dll");
1084 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1085 return hPython3 != NULL;
1086}