Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 1 | /* Return the initial module search path. */ |
| 2 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 3 | #include "Python.h" |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 4 | #include "internal/pystate.h" |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 5 | #include "osdefs.h" |
| 6 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 7 | #include <sys/types.h> |
Guido van Rossum | 21f8497 | 1997-06-02 22:18:31 +0000 | [diff] [blame] | 8 | #include <string.h> |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 9 | |
Brett Cannon | f6af76d | 2004-06-26 04:03:06 +0000 | [diff] [blame] | 10 | #ifdef __APPLE__ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 11 | # include <mach-o/dyld.h> |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 14 | /* Search in some common locations for the associated Python libraries. |
| 15 | * |
| 16 | * Two directories must be found, the platform independent directory |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 17 | * (prefix), containing the common .py and .pyc files, and the platform |
| 18 | * dependent directory (exec_prefix), containing the shared library |
| 19 | * modules. Note that prefix and exec_prefix can be the same directory, |
| 20 | * but for some installations, they are different. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 21 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 22 | * Py_GetPath() carries out separate searches for prefix and exec_prefix. |
| 23 | * Each search tries a number of different locations until a ``landmark'' |
| 24 | * file or directory is found. If no prefix or exec_prefix is found, a |
| 25 | * warning message is issued and the preprocessor defined PREFIX and |
| 26 | * EXEC_PREFIX are used (even though they will not work); python carries on |
| 27 | * as best as is possible, but most imports will fail. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 28 | * |
| 29 | * Before any searches are done, the location of the executable is |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 30 | * determined. If argv[0] has one or more slashes in it, it is used |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 31 | * unchanged. Otherwise, it must have been invoked from the shell's path, |
| 32 | * so we search $PATH for the named executable and use that. If the |
| 33 | * executable was not found on $PATH (or there was no $PATH environment |
| 34 | * variable), the original argv[0] string is used. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 35 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 36 | * Next, the executable location is examined to see if it is a symbolic |
| 37 | * link. If so, the link is chased (correctly interpreting a relative |
| 38 | * pathname if one is found) and the directory of the link target is used. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 39 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 40 | * Finally, argv0_path is set to the directory containing the executable |
| 41 | * (i.e. the last component is stripped). |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 42 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 43 | * With argv0_path in hand, we perform a number of steps. The same steps |
| 44 | * are performed for prefix and for exec_prefix, but with a different |
| 45 | * landmark. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 46 | * |
| 47 | * Step 1. Are we running python out of the build directory? This is |
| 48 | * checked by looking for a different kind of landmark relative to |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 49 | * argv0_path. For prefix, the landmark's path is derived from the VPATH |
| 50 | * preprocessor variable (taking into account that its value is almost, but |
| 51 | * not quite, what we need). For exec_prefix, the landmark is |
Antoine Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 52 | * pybuilddir.txt. If the landmark is found, we're done. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 53 | * |
| 54 | * For the remaining steps, the prefix landmark will always be |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 55 | * lib/python$VERSION/os.py and the exec_prefix will always be |
Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 56 | * lib/python$VERSION/lib-dynload, where $VERSION is Python's version |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 57 | * number as supplied by the Makefile. Note that this means that no more |
| 58 | * build directory checking is performed; if the first step did not find |
| 59 | * the landmarks, the assumption is that python is running from an |
| 60 | * installed setup. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 61 | * |
| 62 | * Step 2. See if the $PYTHONHOME environment variable points to the |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 63 | * installed location of the Python libraries. If $PYTHONHOME is set, then |
| 64 | * it points to prefix and exec_prefix. $PYTHONHOME can be a single |
| 65 | * directory, which is used for both, or the prefix and exec_prefix |
| 66 | * directories separated by a colon. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 67 | * |
| 68 | * Step 3. Try to find prefix and exec_prefix relative to argv0_path, |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 69 | * backtracking up the path until it is exhausted. This is the most common |
| 70 | * step to succeed. Note that if prefix and exec_prefix are different, |
| 71 | * exec_prefix is more likely to be found; however if exec_prefix is a |
| 72 | * subdirectory of prefix, both will be found. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 73 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 74 | * Step 4. Search the directories pointed to by the preprocessor variables |
| 75 | * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be |
| 76 | * passed in as options to the configure script. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 77 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 78 | * That's it! |
| 79 | * |
| 80 | * Well, almost. Once we have determined prefix and exec_prefix, the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 81 | * preprocessor variable PYTHONPATH is used to construct a path. Each |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 82 | * relative path on PYTHONPATH is prefixed with prefix. Then the directory |
| 83 | * containing the shared library modules is appended. The environment |
| 84 | * variable $PYTHONPATH is inserted in front of it all. Finally, the |
| 85 | * prefix and exec_prefix globals are tweaked so they reflect the values |
| 86 | * expected by other code, by stripping the "lib/python$VERSION/..." stuff |
| 87 | * off. If either points to the build directory, the globals are reset to |
| 88 | * the corresponding preprocessor variables (so sys.prefix will reflect the |
| 89 | * installation location, even though sys.path points into the build |
| 90 | * directory). This seems to make more sense given that currently the only |
| 91 | * known use of sys.prefix and sys.exec_prefix is for the ILU installation |
| 92 | * process to find the installed Python tree. |
Antoine Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 93 | * |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 94 | * An embedding application can use Py_SetPath() to override all of |
| 95 | * these authomatic path computations. |
| 96 | * |
Antoine Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 97 | * NOTE: Windows MSVC builds use PC/getpathp.c instead! |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 98 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 99 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 100 | #ifdef __cplusplus |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 101 | extern "C" { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 102 | #endif |
| 103 | |
| 104 | |
Benjamin Peterson | f585414 | 2016-06-02 12:41:35 -0700 | [diff] [blame] | 105 | #if !defined(PREFIX) || !defined(EXEC_PREFIX) || !defined(VERSION) || !defined(VPATH) |
| 106 | #error "PREFIX, EXEC_PREFIX, VERSION, and VPATH must be constant defined" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 107 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 109 | #ifndef LANDMARK |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 110 | #define LANDMARK L"os.py" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 111 | #endif |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 112 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 113 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 114 | ((LEN) == (size_t)-2) \ |
Victor Stinner | 9454060 | 2017-12-16 04:54:22 +0100 | [diff] [blame^] | 115 | ? _Py_INIT_USER_ERR("cannot decode " NAME) \ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 116 | : _Py_INIT_NO_MEMORY() |
| 117 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 118 | typedef struct { |
| 119 | wchar_t *path_env; /* PATH environment variable */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 120 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 121 | wchar_t *pythonpath; /* PYTHONPATH define */ |
| 122 | wchar_t *prefix; /* PREFIX define */ |
| 123 | wchar_t *exec_prefix; /* EXEC_PREFIX define */ |
| 124 | |
| 125 | wchar_t *lib_python; /* "lib/pythonX.Y" */ |
| 126 | wchar_t argv0_path[MAXPATHLEN+1]; |
| 127 | wchar_t zip_path[MAXPATHLEN+1]; /* ".../lib/pythonXY.zip" */ |
| 128 | |
| 129 | int prefix_found; /* found platform independent libraries? */ |
| 130 | int exec_prefix_found; /* found the platform dependent libraries? */ |
| 131 | } PyCalculatePath; |
| 132 | |
| 133 | static const wchar_t delimiter[2] = {DELIM, '\0'}; |
| 134 | static const wchar_t separator[2] = {SEP, '\0'}; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 135 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 136 | |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 137 | /* Get file status. Encode the path to the locale encoding. */ |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 138 | static int |
| 139 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 140 | { |
| 141 | int err; |
| 142 | char *fname; |
| 143 | fname = Py_EncodeLocale(path, NULL); |
| 144 | if (fname == NULL) { |
| 145 | errno = EINVAL; |
| 146 | return -1; |
| 147 | } |
| 148 | err = stat(fname, buf); |
| 149 | PyMem_Free(fname); |
| 150 | return err; |
| 151 | } |
| 152 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 153 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 154 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 155 | reduce(wchar_t *dir) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 156 | { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 157 | size_t i = wcslen(dir); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 158 | while (i > 0 && dir[i] != SEP) |
| 159 | --i; |
| 160 | dir[i] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 161 | } |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 162 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 163 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 164 | static int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 165 | isfile(wchar_t *filename) /* Is file, not directory */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 166 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 167 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 168 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 169 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 170 | } |
| 171 | if (!S_ISREG(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 172 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 173 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 174 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | |
| 178 | static int |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 179 | ismodule(wchar_t *filename) /* Is module -- check for .pyc too */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 180 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 181 | if (isfile(filename)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 182 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 183 | } |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 184 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 185 | /* Check for the compiled version of prefix. */ |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 186 | if (wcslen(filename) < MAXPATHLEN) { |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 187 | wcscat(filename, L"c"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 188 | if (isfile(filename)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 189 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 190 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 191 | } |
| 192 | return 0; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 196 | /* Is executable file */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 197 | static int |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 198 | isxfile(wchar_t *filename) |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 199 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 200 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 201 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 202 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 203 | } |
| 204 | if (!S_ISREG(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 205 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 206 | } |
| 207 | if ((buf.st_mode & 0111) == 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 208 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 209 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 210 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 214 | /* Is directory */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 215 | static int |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 216 | isdir(wchar_t *filename) |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 217 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 218 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 219 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 220 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 221 | } |
| 222 | if (!S_ISDIR(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 223 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 224 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 225 | return 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
Tim Peters | ec8c5a9 | 2004-08-08 01:00:47 +0000 | [diff] [blame] | 229 | /* Add a path component, by appending stuff to buffer. |
| 230 | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
| 231 | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
| 232 | the trailing NUL). It's a fatal error if it contains a string longer than |
| 233 | that (callers must be careful!). If these requirements are met, it's |
| 234 | guaranteed that buffer will still be a NUL-terminated string with no more |
| 235 | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
| 236 | stuff as fits will be appended. |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 237 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 238 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 239 | joinpath(wchar_t *buffer, wchar_t *stuff) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 240 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 241 | size_t n, k; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 242 | if (stuff[0] == SEP) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 243 | n = 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 244 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 245 | else { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 246 | n = wcslen(buffer); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 247 | if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 248 | buffer[n++] = SEP; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 249 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 250 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 251 | if (n > MAXPATHLEN) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | Py_FatalError("buffer overflow in getpath.c's joinpath()"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 253 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 254 | k = wcslen(stuff); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 255 | if (n + k > MAXPATHLEN) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 256 | k = MAXPATHLEN - n; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 257 | } |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 258 | wcsncpy(buffer+n, stuff, k); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 259 | buffer[n+k] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 262 | |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 263 | /* copy_absolute requires that path be allocated at least |
| 264 | MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */ |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 265 | static void |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 266 | copy_absolute(wchar_t *path, wchar_t *p, size_t pathlen) |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 267 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 268 | if (p[0] == SEP) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 269 | wcscpy(path, p); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 270 | } |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 271 | else { |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 272 | if (!_Py_wgetcwd(path, pathlen)) { |
Victor Stinner | 4f3abb0 | 2010-10-07 23:29:18 +0000 | [diff] [blame] | 273 | /* unable to get the current directory */ |
| 274 | wcscpy(path, p); |
| 275 | return; |
| 276 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 277 | if (p[0] == '.' && p[1] == SEP) { |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 278 | p += 2; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 279 | } |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 280 | joinpath(path, p); |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 283 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 284 | |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 285 | /* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */ |
| 286 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 287 | absolutize(wchar_t *path) |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 288 | { |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 289 | wchar_t buffer[MAXPATHLEN+1]; |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 290 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 291 | if (path[0] == SEP) { |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 292 | return; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 293 | } |
Victor Stinner | f4061da | 2010-10-14 12:37:19 +0000 | [diff] [blame] | 294 | copy_absolute(buffer, path, MAXPATHLEN+1); |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 295 | wcscpy(path, buffer); |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 298 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 299 | /* search for a prefix value in an environment file. If found, copy it |
| 300 | to the provided buffer, which is expected to be no more than MAXPATHLEN |
| 301 | bytes long. |
| 302 | */ |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 303 | static int |
| 304 | find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) |
| 305 | { |
| 306 | int result = 0; /* meaning not found */ |
| 307 | char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ |
| 308 | |
| 309 | fseek(env_file, 0, SEEK_SET); |
| 310 | while (!feof(env_file)) { |
| 311 | char * p = fgets(buffer, MAXPATHLEN*2, env_file); |
| 312 | wchar_t tmpbuffer[MAXPATHLEN*2+1]; |
| 313 | PyObject * decoded; |
| 314 | int n; |
| 315 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 316 | if (p == NULL) { |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 317 | break; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 318 | } |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 319 | n = strlen(p); |
| 320 | if (p[n - 1] != '\n') { |
| 321 | /* line has overflowed - bail */ |
| 322 | break; |
| 323 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 324 | if (p[0] == '#') { |
| 325 | /* Comment - skip */ |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 326 | continue; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 327 | } |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 328 | decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape"); |
| 329 | if (decoded != NULL) { |
| 330 | Py_ssize_t k; |
| 331 | wchar_t * state; |
| 332 | k = PyUnicode_AsWideChar(decoded, |
| 333 | tmpbuffer, MAXPATHLEN * 2); |
| 334 | Py_DECREF(decoded); |
| 335 | if (k >= 0) { |
| 336 | wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state); |
| 337 | if ((tok != NULL) && !wcscmp(tok, key)) { |
| 338 | tok = wcstok(NULL, L" \t", &state); |
| 339 | if ((tok != NULL) && !wcscmp(tok, L"=")) { |
| 340 | tok = wcstok(NULL, L"\r\n", &state); |
| 341 | if (tok != NULL) { |
| 342 | wcsncpy(value, tok, MAXPATHLEN); |
| 343 | result = 1; |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | return result; |
| 352 | } |
| 353 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 354 | |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 355 | /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 356 | bytes long. |
| 357 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 358 | static int |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 359 | search_for_prefix(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 360 | PyCalculatePath *calculate, wchar_t *prefix) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 361 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 362 | size_t n; |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 363 | wchar_t *vpath; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 364 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 365 | /* If PYTHONHOME is set, we believe it unconditionally */ |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 366 | if (core_config->home) { |
| 367 | wcsncpy(prefix, core_config->home, MAXPATHLEN); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 368 | prefix[MAXPATHLEN] = L'\0'; |
| 369 | wchar_t *delim = wcschr(prefix, DELIM); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 370 | if (delim) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 371 | *delim = L'\0'; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 372 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 373 | joinpath(prefix, calculate->lib_python); |
| 374 | joinpath(prefix, LANDMARK); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 375 | return 1; |
| 376 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 377 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 378 | /* Check to see if argv[0] is in the build directory */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 379 | wcsncpy(prefix, calculate->argv0_path, MAXPATHLEN); |
| 380 | prefix[MAXPATHLEN] = L'\0'; |
| 381 | joinpath(prefix, L"Modules/Setup"); |
| 382 | if (isfile(prefix)) { |
Neil Schemenauer | 6cf0702 | 2001-01-24 17:13:11 +0000 | [diff] [blame] | 383 | /* Check VPATH to see if argv0_path is in the build directory. */ |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 384 | vpath = Py_DecodeLocale(VPATH, NULL); |
Victor Stinner | 2158231 | 2010-10-23 00:13:28 +0000 | [diff] [blame] | 385 | if (vpath != NULL) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 386 | wcsncpy(prefix, calculate->argv0_path, MAXPATHLEN); |
| 387 | prefix[MAXPATHLEN] = L'\0'; |
| 388 | joinpath(prefix, vpath); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 389 | PyMem_RawFree(vpath); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 390 | joinpath(prefix, L"Lib"); |
| 391 | joinpath(prefix, LANDMARK); |
| 392 | if (ismodule(prefix)) { |
Victor Stinner | 2158231 | 2010-10-23 00:13:28 +0000 | [diff] [blame] | 393 | return -1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 394 | } |
Victor Stinner | 2158231 | 2010-10-23 00:13:28 +0000 | [diff] [blame] | 395 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 396 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 397 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 398 | /* Search from argv0_path, until root is found */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 399 | copy_absolute(prefix, calculate->argv0_path, MAXPATHLEN+1); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 400 | do { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 401 | n = wcslen(prefix); |
| 402 | joinpath(prefix, calculate->lib_python); |
| 403 | joinpath(prefix, LANDMARK); |
| 404 | if (ismodule(prefix)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 405 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 406 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 407 | prefix[n] = L'\0'; |
| 408 | reduce(prefix); |
| 409 | } while (prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 410 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 411 | /* Look at configure's PREFIX */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 412 | wcsncpy(prefix, calculate->prefix, MAXPATHLEN); |
| 413 | prefix[MAXPATHLEN] = L'\0'; |
| 414 | joinpath(prefix, calculate->lib_python); |
| 415 | joinpath(prefix, LANDMARK); |
| 416 | if (ismodule(prefix)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 417 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 418 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 419 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 420 | /* Fail */ |
| 421 | return 0; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 425 | static void |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 426 | calculate_prefix(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 427 | PyCalculatePath *calculate, wchar_t *prefix) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 428 | { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 429 | calculate->prefix_found = search_for_prefix(core_config, calculate, prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 430 | if (!calculate->prefix_found) { |
| 431 | if (!Py_FrozenFlag) { |
| 432 | fprintf(stderr, |
| 433 | "Could not find platform independent libraries <prefix>\n"); |
| 434 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 435 | wcsncpy(prefix, calculate->prefix, MAXPATHLEN); |
| 436 | joinpath(prefix, calculate->lib_python); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 437 | } |
| 438 | else { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 439 | reduce(prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | static void |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 445 | calculate_reduce_prefix(PyCalculatePath *calculate, wchar_t *prefix) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 446 | { |
| 447 | /* Reduce prefix and exec_prefix to their essence, |
| 448 | * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. |
| 449 | * If we're loading relative to the build directory, |
| 450 | * return the compiled-in defaults instead. |
| 451 | */ |
| 452 | if (calculate->prefix_found > 0) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 453 | reduce(prefix); |
| 454 | reduce(prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 455 | /* The prefix is the root directory, but reduce() chopped |
| 456 | * off the "/". */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 457 | if (!prefix[0]) { |
| 458 | wcscpy(prefix, separator); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | else { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 462 | wcsncpy(prefix, calculate->prefix, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 467 | /* search_for_exec_prefix requires that argv0_path be no more than |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 468 | MAXPATHLEN bytes long. |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 469 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 470 | static int |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 471 | search_for_exec_prefix(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 472 | PyCalculatePath *calculate, wchar_t *exec_prefix) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 473 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 474 | size_t n; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 475 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 476 | /* If PYTHONHOME is set, we believe it unconditionally */ |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 477 | if (core_config->home) { |
| 478 | wchar_t *delim = wcschr(core_config->home, DELIM); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 479 | if (delim) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 480 | wcsncpy(exec_prefix, delim+1, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 481 | } |
| 482 | else { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 483 | wcsncpy(exec_prefix, core_config->home, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 484 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 485 | exec_prefix[MAXPATHLEN] = L'\0'; |
| 486 | joinpath(exec_prefix, calculate->lib_python); |
| 487 | joinpath(exec_prefix, L"lib-dynload"); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 488 | return 1; |
| 489 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 491 | /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" |
| 492 | is written by setup.py and contains the relative path to the location |
| 493 | of shared library modules. */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 494 | wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN); |
| 495 | exec_prefix[MAXPATHLEN] = L'\0'; |
| 496 | joinpath(exec_prefix, L"pybuilddir.txt"); |
| 497 | if (isfile(exec_prefix)) { |
| 498 | FILE *f = _Py_wfopen(exec_prefix, L"rb"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 499 | if (f == NULL) { |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 500 | errno = 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 501 | } |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 502 | else { |
| 503 | char buf[MAXPATHLEN+1]; |
| 504 | PyObject *decoded; |
| 505 | wchar_t rel_builddir_path[MAXPATHLEN+1]; |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 506 | n = fread(buf, 1, MAXPATHLEN, f); |
| 507 | buf[n] = '\0'; |
| 508 | fclose(f); |
| 509 | decoded = PyUnicode_DecodeUTF8(buf, n, "surrogateescape"); |
| 510 | if (decoded != NULL) { |
Victor Stinner | 47f637c | 2010-11-10 14:12:20 +0000 | [diff] [blame] | 511 | Py_ssize_t k; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 512 | k = PyUnicode_AsWideChar(decoded, |
Victor Stinner | e406ef4 | 2010-08-14 00:07:14 +0000 | [diff] [blame] | 513 | rel_builddir_path, MAXPATHLEN); |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 514 | Py_DECREF(decoded); |
Victor Stinner | 47f637c | 2010-11-10 14:12:20 +0000 | [diff] [blame] | 515 | if (k >= 0) { |
| 516 | rel_builddir_path[k] = L'\0'; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 517 | wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN); |
| 518 | exec_prefix[MAXPATHLEN] = L'\0'; |
| 519 | joinpath(exec_prefix, rel_builddir_path); |
Antoine Pitrou | e9b428f | 2010-08-13 22:25:01 +0000 | [diff] [blame] | 520 | return -1; |
| 521 | } |
| 522 | } |
| 523 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 524 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 525 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 526 | /* Search from argv0_path, until root is found */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 527 | copy_absolute(exec_prefix, calculate->argv0_path, MAXPATHLEN+1); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 528 | do { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 529 | n = wcslen(exec_prefix); |
| 530 | joinpath(exec_prefix, calculate->lib_python); |
| 531 | joinpath(exec_prefix, L"lib-dynload"); |
| 532 | if (isdir(exec_prefix)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 533 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 534 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 535 | exec_prefix[n] = L'\0'; |
| 536 | reduce(exec_prefix); |
| 537 | } while (exec_prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 538 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 539 | /* Look at configure's EXEC_PREFIX */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 540 | wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN); |
| 541 | exec_prefix[MAXPATHLEN] = L'\0'; |
| 542 | joinpath(exec_prefix, calculate->lib_python); |
| 543 | joinpath(exec_prefix, L"lib-dynload"); |
| 544 | if (isdir(exec_prefix)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 545 | return 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 546 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 547 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 548 | /* Fail */ |
| 549 | return 0; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 552 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 553 | static void |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 554 | calculate_exec_prefix(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 555 | PyCalculatePath *calculate, wchar_t *exec_prefix) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 556 | { |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 557 | calculate->exec_prefix_found = search_for_exec_prefix(core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 558 | calculate, |
| 559 | exec_prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 560 | if (!calculate->exec_prefix_found) { |
| 561 | if (!Py_FrozenFlag) { |
| 562 | fprintf(stderr, |
| 563 | "Could not find platform dependent libraries <exec_prefix>\n"); |
| 564 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 565 | wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN); |
| 566 | joinpath(exec_prefix, L"lib/lib-dynload"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 567 | } |
| 568 | /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ |
| 569 | } |
| 570 | |
| 571 | |
| 572 | static void |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 573 | calculate_reduce_exec_prefix(PyCalculatePath *calculate, wchar_t *exec_prefix) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 574 | { |
| 575 | if (calculate->exec_prefix_found > 0) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 576 | reduce(exec_prefix); |
| 577 | reduce(exec_prefix); |
| 578 | reduce(exec_prefix); |
| 579 | if (!exec_prefix[0]) { |
| 580 | wcscpy(exec_prefix, separator); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | else { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 584 | wcsncpy(exec_prefix, calculate->exec_prefix, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 589 | static _PyInitError |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 590 | calculate_program_full_path(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 591 | PyCalculatePath *calculate, _PyPathConfig *config) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 592 | { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 593 | wchar_t program_full_path[MAXPATHLEN+1]; |
| 594 | memset(program_full_path, 0, sizeof(program_full_path)); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 595 | |
Victor Stinner | b919795 | 2017-11-23 19:02:04 +0100 | [diff] [blame] | 596 | #ifdef __APPLE__ |
| 597 | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 |
| 598 | uint32_t nsexeclength = MAXPATHLEN; |
| 599 | #else |
| 600 | unsigned long nsexeclength = MAXPATHLEN; |
| 601 | #endif |
| 602 | char execpath[MAXPATHLEN+1]; |
| 603 | #endif |
| 604 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 605 | /* If there is no slash in the argv0 path, then we have to |
| 606 | * assume python is on the user's $PATH, since there's no |
| 607 | * other way to find a directory to start the search from. If |
| 608 | * $PATH isn't exported, you lose. |
| 609 | */ |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 610 | if (wcschr(core_config->program_name, SEP)) { |
| 611 | wcsncpy(program_full_path, core_config->program_name, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 612 | } |
Jack Jansen | 1afd480 | 2004-06-03 14:33:03 +0000 | [diff] [blame] | 613 | #ifdef __APPLE__ |
Jack Jansen | 1afd480 | 2004-06-03 14:33:03 +0000 | [diff] [blame] | 614 | /* On Mac OS X, if a script uses an interpreter of the form |
| 615 | * "#!/opt/python2.3/bin/python", the kernel only passes "python" |
| 616 | * as argv[0], which falls through to the $PATH search below. |
| 617 | * If /opt/python2.3/bin isn't in your path, or is near the end, |
| 618 | * this algorithm may incorrectly find /usr/bin/python. To work |
| 619 | * around this, we can use _NSGetExecutablePath to get a better |
| 620 | * hint of what the intended interpreter was, although this |
| 621 | * will fail if a relative path was used. but in that case, |
| 622 | * absolutize() should help us out below |
| 623 | */ |
Victor Stinner | f04ebe2 | 2017-11-25 00:01:23 +0100 | [diff] [blame] | 624 | else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && |
| 625 | execpath[0] == SEP) |
| 626 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 627 | size_t len; |
| 628 | wchar_t *path = Py_DecodeLocale(execpath, &len); |
| 629 | if (path == NULL) { |
| 630 | return DECODE_LOCALE_ERR("executable path", len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | } |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 632 | wcsncpy(program_full_path, path, MAXPATHLEN); |
| 633 | PyMem_RawFree(path); |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 634 | } |
Brett Cannon | 6cc4814 | 2004-06-24 00:48:44 +0000 | [diff] [blame] | 635 | #endif /* __APPLE__ */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 636 | else if (calculate->path_env) { |
| 637 | wchar_t *path = calculate->path_env; |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 638 | while (1) { |
| 639 | wchar_t *delim = wcschr(path, DELIM); |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 641 | if (delim) { |
| 642 | size_t len = delim - path; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 643 | if (len > MAXPATHLEN) { |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 644 | len = MAXPATHLEN; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 645 | } |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 646 | wcsncpy(program_full_path, path, len); |
| 647 | program_full_path[len] = '\0'; |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 648 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 649 | else { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 650 | wcsncpy(program_full_path, path, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 651 | } |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 652 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 653 | joinpath(program_full_path, core_config->program_name); |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 654 | if (isxfile(program_full_path)) { |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 655 | break; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 656 | } |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 658 | if (!delim) { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 659 | program_full_path[0] = L'\0'; |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | path = delim + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | } |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 664 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 665 | else { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 666 | program_full_path[0] = '\0'; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 667 | } |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 668 | if (program_full_path[0] != SEP && program_full_path[0] != '\0') { |
| 669 | absolutize(program_full_path); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 670 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 671 | |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 672 | config->program_full_path = _PyMem_RawWcsdup(program_full_path); |
| 673 | if (config->program_full_path == NULL) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 674 | return _Py_INIT_NO_MEMORY(); |
| 675 | } |
| 676 | return _Py_INIT_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 680 | static _PyInitError |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 681 | calculate_argv0_path(PyCalculatePath *calculate, const wchar_t *program_full_path) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 682 | { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 683 | wcsncpy(calculate->argv0_path, program_full_path, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 684 | calculate->argv0_path[MAXPATHLEN] = '\0'; |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 685 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 686 | #ifdef WITH_NEXT_FRAMEWORK |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 687 | NSModule pythonModule; |
| 688 | |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 689 | /* On Mac OS X we have a special case if we're running from a framework. |
| 690 | ** This is because the python home should be set relative to the library, |
| 691 | ** which is in the framework, not relative to the executable, which may |
| 692 | ** be outside of the framework. Except when we're in the build directory... |
| 693 | */ |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 694 | pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); |
| 695 | /* Use dylib functions to find out where the framework was loaded from */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 696 | const char* modPath = NSLibraryNameForModule(pythonModule); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 697 | if (modPath != NULL) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 698 | /* We're in a framework. */ |
Jack Jansen | e925faf | 2001-08-15 01:14:40 +0000 | [diff] [blame] | 699 | /* See if we might be in the build directory. The framework in the |
| 700 | ** build directory is incomplete, it only has the .dylib and a few |
| 701 | ** needed symlinks, it doesn't have the Lib directories and such. |
| 702 | ** If we're running with the framework from the build directory we must |
| 703 | ** be running the interpreter in the build directory, so we use the |
| 704 | ** build-directory-specific logic to find Lib and such. |
| 705 | */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 706 | size_t len; |
| 707 | wchar_t* wbuf = Py_DecodeLocale(modPath, &len); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 708 | if (wbuf == NULL) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 709 | return DECODE_LOCALE_ERR("framework location", len); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 710 | } |
| 711 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 712 | wcsncpy(calculate->argv0_path, wbuf, MAXPATHLEN); |
| 713 | reduce(calculate->argv0_path); |
| 714 | joinpath(calculate->argv0_path, calculate->lib_python); |
| 715 | joinpath(calculate->argv0_path, LANDMARK); |
| 716 | if (!ismodule(calculate->argv0_path)) { |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 717 | /* We are in the build directory so use the name of the |
| 718 | executable - we know that the absolute path is passed */ |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 719 | wcsncpy(calculate->argv0_path, program_full_path, MAXPATHLEN); |
Jack Jansen | e925faf | 2001-08-15 01:14:40 +0000 | [diff] [blame] | 720 | } |
| 721 | else { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 722 | /* Use the location of the library as the program_full_path */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 723 | wcsncpy(calculate->argv0_path, wbuf, MAXPATHLEN); |
Jack Jansen | e925faf | 2001-08-15 01:14:40 +0000 | [diff] [blame] | 724 | } |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 725 | PyMem_RawFree(wbuf); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 726 | } |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 727 | #endif |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 729 | #if HAVE_READLINK |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 730 | wchar_t tmpbuffer[MAXPATHLEN+1]; |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 731 | int linklen = _Py_wreadlink(program_full_path, tmpbuffer, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 732 | while (linklen != -1) { |
| 733 | if (tmpbuffer[0] == SEP) { |
| 734 | /* tmpbuffer should never be longer than MAXPATHLEN, |
| 735 | but extra check does not hurt */ |
| 736 | wcsncpy(calculate->argv0_path, tmpbuffer, MAXPATHLEN); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 737 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 738 | else { |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 739 | /* Interpret relative to program_full_path */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 740 | reduce(calculate->argv0_path); |
| 741 | joinpath(calculate->argv0_path, tmpbuffer); |
| 742 | } |
| 743 | linklen = _Py_wreadlink(calculate->argv0_path, tmpbuffer, MAXPATHLEN); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 744 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 745 | #endif /* HAVE_READLINK */ |
| 746 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 747 | reduce(calculate->argv0_path); |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 748 | /* At this point, argv0_path is guaranteed to be less than |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 749 | MAXPATHLEN bytes long. */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 750 | return _Py_INIT_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 751 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 752 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 753 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 754 | /* Search for an "pyvenv.cfg" environment configuration file, first in the |
| 755 | executable's directory and then in the parent directory. |
| 756 | If found, open it for use when searching for prefixes. |
| 757 | */ |
| 758 | static void |
| 759 | calculate_read_pyenv(PyCalculatePath *calculate) |
| 760 | { |
| 761 | wchar_t tmpbuffer[MAXPATHLEN+1]; |
| 762 | wchar_t *env_cfg = L"pyvenv.cfg"; |
| 763 | FILE *env_file; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 764 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 765 | wcscpy(tmpbuffer, calculate->argv0_path); |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 766 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 767 | joinpath(tmpbuffer, env_cfg); |
| 768 | env_file = _Py_wfopen(tmpbuffer, L"r"); |
| 769 | if (env_file == NULL) { |
| 770 | errno = 0; |
| 771 | |
| 772 | reduce(tmpbuffer); |
| 773 | reduce(tmpbuffer); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 774 | joinpath(tmpbuffer, env_cfg); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 775 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 776 | env_file = _Py_wfopen(tmpbuffer, L"r"); |
| 777 | if (env_file == NULL) { |
| 778 | errno = 0; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 782 | if (env_file == NULL) { |
| 783 | return; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 784 | } |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 785 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 786 | /* Look for a 'home' variable and set argv0_path to it, if found */ |
| 787 | if (find_env_config_value(env_file, L"home", tmpbuffer)) { |
| 788 | wcscpy(calculate->argv0_path, tmpbuffer); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 789 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 790 | fclose(env_file); |
| 791 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 792 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 793 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 794 | static void |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 795 | calculate_zip_path(PyCalculatePath *calculate, const wchar_t *prefix) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 796 | { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 797 | wcsncpy(calculate->zip_path, prefix, MAXPATHLEN); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 798 | calculate->zip_path[MAXPATHLEN] = L'\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 799 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 800 | if (calculate->prefix_found > 0) { |
| 801 | /* Use the reduced prefix returned by Py_GetPrefix() */ |
| 802 | reduce(calculate->zip_path); |
| 803 | reduce(calculate->zip_path); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 804 | } |
| 805 | else { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 806 | wcsncpy(calculate->zip_path, calculate->prefix, MAXPATHLEN); |
| 807 | } |
| 808 | joinpath(calculate->zip_path, L"lib/python00.zip"); |
| 809 | |
| 810 | /* Replace "00" with version */ |
| 811 | size_t bufsz = wcslen(calculate->zip_path); |
| 812 | calculate->zip_path[bufsz - 6] = VERSION[0]; |
| 813 | calculate->zip_path[bufsz - 5] = VERSION[2]; |
| 814 | } |
| 815 | |
| 816 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 817 | static _PyInitError |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 818 | calculate_module_search_path(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 819 | PyCalculatePath *calculate, |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 820 | const wchar_t *prefix, const wchar_t *exec_prefix, |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 821 | _PyPathConfig *config) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 822 | { |
| 823 | /* Calculate size of return buffer */ |
| 824 | size_t bufsz = 0; |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 825 | if (core_config->module_search_path_env != NULL) { |
| 826 | bufsz += wcslen(core_config->module_search_path_env) + 1; |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 827 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 828 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 829 | wchar_t *defpath = calculate->pythonpath; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 830 | size_t prefixsz = wcslen(prefix) + 1; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 831 | while (1) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 832 | wchar_t *delim = wcschr(defpath, DELIM); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 833 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 834 | if (defpath[0] != SEP) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 835 | /* Paths are relative to prefix */ |
| 836 | bufsz += prefixsz; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 837 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 838 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 839 | if (delim) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 840 | bufsz += delim - defpath + 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 841 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 842 | else { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 843 | bufsz += wcslen(defpath) + 1; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | defpath = delim + 1; |
| 847 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 848 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 849 | bufsz += wcslen(calculate->zip_path) + 1; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 850 | bufsz += wcslen(exec_prefix) + 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 851 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 852 | /* Allocate the buffer */ |
| 853 | wchar_t *buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t)); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 854 | if (buf == NULL) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 855 | return _Py_INIT_NO_MEMORY(); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 856 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 857 | buf[0] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 858 | |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 859 | /* Run-time value of $PYTHONPATH goes first */ |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 860 | if (core_config->module_search_path_env) { |
| 861 | wcscpy(buf, core_config->module_search_path_env); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 862 | wcscat(buf, delimiter); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 863 | } |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 864 | |
| 865 | /* Next is the default zip path */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 866 | wcscat(buf, calculate->zip_path); |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 867 | wcscat(buf, delimiter); |
| 868 | |
| 869 | /* Next goes merge of compile-time $PYTHONPATH with |
| 870 | * dynamically located prefix. |
| 871 | */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 872 | defpath = calculate->pythonpath; |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 873 | while (1) { |
| 874 | wchar_t *delim = wcschr(defpath, DELIM); |
| 875 | |
| 876 | if (defpath[0] != SEP) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 877 | wcscat(buf, prefix); |
| 878 | if (prefixsz >= 2 && prefix[prefixsz - 2] != SEP && |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 879 | defpath[0] != (delim ? DELIM : L'\0')) |
| 880 | { |
| 881 | /* not empty */ |
Serhiy Storchaka | 62e32d6 | 2016-11-11 12:05:01 +0200 | [diff] [blame] | 882 | wcscat(buf, separator); |
| 883 | } |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | if (delim) { |
| 887 | size_t len = delim - defpath + 1; |
| 888 | size_t end = wcslen(buf) + len; |
| 889 | wcsncat(buf, defpath, len); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 890 | buf[end] = '\0'; |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 891 | } |
| 892 | else { |
| 893 | wcscat(buf, defpath); |
| 894 | break; |
| 895 | } |
| 896 | defpath = delim + 1; |
| 897 | } |
| 898 | wcscat(buf, delimiter); |
| 899 | |
| 900 | /* Finally, on goes the directory for dynamic-load modules */ |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 901 | wcscat(buf, exec_prefix); |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 902 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 903 | config->module_search_path = buf; |
| 904 | return _Py_INIT_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 905 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 906 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 907 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 908 | static _PyInitError |
| 909 | calculate_init(PyCalculatePath *calculate, |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 910 | const _PyCoreConfig *core_config) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 911 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 912 | size_t len; |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 913 | const char *path = getenv("PATH"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 914 | if (path) { |
| 915 | calculate->path_env = Py_DecodeLocale(path, &len); |
| 916 | if (!calculate->path_env) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 917 | return DECODE_LOCALE_ERR("PATH environment variable", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 918 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 919 | } |
Victor Stinner | ae4836d | 2010-11-08 23:49:47 +0000 | [diff] [blame] | 920 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 921 | calculate->pythonpath = Py_DecodeLocale(PYTHONPATH, &len); |
| 922 | if (!calculate->pythonpath) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 923 | return DECODE_LOCALE_ERR("PYTHONPATH define", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 924 | } |
| 925 | calculate->prefix = Py_DecodeLocale(PREFIX, &len); |
| 926 | if (!calculate->prefix) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 927 | return DECODE_LOCALE_ERR("PREFIX define", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 928 | } |
| 929 | calculate->exec_prefix = Py_DecodeLocale(EXEC_PREFIX, &len); |
| 930 | if (!calculate->prefix) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 931 | return DECODE_LOCALE_ERR("EXEC_PREFIX define", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 932 | } |
| 933 | calculate->lib_python = Py_DecodeLocale("lib/python" VERSION, &len); |
| 934 | if (!calculate->lib_python) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 935 | return DECODE_LOCALE_ERR("EXEC_PREFIX define", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 936 | } |
| 937 | return _Py_INIT_OK(); |
| 938 | } |
| 939 | |
| 940 | |
| 941 | static void |
| 942 | calculate_free(PyCalculatePath *calculate) |
| 943 | { |
| 944 | PyMem_RawFree(calculate->pythonpath); |
| 945 | PyMem_RawFree(calculate->prefix); |
| 946 | PyMem_RawFree(calculate->exec_prefix); |
| 947 | PyMem_RawFree(calculate->lib_python); |
| 948 | PyMem_RawFree(calculate->path_env); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 952 | static _PyInitError |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 953 | calculate_path_impl(const _PyCoreConfig *core_config, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 954 | PyCalculatePath *calculate, _PyPathConfig *config) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 955 | { |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 956 | _PyInitError err; |
| 957 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 958 | err = calculate_program_full_path(core_config, calculate, config); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 959 | if (_Py_INIT_FAILED(err)) { |
| 960 | return err; |
| 961 | } |
| 962 | |
Victor Stinner | b64de46 | 2017-12-01 18:27:09 +0100 | [diff] [blame] | 963 | err = calculate_argv0_path(calculate, config->program_full_path); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 964 | if (_Py_INIT_FAILED(err)) { |
| 965 | return err; |
| 966 | } |
| 967 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 968 | calculate_read_pyenv(calculate); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 969 | |
| 970 | wchar_t prefix[MAXPATHLEN+1]; |
| 971 | memset(prefix, 0, sizeof(prefix)); |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 972 | calculate_prefix(core_config, calculate, prefix); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 973 | |
| 974 | calculate_zip_path(calculate, prefix); |
| 975 | |
| 976 | wchar_t exec_prefix[MAXPATHLEN+1]; |
| 977 | memset(exec_prefix, 0, sizeof(exec_prefix)); |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 978 | calculate_exec_prefix(core_config, calculate, exec_prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 979 | |
Victor Stinner | f04ebe2 | 2017-11-25 00:01:23 +0100 | [diff] [blame] | 980 | if ((!calculate->prefix_found || !calculate->exec_prefix_found) && |
| 981 | !Py_FrozenFlag) |
| 982 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 983 | fprintf(stderr, |
| 984 | "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); |
| 985 | } |
| 986 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 987 | err = calculate_module_search_path(core_config, calculate, |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 988 | prefix, exec_prefix, config); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 989 | if (_Py_INIT_FAILED(err)) { |
| 990 | return err; |
| 991 | } |
| 992 | |
| 993 | calculate_reduce_prefix(calculate, prefix); |
| 994 | |
| 995 | config->prefix = _PyMem_RawWcsdup(prefix); |
| 996 | if (config->prefix == NULL) { |
| 997 | return _Py_INIT_NO_MEMORY(); |
| 998 | } |
| 999 | |
| 1000 | calculate_reduce_exec_prefix(calculate, exec_prefix); |
| 1001 | |
| 1002 | config->exec_prefix = _PyMem_RawWcsdup(exec_prefix); |
| 1003 | if (config->exec_prefix == NULL) { |
| 1004 | return _Py_INIT_NO_MEMORY(); |
| 1005 | } |
| 1006 | |
| 1007 | return _Py_INIT_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | |
Serhiy Storchaka | 13badcb | 2017-12-02 21:36:00 +0200 | [diff] [blame] | 1011 | _PyInitError |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1012 | _PyPathConfig_Calculate(_PyPathConfig *config, const _PyCoreConfig *core_config) |
Serhiy Storchaka | 13badcb | 2017-12-02 21:36:00 +0200 | [diff] [blame] | 1013 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1014 | PyCalculatePath calculate; |
| 1015 | memset(&calculate, 0, sizeof(calculate)); |
| 1016 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1017 | _PyInitError err = calculate_init(&calculate, core_config); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1018 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1019 | goto done; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1020 | } |
| 1021 | |
Victor Stinner | b5fd9ad | 2017-12-14 02:20:52 +0100 | [diff] [blame] | 1022 | err = calculate_path_impl(core_config, &calculate, config); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1023 | if (_Py_INIT_FAILED(err)) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1024 | goto done; |
| 1025 | } |
| 1026 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1027 | err = _Py_INIT_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1028 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1029 | done: |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1030 | calculate_free(&calculate); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1031 | return err; |
| 1032 | } |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1033 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1034 | #ifdef __cplusplus |
| 1035 | } |
| 1036 | #endif |