blob: fe0226ba5d277cb595dbe5f54ee24e95057e2e7d [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001
2/* Return the initial module search path. */
Jesus Ceaf1af7052012-10-05 02:48:46 +02003/* Used by DOS, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00004
Guido van Rossum88716bb2000-03-30 19:45:39 +00005/* ----------------------------------------------------------------
6 PATH RULES FOR WINDOWS:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 This describes how sys.path is formed on Windows. It describes the
8 functionality, not the implementation (ie, the order in which these
Steve Dowered51b262016-09-17 12:54:06 -07009 are actually fetched is different). The presence of a python._pth or
10 pythonXY._pth file alongside the program overrides these rules - see
11 below.
Guido van Rossum88716bb2000-03-30 19:45:39 +000012
13 * Python always adds an empty entry at the start, which corresponds
14 to the current directory.
15
Georg Brandl7eb4b7d2005-07-22 21:49:32 +000016 * If the PYTHONPATH env. var. exists, its entries are added next.
Guido van Rossum88716bb2000-03-30 19:45:39 +000017
18 * We look in the registry for "application paths" - that is, sub-keys
19 under the main PythonPath registry key. These are added next (the
20 order of sub-key processing is undefined).
21 HKEY_CURRENT_USER is searched and added first.
22 HKEY_LOCAL_MACHINE is searched and added next.
23 (Note that all known installers only use HKLM, so HKCU is typically
24 empty)
25
26 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
27 is set, we believe it. Otherwise, we use the path of our host .EXE's
Martin Panterfd13c0f2016-09-10 10:45:28 +000028 to try and locate one of our "landmarks" and deduce our home.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 - If we DO have a Python Home: The relevant sub-directories (Lib,
Zachary Warec4b53af2016-09-09 17:59:49 -070030 DLLs, etc) are based on the Python Home
Guido van Rossum88716bb2000-03-30 19:45:39 +000031 - If we DO NOT have a Python Home, the core Python Path is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 loaded from the registry. This is the main PythonPath key,
Guido van Rossum88716bb2000-03-30 19:45:39 +000033 and both HKLM and HKCU are combined to form the path)
34
35 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
36 specified, and can't locate any Registry entries (ie, we have _nothing_
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 we can assume is a good path), a default path with relative entries is
Zachary Warec4b53af2016-09-09 17:59:49 -070038 used (eg. .\Lib;.\DLLs, etc)
Guido van Rossum88716bb2000-03-30 19:45:39 +000039
40
Steve Dowered51b262016-09-17 12:54:06 -070041 If a '._pth' file exists adjacent to the executable with the same base name
42 (e.g. python._pth adjacent to python.exe) or adjacent to the shared library
43 (e.g. python36._pth adjacent to python36.dll), it is used in preference to
44 the above process. The shared library file takes precedence over the
45 executable. The path file must contain a list of paths to add to sys.path,
46 one per line. Each path is relative to the directory containing the file.
47 Blank lines and comments beginning with '#' are permitted.
48
49 In the presence of this ._pth file, no other paths are added to the search
50 path, the registry finder is not enabled, site.py is not imported and
51 isolated mode is enabled. The site package can be enabled by including a
52 line reading "import site"; no other imports are recognized. Any invalid
53 entry (other than directories that do not exist) will result in immediate
54 termination of the program.
55
Steve Dower4db86bc2016-09-09 09:17:35 -070056
Guido van Rossum88716bb2000-03-30 19:45:39 +000057 The end result of all this is:
58 * When running python.exe, or any other .exe in the main Python directory
59 (either an installed version, or directly from the PCbuild directory),
60 the core path is deduced, and the core paths in the registry are
61 ignored. Other "application paths" in the registry are always read.
62
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 * When Python is hosted in another exe (different directory, embedded via
Guido van Rossum88716bb2000-03-30 19:45:39 +000064 COM, etc), the Python Home will not be deduced, so the core path from
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 the registry is used. Other "application paths" in the registry are
Guido van Rossum88716bb2000-03-30 19:45:39 +000066 always read.
67
68 * If Python can't find its home and there is no registry (eg, frozen
69 exe, some very strange installation setup) you get a path with
70 some default, but relative, paths.
71
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000072 * An embedding application can use Py_SetPath() to override all of
Steve Dower4db86bc2016-09-09 09:17:35 -070073 these automatic path computations.
74
Steve Dowered51b262016-09-17 12:54:06 -070075 * An install of Python can fully specify the contents of sys.path using
76 either a 'EXENAME._pth' or 'DLLNAME._pth' file, optionally including
77 "import site" to enable the site module.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000078
Guido van Rossum88716bb2000-03-30 19:45:39 +000079 ---------------------------------------------------------------- */
80
81
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000082#include "Python.h"
83#include "osdefs.h"
Martin v. Löwis790465f2008-04-05 20:41:37 +000084#include <wchar.h>
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000085
Steve Dower4db86bc2016-09-09 09:17:35 -070086#ifndef MS_WINDOWS
87#error getpathp.c should only be built on Windows
Guido van Rossum8f1b6511997-08-13 19:55:43 +000088#endif
89
Steve Dower4db86bc2016-09-09 09:17:35 -070090#include <windows.h>
91#include <Shlwapi.h>
92
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093#ifdef HAVE_SYS_TYPES_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000094#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095#endif /* HAVE_SYS_TYPES_H */
96
97#ifdef HAVE_SYS_STAT_H
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000098#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000099#endif /* HAVE_SYS_STAT_H */
100
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000101#include <string.h>
102
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000103/* Search in some common locations for the associated Python libraries.
104 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 * Py_GetPath() tries to return a sensible Python module search path.
106 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000107 * The approach is an adaptation for Windows of the strategy used in
108 * ../Modules/getpath.c; it uses the Windows Registry as one of its
109 * information sources.
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +0000110 *
111 * Py_SetPath() can be used to override this mechanism. Call Py_SetPath
112 * with a semicolon separated path prior to calling Py_Initialize.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000113 */
114
115#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000116#define LANDMARK L"lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000117#endif
118
Victor Stinner0327bde2017-11-23 17:03:20 +0100119typedef struct {
Victor Stinner9316ee42017-11-25 03:17:57 +0100120 wchar_t *prefix;
121 wchar_t *program_name;
122 wchar_t *dll_path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100123 wchar_t *module_search_path;
124} PyPathConfig;
125
126typedef struct {
127 wchar_t *module_search_path_env; /* PYTHONPATH environment variable */
128 wchar_t *path_env; /* PATH environment variable */
129 wchar_t *home; /* PYTHONHOME environment variable */
130
131 /* Registry key "Software\Python\PythonCore\PythonPath" */
132 wchar_t *machine_path; /* from HKEY_LOCAL_MACHINE */
133 wchar_t *user_path; /* from HKEY_CURRENT_USER */
134
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100135 wchar_t *program_name; /* Program name */
Victor Stinner0327bde2017-11-23 17:03:20 +0100136 wchar_t argv0_path[MAXPATHLEN+1];
137 wchar_t zip_path[MAXPATHLEN+1];
138} PyCalculatePath;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000139
Guido van Rossumeea14491997-08-13 21:30:44 +0000140
Victor Stinner0327bde2017-11-23 17:03:20 +0100141static PyPathConfig path_config = {.module_search_path = NULL};
142
143
144/* determine if "ch" is a separator character */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000145static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100146is_sep(wchar_t ch)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000147{
148#ifdef ALTSEP
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 return ch == SEP || ch == ALTSEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000150#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return ch == SEP;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000152#endif
153}
154
Victor Stinner0327bde2017-11-23 17:03:20 +0100155
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000156/* assumes 'dir' null terminated in bounds. Never writes
Victor Stinner0327bde2017-11-23 17:03:20 +0100157 beyond existing terminator. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000158static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000159reduce(wchar_t *dir)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000160{
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700161 size_t i = wcsnlen_s(dir, MAXPATHLEN+1);
Victor Stinner0327bde2017-11-23 17:03:20 +0100162 if (i >= MAXPATHLEN+1) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700163 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100164 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 while (i > 0 && !is_sep(dir[i]))
167 --i;
168 dir[i] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000169}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170
Victor Stinner0327bde2017-11-23 17:03:20 +0100171
Steve Dowered51b262016-09-17 12:54:06 -0700172static int
173change_ext(wchar_t *dest, const wchar_t *src, const wchar_t *ext)
174{
175 size_t src_len = wcsnlen_s(src, MAXPATHLEN+1);
176 size_t i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100177 if (i >= MAXPATHLEN+1) {
Steve Dowered51b262016-09-17 12:54:06 -0700178 Py_FatalError("buffer overflow in getpathp.c's reduce()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100179 }
Steve Dowered51b262016-09-17 12:54:06 -0700180
181 while (i > 0 && src[i] != '.' && !is_sep(src[i]))
182 --i;
183
184 if (i == 0) {
185 dest[0] = '\0';
186 return -1;
187 }
188
Victor Stinner0327bde2017-11-23 17:03:20 +0100189 if (is_sep(src[i])) {
Steve Dowered51b262016-09-17 12:54:06 -0700190 i = src_len;
Victor Stinner0327bde2017-11-23 17:03:20 +0100191 }
Steve Dowered51b262016-09-17 12:54:06 -0700192
193 if (wcsncpy_s(dest, MAXPATHLEN+1, src, i) ||
Victor Stinner0327bde2017-11-23 17:03:20 +0100194 wcscat_s(dest, MAXPATHLEN+1, ext))
195 {
Steve Dowered51b262016-09-17 12:54:06 -0700196 dest[0] = '\0';
197 return -1;
198 }
199
200 return 0;
201}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000202
Victor Stinner0327bde2017-11-23 17:03:20 +0100203
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000204static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000205exists(wchar_t *filename)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return GetFileAttributesW(filename) != 0xFFFFFFFF;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000208}
209
Victor Stinner0327bde2017-11-23 17:03:20 +0100210
211/* Is module -- check for .pyc too.
212 Assumes 'filename' MAXPATHLEN+1 bytes long -
213 may extend 'filename' by one character. */
Guido van Rossum43ff1141998-08-08 23:40:40 +0000214static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100215ismodule(wchar_t *filename, int update_filename)
Guido van Rossum43ff1141998-08-08 23:40:40 +0000216{
Victor Stinnerccb1f8c2016-03-23 11:31:58 +0100217 size_t n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700218
Victor Stinner0327bde2017-11-23 17:03:20 +0100219 if (exists(filename)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100221 }
Guido van Rossum43ff1141998-08-08 23:40:40 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 /* Check for the compiled version of prefix. */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700224 n = wcsnlen_s(filename, MAXPATHLEN+1);
225 if (n < MAXPATHLEN) {
226 int exist = 0;
Xiang Zhang0710d752017-03-11 13:02:52 +0800227 filename[n] = L'c';
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700228 filename[n + 1] = L'\0';
229 exist = exists(filename);
Victor Stinner0327bde2017-11-23 17:03:20 +0100230 if (!update_filename) {
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700231 filename[n] = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100232 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700233 return exist;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 }
235 return 0;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000236}
237
Victor Stinner0327bde2017-11-23 17:03:20 +0100238
Tim Peters8484fbf2004-08-07 19:12:27 +0000239/* Add a path component, by appending stuff to buffer.
240 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
241 NUL-terminated string with no more than MAXPATHLEN characters (not counting
242 the trailing NUL). It's a fatal error if it contains a string longer than
243 that (callers must be careful!). If these requirements are met, it's
244 guaranteed that buffer will still be a NUL-terminated string with no more
245 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
246 stuff as fits will be appended.
247*/
Steve Dower4db86bc2016-09-09 09:17:35 -0700248
249static int _PathCchCombineEx_Initialized = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100250typedef HRESULT(__stdcall *PPathCchCombineEx) (PWSTR pszPathOut, size_t cchPathOut,
251 PCWSTR pszPathIn, PCWSTR pszMore,
252 unsigned long dwFlags);
Steve Dower4db86bc2016-09-09 09:17:35 -0700253static PPathCchCombineEx _PathCchCombineEx;
254
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000255static void
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700256join(wchar_t *buffer, const wchar_t *stuff)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000257{
Steve Dower4db86bc2016-09-09 09:17:35 -0700258 if (_PathCchCombineEx_Initialized == 0) {
259 HMODULE pathapi = LoadLibraryW(L"api-ms-win-core-path-l1-1-0.dll");
Victor Stinner0327bde2017-11-23 17:03:20 +0100260 if (pathapi) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700261 _PathCchCombineEx = (PPathCchCombineEx)GetProcAddress(pathapi, "PathCchCombineEx");
Victor Stinner0327bde2017-11-23 17:03:20 +0100262 }
263 else {
Steve Dower4db86bc2016-09-09 09:17:35 -0700264 _PathCchCombineEx = NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +0100265 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700266 _PathCchCombineEx_Initialized = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700268
Steve Dower4db86bc2016-09-09 09:17:35 -0700269 if (_PathCchCombineEx) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100270 if (FAILED(_PathCchCombineEx(buffer, MAXPATHLEN+1, buffer, stuff, 0))) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700271 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700273 } else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100274 if (!PathCombineW(buffer, buffer, stuff)) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700275 Py_FatalError("buffer overflow in getpathp.c's join()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100276 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700277 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000278}
279
Victor Stinner0327bde2017-11-23 17:03:20 +0100280
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000281/* gotlandmark only called by search_for_prefix, which ensures
282 'prefix' is null terminated in bounds. join() ensures
Victor Stinner0327bde2017-11-23 17:03:20 +0100283 'landmark' can not overflow prefix if too long. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000284static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100285gotlandmark(wchar_t *prefix, const wchar_t *landmark)
Guido van Rossume02e48b2000-03-29 01:49:47 +0000286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 int ok;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700288 Py_ssize_t n = wcsnlen_s(prefix, MAXPATHLEN);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 join(prefix, landmark);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700291 ok = ismodule(prefix, FALSE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 prefix[n] = '\0';
293 return ok;
Guido van Rossume02e48b2000-03-29 01:49:47 +0000294}
295
Victor Stinner0327bde2017-11-23 17:03:20 +0100296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297/* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
Mark Hammond8bf9e3b2000-10-07 11:10:50 +0000298 assumption provided by only caller, calculate_path() */
Guido van Rossume02e48b2000-03-29 01:49:47 +0000299static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100300search_for_prefix(wchar_t *prefix, wchar_t *argv0_path, const wchar_t *landmark)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 /* Search from argv0_path, until landmark is found */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700303 wcscpy_s(prefix, MAXPATHLEN + 1, argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 do {
Victor Stinner0327bde2017-11-23 17:03:20 +0100305 if (gotlandmark(prefix, landmark)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100307 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 reduce(prefix);
309 } while (prefix[0]);
310 return 0;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000311}
312
Victor Stinner0327bde2017-11-23 17:03:20 +0100313
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000314#ifdef Py_ENABLE_SHARED
Guido van Rossum43ff1141998-08-08 23:40:40 +0000315
Guido van Rossum88716bb2000-03-30 19:45:39 +0000316/* a string loaded from the DLL at startup.*/
317extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000318
Guido van Rossumeea14491997-08-13 21:30:44 +0000319/* Load a PYTHONPATH value from the registry.
320 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
321
Guido van Rossum88716bb2000-03-30 19:45:39 +0000322 Works in both Unicode and 8bit environments. Only uses the
323 Ex family of functions so it also works with Windows CE.
324
Guido van Rossumeea14491997-08-13 21:30:44 +0000325 Returns NULL, or a pointer that should be freed.
Mark Hammond5edc6272001-02-23 11:38:38 +0000326
327 XXX - this code is pretty strange, as it used to also
328 work on Win16, where the buffer sizes werent available
329 in advance. It could be simplied now Win16/Win32s is dead!
Guido van Rossumeea14491997-08-13 21:30:44 +0000330*/
Martin v. Löwis790465f2008-04-05 20:41:37 +0000331static wchar_t *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000332getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 HKEY newKey = 0;
335 DWORD dataSize = 0;
336 DWORD numKeys = 0;
337 LONG rc;
338 wchar_t *retval = NULL;
339 WCHAR *dataBuf = NULL;
340 static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\";
341 static const WCHAR keySuffix[] = L"\\PythonPath";
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700342 size_t versionLen, keyBufLen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 DWORD index;
344 WCHAR *keyBuf = NULL;
345 WCHAR *keyBufPtr;
346 WCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 /* Tried to use sysget("winver") but here is too early :-( */
349 versionLen = strlen(PyWin_DLLVersionString);
350 /* Space for all the chars, plus one \0 */
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700351 keyBufLen = sizeof(keyPrefix) +
352 sizeof(WCHAR)*(versionLen-1) +
353 sizeof(keySuffix);
354 keyBuf = keyBufPtr = PyMem_RawMalloc(keyBufLen);
Victor Stinner0327bde2017-11-23 17:03:20 +0100355 if (keyBuf==NULL) {
356 goto done;
357 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700359 memcpy_s(keyBufPtr, keyBufLen, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR));
Victor Stinner63941882011-09-29 00:42:28 +0200360 keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen);
362 keyBufPtr += versionLen;
363 /* NULL comes with this one! */
364 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
365 /* Open the root Python key */
366 rc=RegOpenKeyExW(keyBase,
367 keyBuf, /* subkey */
368 0, /* reserved */
369 KEY_READ,
370 &newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100371 if (rc!=ERROR_SUCCESS) {
372 goto done;
373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 /* Find out how big our core buffer is, and how many subkeys we have */
375 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
376 NULL, NULL, &dataSize, NULL, NULL);
Victor Stinner0327bde2017-11-23 17:03:20 +0100377 if (rc!=ERROR_SUCCESS) {
378 goto done;
379 }
380 if (skipcore) {
381 dataSize = 0; /* Only count core ones if we want them! */
382 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 /* Allocate a temp array of char buffers, so we only need to loop
384 reading the registry once
385 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200386 ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys );
Victor Stinner0327bde2017-11-23 17:03:20 +0100387 if (ppPaths==NULL) {
388 goto done;
389 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 memset(ppPaths, 0, sizeof(WCHAR *) * numKeys);
391 /* Loop over all subkeys, allocating a temp sub-buffer. */
392 for(index=0;index<numKeys;index++) {
393 WCHAR keyBuf[MAX_PATH+1];
394 HKEY subKey = 0;
395 DWORD reqdSize = MAX_PATH+1;
396 /* Get the sub-key name */
397 DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize,
398 NULL, NULL, NULL, NULL );
Victor Stinner0327bde2017-11-23 17:03:20 +0100399 if (rc!=ERROR_SUCCESS) {
400 goto done;
401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 /* Open the sub-key */
403 rc=RegOpenKeyExW(newKey,
404 keyBuf, /* subkey */
405 0, /* reserved */
406 KEY_READ,
407 &subKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100408 if (rc!=ERROR_SUCCESS) {
409 goto done;
410 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 /* Find the value of the buffer size, malloc, then read it */
412 RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize);
413 if (reqdSize) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200414 ppPaths[index] = PyMem_RawMalloc(reqdSize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 if (ppPaths[index]) {
416 RegQueryValueExW(subKey, NULL, 0, NULL,
417 (LPBYTE)ppPaths[index],
418 &reqdSize);
419 dataSize += reqdSize + 1; /* 1 for the ";" */
420 }
421 }
422 RegCloseKey(subKey);
423 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 /* return null if no path to return */
Victor Stinner0327bde2017-11-23 17:03:20 +0100426 if (dataSize == 0) {
427 goto done;
428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429
430 /* original datasize from RegQueryInfo doesn't include the \0 */
Victor Stinner1a7425f2013-07-07 16:25:15 +0200431 dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (dataBuf) {
433 WCHAR *szCur = dataBuf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 /* Copy our collected strings */
435 for (index=0;index<numKeys;index++) {
436 if (index > 0) {
437 *(szCur++) = L';';
438 dataSize--;
439 }
440 if (ppPaths[index]) {
441 Py_ssize_t len = wcslen(ppPaths[index]);
442 wcsncpy(szCur, ppPaths[index], len);
443 szCur += len;
444 assert(dataSize > (DWORD)len);
445 dataSize -= (DWORD)len;
446 }
447 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100448 if (skipcore) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 *szCur = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 else {
luzpaza5293b42017-11-05 07:37:50 -0600452 /* If we have no values, we don't need a ';' */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (numKeys) {
454 *(szCur++) = L';';
455 dataSize--;
456 }
457 /* Now append the core path entries -
458 this will include the NULL
459 */
460 rc = RegQueryValueExW(newKey, NULL, 0, NULL,
461 (LPBYTE)szCur, &dataSize);
Serhiy Storchakae0cb9da2015-12-18 09:54:19 +0200462 if (rc != ERROR_SUCCESS) {
463 PyMem_RawFree(dataBuf);
464 goto done;
465 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 }
467 /* And set the result - caller must free */
468 retval = dataBuf;
469 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000470done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 /* Loop freeing my temp buffers */
472 if (ppPaths) {
Victor Stinner1a7425f2013-07-07 16:25:15 +0200473 for(index=0; index<numKeys; index++)
474 PyMem_RawFree(ppPaths[index]);
475 PyMem_RawFree(ppPaths);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100477 if (newKey) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 RegCloseKey(newKey);
Victor Stinner0327bde2017-11-23 17:03:20 +0100479 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200480 PyMem_RawFree(keyBuf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 return retval;
Guido van Rossumeea14491997-08-13 21:30:44 +0000482}
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000483#endif /* Py_ENABLE_SHARED */
Guido van Rossumeea14491997-08-13 21:30:44 +0000484
Victor Stinner0327bde2017-11-23 17:03:20 +0100485
Victor Stinner9316ee42017-11-25 03:17:57 +0100486static _PyInitError
487get_dll_path(PyCalculatePath *calculate, PyPathConfig *config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488{
Victor Stinner9316ee42017-11-25 03:17:57 +0100489 wchar_t dll_path[MAXPATHLEN+1];
490 memset(dll_path, 0, sizeof(dll_path));
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000491
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000492#ifdef Py_ENABLE_SHARED
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 extern HANDLE PyWin_DLLhModule;
Victor Stinner0327bde2017-11-23 17:03:20 +0100494 if (PyWin_DLLhModule) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100495 if (!GetModuleFileNameW(PyWin_DLLhModule, dll_path, MAXPATHLEN)) {
496 dll_path[0] = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100497 }
498 }
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000499#else
Victor Stinner9316ee42017-11-25 03:17:57 +0100500 dll_path[0] = 0;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000501#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100502
503 config->dll_path = _PyMem_RawWcsdup(dll_path);
504 if (config->dll_path == NULL) {
505 return _Py_INIT_NO_MEMORY();
506 }
507 return _Py_INIT_OK();
508}
509
510
511static _PyInitError
512get_program_name(PyCalculatePath *calculate, PyPathConfig *config)
513{
514 wchar_t program_name[MAXPATHLEN+1];
515 memset(program_name, 0, sizeof(program_name));
516
517 if (GetModuleFileNameW(NULL, program_name, MAXPATHLEN)) {
518 goto done;
Victor Stinner0327bde2017-11-23 17:03:20 +0100519 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 /* If there is no slash in the argv0 path, then we have to
522 * assume python is on the user's $PATH, since there's no
523 * other way to find a directory to start the search from. If
524 * $PATH isn't exported, you lose.
525 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000526#ifdef ALTSEP
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100527 if (wcschr(calculate->program_name, SEP) || wcschr(calculate->program_name, ALTSEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000528#else
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100529 if (wcschr(calculate->program_name, SEP))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000530#endif
Victor Stinner0327bde2017-11-23 17:03:20 +0100531 {
Victor Stinner9316ee42017-11-25 03:17:57 +0100532 wcsncpy(program_name, calculate->program_name, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100533 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100534 else if (calculate->path_env) {
535 wchar_t *path = calculate->path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 while (1) {
537 wchar_t *delim = wcschr(path, DELIM);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 if (delim) {
540 size_t len = delim - path;
541 /* ensure we can't overwrite buffer */
542 len = min(MAXPATHLEN,len);
Victor Stinner9316ee42017-11-25 03:17:57 +0100543 wcsncpy(program_name, path, len);
544 program_name[len] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100546 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100547 wcsncpy(program_name, path, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100548 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* join() is safe for MAXPATHLEN+1 size buffer */
Victor Stinner9316ee42017-11-25 03:17:57 +0100551 join(program_name, calculate->program_name);
552 if (exists(program_name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100554 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 if (!delim) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100557 program_name[0] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 break;
559 }
560 path = delim + 1;
561 }
562 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100563 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100564 program_name[0] = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100565 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100566
567done:
568 config->program_name = _PyMem_RawWcsdup(program_name);
569 if (config->program_name == NULL) {
570 return _Py_INIT_NO_MEMORY();
571 }
572 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000573}
574
Victor Stinner0327bde2017-11-23 17:03:20 +0100575
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100576static int
577find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
578{
579 int result = 0; /* meaning not found */
580 char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
581
582 fseek(env_file, 0, SEEK_SET);
583 while (!feof(env_file)) {
584 char * p = fgets(buffer, MAXPATHLEN*2, env_file);
585 wchar_t tmpbuffer[MAXPATHLEN*2+1];
586 PyObject * decoded;
Victor Stinner8bda4652013-06-05 00:22:34 +0200587 size_t n;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100588
Victor Stinner0327bde2017-11-23 17:03:20 +0100589 if (p == NULL) {
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100590 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100591 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100592 n = strlen(p);
593 if (p[n - 1] != '\n') {
594 /* line has overflowed - bail */
595 break;
596 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100597 if (p[0] == '#') {
598 /* Comment - skip */
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100599 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100600 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100601 decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
602 if (decoded != NULL) {
603 Py_ssize_t k;
604 k = PyUnicode_AsWideChar(decoded,
605 tmpbuffer, MAXPATHLEN * 2);
606 Py_DECREF(decoded);
607 if (k >= 0) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800608 wchar_t * context = NULL;
609 wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100610 if ((tok != NULL) && !wcscmp(tok, key)) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800611 tok = wcstok_s(NULL, L" \t", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100612 if ((tok != NULL) && !wcscmp(tok, L"=")) {
Steve Dowerf63dab52015-02-25 20:48:01 -0800613 tok = wcstok_s(NULL, L"\r\n", &context);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100614 if (tok != NULL) {
615 wcsncpy(value, tok, MAXPATHLEN);
616 result = 1;
617 break;
618 }
619 }
620 }
621 }
622 }
623 }
624 return result;
625}
626
Victor Stinner0327bde2017-11-23 17:03:20 +0100627
Victor Stinner9316ee42017-11-25 03:17:57 +0100628static int
629read_pth_file(PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
630 int *isolated, int *nosite)
Steve Dower4db86bc2016-09-09 09:17:35 -0700631{
632 FILE *sp_file = _Py_wfopen(path, L"r");
Victor Stinner0327bde2017-11-23 17:03:20 +0100633 if (sp_file == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100634 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100635 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700636
Steve Dowered51b262016-09-17 12:54:06 -0700637 wcscpy_s(prefix, MAXPATHLEN+1, path);
638 reduce(prefix);
639 *isolated = 1;
640 *nosite = 1;
641
Steve Dower4db86bc2016-09-09 09:17:35 -0700642 size_t bufsiz = MAXPATHLEN;
643 size_t prefixlen = wcslen(prefix);
644
645 wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));
646 buf[0] = '\0';
647
648 while (!feof(sp_file)) {
649 char line[MAXPATHLEN + 1];
650 char *p = fgets(line, MAXPATHLEN + 1, sp_file);
Victor Stinner0327bde2017-11-23 17:03:20 +0100651 if (!p) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700652 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100653 }
654 if (*p == '\0' || *p == '\r' || *p == '\n' || *p == '#') {
Steve Dowered51b262016-09-17 12:54:06 -0700655 continue;
Victor Stinner0327bde2017-11-23 17:03:20 +0100656 }
Steve Dowered51b262016-09-17 12:54:06 -0700657 while (*++p) {
658 if (*p == '\r' || *p == '\n') {
659 *p = '\0';
660 break;
661 }
662 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700663
Steve Dowered51b262016-09-17 12:54:06 -0700664 if (strcmp(line, "import site") == 0) {
665 *nosite = 0;
666 continue;
667 } else if (strncmp(line, "import ", 7) == 0) {
668 Py_FatalError("only 'import site' is supported in ._pth file");
669 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700670
Steve Dowered51b262016-09-17 12:54:06 -0700671 DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);
Steve Dower4db86bc2016-09-09 09:17:35 -0700672 wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));
Steve Dowered51b262016-09-17 12:54:06 -0700673 wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);
Steve Dower4db86bc2016-09-09 09:17:35 -0700674 wline[wn] = '\0';
675
Steve Dowerc6dd4152016-10-27 14:28:07 -0700676 size_t usedsiz = wcslen(buf);
677 while (usedsiz + wn + prefixlen + 4 > bufsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700678 bufsiz += MAXPATHLEN;
679 buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));
680 if (!buf) {
681 PyMem_RawFree(wline);
682 goto error;
683 }
684 }
685
Steve Dowerc6dd4152016-10-27 14:28:07 -0700686 if (usedsiz) {
Steve Dower4db86bc2016-09-09 09:17:35 -0700687 wcscat_s(buf, bufsiz, L";");
Steve Dowerc6dd4152016-10-27 14:28:07 -0700688 usedsiz += 1;
689 }
Steve Dowered51b262016-09-17 12:54:06 -0700690
Steve Dowerc6dd4152016-10-27 14:28:07 -0700691 errno_t result;
692 _Py_BEGIN_SUPPRESS_IPH
693 result = wcscat_s(buf, bufsiz, prefix);
694 _Py_END_SUPPRESS_IPH
695 if (result == EINVAL) {
696 Py_FatalError("invalid argument during ._pth processing");
697 } else if (result == ERANGE) {
698 Py_FatalError("buffer overflow during ._pth processing");
699 }
700 wchar_t *b = &buf[usedsiz];
Steve Dower4db86bc2016-09-09 09:17:35 -0700701 join(b, wline);
702
703 PyMem_RawFree(wline);
704 }
705
Steve Dower4db86bc2016-09-09 09:17:35 -0700706 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100707 config->module_search_path = buf;
708 return 1;
Steve Dower4db86bc2016-09-09 09:17:35 -0700709
710error:
711 PyMem_RawFree(buf);
712 fclose(sp_file);
Victor Stinner9316ee42017-11-25 03:17:57 +0100713 return 0;
Steve Dower4db86bc2016-09-09 09:17:35 -0700714}
715
716
Victor Stinner46972b72017-11-24 22:55:40 +0100717static void
Victor Stinner0327bde2017-11-23 17:03:20 +0100718calculate_init(PyCalculatePath *calculate,
719 const _PyMainInterpreterConfig *main_config)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000720{
Victor Stinner46972b72017-11-24 22:55:40 +0100721 calculate->home = main_config->home;
722 calculate->module_search_path_env = main_config->module_search_path_env;
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100723 calculate->program_name = main_config->program_name;
Steve Dower4db86bc2016-09-09 09:17:35 -0700724
Victor Stinner0327bde2017-11-23 17:03:20 +0100725 calculate->path_env = _wgetenv(L"PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100726}
727
728
729static int
730get_pth_filename(wchar_t *spbuffer, PyPathConfig *config)
731{
Victor Stinner9316ee42017-11-25 03:17:57 +0100732 if (config->dll_path[0]) {
733 if (!change_ext(spbuffer, config->dll_path, L"._pth") && exists(spbuffer)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100734 return 1;
735 }
736 }
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100737 if (config->program_name[0]) {
738 if (!change_ext(spbuffer, config->program_name, L"._pth") && exists(spbuffer)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100739 return 1;
740 }
741 }
742 return 0;
743}
744
745
746static int
Victor Stinner9316ee42017-11-25 03:17:57 +0100747calculate_pth_file(PyPathConfig *config, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100748{
749 wchar_t spbuffer[MAXPATHLEN+1];
750
751 if (!get_pth_filename(spbuffer, config)) {
752 return 0;
753 }
754
Victor Stinner9316ee42017-11-25 03:17:57 +0100755 return read_pth_file(config, prefix, spbuffer,
756 &Py_IsolatedFlag, &Py_NoSiteFlag);
Victor Stinner0327bde2017-11-23 17:03:20 +0100757}
758
759
760/* Search for an environment configuration file, first in the
761 executable's directory and then in the parent directory.
762 If found, open it for use when searching for prefixes.
763*/
764static void
765calculate_pyvenv_file(PyCalculatePath *calculate)
766{
767 wchar_t envbuffer[MAXPATHLEN+1];
768 const wchar_t *env_cfg = L"pyvenv.cfg";
769
770 wcscpy_s(envbuffer, MAXPATHLEN+1, calculate->argv0_path);
771 join(envbuffer, env_cfg);
772
773 FILE *env_file = _Py_wfopen(envbuffer, L"r");
774 if (env_file == NULL) {
775 errno = 0;
776 reduce(envbuffer);
777 reduce(envbuffer);
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700778 join(envbuffer, env_cfg);
779 env_file = _Py_wfopen(envbuffer, L"r");
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100780 if (env_file == NULL) {
781 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100782 }
783 }
784
Victor Stinner0327bde2017-11-23 17:03:20 +0100785 if (env_file == NULL) {
786 return;
787 }
788
789 /* Look for a 'home' variable and set argv0_path to it, if found */
790 wchar_t tmpbuffer[MAXPATHLEN+1];
791 if (find_env_config_value(env_file, L"home", tmpbuffer)) {
792 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
793 }
794 fclose(env_file);
795}
796
797
Victor Stinner9316ee42017-11-25 03:17:57 +0100798#define INIT_ERR_BUFFER_OVERFLOW() _Py_INIT_ERR("buffer overflow")
799
800
Victor Stinner0327bde2017-11-23 17:03:20 +0100801static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100802calculate_home_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100803{
Victor Stinner0327bde2017-11-23 17:03:20 +0100804 if (calculate->home == NULL || *calculate->home == '\0') {
805 if (calculate->zip_path[0] && exists(calculate->zip_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100806 wcscpy_s(prefix, MAXPATHLEN+1, calculate->zip_path);
807 reduce(prefix);
808 calculate->home = prefix;
809 }
810 else if (search_for_prefix(prefix, calculate->argv0_path, LANDMARK)) {
811 calculate->home = prefix;
Victor Stinner0327bde2017-11-23 17:03:20 +0100812 }
813 else {
814 calculate->home = NULL;
815 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100817 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100818 wcscpy_s(prefix, MAXPATHLEN+1, calculate->home);
Victor Stinner0327bde2017-11-23 17:03:20 +0100819 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100820}
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000821
Victor Stinner9316ee42017-11-25 03:17:57 +0100822
823static _PyInitError
824calculate_module_search_path(PyCalculatePath *calculate, PyPathConfig *config, wchar_t *prefix)
825{
Victor Stinner0327bde2017-11-23 17:03:20 +0100826 int skiphome = calculate->home==NULL ? 0 : 1;
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000827#ifdef Py_ENABLE_SHARED
Victor Stinner0327bde2017-11-23 17:03:20 +0100828 calculate->machine_path = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
829 calculate->user_path = getpythonregpath(HKEY_CURRENT_USER, skiphome);
Martin v. Löwisbc186a82009-02-02 15:32:22 +0000830#endif
luzpaza5293b42017-11-05 07:37:50 -0600831 /* We only use the default relative PYTHONPATH if we haven't
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 anything better to use! */
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 int skipdefault = (calculate->module_search_path_env!=NULL || calculate->home!=NULL || \
834 calculate->machine_path!=NULL || calculate->user_path!=NULL);
Guido van Rossum43ff1141998-08-08 23:40:40 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 /* We need to construct a path from the following parts.
837 (1) the PYTHONPATH environment variable, if set;
838 (2) for Win32, the zip archive file path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100839 (3) for Win32, the machine_path and user_path, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 (4) the PYTHONPATH config macro, with the leading "."
Victor Stinner0327bde2017-11-23 17:03:20 +0100841 of each component replaced with home, if set;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 (5) the directory containing the executable (argv0_path).
843 The length calculation calculates #4 first.
844 Extra rules:
845 - If PYTHONHOME is set (in any way) item (3) is ignored.
846 - If registry values are used, (4) and (5) are ignored.
847 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 /* Calculate size of return buffer */
Victor Stinner0327bde2017-11-23 17:03:20 +0100850 size_t bufsz = 0;
851 if (calculate->home != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 wchar_t *p;
853 bufsz = 1;
854 for (p = PYTHONPATH; *p; p++) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100855 if (*p == DELIM) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 bufsz++; /* number of DELIM plus one */
Victor Stinner0327bde2017-11-23 17:03:20 +0100857 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100859 bufsz *= wcslen(calculate->home);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 }
Steve Dowerf64b9d52015-05-23 17:34:50 -0700861 bufsz += wcslen(PYTHONPATH) + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100862 bufsz += wcslen(calculate->argv0_path) + 1;
863 if (calculate->user_path) {
864 bufsz += wcslen(calculate->user_path) + 1;
865 }
866 if (calculate->machine_path) {
867 bufsz += wcslen(calculate->machine_path) + 1;
868 }
869 bufsz += wcslen(calculate->zip_path) + 1;
870 if (calculate->module_search_path_env != NULL) {
871 bufsz += wcslen(calculate->module_search_path_env) + 1;
872 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000873
Victor Stinner0327bde2017-11-23 17:03:20 +0100874 wchar_t *buf, *start_buf;
875 buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (buf == NULL) {
877 /* We can't exit, so print a warning and limp along */
878 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100879 if (calculate->module_search_path_env) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100881 config->module_search_path = calculate->module_search_path_env;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 }
883 else {
884 fprintf(stderr, "Using default static path.\n");
Victor Stinner0327bde2017-11-23 17:03:20 +0100885 config->module_search_path = PYTHONPATH;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100887 return _Py_INIT_OK();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100889 start_buf = buf;
Guido van Rossumeea14491997-08-13 21:30:44 +0000890
Victor Stinner0327bde2017-11-23 17:03:20 +0100891 if (calculate->module_search_path_env) {
892 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->module_search_path_env)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100893 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100894 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 buf = wcschr(buf, L'\0');
896 *buf++ = DELIM;
897 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100898 if (calculate->zip_path[0]) {
899 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->zip_path)) {
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 *buf++ = DELIM;
904 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 if (calculate->user_path) {
906 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->user_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100907 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 buf = wcschr(buf, L'\0');
910 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 if (calculate->machine_path) {
913 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->machine_path)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100914 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 buf = wcschr(buf, L'\0');
917 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100919 if (calculate->home == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (!skipdefault) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100921 if (wcscpy_s(buf, bufsz - (buf - start_buf), PYTHONPATH)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100922 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100923 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700925 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 }
Steve Dower4db86bc2016-09-09 09:17:35 -0700927 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 wchar_t *p = PYTHONPATH;
929 wchar_t *q;
930 size_t n;
931 for (;;) {
932 q = wcschr(p, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100933 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 n = wcslen(p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100935 }
936 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 n = q-p;
Victor Stinner0327bde2017-11-23 17:03:20 +0100938 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 if (p[0] == '.' && is_sep(p[1])) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100940 if (wcscpy_s(buf, bufsz - (buf - start_buf), calculate->home)) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100941 return INIT_ERR_BUFFER_OVERFLOW();
Victor Stinner0327bde2017-11-23 17:03:20 +0100942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 buf = wcschr(buf, L'\0');
944 p++;
945 n--;
946 }
947 wcsncpy(buf, p, n);
948 buf += n;
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700949 *buf++ = DELIM;
Victor Stinner0327bde2017-11-23 17:03:20 +0100950 if (q == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 p = q+1;
954 }
955 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100956 if (calculate->argv0_path) {
957 wcscpy(buf, calculate->argv0_path);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 buf = wcschr(buf, L'\0');
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700959 *buf++ = DELIM;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 }
Steve Dower4a7fe7e2015-05-22 15:10:10 -0700961 *(buf - 1) = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* Now to pull one last hack/trick. If sys.prefix is
964 empty, then try and find it somewhere on the paths
965 we calculated. We scan backwards, as our general policy
966 is that Python core directories are at the *end* of
967 sys.path. We assume that our "lib" directory is
968 on the path, and that our 'prefix' directory is
969 the parent of that.
970 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100971 if (prefix[0] == L'\0') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 wchar_t lookBuf[MAXPATHLEN+1];
973 wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */
974 while (1) {
975 Py_ssize_t nchars;
976 wchar_t *lookEnd = look;
977 /* 'look' will end up one character before the
978 start of the path in question - even if this
979 is one character before the start of the buffer
980 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100981 while (look >= start_buf && *look != DELIM)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 look--;
983 nchars = lookEnd-look;
984 wcsncpy(lookBuf, look+1, nchars);
985 lookBuf[nchars] = L'\0';
986 /* Up one level to the parent */
987 reduce(lookBuf);
Victor Stinner9316ee42017-11-25 03:17:57 +0100988 if (search_for_prefix(prefix, lookBuf, LANDMARK)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 break;
990 }
991 /* If we are out of paths to search - give up */
Victor Stinner0327bde2017-11-23 17:03:20 +0100992 if (look < start_buf) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100994 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 look--;
996 }
997 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100998
999 config->module_search_path = start_buf;
Victor Stinner9316ee42017-11-25 03:17:57 +01001000 return _Py_INIT_OK();
1001}
1002
1003
1004static _PyInitError
1005calculate_path_impl(PyCalculatePath *calculate, PyPathConfig *config,
1006 const _PyMainInterpreterConfig *main_config)
1007{
1008 _PyInitError err;
1009
1010 err = get_dll_path(calculate, config);
1011 if (_Py_INIT_FAILED(err)) {
1012 return err;
1013 }
1014
1015 err = get_program_name(calculate, config);
1016 if (_Py_INIT_FAILED(err)) {
1017 return err;
1018 }
1019
1020 /* program_name guaranteed \0 terminated in MAXPATH+1 bytes. */
1021 wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, config->program_name);
1022 reduce(calculate->argv0_path);
1023
1024 wchar_t prefix[MAXPATHLEN+1];
1025 memset(prefix, 0, sizeof(prefix));
1026
1027 /* Search for a sys.path file */
1028 if (calculate_pth_file(config, prefix)) {
1029 goto done;
1030 }
1031
1032 calculate_pyvenv_file(calculate);
1033
1034 /* Calculate zip archive path from DLL or exe path */
1035 change_ext(calculate->zip_path,
1036 config->dll_path[0] ? config->dll_path : config->program_name,
1037 L".zip");
1038
1039 calculate_home_prefix(calculate, prefix);
1040
1041 err = calculate_module_search_path(calculate, config, prefix);
1042 if (_Py_INIT_FAILED(err)) {
1043 return err;
1044 }
1045
1046done:
1047 config->prefix = _PyMem_RawWcsdup(prefix);
1048 if (config->prefix == NULL) {
1049 return _Py_INIT_NO_MEMORY();
1050 }
1051
1052 return _Py_INIT_OK();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001053}
1054
1055
Victor Stinner0327bde2017-11-23 17:03:20 +01001056static void
1057calculate_free(PyCalculatePath *calculate)
1058{
1059 PyMem_RawFree(calculate->machine_path);
1060 PyMem_RawFree(calculate->user_path);
1061}
1062
Victor Stinner9316ee42017-11-25 03:17:57 +01001063
Victor Stinner0327bde2017-11-23 17:03:20 +01001064static void
Victor Stinner9316ee42017-11-25 03:17:57 +01001065pathconfig_clear(PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +01001066{
Victor Stinner9316ee42017-11-25 03:17:57 +01001067#define CLEAR(ATTR) \
1068 do { \
1069 PyMem_RawFree(ATTR); \
1070 ATTR = NULL; \
1071 } while (0)
1072
1073 CLEAR(config->prefix);
1074 CLEAR(config->program_name);
1075 CLEAR(config->dll_path);
1076 CLEAR(config->module_search_path);
1077#undef CLEAR
1078}
1079
1080
1081/* Initialize paths for Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix()
1082 and Py_GetProgramFullPath() */
1083_PyInitError
1084_PyPathConfig_Init(const _PyMainInterpreterConfig *main_config)
1085{
1086 if (path_config.module_search_path) {
1087 /* Already initialized */
1088 return _Py_INIT_OK();
1089 }
1090
Victor Stinner46972b72017-11-24 22:55:40 +01001091 _PyInitError err;
Victor Stinner9316ee42017-11-25 03:17:57 +01001092
Victor Stinner0327bde2017-11-23 17:03:20 +01001093 PyCalculatePath calculate;
1094 memset(&calculate, 0, sizeof(calculate));
1095
Victor Stinner46972b72017-11-24 22:55:40 +01001096 calculate_init(&calculate, main_config);
1097
Victor Stinner0327bde2017-11-23 17:03:20 +01001098 PyPathConfig new_path_config;
1099 memset(&new_path_config, 0, sizeof(new_path_config));
1100
Victor Stinner9316ee42017-11-25 03:17:57 +01001101 err = calculate_path_impl(&calculate, &new_path_config, main_config);
1102 if (_Py_INIT_FAILED(err)) {
1103 goto done;
1104 }
1105
Victor Stinner0327bde2017-11-23 17:03:20 +01001106 path_config = new_path_config;
Victor Stinner9316ee42017-11-25 03:17:57 +01001107 err = _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001108
Victor Stinner9316ee42017-11-25 03:17:57 +01001109done:
1110 if (_Py_INIT_FAILED(err)) {
1111 pathconfig_clear(&new_path_config);
Victor Stinner46972b72017-11-24 22:55:40 +01001112 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001113 calculate_free(&calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +01001114 return err;
Victor Stinner0327bde2017-11-23 17:03:20 +01001115}
1116
1117
Victor Stinner9316ee42017-11-25 03:17:57 +01001118static void
1119calculate_path(void)
1120{
1121 _PyInitError err;
1122 _PyMainInterpreterConfig config = _PyMainInterpreterConfig_INIT;
1123
1124 err = _PyMainInterpreterConfig_ReadEnv(&config);
1125 if (!_Py_INIT_FAILED(err)) {
1126 err = _PyPathConfig_Init(&config);
1127 }
1128 _PyMainInterpreterConfig_Clear(&config);
1129
1130 if (_Py_INIT_FAILED(err)) {
1131 _Py_FatalInitError(err);
1132 }
1133}
1134
1135
1136void
1137_PyPathConfig_Fini(void)
1138{
1139 pathconfig_clear(&path_config);
1140}
1141
Victor Stinner0327bde2017-11-23 17:03:20 +01001142
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001143/* External interface */
1144
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001145void
1146Py_SetPath(const wchar_t *path)
1147{
Victor Stinner0327bde2017-11-23 17:03:20 +01001148 if (path_config.module_search_path != NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001149 pathconfig_clear(&path_config);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001150 }
Victor Stinner0327bde2017-11-23 17:03:20 +01001151
1152 if (path == NULL) {
1153 return;
1154 }
1155
Victor Stinner9316ee42017-11-25 03:17:57 +01001156 PyPathConfig new_config;
1157 new_config.program_name = _PyMem_RawWcsdup(Py_GetProgramName());
1158 new_config.prefix = _PyMem_RawWcsdup(L"");
1159 new_config.dll_path = _PyMem_RawWcsdup(L"");
1160 new_config.module_search_path = _PyMem_RawWcsdup(path);
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +00001161
Victor Stinner9316ee42017-11-25 03:17:57 +01001162 pathconfig_clear(&path_config);
1163 path_config = new_config;
Victor Stinnerd4341102017-11-23 00:12:09 +01001164}
1165
Victor Stinner0327bde2017-11-23 17:03:20 +01001166
Victor Stinnerd4341102017-11-23 00:12:09 +01001167wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001168Py_GetPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001169{
Victor Stinner0327bde2017-11-23 17:03:20 +01001170 if (!path_config.module_search_path) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001171 calculate_path();
Victor Stinner0327bde2017-11-23 17:03:20 +01001172 }
1173 return path_config.module_search_path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001174}
1175
Victor Stinner0327bde2017-11-23 17:03:20 +01001176
Martin v. Löwis790465f2008-04-05 20:41:37 +00001177wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001178Py_GetPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001179{
Victor Stinner0327bde2017-11-23 17:03:20 +01001180 if (!path_config.module_search_path) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001181 calculate_path();
Victor Stinner0327bde2017-11-23 17:03:20 +01001182 }
1183 return path_config.prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001184}
1185
Victor Stinner0327bde2017-11-23 17:03:20 +01001186
Martin v. Löwis790465f2008-04-05 20:41:37 +00001187wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001188Py_GetExecPrefix(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001191}
1192
Victor Stinner0327bde2017-11-23 17:03:20 +01001193
Martin v. Löwis790465f2008-04-05 20:41:37 +00001194wchar_t *
Thomas Wouters78890102000-07-22 19:25:51 +00001195Py_GetProgramFullPath(void)
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001196{
Victor Stinner0327bde2017-11-23 17:03:20 +01001197 if (!path_config.module_search_path) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001198 calculate_path();
Victor Stinner0327bde2017-11-23 17:03:20 +01001199 }
Victor Stinnerf04ebe22017-11-25 00:01:23 +01001200 return path_config.program_name;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001201}
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001202
Victor Stinner0327bde2017-11-23 17:03:20 +01001203
Victor Stinner63941882011-09-29 00:42:28 +02001204/* Load python3.dll before loading any extension module that might refer
1205 to it. That way, we can be sure that always the python3.dll corresponding
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001206 to this python DLL is loaded, not a python3.dll that might be on the path
1207 by chance.
1208 Return whether the DLL was found.
1209*/
1210static int python3_checked = 0;
1211static HANDLE hPython3;
1212int
1213_Py_CheckPython3()
1214{
1215 wchar_t py3path[MAXPATHLEN+1];
1216 wchar_t *s;
Victor Stinner0327bde2017-11-23 17:03:20 +01001217 if (python3_checked) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001218 return hPython3 != NULL;
Victor Stinner0327bde2017-11-23 17:03:20 +01001219 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001220 python3_checked = 1;
1221
1222 /* If there is a python3.dll next to the python3y.dll,
1223 assume this is a build tree; use that DLL */
Victor Stinner9316ee42017-11-25 03:17:57 +01001224 wcscpy(py3path, path_config.dll_path);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001225 s = wcsrchr(py3path, L'\\');
Victor Stinner0327bde2017-11-23 17:03:20 +01001226 if (!s) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001227 s = py3path;
Victor Stinner0327bde2017-11-23 17:03:20 +01001228 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001229 wcscpy(s, L"\\python3.dll");
1230 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
Victor Stinner0327bde2017-11-23 17:03:20 +01001231 if (hPython3 != NULL) {
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001232 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +01001233 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001234
1235 /* Check sys.prefix\DLLs\python3.dll */
1236 wcscpy(py3path, Py_GetPrefix());
1237 wcscat(py3path, L"\\DLLs\\python3.dll");
1238 hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
1239 return hPython3 != NULL;
1240}