blob: 18df795c93fc96d8fa9f191c93457b426f88ca4f [file] [log] [blame]
Guido van Rossum582646a1996-05-28 22:30:17 +00001/* Return the initial module search path. */
2
Guido van Rossum667d7041995-08-04 04:20:48 +00003#include "Python.h"
4#include "osdefs.h"
Victor Stinner9fc57a32018-11-07 00:44:03 +01005#include "pycore_fileutils.h"
Victor Stinnera1c249c2018-11-01 03:15:58 +01006#include "pycore_pathconfig.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01007#include "pycore_pystate.h"
Guido van Rossum667d7041995-08-04 04:20:48 +00008
Guido van Rossum305e5d01997-04-11 17:18:45 +00009#include <sys/types.h>
Guido van Rossum21f84971997-06-02 22:18:31 +000010#include <string.h>
Guido van Rossum667d7041995-08-04 04:20:48 +000011
Brett Cannonf6af76d2004-06-26 04:03:06 +000012#ifdef __APPLE__
Victor Stinner0327bde2017-11-23 17:03:20 +010013# include <mach-o/dyld.h>
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000014#endif
15
Guido van Rossum305e5d01997-04-11 17:18:45 +000016/* Search in some common locations for the associated Python libraries.
17 *
18 * Two directories must be found, the platform independent directory
Barry Warsaw90126031997-04-11 20:27:03 +000019 * (prefix), containing the common .py and .pyc files, and the platform
20 * dependent directory (exec_prefix), containing the shared library
21 * modules. Note that prefix and exec_prefix can be the same directory,
22 * but for some installations, they are different.
Guido van Rossum305e5d01997-04-11 17:18:45 +000023 *
Barry Warsaw90126031997-04-11 20:27:03 +000024 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
25 * Each search tries a number of different locations until a ``landmark''
26 * file or directory is found. If no prefix or exec_prefix is found, a
27 * warning message is issued and the preprocessor defined PREFIX and
28 * EXEC_PREFIX are used (even though they will not work); python carries on
29 * as best as is possible, but most imports will fail.
Guido van Rossum305e5d01997-04-11 17:18:45 +000030 *
31 * Before any searches are done, the location of the executable is
Guido van Rossumd8faa362007-04-27 19:54:29 +000032 * determined. If argv[0] has one or more slashes in it, it is used
Barry Warsaw90126031997-04-11 20:27:03 +000033 * unchanged. Otherwise, it must have been invoked from the shell's path,
34 * so we search $PATH for the named executable and use that. If the
35 * executable was not found on $PATH (or there was no $PATH environment
36 * variable), the original argv[0] string is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000037 *
Barry Warsaw90126031997-04-11 20:27:03 +000038 * Next, the executable location is examined to see if it is a symbolic
39 * link. If so, the link is chased (correctly interpreting a relative
40 * pathname if one is found) and the directory of the link target is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000041 *
Barry Warsaw90126031997-04-11 20:27:03 +000042 * Finally, argv0_path is set to the directory containing the executable
43 * (i.e. the last component is stripped).
Guido van Rossum305e5d01997-04-11 17:18:45 +000044 *
Barry Warsaw90126031997-04-11 20:27:03 +000045 * With argv0_path in hand, we perform a number of steps. The same steps
46 * are performed for prefix and for exec_prefix, but with a different
47 * landmark.
Guido van Rossum305e5d01997-04-11 17:18:45 +000048 *
49 * Step 1. Are we running python out of the build directory? This is
50 * checked by looking for a different kind of landmark relative to
Barry Warsaw90126031997-04-11 20:27:03 +000051 * argv0_path. For prefix, the landmark's path is derived from the VPATH
52 * preprocessor variable (taking into account that its value is almost, but
53 * not quite, what we need). For exec_prefix, the landmark is
Antoine Pitroueba57b62010-08-14 12:33:18 +000054 * pybuilddir.txt. If the landmark is found, we're done.
Guido van Rossum305e5d01997-04-11 17:18:45 +000055 *
56 * For the remaining steps, the prefix landmark will always be
Jeremy Hylton847a9962000-05-26 21:49:07 +000057 * lib/python$VERSION/os.py and the exec_prefix will always be
Guido van Rossum266033e1997-10-20 23:20:32 +000058 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
Barry Warsaw90126031997-04-11 20:27:03 +000059 * number as supplied by the Makefile. Note that this means that no more
60 * build directory checking is performed; if the first step did not find
61 * the landmarks, the assumption is that python is running from an
62 * installed setup.
Guido van Rossum305e5d01997-04-11 17:18:45 +000063 *
64 * Step 2. See if the $PYTHONHOME environment variable points to the
Barry Warsaw90126031997-04-11 20:27:03 +000065 * installed location of the Python libraries. If $PYTHONHOME is set, then
66 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
67 * directory, which is used for both, or the prefix and exec_prefix
68 * directories separated by a colon.
Guido van Rossum305e5d01997-04-11 17:18:45 +000069 *
70 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
Barry Warsaw90126031997-04-11 20:27:03 +000071 * backtracking up the path until it is exhausted. This is the most common
72 * step to succeed. Note that if prefix and exec_prefix are different,
73 * exec_prefix is more likely to be found; however if exec_prefix is a
74 * subdirectory of prefix, both will be found.
Guido van Rossum305e5d01997-04-11 17:18:45 +000075 *
Barry Warsaw90126031997-04-11 20:27:03 +000076 * Step 4. Search the directories pointed to by the preprocessor variables
77 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
78 * passed in as options to the configure script.
Guido van Rossum305e5d01997-04-11 17:18:45 +000079 *
Barry Warsaw90126031997-04-11 20:27:03 +000080 * That's it!
81 *
82 * Well, almost. Once we have determined prefix and exec_prefix, the
Thomas Wouters7e474022000-07-16 12:04:32 +000083 * preprocessor variable PYTHONPATH is used to construct a path. Each
Barry Warsaw90126031997-04-11 20:27:03 +000084 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
85 * containing the shared library modules is appended. The environment
86 * variable $PYTHONPATH is inserted in front of it all. Finally, the
87 * prefix and exec_prefix globals are tweaked so they reflect the values
88 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
89 * off. If either points to the build directory, the globals are reset to
90 * the corresponding preprocessor variables (so sys.prefix will reflect the
91 * installation location, even though sys.path points into the build
92 * directory). This seems to make more sense given that currently the only
93 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
94 * process to find the installed Python tree.
Antoine Pitroueba57b62010-08-14 12:33:18 +000095 *
Kristján Valur Jónsson3b69db22010-09-27 05:32:54 +000096 * An embedding application can use Py_SetPath() to override all of
97 * these authomatic path computations.
98 *
Antoine Pitroueba57b62010-08-14 12:33:18 +000099 * NOTE: Windows MSVC builds use PC/getpathp.c instead!
Barry Warsaw90126031997-04-11 20:27:03 +0000100 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000101
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102#ifdef __cplusplus
Victor Stinner0327bde2017-11-23 17:03:20 +0100103extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104#endif
105
106
Benjamin Petersonf5854142016-06-02 12:41:35 -0700107#if !defined(PREFIX) || !defined(EXEC_PREFIX) || !defined(VERSION) || !defined(VPATH)
108#error "PREFIX, EXEC_PREFIX, VERSION, and VPATH must be constant defined"
Guido van Rossum305e5d01997-04-11 17:18:45 +0000109#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000110
Guido van Rossum305e5d01997-04-11 17:18:45 +0000111#ifndef LANDMARK
Martin v. Löwis790465f2008-04-05 20:41:37 +0000112#define LANDMARK L"os.py"
Guido van Rossum305e5d01997-04-11 17:18:45 +0000113#endif
Guido van Rossume296ced2001-09-28 20:00:29 +0000114
Victor Stinner9316ee42017-11-25 03:17:57 +0100115#define DECODE_LOCALE_ERR(NAME, LEN) \
116 ((LEN) == (size_t)-2) \
Victor Stinner94540602017-12-16 04:54:22 +0100117 ? _Py_INIT_USER_ERR("cannot decode " NAME) \
Victor Stinner9316ee42017-11-25 03:17:57 +0100118 : _Py_INIT_NO_MEMORY()
119
Victor Stinner0327bde2017-11-23 17:03:20 +0100120typedef struct {
121 wchar_t *path_env; /* PATH environment variable */
Victor Stinner0327bde2017-11-23 17:03:20 +0100122
Victor Stinner0327bde2017-11-23 17:03:20 +0100123 wchar_t *pythonpath; /* PYTHONPATH define */
124 wchar_t *prefix; /* PREFIX define */
125 wchar_t *exec_prefix; /* EXEC_PREFIX define */
126
127 wchar_t *lib_python; /* "lib/pythonX.Y" */
128 wchar_t argv0_path[MAXPATHLEN+1];
129 wchar_t zip_path[MAXPATHLEN+1]; /* ".../lib/pythonXY.zip" */
130
131 int prefix_found; /* found platform independent libraries? */
132 int exec_prefix_found; /* found the platform dependent libraries? */
133} PyCalculatePath;
134
135static const wchar_t delimiter[2] = {DELIM, '\0'};
136static const wchar_t separator[2] = {SEP, '\0'};
Victor Stinner0327bde2017-11-23 17:03:20 +0100137
Martin v. Löwis790465f2008-04-05 20:41:37 +0000138
Victor Stinner91afbb62015-03-24 12:16:28 +0100139/* Get file status. Encode the path to the locale encoding. */
Victor Stinner91afbb62015-03-24 12:16:28 +0100140static int
141_Py_wstat(const wchar_t* path, struct stat *buf)
142{
143 int err;
144 char *fname;
Victor Stinner9dd76202017-12-21 16:20:32 +0100145 fname = _Py_EncodeLocaleRaw(path, NULL);
Victor Stinner91afbb62015-03-24 12:16:28 +0100146 if (fname == NULL) {
147 errno = EINVAL;
148 return -1;
149 }
150 err = stat(fname, buf);
Victor Stinner9dd76202017-12-21 16:20:32 +0100151 PyMem_RawFree(fname);
Victor Stinner91afbb62015-03-24 12:16:28 +0100152 return err;
153}
154
Victor Stinner0327bde2017-11-23 17:03:20 +0100155
Guido van Rossum305e5d01997-04-11 17:18:45 +0000156static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000157reduce(wchar_t *dir)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000158{
Martin v. Löwis790465f2008-04-05 20:41:37 +0000159 size_t i = wcslen(dir);
Fred Drakeedabdc12000-07-08 06:16:37 +0000160 while (i > 0 && dir[i] != SEP)
161 --i;
162 dir[i] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000163}
Guido van Rossumd29806c1998-01-19 22:06:22 +0000164
Victor Stinner0327bde2017-11-23 17:03:20 +0100165
Guido van Rossum305e5d01997-04-11 17:18:45 +0000166static int
Martin v. Löwis790465f2008-04-05 20:41:37 +0000167isfile(wchar_t *filename) /* Is file, not directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000168{
Fred Drakeedabdc12000-07-08 06:16:37 +0000169 struct stat buf;
Victor Stinner0327bde2017-11-23 17:03:20 +0100170 if (_Py_wstat(filename, &buf) != 0) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000171 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100172 }
173 if (!S_ISREG(buf.st_mode)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000174 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100175 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000176 return 1;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000177}
178
179
180static int
Brett Cannonf299abd2015-04-13 14:21:02 -0400181ismodule(wchar_t *filename) /* Is module -- check for .pyc too */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000182{
Victor Stinner0327bde2017-11-23 17:03:20 +0100183 if (isfile(filename)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000184 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100185 }
Guido van Rossumd29806c1998-01-19 22:06:22 +0000186
Fred Drakeedabdc12000-07-08 06:16:37 +0000187 /* Check for the compiled version of prefix. */
Martin v. Löwis790465f2008-04-05 20:41:37 +0000188 if (wcslen(filename) < MAXPATHLEN) {
Brett Cannonf299abd2015-04-13 14:21:02 -0400189 wcscat(filename, L"c");
Victor Stinner0327bde2017-11-23 17:03:20 +0100190 if (isfile(filename)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000191 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100192 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000193 }
194 return 0;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000195}
196
197
Victor Stinner0327bde2017-11-23 17:03:20 +0100198/* Is executable file */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000199static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100200isxfile(wchar_t *filename)
Guido van Rossumd29806c1998-01-19 22:06:22 +0000201{
Fred Drakeedabdc12000-07-08 06:16:37 +0000202 struct stat buf;
Victor Stinner0327bde2017-11-23 17:03:20 +0100203 if (_Py_wstat(filename, &buf) != 0) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000204 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100205 }
206 if (!S_ISREG(buf.st_mode)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000207 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100208 }
209 if ((buf.st_mode & 0111) == 0) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000210 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100211 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000212 return 1;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000213}
214
215
Victor Stinner0327bde2017-11-23 17:03:20 +0100216/* Is directory */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000217static int
Victor Stinner0327bde2017-11-23 17:03:20 +0100218isdir(wchar_t *filename)
Guido van Rossumd29806c1998-01-19 22:06:22 +0000219{
Fred Drakeedabdc12000-07-08 06:16:37 +0000220 struct stat buf;
Victor Stinner0327bde2017-11-23 17:03:20 +0100221 if (_Py_wstat(filename, &buf) != 0) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000222 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100223 }
224 if (!S_ISDIR(buf.st_mode)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000225 return 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100226 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000227 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000228}
229
230
Tim Petersec8c5a92004-08-08 01:00:47 +0000231/* Add a path component, by appending stuff to buffer.
232 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
233 NUL-terminated string with no more than MAXPATHLEN characters (not counting
234 the trailing NUL). It's a fatal error if it contains a string longer than
235 that (callers must be careful!). If these requirements are met, it's
236 guaranteed that buffer will still be a NUL-terminated string with no more
237 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
238 stuff as fits will be appended.
Jeremy Hylton6372fe12000-09-27 20:51:17 +0000239*/
Guido van Rossum305e5d01997-04-11 17:18:45 +0000240static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000241joinpath(wchar_t *buffer, wchar_t *stuff)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000242{
Fred Drakeedabdc12000-07-08 06:16:37 +0000243 size_t n, k;
Victor Stinner0327bde2017-11-23 17:03:20 +0100244 if (stuff[0] == SEP) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000245 n = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100246 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000247 else {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000248 n = wcslen(buffer);
Victor Stinner0327bde2017-11-23 17:03:20 +0100249 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000250 buffer[n++] = SEP;
Victor Stinner0327bde2017-11-23 17:03:20 +0100251 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000252 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100253 if (n > MAXPATHLEN) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Py_FatalError("buffer overflow in getpath.c's joinpath()");
Victor Stinner0327bde2017-11-23 17:03:20 +0100255 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000256 k = wcslen(stuff);
Victor Stinner0327bde2017-11-23 17:03:20 +0100257 if (n + k > MAXPATHLEN) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000258 k = MAXPATHLEN - n;
Victor Stinner0327bde2017-11-23 17:03:20 +0100259 }
Martin v. Löwis790465f2008-04-05 20:41:37 +0000260 wcsncpy(buffer+n, stuff, k);
Fred Drakeedabdc12000-07-08 06:16:37 +0000261 buffer[n+k] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000262}
263
Victor Stinner0327bde2017-11-23 17:03:20 +0100264
Guido van Rossume296ced2001-09-28 20:00:29 +0000265/* copy_absolute requires that path be allocated at least
266 MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
Jeremy Hylton7198ba92000-09-25 17:00:24 +0000267static void
Victor Stinnerf4061da2010-10-14 12:37:19 +0000268copy_absolute(wchar_t *path, wchar_t *p, size_t pathlen)
Jeremy Hylton7198ba92000-09-25 17:00:24 +0000269{
Victor Stinner0327bde2017-11-23 17:03:20 +0100270 if (p[0] == SEP) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000271 wcscpy(path, p);
Victor Stinner0327bde2017-11-23 17:03:20 +0100272 }
Jeremy Hylton7198ba92000-09-25 17:00:24 +0000273 else {
Victor Stinnerf4061da2010-10-14 12:37:19 +0000274 if (!_Py_wgetcwd(path, pathlen)) {
Victor Stinner4f3abb02010-10-07 23:29:18 +0000275 /* unable to get the current directory */
276 wcscpy(path, p);
277 return;
278 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100279 if (p[0] == '.' && p[1] == SEP) {
Guido van Rossume296ced2001-09-28 20:00:29 +0000280 p += 2;
Victor Stinner0327bde2017-11-23 17:03:20 +0100281 }
Guido van Rossume296ced2001-09-28 20:00:29 +0000282 joinpath(path, p);
Jeremy Hylton7198ba92000-09-25 17:00:24 +0000283 }
284}
Guido van Rossum305e5d01997-04-11 17:18:45 +0000285
Victor Stinner0327bde2017-11-23 17:03:20 +0100286
Guido van Rossume296ced2001-09-28 20:00:29 +0000287/* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */
288static void
Martin v. Löwis790465f2008-04-05 20:41:37 +0000289absolutize(wchar_t *path)
Guido van Rossume296ced2001-09-28 20:00:29 +0000290{
Victor Stinnerf4061da2010-10-14 12:37:19 +0000291 wchar_t buffer[MAXPATHLEN+1];
Guido van Rossume296ced2001-09-28 20:00:29 +0000292
Victor Stinner0327bde2017-11-23 17:03:20 +0100293 if (path[0] == SEP) {
Guido van Rossume296ced2001-09-28 20:00:29 +0000294 return;
Victor Stinner0327bde2017-11-23 17:03:20 +0100295 }
Victor Stinnerf4061da2010-10-14 12:37:19 +0000296 copy_absolute(buffer, path, MAXPATHLEN+1);
Martin v. Löwis790465f2008-04-05 20:41:37 +0000297 wcscpy(path, buffer);
Guido van Rossume296ced2001-09-28 20:00:29 +0000298}
299
Victor Stinner0327bde2017-11-23 17:03:20 +0100300
E. M. Bray7a7693e2018-10-05 13:38:50 +0200301#if defined(__CYGWIN__) || defined(__MINGW32__)
302/* add_exe_suffix requires that progpath be allocated at least
303 MAXPATHLEN + 1 bytes.
304*/
305
306#ifndef EXE_SUFFIX
307#define EXE_SUFFIX L".exe"
308#endif
309
310static void
311add_exe_suffix(wchar_t *progpath)
312{
313 /* Check for already have an executable suffix */
314 size_t n = wcslen(progpath);
315 size_t s = wcslen(EXE_SUFFIX);
316 if (wcsncasecmp(EXE_SUFFIX, progpath+n-s, s) != 0) {
317 if (n + s > MAXPATHLEN) {
318 Py_FatalError("progpath overflow in getpath.c's add_exe_suffix()");
319 }
320 /* Save original path for revert */
321 wchar_t orig[MAXPATHLEN+1];
322 wcsncpy(orig, progpath, MAXPATHLEN);
323
324 wcsncpy(progpath+n, EXE_SUFFIX, s);
325 progpath[n+s] = '\0';
326
327 if (!isxfile(progpath)) {
328 /* Path that added suffix is invalid */
329 wcsncpy(progpath, orig, MAXPATHLEN);
330 }
331 }
332}
333#endif
334
335
Guido van Rossume296ced2001-09-28 20:00:29 +0000336/* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
Jeremy Hylton6372fe12000-09-27 20:51:17 +0000337 bytes long.
338*/
Guido van Rossum305e5d01997-04-11 17:18:45 +0000339static int
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100340search_for_prefix(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100341 PyCalculatePath *calculate, wchar_t *prefix)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000342{
Fred Drakeedabdc12000-07-08 06:16:37 +0000343 size_t n;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000344 wchar_t *vpath;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000345
Fred Drakeedabdc12000-07-08 06:16:37 +0000346 /* If PYTHONHOME is set, we believe it unconditionally */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100347 if (core_config->home) {
348 wcsncpy(prefix, core_config->home, MAXPATHLEN);
Victor Stinner9316ee42017-11-25 03:17:57 +0100349 prefix[MAXPATHLEN] = L'\0';
350 wchar_t *delim = wcschr(prefix, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100351 if (delim) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000352 *delim = L'\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100353 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100354 joinpath(prefix, calculate->lib_python);
355 joinpath(prefix, LANDMARK);
Fred Drakeedabdc12000-07-08 06:16:37 +0000356 return 1;
357 }
Jeremy Hylton847a9962000-05-26 21:49:07 +0000358
Fred Drakeedabdc12000-07-08 06:16:37 +0000359 /* Check to see if argv[0] is in the build directory */
Victor Stinner9316ee42017-11-25 03:17:57 +0100360 wcsncpy(prefix, calculate->argv0_path, MAXPATHLEN);
361 prefix[MAXPATHLEN] = L'\0';
Antoine Pitrou961d54c2018-07-16 19:03:03 +0200362 joinpath(prefix, L"Modules/Setup.local");
Victor Stinner9316ee42017-11-25 03:17:57 +0100363 if (isfile(prefix)) {
Neil Schemenauer6cf07022001-01-24 17:13:11 +0000364 /* Check VPATH to see if argv0_path is in the build directory. */
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200365 vpath = Py_DecodeLocale(VPATH, NULL);
Victor Stinner21582312010-10-23 00:13:28 +0000366 if (vpath != NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100367 wcsncpy(prefix, calculate->argv0_path, MAXPATHLEN);
368 prefix[MAXPATHLEN] = L'\0';
369 joinpath(prefix, vpath);
Victor Stinner1a7425f2013-07-07 16:25:15 +0200370 PyMem_RawFree(vpath);
Victor Stinner9316ee42017-11-25 03:17:57 +0100371 joinpath(prefix, L"Lib");
372 joinpath(prefix, LANDMARK);
373 if (ismodule(prefix)) {
Victor Stinner21582312010-10-23 00:13:28 +0000374 return -1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100375 }
Victor Stinner21582312010-10-23 00:13:28 +0000376 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000377 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000378
Fred Drakeedabdc12000-07-08 06:16:37 +0000379 /* Search from argv0_path, until root is found */
Victor Stinner9316ee42017-11-25 03:17:57 +0100380 copy_absolute(prefix, calculate->argv0_path, MAXPATHLEN+1);
Fred Drakeedabdc12000-07-08 06:16:37 +0000381 do {
Victor Stinner9316ee42017-11-25 03:17:57 +0100382 n = wcslen(prefix);
383 joinpath(prefix, calculate->lib_python);
384 joinpath(prefix, LANDMARK);
385 if (ismodule(prefix)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000386 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100387 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100388 prefix[n] = L'\0';
389 reduce(prefix);
390 } while (prefix[0]);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000391
Fred Drakeedabdc12000-07-08 06:16:37 +0000392 /* Look at configure's PREFIX */
Victor Stinner9316ee42017-11-25 03:17:57 +0100393 wcsncpy(prefix, calculate->prefix, MAXPATHLEN);
394 prefix[MAXPATHLEN] = L'\0';
395 joinpath(prefix, calculate->lib_python);
396 joinpath(prefix, LANDMARK);
397 if (ismodule(prefix)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000398 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100399 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000400
Fred Drakeedabdc12000-07-08 06:16:37 +0000401 /* Fail */
402 return 0;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000403}
404
405
Victor Stinner0327bde2017-11-23 17:03:20 +0100406static void
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100407calculate_prefix(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100408 PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100409{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100410 calculate->prefix_found = search_for_prefix(core_config, calculate, prefix);
Victor Stinner0327bde2017-11-23 17:03:20 +0100411 if (!calculate->prefix_found) {
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200412 if (!core_config->_frozen) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100413 fprintf(stderr,
414 "Could not find platform independent libraries <prefix>\n");
415 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100416 wcsncpy(prefix, calculate->prefix, MAXPATHLEN);
417 joinpath(prefix, calculate->lib_python);
Victor Stinner0327bde2017-11-23 17:03:20 +0100418 }
419 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100420 reduce(prefix);
Victor Stinner0327bde2017-11-23 17:03:20 +0100421 }
422}
423
424
425static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100426calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100427{
428 /* Reduce prefix and exec_prefix to their essence,
429 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
430 * If we're loading relative to the build directory,
431 * return the compiled-in defaults instead.
432 */
433 if (calculate->prefix_found > 0) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100434 reduce(prefix);
435 reduce(prefix);
Victor Stinner0327bde2017-11-23 17:03:20 +0100436 /* The prefix is the root directory, but reduce() chopped
437 * off the "/". */
Victor Stinner9316ee42017-11-25 03:17:57 +0100438 if (!prefix[0]) {
439 wcscpy(prefix, separator);
Victor Stinner0327bde2017-11-23 17:03:20 +0100440 }
441 }
442 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100443 wcsncpy(prefix, calculate->prefix, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100444 }
445}
446
447
Jeremy Hylton6372fe12000-09-27 20:51:17 +0000448/* search_for_exec_prefix requires that argv0_path be no more than
Guido van Rossume296ced2001-09-28 20:00:29 +0000449 MAXPATHLEN bytes long.
Jeremy Hylton6372fe12000-09-27 20:51:17 +0000450*/
Guido van Rossum305e5d01997-04-11 17:18:45 +0000451static int
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100452search_for_exec_prefix(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100453 PyCalculatePath *calculate, wchar_t *exec_prefix)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000454{
Fred Drakeedabdc12000-07-08 06:16:37 +0000455 size_t n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000456
Fred Drakeedabdc12000-07-08 06:16:37 +0000457 /* If PYTHONHOME is set, we believe it unconditionally */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100458 if (core_config->home) {
459 wchar_t *delim = wcschr(core_config->home, DELIM);
Victor Stinner0327bde2017-11-23 17:03:20 +0100460 if (delim) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100461 wcsncpy(exec_prefix, delim+1, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100462 }
463 else {
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100464 wcsncpy(exec_prefix, core_config->home, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100465 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100466 exec_prefix[MAXPATHLEN] = L'\0';
467 joinpath(exec_prefix, calculate->lib_python);
468 joinpath(exec_prefix, L"lib-dynload");
Fred Drakeedabdc12000-07-08 06:16:37 +0000469 return 1;
470 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000471
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000472 /* Check to see if argv[0] is in the build directory. "pybuilddir.txt"
473 is written by setup.py and contains the relative path to the location
474 of shared library modules. */
Victor Stinner9316ee42017-11-25 03:17:57 +0100475 wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
476 exec_prefix[MAXPATHLEN] = L'\0';
477 joinpath(exec_prefix, L"pybuilddir.txt");
478 if (isfile(exec_prefix)) {
479 FILE *f = _Py_wfopen(exec_prefix, L"rb");
Victor Stinner0327bde2017-11-23 17:03:20 +0100480 if (f == NULL) {
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000481 errno = 0;
Victor Stinner0327bde2017-11-23 17:03:20 +0100482 }
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000483 else {
484 char buf[MAXPATHLEN+1];
Victor Stinner9bee3292017-12-21 16:49:13 +0100485 wchar_t *rel_builddir_path;
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000486 n = fread(buf, 1, MAXPATHLEN, f);
487 buf[n] = '\0';
488 fclose(f);
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100489 rel_builddir_path = _Py_DecodeUTF8_surrogateescape(buf, n);
490 if (rel_builddir_path) {
Victor Stinner9bee3292017-12-21 16:49:13 +0100491 wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
492 exec_prefix[MAXPATHLEN] = L'\0';
493 joinpath(exec_prefix, rel_builddir_path);
494 PyMem_RawFree(rel_builddir_path );
495 return -1;
Antoine Pitroue9b428f2010-08-13 22:25:01 +0000496 }
497 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000498 }
Jeremy Hylton847a9962000-05-26 21:49:07 +0000499
Fred Drakeedabdc12000-07-08 06:16:37 +0000500 /* Search from argv0_path, until root is found */
Victor Stinner9316ee42017-11-25 03:17:57 +0100501 copy_absolute(exec_prefix, calculate->argv0_path, MAXPATHLEN+1);
Fred Drakeedabdc12000-07-08 06:16:37 +0000502 do {
Victor Stinner9316ee42017-11-25 03:17:57 +0100503 n = wcslen(exec_prefix);
504 joinpath(exec_prefix, calculate->lib_python);
505 joinpath(exec_prefix, L"lib-dynload");
506 if (isdir(exec_prefix)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000507 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100508 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100509 exec_prefix[n] = L'\0';
510 reduce(exec_prefix);
511 } while (exec_prefix[0]);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000512
Fred Drakeedabdc12000-07-08 06:16:37 +0000513 /* Look at configure's EXEC_PREFIX */
Victor Stinner9316ee42017-11-25 03:17:57 +0100514 wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN);
515 exec_prefix[MAXPATHLEN] = L'\0';
516 joinpath(exec_prefix, calculate->lib_python);
517 joinpath(exec_prefix, L"lib-dynload");
518 if (isdir(exec_prefix)) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000519 return 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100520 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000521
Fred Drakeedabdc12000-07-08 06:16:37 +0000522 /* Fail */
523 return 0;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000524}
525
Guido van Rossum305e5d01997-04-11 17:18:45 +0000526
Victor Stinner0327bde2017-11-23 17:03:20 +0100527static void
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100528calculate_exec_prefix(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100529 PyCalculatePath *calculate, wchar_t *exec_prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100530{
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100531 calculate->exec_prefix_found = search_for_exec_prefix(core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100532 calculate,
533 exec_prefix);
Victor Stinner0327bde2017-11-23 17:03:20 +0100534 if (!calculate->exec_prefix_found) {
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200535 if (!core_config->_frozen) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100536 fprintf(stderr,
537 "Could not find platform dependent libraries <exec_prefix>\n");
538 }
Victor Stinner9316ee42017-11-25 03:17:57 +0100539 wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN);
540 joinpath(exec_prefix, L"lib/lib-dynload");
Victor Stinner0327bde2017-11-23 17:03:20 +0100541 }
542 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
543}
544
545
546static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100547calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100548{
549 if (calculate->exec_prefix_found > 0) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100550 reduce(exec_prefix);
551 reduce(exec_prefix);
552 reduce(exec_prefix);
553 if (!exec_prefix[0]) {
554 wcscpy(exec_prefix, separator);
Victor Stinner0327bde2017-11-23 17:03:20 +0100555 }
556 }
557 else {
Victor Stinner9316ee42017-11-25 03:17:57 +0100558 wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100559 }
560}
561
562
Victor Stinner9316ee42017-11-25 03:17:57 +0100563static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100564calculate_program_full_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100565 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100566{
Victor Stinnerb64de462017-12-01 18:27:09 +0100567 wchar_t program_full_path[MAXPATHLEN+1];
568 memset(program_full_path, 0, sizeof(program_full_path));
Victor Stinner9316ee42017-11-25 03:17:57 +0100569
Victor Stinnerb9197952017-11-23 19:02:04 +0100570#ifdef __APPLE__
571#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
572 uint32_t nsexeclength = MAXPATHLEN;
573#else
574 unsigned long nsexeclength = MAXPATHLEN;
575#endif
576 char execpath[MAXPATHLEN+1];
577#endif
578
Victor Stinner0327bde2017-11-23 17:03:20 +0100579 /* If there is no slash in the argv0 path, then we have to
580 * assume python is on the user's $PATH, since there's no
581 * other way to find a directory to start the search from. If
582 * $PATH isn't exported, you lose.
583 */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100584 if (wcschr(core_config->program_name, SEP)) {
585 wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100586 }
Jack Jansen1afd4802004-06-03 14:33:03 +0000587#ifdef __APPLE__
Jack Jansen1afd4802004-06-03 14:33:03 +0000588 /* On Mac OS X, if a script uses an interpreter of the form
589 * "#!/opt/python2.3/bin/python", the kernel only passes "python"
590 * as argv[0], which falls through to the $PATH search below.
591 * If /opt/python2.3/bin isn't in your path, or is near the end,
592 * this algorithm may incorrectly find /usr/bin/python. To work
593 * around this, we can use _NSGetExecutablePath to get a better
594 * hint of what the intended interpreter was, although this
595 * will fail if a relative path was used. but in that case,
596 * absolutize() should help us out below
597 */
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100598 else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) &&
599 execpath[0] == SEP)
600 {
Victor Stinner31a83932017-12-04 13:39:15 +0100601 size_t len;
602 wchar_t *path = Py_DecodeLocale(execpath, &len);
603 if (path == NULL) {
604 return DECODE_LOCALE_ERR("executable path", len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 }
Victor Stinner31a83932017-12-04 13:39:15 +0100606 wcsncpy(program_full_path, path, MAXPATHLEN);
607 PyMem_RawFree(path);
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000608 }
Brett Cannon6cc48142004-06-24 00:48:44 +0000609#endif /* __APPLE__ */
Victor Stinner0327bde2017-11-23 17:03:20 +0100610 else if (calculate->path_env) {
611 wchar_t *path = calculate->path_env;
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000612 while (1) {
613 wchar_t *delim = wcschr(path, DELIM);
Jack Jansen55070f52001-12-02 23:56:28 +0000614
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000615 if (delim) {
616 size_t len = delim - path;
Victor Stinner0327bde2017-11-23 17:03:20 +0100617 if (len > MAXPATHLEN) {
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000618 len = MAXPATHLEN;
Victor Stinner0327bde2017-11-23 17:03:20 +0100619 }
Victor Stinnerb64de462017-12-01 18:27:09 +0100620 wcsncpy(program_full_path, path, len);
621 program_full_path[len] = '\0';
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000622 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100623 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100624 wcsncpy(program_full_path, path, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100625 }
Jack Jansen55070f52001-12-02 23:56:28 +0000626
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100627 joinpath(program_full_path, core_config->program_name);
Victor Stinnerb64de462017-12-01 18:27:09 +0100628 if (isxfile(program_full_path)) {
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000629 break;
Victor Stinner0327bde2017-11-23 17:03:20 +0100630 }
Jack Jansen55070f52001-12-02 23:56:28 +0000631
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000632 if (!delim) {
Victor Stinnerb64de462017-12-01 18:27:09 +0100633 program_full_path[0] = L'\0';
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000634 break;
635 }
636 path = delim + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000638 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100639 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100640 program_full_path[0] = '\0';
Victor Stinner0327bde2017-11-23 17:03:20 +0100641 }
Victor Stinnerb64de462017-12-01 18:27:09 +0100642 if (program_full_path[0] != SEP && program_full_path[0] != '\0') {
643 absolutize(program_full_path);
Victor Stinner0327bde2017-11-23 17:03:20 +0100644 }
E. M. Bray7a7693e2018-10-05 13:38:50 +0200645#if defined(__CYGWIN__) || defined(__MINGW32__)
646 /* For these platforms it is necessary to ensure that the .exe suffix
647 * is appended to the filename, otherwise there is potential for
648 * sys.executable to return the name of a directory under the same
649 * path (bpo-28441).
650 */
651 if (program_full_path[0] != '\0') {
652 add_exe_suffix(program_full_path);
653 }
654#endif
Victor Stinner9316ee42017-11-25 03:17:57 +0100655
Victor Stinnerb64de462017-12-01 18:27:09 +0100656 config->program_full_path = _PyMem_RawWcsdup(program_full_path);
657 if (config->program_full_path == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100658 return _Py_INIT_NO_MEMORY();
659 }
660 return _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100661}
662
663
Victor Stinner9316ee42017-11-25 03:17:57 +0100664static _PyInitError
Victor Stinnerb64de462017-12-01 18:27:09 +0100665calculate_argv0_path(PyCalculatePath *calculate, const wchar_t *program_full_path)
Victor Stinner0327bde2017-11-23 17:03:20 +0100666{
Victor Stinnerb64de462017-12-01 18:27:09 +0100667 wcsncpy(calculate->argv0_path, program_full_path, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100668 calculate->argv0_path[MAXPATHLEN] = '\0';
Jack Jansen55070f52001-12-02 23:56:28 +0000669
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000670#ifdef WITH_NEXT_FRAMEWORK
Victor Stinner0327bde2017-11-23 17:03:20 +0100671 NSModule pythonModule;
672
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000673 /* On Mac OS X we have a special case if we're running from a framework.
674 ** This is because the python home should be set relative to the library,
675 ** which is in the framework, not relative to the executable, which may
676 ** be outside of the framework. Except when we're in the build directory...
677 */
Fred Drakeedabdc12000-07-08 06:16:37 +0000678 pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
679 /* Use dylib functions to find out where the framework was loaded from */
Victor Stinner0327bde2017-11-23 17:03:20 +0100680 const char* modPath = NSLibraryNameForModule(pythonModule);
Vinay Sajip90db6612012-07-17 17:33:46 +0100681 if (modPath != NULL) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000682 /* We're in a framework. */
Jack Jansene925faf2001-08-15 01:14:40 +0000683 /* See if we might be in the build directory. The framework in the
684 ** build directory is incomplete, it only has the .dylib and a few
685 ** needed symlinks, it doesn't have the Lib directories and such.
686 ** If we're running with the framework from the build directory we must
687 ** be running the interpreter in the build directory, so we use the
688 ** build-directory-specific logic to find Lib and such.
689 */
Victor Stinner9316ee42017-11-25 03:17:57 +0100690 size_t len;
691 wchar_t* wbuf = Py_DecodeLocale(modPath, &len);
Vinay Sajip90db6612012-07-17 17:33:46 +0100692 if (wbuf == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100693 return DECODE_LOCALE_ERR("framework location", len);
Vinay Sajip90db6612012-07-17 17:33:46 +0100694 }
695
Victor Stinner0327bde2017-11-23 17:03:20 +0100696 wcsncpy(calculate->argv0_path, wbuf, MAXPATHLEN);
697 reduce(calculate->argv0_path);
698 joinpath(calculate->argv0_path, calculate->lib_python);
699 joinpath(calculate->argv0_path, LANDMARK);
700 if (!ismodule(calculate->argv0_path)) {
Antoine Pitrou99773ac2010-08-14 12:34:41 +0000701 /* We are in the build directory so use the name of the
702 executable - we know that the absolute path is passed */
Victor Stinnerb64de462017-12-01 18:27:09 +0100703 wcsncpy(calculate->argv0_path, program_full_path, MAXPATHLEN);
Jack Jansene925faf2001-08-15 01:14:40 +0000704 }
705 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100706 /* Use the location of the library as the program_full_path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100707 wcsncpy(calculate->argv0_path, wbuf, MAXPATHLEN);
Jack Jansene925faf2001-08-15 01:14:40 +0000708 }
Victor Stinner1a7425f2013-07-07 16:25:15 +0200709 PyMem_RawFree(wbuf);
Fred Drakeedabdc12000-07-08 06:16:37 +0000710 }
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000711#endif
Guido van Rossume296ced2001-09-28 20:00:29 +0000712
Guido van Rossum305e5d01997-04-11 17:18:45 +0000713#if HAVE_READLINK
Victor Stinner0327bde2017-11-23 17:03:20 +0100714 wchar_t tmpbuffer[MAXPATHLEN+1];
Victor Stinnerb64de462017-12-01 18:27:09 +0100715 int linklen = _Py_wreadlink(program_full_path, tmpbuffer, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100716 while (linklen != -1) {
717 if (tmpbuffer[0] == SEP) {
718 /* tmpbuffer should never be longer than MAXPATHLEN,
719 but extra check does not hurt */
720 wcsncpy(calculate->argv0_path, tmpbuffer, MAXPATHLEN);
Fred Drakeedabdc12000-07-08 06:16:37 +0000721 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100722 else {
Victor Stinnerb64de462017-12-01 18:27:09 +0100723 /* Interpret relative to program_full_path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100724 reduce(calculate->argv0_path);
725 joinpath(calculate->argv0_path, tmpbuffer);
726 }
727 linklen = _Py_wreadlink(calculate->argv0_path, tmpbuffer, MAXPATHLEN);
Fred Drakeedabdc12000-07-08 06:16:37 +0000728 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000729#endif /* HAVE_READLINK */
730
Victor Stinner0327bde2017-11-23 17:03:20 +0100731 reduce(calculate->argv0_path);
Jeremy Hylton6372fe12000-09-27 20:51:17 +0000732 /* At this point, argv0_path is guaranteed to be less than
Victor Stinner0327bde2017-11-23 17:03:20 +0100733 MAXPATHLEN bytes long. */
Victor Stinner9316ee42017-11-25 03:17:57 +0100734 return _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100735}
Guido van Rossum305e5d01997-04-11 17:18:45 +0000736
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100737
Victor Stinner0327bde2017-11-23 17:03:20 +0100738/* Search for an "pyvenv.cfg" environment configuration file, first in the
739 executable's directory and then in the parent directory.
740 If found, open it for use when searching for prefixes.
741*/
742static void
743calculate_read_pyenv(PyCalculatePath *calculate)
744{
745 wchar_t tmpbuffer[MAXPATHLEN+1];
746 wchar_t *env_cfg = L"pyvenv.cfg";
747 FILE *env_file;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100748
Victor Stinner0327bde2017-11-23 17:03:20 +0100749 wcscpy(tmpbuffer, calculate->argv0_path);
Vinay Sajip90db6612012-07-17 17:33:46 +0100750
Victor Stinner0327bde2017-11-23 17:03:20 +0100751 joinpath(tmpbuffer, env_cfg);
752 env_file = _Py_wfopen(tmpbuffer, L"r");
753 if (env_file == NULL) {
754 errno = 0;
755
756 reduce(tmpbuffer);
757 reduce(tmpbuffer);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100758 joinpath(tmpbuffer, env_cfg);
Victor Stinner0327bde2017-11-23 17:03:20 +0100759
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100760 env_file = _Py_wfopen(tmpbuffer, L"r");
761 if (env_file == NULL) {
762 errno = 0;
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100763 }
764 }
765
Victor Stinner0327bde2017-11-23 17:03:20 +0100766 if (env_file == NULL) {
767 return;
Fred Drakeedabdc12000-07-08 06:16:37 +0000768 }
Guido van Rossume296ced2001-09-28 20:00:29 +0000769
Victor Stinner0327bde2017-11-23 17:03:20 +0100770 /* Look for a 'home' variable and set argv0_path to it, if found */
Victor Stinner9bee3292017-12-21 16:49:13 +0100771 if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
Victor Stinner0327bde2017-11-23 17:03:20 +0100772 wcscpy(calculate->argv0_path, tmpbuffer);
Just van Rossum52e14d62002-12-30 22:08:05 +0000773 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100774 fclose(env_file);
775}
Just van Rossum52e14d62002-12-30 22:08:05 +0000776
Guido van Rossum305e5d01997-04-11 17:18:45 +0000777
Victor Stinner0327bde2017-11-23 17:03:20 +0100778static void
Victor Stinner9316ee42017-11-25 03:17:57 +0100779calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix)
Victor Stinner0327bde2017-11-23 17:03:20 +0100780{
Victor Stinner9316ee42017-11-25 03:17:57 +0100781 wcsncpy(calculate->zip_path, prefix, MAXPATHLEN);
Victor Stinner0327bde2017-11-23 17:03:20 +0100782 calculate->zip_path[MAXPATHLEN] = L'\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000783
Victor Stinner0327bde2017-11-23 17:03:20 +0100784 if (calculate->prefix_found > 0) {
785 /* Use the reduced prefix returned by Py_GetPrefix() */
786 reduce(calculate->zip_path);
787 reduce(calculate->zip_path);
Victor Stinnerd4341102017-11-23 00:12:09 +0100788 }
789 else {
Victor Stinner0327bde2017-11-23 17:03:20 +0100790 wcsncpy(calculate->zip_path, calculate->prefix, MAXPATHLEN);
791 }
792 joinpath(calculate->zip_path, L"lib/python00.zip");
793
794 /* Replace "00" with version */
795 size_t bufsz = wcslen(calculate->zip_path);
796 calculate->zip_path[bufsz - 6] = VERSION[0];
797 calculate->zip_path[bufsz - 5] = VERSION[2];
798}
799
800
Victor Stinner9316ee42017-11-25 03:17:57 +0100801static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100802calculate_module_search_path(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100803 PyCalculatePath *calculate,
Victor Stinner9316ee42017-11-25 03:17:57 +0100804 const wchar_t *prefix, const wchar_t *exec_prefix,
Victor Stinnerb64de462017-12-01 18:27:09 +0100805 _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100806{
807 /* Calculate size of return buffer */
808 size_t bufsz = 0;
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100809 if (core_config->module_search_path_env != NULL) {
810 bufsz += wcslen(core_config->module_search_path_env) + 1;
Martin v. Löwis790465f2008-04-05 20:41:37 +0000811 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000812
Victor Stinner0327bde2017-11-23 17:03:20 +0100813 wchar_t *defpath = calculate->pythonpath;
Victor Stinner9316ee42017-11-25 03:17:57 +0100814 size_t prefixsz = wcslen(prefix) + 1;
Fred Drakeedabdc12000-07-08 06:16:37 +0000815 while (1) {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000816 wchar_t *delim = wcschr(defpath, DELIM);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000817
Victor Stinner0327bde2017-11-23 17:03:20 +0100818 if (defpath[0] != SEP) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000819 /* Paths are relative to prefix */
820 bufsz += prefixsz;
Victor Stinner0327bde2017-11-23 17:03:20 +0100821 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000822
Victor Stinner0327bde2017-11-23 17:03:20 +0100823 if (delim) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000824 bufsz += delim - defpath + 1;
Victor Stinner0327bde2017-11-23 17:03:20 +0100825 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000826 else {
Martin v. Löwis790465f2008-04-05 20:41:37 +0000827 bufsz += wcslen(defpath) + 1;
Fred Drakeedabdc12000-07-08 06:16:37 +0000828 break;
829 }
830 defpath = delim + 1;
831 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000832
Victor Stinner0327bde2017-11-23 17:03:20 +0100833 bufsz += wcslen(calculate->zip_path) + 1;
Victor Stinner9316ee42017-11-25 03:17:57 +0100834 bufsz += wcslen(exec_prefix) + 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000835
Victor Stinner0327bde2017-11-23 17:03:20 +0100836 /* Allocate the buffer */
837 wchar_t *buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t));
Fred Drakeedabdc12000-07-08 06:16:37 +0000838 if (buf == NULL) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100839 return _Py_INIT_NO_MEMORY();
Fred Drakeedabdc12000-07-08 06:16:37 +0000840 }
Victor Stinner0327bde2017-11-23 17:03:20 +0100841 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000842
Victor Stinner72967a42013-11-16 01:22:04 +0100843 /* Run-time value of $PYTHONPATH goes first */
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100844 if (core_config->module_search_path_env) {
845 wcscpy(buf, core_config->module_search_path_env);
Victor Stinner0327bde2017-11-23 17:03:20 +0100846 wcscat(buf, delimiter);
Fred Drakeedabdc12000-07-08 06:16:37 +0000847 }
Victor Stinner72967a42013-11-16 01:22:04 +0100848
849 /* Next is the default zip path */
Victor Stinner0327bde2017-11-23 17:03:20 +0100850 wcscat(buf, calculate->zip_path);
Victor Stinner72967a42013-11-16 01:22:04 +0100851 wcscat(buf, delimiter);
852
853 /* Next goes merge of compile-time $PYTHONPATH with
854 * dynamically located prefix.
855 */
Victor Stinner0327bde2017-11-23 17:03:20 +0100856 defpath = calculate->pythonpath;
Victor Stinner72967a42013-11-16 01:22:04 +0100857 while (1) {
858 wchar_t *delim = wcschr(defpath, DELIM);
859
860 if (defpath[0] != SEP) {
Victor Stinner9316ee42017-11-25 03:17:57 +0100861 wcscat(buf, prefix);
862 if (prefixsz >= 2 && prefix[prefixsz - 2] != SEP &&
Victor Stinner0327bde2017-11-23 17:03:20 +0100863 defpath[0] != (delim ? DELIM : L'\0'))
864 {
865 /* not empty */
Serhiy Storchaka62e32d62016-11-11 12:05:01 +0200866 wcscat(buf, separator);
867 }
Victor Stinner72967a42013-11-16 01:22:04 +0100868 }
869
870 if (delim) {
871 size_t len = delim - defpath + 1;
872 size_t end = wcslen(buf) + len;
873 wcsncat(buf, defpath, len);
Victor Stinner9316ee42017-11-25 03:17:57 +0100874 buf[end] = '\0';
Victor Stinner72967a42013-11-16 01:22:04 +0100875 }
876 else {
877 wcscat(buf, defpath);
878 break;
879 }
880 defpath = delim + 1;
881 }
882 wcscat(buf, delimiter);
883
884 /* Finally, on goes the directory for dynamic-load modules */
Victor Stinner9316ee42017-11-25 03:17:57 +0100885 wcscat(buf, exec_prefix);
Victor Stinner72967a42013-11-16 01:22:04 +0100886
Victor Stinner9316ee42017-11-25 03:17:57 +0100887 config->module_search_path = buf;
888 return _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100889}
Guido van Rossum305e5d01997-04-11 17:18:45 +0000890
Victor Stinner0327bde2017-11-23 17:03:20 +0100891
Victor Stinner0327bde2017-11-23 17:03:20 +0100892static _PyInitError
893calculate_init(PyCalculatePath *calculate,
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100894 const _PyCoreConfig *core_config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100895{
Victor Stinner0327bde2017-11-23 17:03:20 +0100896 size_t len;
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +0200897 const char *path = getenv("PATH");
Victor Stinner0327bde2017-11-23 17:03:20 +0100898 if (path) {
899 calculate->path_env = Py_DecodeLocale(path, &len);
900 if (!calculate->path_env) {
Victor Stinner46972b72017-11-24 22:55:40 +0100901 return DECODE_LOCALE_ERR("PATH environment variable", len);
Victor Stinner0327bde2017-11-23 17:03:20 +0100902 }
Fred Drakeedabdc12000-07-08 06:16:37 +0000903 }
Victor Stinnerae4836d2010-11-08 23:49:47 +0000904
Victor Stinner0327bde2017-11-23 17:03:20 +0100905 calculate->pythonpath = Py_DecodeLocale(PYTHONPATH, &len);
906 if (!calculate->pythonpath) {
Victor Stinner46972b72017-11-24 22:55:40 +0100907 return DECODE_LOCALE_ERR("PYTHONPATH define", len);
Victor Stinner0327bde2017-11-23 17:03:20 +0100908 }
909 calculate->prefix = Py_DecodeLocale(PREFIX, &len);
910 if (!calculate->prefix) {
Victor Stinner46972b72017-11-24 22:55:40 +0100911 return DECODE_LOCALE_ERR("PREFIX define", len);
Victor Stinner0327bde2017-11-23 17:03:20 +0100912 }
913 calculate->exec_prefix = Py_DecodeLocale(EXEC_PREFIX, &len);
914 if (!calculate->prefix) {
Victor Stinner46972b72017-11-24 22:55:40 +0100915 return DECODE_LOCALE_ERR("EXEC_PREFIX define", len);
Victor Stinner0327bde2017-11-23 17:03:20 +0100916 }
917 calculate->lib_python = Py_DecodeLocale("lib/python" VERSION, &len);
918 if (!calculate->lib_python) {
Victor Stinner46972b72017-11-24 22:55:40 +0100919 return DECODE_LOCALE_ERR("EXEC_PREFIX define", len);
Victor Stinner0327bde2017-11-23 17:03:20 +0100920 }
921 return _Py_INIT_OK();
922}
923
924
925static void
926calculate_free(PyCalculatePath *calculate)
927{
928 PyMem_RawFree(calculate->pythonpath);
929 PyMem_RawFree(calculate->prefix);
930 PyMem_RawFree(calculate->exec_prefix);
931 PyMem_RawFree(calculate->lib_python);
932 PyMem_RawFree(calculate->path_env);
Victor Stinner0327bde2017-11-23 17:03:20 +0100933}
934
935
Victor Stinner9316ee42017-11-25 03:17:57 +0100936static _PyInitError
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100937calculate_path_impl(const _PyCoreConfig *core_config,
Victor Stinner31a83932017-12-04 13:39:15 +0100938 PyCalculatePath *calculate, _PyPathConfig *config)
Victor Stinner0327bde2017-11-23 17:03:20 +0100939{
Victor Stinner31a83932017-12-04 13:39:15 +0100940 _PyInitError err;
941
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100942 err = calculate_program_full_path(core_config, calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +0100943 if (_Py_INIT_FAILED(err)) {
944 return err;
945 }
946
Victor Stinnerb64de462017-12-01 18:27:09 +0100947 err = calculate_argv0_path(calculate, config->program_full_path);
Victor Stinner9316ee42017-11-25 03:17:57 +0100948 if (_Py_INIT_FAILED(err)) {
949 return err;
950 }
951
Victor Stinner0327bde2017-11-23 17:03:20 +0100952 calculate_read_pyenv(calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +0100953
954 wchar_t prefix[MAXPATHLEN+1];
955 memset(prefix, 0, sizeof(prefix));
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100956 calculate_prefix(core_config, calculate, prefix);
Victor Stinner9316ee42017-11-25 03:17:57 +0100957
958 calculate_zip_path(calculate, prefix);
959
960 wchar_t exec_prefix[MAXPATHLEN+1];
961 memset(exec_prefix, 0, sizeof(exec_prefix));
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100962 calculate_exec_prefix(core_config, calculate, exec_prefix);
Victor Stinner0327bde2017-11-23 17:03:20 +0100963
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100964 if ((!calculate->prefix_found || !calculate->exec_prefix_found) &&
Victor Stinnerb75d7e22018-08-01 02:13:04 +0200965 !core_config->_frozen)
Victor Stinnerf04ebe22017-11-25 00:01:23 +0100966 {
Victor Stinner0327bde2017-11-23 17:03:20 +0100967 fprintf(stderr,
968 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
969 }
970
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +0100971 err = calculate_module_search_path(core_config, calculate,
Victor Stinner31a83932017-12-04 13:39:15 +0100972 prefix, exec_prefix, config);
Victor Stinner9316ee42017-11-25 03:17:57 +0100973 if (_Py_INIT_FAILED(err)) {
974 return err;
975 }
976
977 calculate_reduce_prefix(calculate, prefix);
978
979 config->prefix = _PyMem_RawWcsdup(prefix);
980 if (config->prefix == NULL) {
981 return _Py_INIT_NO_MEMORY();
982 }
983
984 calculate_reduce_exec_prefix(calculate, exec_prefix);
985
986 config->exec_prefix = _PyMem_RawWcsdup(exec_prefix);
987 if (config->exec_prefix == NULL) {
988 return _Py_INIT_NO_MEMORY();
989 }
990
991 return _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +0100992}
993
994
Serhiy Storchaka13badcb2017-12-02 21:36:00 +0200995_PyInitError
Victor Stinnerb1147e42018-07-21 02:06:16 +0200996_PyPathConfig_Calculate_impl(_PyPathConfig *config, const _PyCoreConfig *core_config)
Serhiy Storchaka13badcb2017-12-02 21:36:00 +0200997{
Victor Stinner0327bde2017-11-23 17:03:20 +0100998 PyCalculatePath calculate;
999 memset(&calculate, 0, sizeof(calculate));
1000
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001001 _PyInitError err = calculate_init(&calculate, core_config);
Victor Stinner0327bde2017-11-23 17:03:20 +01001002 if (_Py_INIT_FAILED(err)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001003 goto done;
Victor Stinner0327bde2017-11-23 17:03:20 +01001004 }
1005
Victor Stinnerb5fd9ad2017-12-14 02:20:52 +01001006 err = calculate_path_impl(core_config, &calculate, config);
Victor Stinner9316ee42017-11-25 03:17:57 +01001007 if (_Py_INIT_FAILED(err)) {
Victor Stinner9316ee42017-11-25 03:17:57 +01001008 goto done;
1009 }
1010
Victor Stinner9316ee42017-11-25 03:17:57 +01001011 err = _Py_INIT_OK();
Victor Stinner0327bde2017-11-23 17:03:20 +01001012
Victor Stinner9316ee42017-11-25 03:17:57 +01001013done:
Victor Stinner0327bde2017-11-23 17:03:20 +01001014 calculate_free(&calculate);
Victor Stinner9316ee42017-11-25 03:17:57 +01001015 return err;
1016}
Victor Stinner46972b72017-11-24 22:55:40 +01001017
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001018#ifdef __cplusplus
1019}
1020#endif