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 | 9fc57a3 | 2018-11-07 00:44:03 +0100 | [diff] [blame] | 4 | #include "pycore_fileutils.h" |
Victor Stinner | e5014be | 2020-04-14 17:52:15 +0200 | [diff] [blame] | 5 | #include "pycore_initconfig.h" |
Victor Stinner | a1c249c | 2018-11-01 03:15:58 +0100 | [diff] [blame] | 6 | #include "pycore_pathconfig.h" |
Victor Stinner | 361dcdc | 2020-04-15 03:24:57 +0200 | [diff] [blame] | 7 | #include "osdefs.h" // DELIM |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
Guido van Rossum | 21f8497 | 1997-06-02 22:18:31 +0000 | [diff] [blame] | 10 | #include <string.h> |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 11 | |
Brett Cannon | f6af76d | 2004-06-26 04:03:06 +0000 | [diff] [blame] | 12 | #ifdef __APPLE__ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 13 | # include <mach-o/dyld.h> |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 16 | /* Search in some common locations for the associated Python libraries. |
| 17 | * |
| 18 | * Two directories must be found, the platform independent directory |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 19 | * (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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 23 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 24 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 30 | * |
| 31 | * Before any searches are done, the location of the executable is |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | * 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] | 33 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 37 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 38 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 41 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 42 | * Finally, argv0_path is set to the directory containing the executable |
| 43 | * (i.e. the last component is stripped). |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 44 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 45 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 48 | * |
| 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 Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 51 | * 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 Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 54 | * pybuilddir.txt. If the landmark is found, we're done. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 55 | * |
| 56 | * For the remaining steps, the prefix landmark will always be |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 57 | * 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] | 58 | * lib/python$VERSION/lib-dynload, where $VERSION is Python's version |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 59 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 63 | * |
| 64 | * Step 2. See if the $PYTHONHOME environment variable points to the |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 65 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 69 | * |
| 70 | * 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] | 71 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 75 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 76 | * 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 Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 79 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 80 | * That's it! |
| 81 | * |
| 82 | * Well, almost. Once we have determined prefix and exec_prefix, the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 83 | * preprocessor variable PYTHONPATH is used to construct a path. Each |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 84 | * 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 Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 95 | * |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 96 | * An embedding application can use Py_SetPath() to override all of |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 97 | * these automatic path computations. |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 98 | * |
Antoine Pitrou | eba57b6 | 2010-08-14 12:33:18 +0000 | [diff] [blame] | 99 | * NOTE: Windows MSVC builds use PC/getpathp.c instead! |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 100 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 101 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 102 | #ifdef __cplusplus |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 103 | extern "C" { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
| 106 | |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 107 | #if (!defined(PREFIX) || !defined(EXEC_PREFIX) \ |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 108 | || !defined(VERSION) || !defined(VPATH)) |
| 109 | #error "PREFIX, EXEC_PREFIX, VERSION and VPATH macros must be defined" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 110 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 112 | #ifndef LANDMARK |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 113 | #define LANDMARK L"os.py" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 114 | #endif |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 115 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 116 | #define BUILD_LANDMARK L"Modules/Setup.local" |
| 117 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 118 | #define DECODE_LOCALE_ERR(NAME, LEN) \ |
| 119 | ((LEN) == (size_t)-2) \ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 120 | ? _PyStatus_ERR("cannot decode " NAME) \ |
| 121 | : _PyStatus_NO_MEMORY() |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 122 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 123 | #define PATHLEN_ERR() _PyStatus_ERR("path configuration: path too long") |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 124 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 125 | typedef struct { |
| 126 | wchar_t *path_env; /* PATH environment variable */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 127 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 128 | wchar_t *pythonpath_macro; /* PYTHONPATH macro */ |
| 129 | wchar_t *prefix_macro; /* PREFIX macro */ |
| 130 | wchar_t *exec_prefix_macro; /* EXEC_PREFIX macro */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 131 | wchar_t *vpath_macro; /* VPATH macro */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 132 | |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 133 | wchar_t *lib_python; /* <platlibdir> / "pythonX.Y" */ |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 134 | |
| 135 | int prefix_found; /* found platform independent libraries? */ |
| 136 | int exec_prefix_found; /* found the platform dependent libraries? */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 137 | |
| 138 | int warnings; |
| 139 | const wchar_t *pythonpath_env; |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 140 | const wchar_t *platlibdir; |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 141 | |
| 142 | wchar_t *argv0_path; |
| 143 | wchar_t *zip_path; |
| 144 | wchar_t *prefix; |
| 145 | wchar_t *exec_prefix; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 146 | } PyCalculatePath; |
| 147 | |
| 148 | static const wchar_t delimiter[2] = {DELIM, '\0'}; |
| 149 | static const wchar_t separator[2] = {SEP, '\0'}; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 150 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 151 | |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 152 | /* Get file status. Encode the path to the locale encoding. */ |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 153 | static int |
| 154 | _Py_wstat(const wchar_t* path, struct stat *buf) |
| 155 | { |
| 156 | int err; |
| 157 | char *fname; |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 158 | fname = _Py_EncodeLocaleRaw(path, NULL); |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 159 | if (fname == NULL) { |
| 160 | errno = EINVAL; |
| 161 | return -1; |
| 162 | } |
| 163 | err = stat(fname, buf); |
Victor Stinner | 9dd7620 | 2017-12-21 16:20:32 +0100 | [diff] [blame] | 164 | PyMem_RawFree(fname); |
Victor Stinner | 91afbb6 | 2015-03-24 12:16:28 +0100 | [diff] [blame] | 165 | return err; |
| 166 | } |
| 167 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 168 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 169 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 170 | reduce(wchar_t *dir) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 171 | { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 172 | size_t i = wcslen(dir); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 173 | while (i > 0 && dir[i] != SEP) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 174 | --i; |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 175 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 176 | dir[i] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 177 | } |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 178 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 179 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 180 | /* Is file, not directory */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 181 | static int |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 182 | isfile(const wchar_t *filename) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 183 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 184 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 185 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 186 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 187 | } |
| 188 | if (!S_ISREG(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 189 | return 0; |
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 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 195 | /* Is executable file */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 196 | static int |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 197 | isxfile(const wchar_t *filename) |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 198 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 199 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 200 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 201 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 202 | } |
| 203 | if (!S_ISREG(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 204 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 205 | } |
| 206 | if ((buf.st_mode & 0111) == 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 207 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 208 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 209 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 213 | /* Is directory */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 214 | static int |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 215 | isdir(const wchar_t *filename) |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 216 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 217 | struct stat buf; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 218 | if (_Py_wstat(filename, &buf) != 0) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 219 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 220 | } |
| 221 | if (!S_ISDIR(buf.st_mode)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 222 | return 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 223 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 224 | return 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
Tim Peters | ec8c5a9 | 2004-08-08 01:00:47 +0000 | [diff] [blame] | 228 | /* Add a path component, by appending stuff to buffer. |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 229 | buflen: 'buffer' length in characters including trailing NUL. |
| 230 | |
| 231 | If path2 is empty: |
| 232 | |
| 233 | - if path doesn't end with SEP and is not empty, add SEP to path |
| 234 | - otherwise, do nothing. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 235 | static PyStatus |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 236 | joinpath(wchar_t *path, const wchar_t *path2, size_t path_len) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 237 | { |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 238 | size_t n; |
| 239 | if (!_Py_isabs(path2)) { |
| 240 | n = wcslen(path); |
| 241 | if (n >= path_len) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 242 | return PATHLEN_ERR(); |
| 243 | } |
| 244 | |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 245 | if (n > 0 && path[n-1] != SEP) { |
| 246 | path[n++] = SEP; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 247 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 248 | } |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 249 | else { |
| 250 | n = 0; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 251 | } |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 252 | |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 253 | size_t k = wcslen(path2); |
| 254 | if (n + k >= path_len) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 255 | return PATHLEN_ERR(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 256 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 257 | wcsncpy(path + n, path2, k); |
| 258 | path[n + k] = '\0'; |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 259 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 260 | return _PyStatus_OK(); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 263 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 264 | static wchar_t* |
| 265 | substring(const wchar_t *str, size_t len) |
| 266 | { |
| 267 | wchar_t *substr = PyMem_RawMalloc((len + 1) * sizeof(wchar_t)); |
| 268 | if (substr == NULL) { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | if (len) { |
| 273 | memcpy(substr, str, len * sizeof(wchar_t)); |
| 274 | } |
| 275 | substr[len] = L'\0'; |
| 276 | return substr; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | static wchar_t* |
| 281 | joinpath2(const wchar_t *path, const wchar_t *path2) |
| 282 | { |
| 283 | if (_Py_isabs(path2)) { |
| 284 | return _PyMem_RawWcsdup(path2); |
| 285 | } |
| 286 | |
| 287 | size_t len = wcslen(path); |
| 288 | int add_sep = (len > 0 && path[len - 1] != SEP); |
| 289 | len += add_sep; |
| 290 | len += wcslen(path2); |
| 291 | |
| 292 | wchar_t *new_path = PyMem_RawMalloc((len + 1) * sizeof(wchar_t)); |
| 293 | if (new_path == NULL) { |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | wcscpy(new_path, path); |
| 298 | if (add_sep) { |
| 299 | wcscat(new_path, separator); |
| 300 | } |
| 301 | wcscat(new_path, path2); |
| 302 | return new_path; |
| 303 | } |
| 304 | |
| 305 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 306 | static inline int |
| 307 | safe_wcscpy(wchar_t *dst, const wchar_t *src, size_t n) |
| 308 | { |
| 309 | size_t srclen = wcslen(src); |
| 310 | if (n <= srclen) { |
| 311 | dst[0] = L'\0'; |
| 312 | return -1; |
| 313 | } |
| 314 | memcpy(dst, src, (srclen + 1) * sizeof(wchar_t)); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 319 | /* copy_absolute requires that path be allocated at least |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 320 | 'abs_path_len' characters (including trailing NUL). */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 321 | static PyStatus |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 322 | copy_absolute(wchar_t *abs_path, const wchar_t *path, size_t abs_path_len) |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 323 | { |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 324 | if (_Py_isabs(path)) { |
| 325 | if (safe_wcscpy(abs_path, path, abs_path_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 326 | return PATHLEN_ERR(); |
| 327 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 328 | } |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 329 | else { |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 330 | if (!_Py_wgetcwd(abs_path, abs_path_len)) { |
Victor Stinner | 4f3abb0 | 2010-10-07 23:29:18 +0000 | [diff] [blame] | 331 | /* unable to get the current directory */ |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 332 | if (safe_wcscpy(abs_path, path, abs_path_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 333 | return PATHLEN_ERR(); |
| 334 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 335 | return _PyStatus_OK(); |
Victor Stinner | 4f3abb0 | 2010-10-07 23:29:18 +0000 | [diff] [blame] | 336 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 337 | if (path[0] == '.' && path[1] == SEP) { |
| 338 | path += 2; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 339 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 340 | PyStatus status = joinpath(abs_path, path, abs_path_len); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 341 | if (_PyStatus_EXCEPTION(status)) { |
| 342 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 343 | } |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 344 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 345 | return _PyStatus_OK(); |
Jeremy Hylton | 7198ba9 | 2000-09-25 17:00:24 +0000 | [diff] [blame] | 346 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 347 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 348 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 349 | /* path_len: path length in characters including trailing NUL */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 350 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 351 | absolutize(wchar_t **path_p) |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 352 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 353 | assert(!_Py_isabs(*path_p)); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 354 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 355 | wchar_t abs_path[MAXPATHLEN+1]; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 356 | wchar_t *path = *path_p; |
| 357 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 358 | PyStatus status = copy_absolute(abs_path, path, Py_ARRAY_LENGTH(abs_path)); |
| 359 | if (_PyStatus_EXCEPTION(status)) { |
| 360 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 361 | } |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 362 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 363 | PyMem_RawFree(*path_p); |
| 364 | *path_p = _PyMem_RawWcsdup(abs_path); |
| 365 | if (*path_p == NULL) { |
| 366 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 367 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 368 | return _PyStatus_OK(); |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 371 | |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 372 | /* Is module -- check for .pyc too */ |
| 373 | static PyStatus |
| 374 | ismodule(const wchar_t *path, int *result) |
| 375 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 376 | wchar_t *filename = joinpath2(path, LANDMARK); |
| 377 | if (filename == NULL) { |
| 378 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (isfile(filename)) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 382 | PyMem_RawFree(filename); |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 383 | *result = 1; |
| 384 | return _PyStatus_OK(); |
| 385 | } |
| 386 | |
| 387 | /* Check for the compiled version of prefix. */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 388 | size_t len = wcslen(filename); |
| 389 | wchar_t *pyc = PyMem_RawMalloc((len + 2) * sizeof(wchar_t)); |
| 390 | if (pyc == NULL) { |
| 391 | PyMem_RawFree(filename); |
| 392 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 395 | memcpy(pyc, filename, len * sizeof(wchar_t)); |
| 396 | pyc[len] = L'c'; |
| 397 | pyc[len + 1] = L'\0'; |
| 398 | *result = isfile(pyc); |
| 399 | |
| 400 | PyMem_RawFree(filename); |
| 401 | PyMem_RawFree(pyc); |
| 402 | |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 403 | return _PyStatus_OK(); |
| 404 | } |
| 405 | |
| 406 | |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 407 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 408 | #ifndef EXE_SUFFIX |
| 409 | #define EXE_SUFFIX L".exe" |
| 410 | #endif |
| 411 | |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 412 | /* pathlen: 'path' length in characters including trailing NUL */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 413 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 414 | add_exe_suffix(wchar_t **progpath_p) |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 415 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 416 | wchar_t *progpath = *progpath_p; |
| 417 | |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 418 | /* Check for already have an executable suffix */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 419 | size_t n = wcslen(progpath); |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 420 | size_t s = wcslen(EXE_SUFFIX); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 421 | if (wcsncasecmp(EXE_SUFFIX, progpath + n - s, s) == 0) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 422 | return _PyStatus_OK(); |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 423 | } |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 424 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 425 | wchar_t *progpath2 = PyMem_RawMalloc((n + s + 1) * sizeof(wchar_t)); |
| 426 | if (progpath2 == NULL) { |
| 427 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 428 | } |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 429 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 430 | memcpy(progpath2, progpath, n * sizeof(wchar_t)); |
| 431 | memcpy(progpath2 + n, EXE_SUFFIX, s * sizeof(wchar_t)); |
| 432 | progpath2[n+s] = L'\0'; |
| 433 | |
| 434 | if (isxfile(progpath2)) { |
| 435 | PyMem_RawFree(*progpath_p); |
| 436 | *progpath_p = progpath2; |
| 437 | } |
| 438 | else { |
| 439 | PyMem_RawFree(progpath2); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 440 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 441 | return _PyStatus_OK(); |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 442 | } |
| 443 | #endif |
| 444 | |
| 445 | |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 446 | /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 447 | bytes long. |
| 448 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 449 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 450 | search_for_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig, |
| 451 | wchar_t *prefix, size_t prefix_len, int *found) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 452 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 453 | PyStatus status; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 454 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 455 | /* If PYTHONHOME is set, we believe it unconditionally */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 456 | if (pathconfig->home) { |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 457 | /* Path: <home> / <lib_python> */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 458 | if (safe_wcscpy(prefix, pathconfig->home, prefix_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 459 | return PATHLEN_ERR(); |
| 460 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 461 | wchar_t *delim = wcschr(prefix, DELIM); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 462 | if (delim) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 463 | *delim = L'\0'; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 464 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 465 | status = joinpath(prefix, calculate->lib_python, prefix_len); |
| 466 | if (_PyStatus_EXCEPTION(status)) { |
| 467 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 468 | } |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 469 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 470 | return _PyStatus_OK(); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 471 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 472 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 473 | /* Check to see if argv0_path is in the build directory |
| 474 | |
| 475 | Path: <argv0_path> / <BUILD_LANDMARK define> */ |
| 476 | wchar_t *path = joinpath2(calculate->argv0_path, BUILD_LANDMARK); |
| 477 | if (path == NULL) { |
| 478 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 481 | int is_build_dir = isfile(path); |
| 482 | PyMem_RawFree(path); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 483 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 484 | if (is_build_dir) { |
| 485 | /* argv0_path is the build directory (BUILD_LANDMARK exists), |
| 486 | now also check LANDMARK using ismodule(). */ |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 487 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 488 | /* Path: <argv0_path> / <VPATH macro> / Lib */ |
| 489 | /* or if VPATH is empty: <argv0_path> / Lib */ |
| 490 | if (safe_wcscpy(prefix, calculate->argv0_path, prefix_len) < 0) { |
| 491 | return PATHLEN_ERR(); |
| 492 | } |
| 493 | |
| 494 | status = joinpath(prefix, calculate->vpath_macro, prefix_len); |
| 495 | if (_PyStatus_EXCEPTION(status)) { |
| 496 | return status; |
| 497 | } |
| 498 | |
| 499 | status = joinpath(prefix, L"Lib", prefix_len); |
| 500 | if (_PyStatus_EXCEPTION(status)) { |
| 501 | return status; |
| 502 | } |
| 503 | |
| 504 | int module; |
| 505 | status = ismodule(prefix, &module); |
| 506 | if (_PyStatus_EXCEPTION(status)) { |
| 507 | return status; |
| 508 | } |
| 509 | if (module) { |
| 510 | /* BUILD_LANDMARK and LANDMARK found */ |
| 511 | *found = -1; |
| 512 | return _PyStatus_OK(); |
Victor Stinner | 2158231 | 2010-10-23 00:13:28 +0000 | [diff] [blame] | 513 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 514 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 515 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 516 | /* Search from argv0_path, until root is found */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 517 | status = copy_absolute(prefix, calculate->argv0_path, prefix_len); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 518 | if (_PyStatus_EXCEPTION(status)) { |
| 519 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 520 | } |
| 521 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 522 | do { |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 523 | /* Path: <argv0_path or substring> / <lib_python> / LANDMARK */ |
| 524 | size_t n = wcslen(prefix); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 525 | status = joinpath(prefix, calculate->lib_python, prefix_len); |
| 526 | if (_PyStatus_EXCEPTION(status)) { |
| 527 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 528 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 529 | |
| 530 | int module; |
| 531 | status = ismodule(prefix, &module); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 532 | if (_PyStatus_EXCEPTION(status)) { |
| 533 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 534 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 535 | if (module) { |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 536 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 537 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 538 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 539 | prefix[n] = L'\0'; |
| 540 | reduce(prefix); |
| 541 | } while (prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 542 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 543 | /* Look at configure's PREFIX. |
| 544 | Path: <PREFIX macro> / <lib_python> / LANDMARK */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 545 | if (safe_wcscpy(prefix, calculate->prefix_macro, prefix_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 546 | return PATHLEN_ERR(); |
| 547 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 548 | status = joinpath(prefix, calculate->lib_python, prefix_len); |
| 549 | if (_PyStatus_EXCEPTION(status)) { |
| 550 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 551 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 552 | |
| 553 | int module; |
| 554 | status = ismodule(prefix, &module); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 555 | if (_PyStatus_EXCEPTION(status)) { |
| 556 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 557 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 558 | if (module) { |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 559 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 560 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 561 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 562 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 563 | /* Fail */ |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 564 | *found = 0; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 565 | return _PyStatus_OK(); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 569 | static PyStatus |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 570 | calculate_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 571 | { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 572 | wchar_t prefix[MAXPATHLEN+1]; |
| 573 | memset(prefix, 0, sizeof(prefix)); |
| 574 | size_t prefix_len = Py_ARRAY_LENGTH(prefix); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 575 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 576 | PyStatus status; |
| 577 | status = search_for_prefix(calculate, pathconfig, |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 578 | prefix, prefix_len, |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 579 | &calculate->prefix_found); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 580 | if (_PyStatus_EXCEPTION(status)) { |
| 581 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 582 | } |
| 583 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 584 | if (!calculate->prefix_found) { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 585 | if (calculate->warnings) { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 586 | fprintf(stderr, |
| 587 | "Could not find platform independent libraries <prefix>\n"); |
| 588 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 589 | |
| 590 | calculate->prefix = joinpath2(calculate->prefix_macro, |
| 591 | calculate->lib_python); |
| 592 | } |
| 593 | else { |
| 594 | calculate->prefix = _PyMem_RawWcsdup(prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 595 | } |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 596 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 597 | if (calculate->prefix == NULL) { |
| 598 | return _PyStatus_NO_MEMORY(); |
| 599 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 600 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 604 | static PyStatus |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 605 | calculate_set_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 606 | { |
| 607 | /* Reduce prefix and exec_prefix to their essence, |
| 608 | * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. |
| 609 | * If we're loading relative to the build directory, |
| 610 | * return the compiled-in defaults instead. |
| 611 | */ |
| 612 | if (calculate->prefix_found > 0) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 613 | wchar_t *prefix = _PyMem_RawWcsdup(calculate->prefix); |
| 614 | if (prefix == NULL) { |
| 615 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 616 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 617 | |
| 618 | reduce(prefix); |
| 619 | reduce(prefix); |
| 620 | if (prefix[0]) { |
| 621 | pathconfig->prefix = prefix; |
| 622 | } |
| 623 | else { |
| 624 | PyMem_RawFree(prefix); |
| 625 | |
| 626 | /* The prefix is the root directory, but reduce() chopped |
| 627 | off the "/". */ |
| 628 | pathconfig->prefix = _PyMem_RawWcsdup(separator); |
| 629 | if (pathconfig->prefix == NULL) { |
| 630 | return _PyStatus_NO_MEMORY(); |
| 631 | } |
| 632 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 633 | } |
| 634 | else { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 635 | pathconfig->prefix = _PyMem_RawWcsdup(calculate->prefix_macro); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 636 | if (pathconfig->prefix == NULL) { |
| 637 | return _PyStatus_NO_MEMORY(); |
| 638 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 639 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 640 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 644 | static PyStatus |
| 645 | calculate_pybuilddir(const wchar_t *argv0_path, |
| 646 | wchar_t *exec_prefix, size_t exec_prefix_len, |
| 647 | int *found) |
| 648 | { |
| 649 | PyStatus status; |
| 650 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 651 | /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" |
| 652 | is written by setup.py and contains the relative path to the location |
| 653 | of shared library modules. |
| 654 | |
| 655 | Filename: <argv0_path> / "pybuilddir.txt" */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 656 | wchar_t *filename = joinpath2(argv0_path, L"pybuilddir.txt"); |
| 657 | if (filename == NULL) { |
| 658 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 661 | FILE *fp = _Py_wfopen(filename, L"rb"); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 662 | PyMem_RawFree(filename); |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 663 | if (fp == NULL) { |
| 664 | errno = 0; |
| 665 | return _PyStatus_OK(); |
| 666 | } |
| 667 | |
| 668 | char buf[MAXPATHLEN + 1]; |
| 669 | size_t n = fread(buf, 1, Py_ARRAY_LENGTH(buf) - 1, fp); |
| 670 | buf[n] = '\0'; |
| 671 | fclose(fp); |
| 672 | |
| 673 | size_t dec_len; |
| 674 | wchar_t *pybuilddir = _Py_DecodeUTF8_surrogateescape(buf, n, &dec_len); |
| 675 | if (!pybuilddir) { |
| 676 | return DECODE_LOCALE_ERR("pybuilddir.txt", dec_len); |
| 677 | } |
| 678 | |
| 679 | /* Path: <argv0_path> / <pybuilddir content> */ |
| 680 | if (safe_wcscpy(exec_prefix, argv0_path, exec_prefix_len) < 0) { |
| 681 | PyMem_RawFree(pybuilddir); |
| 682 | return PATHLEN_ERR(); |
| 683 | } |
| 684 | status = joinpath(exec_prefix, pybuilddir, exec_prefix_len); |
| 685 | PyMem_RawFree(pybuilddir); |
| 686 | if (_PyStatus_EXCEPTION(status)) { |
| 687 | return status; |
| 688 | } |
| 689 | |
| 690 | *found = -1; |
| 691 | return _PyStatus_OK(); |
| 692 | } |
| 693 | |
| 694 | |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 695 | /* 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] | 696 | MAXPATHLEN bytes long. |
Jeremy Hylton | 6372fe1 | 2000-09-27 20:51:17 +0000 | [diff] [blame] | 697 | */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 698 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 699 | search_for_exec_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig, |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 700 | wchar_t *exec_prefix, size_t exec_prefix_len, |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 701 | int *found) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 702 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 703 | PyStatus status; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 704 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 705 | /* If PYTHONHOME is set, we believe it unconditionally */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 706 | if (pathconfig->home) { |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 707 | /* Path: <home> / <lib_python> / "lib-dynload" */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 708 | wchar_t *delim = wcschr(pathconfig->home, DELIM); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 709 | if (delim) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 710 | if (safe_wcscpy(exec_prefix, delim+1, exec_prefix_len) < 0) { |
| 711 | return PATHLEN_ERR(); |
| 712 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 713 | } |
| 714 | else { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 715 | if (safe_wcscpy(exec_prefix, pathconfig->home, exec_prefix_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 716 | return PATHLEN_ERR(); |
| 717 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 718 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 719 | status = joinpath(exec_prefix, calculate->lib_python, exec_prefix_len); |
| 720 | if (_PyStatus_EXCEPTION(status)) { |
| 721 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 722 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 723 | status = joinpath(exec_prefix, L"lib-dynload", exec_prefix_len); |
| 724 | if (_PyStatus_EXCEPTION(status)) { |
| 725 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 726 | } |
| 727 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 728 | return _PyStatus_OK(); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 729 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 730 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 731 | /* Check for pybuilddir.txt */ |
| 732 | assert(*found == 0); |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 733 | status = calculate_pybuilddir(calculate->argv0_path, |
| 734 | exec_prefix, exec_prefix_len, found); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 735 | if (_PyStatus_EXCEPTION(status)) { |
| 736 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 737 | } |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 738 | if (*found) { |
| 739 | return _PyStatus_OK(); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 740 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 741 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 742 | /* Search from argv0_path, until root is found */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 743 | status = copy_absolute(exec_prefix, calculate->argv0_path, exec_prefix_len); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 744 | if (_PyStatus_EXCEPTION(status)) { |
| 745 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 748 | do { |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 749 | /* Path: <argv0_path or substring> / <lib_python> / "lib-dynload" */ |
| 750 | size_t n = wcslen(exec_prefix); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 751 | status = joinpath(exec_prefix, calculate->lib_python, exec_prefix_len); |
| 752 | if (_PyStatus_EXCEPTION(status)) { |
| 753 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 754 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 755 | status = joinpath(exec_prefix, L"lib-dynload", exec_prefix_len); |
| 756 | if (_PyStatus_EXCEPTION(status)) { |
| 757 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 758 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 759 | if (isdir(exec_prefix)) { |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 760 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 761 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 762 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 763 | exec_prefix[n] = L'\0'; |
| 764 | reduce(exec_prefix); |
| 765 | } while (exec_prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 766 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 767 | /* Look at configure's EXEC_PREFIX. |
| 768 | |
| 769 | Path: <EXEC_PREFIX macro> / <lib_python> / "lib-dynload" */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 770 | if (safe_wcscpy(exec_prefix, calculate->exec_prefix_macro, exec_prefix_len) < 0) { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 771 | return PATHLEN_ERR(); |
| 772 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 773 | status = joinpath(exec_prefix, calculate->lib_python, exec_prefix_len); |
| 774 | if (_PyStatus_EXCEPTION(status)) { |
| 775 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 776 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 777 | status = joinpath(exec_prefix, L"lib-dynload", exec_prefix_len); |
| 778 | if (_PyStatus_EXCEPTION(status)) { |
| 779 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 780 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 781 | if (isdir(exec_prefix)) { |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 782 | *found = 1; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 783 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 784 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 785 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 786 | /* Fail */ |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 787 | *found = 0; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 788 | return _PyStatus_OK(); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 791 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 792 | static PyStatus |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 793 | calculate_exec_prefix(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 794 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 795 | PyStatus status; |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 796 | wchar_t exec_prefix[MAXPATHLEN+1]; |
| 797 | memset(exec_prefix, 0, sizeof(exec_prefix)); |
| 798 | size_t exec_prefix_len = Py_ARRAY_LENGTH(exec_prefix); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 799 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 800 | status = search_for_exec_prefix(calculate, pathconfig, |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 801 | exec_prefix, exec_prefix_len, |
| 802 | &calculate->exec_prefix_found); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 803 | if (_PyStatus_EXCEPTION(status)) { |
| 804 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 805 | } |
| 806 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 807 | if (!calculate->exec_prefix_found) { |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 808 | if (calculate->warnings) { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 809 | fprintf(stderr, |
| 810 | "Could not find platform dependent libraries <exec_prefix>\n"); |
| 811 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 812 | |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 813 | /* <platlibdir> / "lib-dynload" */ |
Sandro Mani | 8f023a2 | 2020-06-08 17:28:11 +0200 | [diff] [blame] | 814 | wchar_t *lib_dynload = joinpath2(calculate->platlibdir, |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 815 | L"lib-dynload"); |
| 816 | if (lib_dynload == NULL) { |
| 817 | return _PyStatus_NO_MEMORY(); |
| 818 | } |
| 819 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 820 | calculate->exec_prefix = joinpath2(calculate->exec_prefix_macro, |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 821 | lib_dynload); |
| 822 | PyMem_RawFree(lib_dynload); |
| 823 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 824 | if (calculate->exec_prefix == NULL) { |
| 825 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 826 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 827 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 828 | else { |
| 829 | /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ |
| 830 | calculate->exec_prefix = _PyMem_RawWcsdup(exec_prefix); |
| 831 | if (calculate->exec_prefix == NULL) { |
| 832 | return _PyStatus_NO_MEMORY(); |
| 833 | } |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 834 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 835 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 839 | static PyStatus |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 840 | calculate_set_exec_prefix(PyCalculatePath *calculate, |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 841 | _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 842 | { |
| 843 | if (calculate->exec_prefix_found > 0) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 844 | wchar_t *exec_prefix = _PyMem_RawWcsdup(calculate->exec_prefix); |
| 845 | if (exec_prefix == NULL) { |
| 846 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 847 | } |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 848 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 849 | reduce(exec_prefix); |
| 850 | reduce(exec_prefix); |
| 851 | reduce(exec_prefix); |
| 852 | |
| 853 | if (exec_prefix[0]) { |
| 854 | pathconfig->exec_prefix = exec_prefix; |
| 855 | } |
| 856 | else { |
| 857 | /* empty string: use SEP instead */ |
| 858 | PyMem_RawFree(exec_prefix); |
| 859 | |
| 860 | /* The exec_prefix is the root directory, but reduce() chopped |
| 861 | off the "/". */ |
| 862 | pathconfig->exec_prefix = _PyMem_RawWcsdup(separator); |
| 863 | if (pathconfig->exec_prefix == NULL) { |
| 864 | return _PyStatus_NO_MEMORY(); |
| 865 | } |
| 866 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 867 | } |
| 868 | else { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 869 | pathconfig->exec_prefix = _PyMem_RawWcsdup(calculate->exec_prefix_macro); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 870 | if (pathconfig->exec_prefix == NULL) { |
| 871 | return _PyStatus_NO_MEMORY(); |
| 872 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 873 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 874 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 878 | /* Similar to shutil.which(). |
| 879 | If found, write the path into *abs_path_p. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 880 | static PyStatus |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 881 | calculate_which(const wchar_t *path_env, wchar_t *program_name, |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 882 | wchar_t **abs_path_p) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 883 | { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 884 | while (1) { |
| 885 | wchar_t *delim = wcschr(path_env, DELIM); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 886 | wchar_t *abs_path; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 887 | |
| 888 | if (delim) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 889 | wchar_t *path = substring(path_env, delim - path_env); |
| 890 | if (path == NULL) { |
| 891 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 892 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 893 | abs_path = joinpath2(path, program_name); |
| 894 | PyMem_RawFree(path); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 895 | } |
| 896 | else { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 897 | abs_path = joinpath2(path_env, program_name); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 900 | if (abs_path == NULL) { |
| 901 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 902 | } |
| 903 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 904 | if (isxfile(abs_path)) { |
| 905 | *abs_path_p = abs_path; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 906 | return _PyStatus_OK(); |
| 907 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 908 | PyMem_RawFree(abs_path); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 909 | |
| 910 | if (!delim) { |
| 911 | break; |
| 912 | } |
| 913 | path_env = delim + 1; |
| 914 | } |
| 915 | |
| 916 | /* not found */ |
| 917 | return _PyStatus_OK(); |
| 918 | } |
| 919 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 920 | |
Victor Stinner | b919795 | 2017-11-23 19:02:04 +0100 | [diff] [blame] | 921 | #ifdef __APPLE__ |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 922 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 923 | calculate_program_macos(wchar_t **abs_path_p) |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 924 | { |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 925 | char execpath[MAXPATHLEN + 1]; |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 926 | uint32_t nsexeclength = Py_ARRAY_LENGTH(execpath) - 1; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 927 | |
| 928 | /* On Mac OS X, if a script uses an interpreter of the form |
| 929 | "#!/opt/python2.3/bin/python", the kernel only passes "python" |
| 930 | as argv[0], which falls through to the $PATH search below. |
| 931 | If /opt/python2.3/bin isn't in your path, or is near the end, |
| 932 | this algorithm may incorrectly find /usr/bin/python. To work |
| 933 | around this, we can use _NSGetExecutablePath to get a better |
| 934 | hint of what the intended interpreter was, although this |
| 935 | will fail if a relative path was used. but in that case, |
| 936 | absolutize() should help us out below |
| 937 | */ |
| 938 | if (_NSGetExecutablePath(execpath, &nsexeclength) != 0 |
| 939 | || (wchar_t)execpath[0] != SEP) |
| 940 | { |
| 941 | /* _NSGetExecutablePath() failed or the path is relative */ |
| 942 | return _PyStatus_OK(); |
| 943 | } |
| 944 | |
| 945 | size_t len; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 946 | *abs_path_p = Py_DecodeLocale(execpath, &len); |
| 947 | if (*abs_path_p == NULL) { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 948 | return DECODE_LOCALE_ERR("executable path", len); |
| 949 | } |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 950 | return _PyStatus_OK(); |
| 951 | } |
| 952 | #endif /* __APPLE__ */ |
| 953 | |
| 954 | |
| 955 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 956 | calculate_program_impl(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 957 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 958 | assert(pathconfig->program_full_path == NULL); |
| 959 | |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 960 | PyStatus status; |
Victor Stinner | b919795 | 2017-11-23 19:02:04 +0100 | [diff] [blame] | 961 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 962 | /* If there is no slash in the argv0 path, then we have to |
| 963 | * assume python is on the user's $PATH, since there's no |
| 964 | * other way to find a directory to start the search from. If |
| 965 | * $PATH isn't exported, you lose. |
| 966 | */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 967 | if (wcschr(pathconfig->program_name, SEP)) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 968 | pathconfig->program_full_path = _PyMem_RawWcsdup(pathconfig->program_name); |
| 969 | if (pathconfig->program_full_path == NULL) { |
| 970 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 971 | } |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 972 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 973 | } |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 974 | |
Jack Jansen | 1afd480 | 2004-06-03 14:33:03 +0000 | [diff] [blame] | 975 | #ifdef __APPLE__ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 976 | wchar_t *abs_path = NULL; |
| 977 | status = calculate_program_macos(&abs_path); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 978 | if (_PyStatus_EXCEPTION(status)) { |
| 979 | return status; |
| 980 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 981 | if (abs_path) { |
| 982 | pathconfig->program_full_path = abs_path; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 983 | return _PyStatus_OK(); |
Antoine Pitrou | 99773ac | 2010-08-14 12:34:41 +0000 | [diff] [blame] | 984 | } |
Brett Cannon | 6cc4814 | 2004-06-24 00:48:44 +0000 | [diff] [blame] | 985 | #endif /* __APPLE__ */ |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 986 | |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 987 | if (calculate->path_env) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 988 | wchar_t *abs_path = NULL; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 989 | status = calculate_which(calculate->path_env, pathconfig->program_name, |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 990 | &abs_path); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 991 | if (_PyStatus_EXCEPTION(status)) { |
| 992 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 993 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 994 | if (abs_path) { |
| 995 | pathconfig->program_full_path = abs_path; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 996 | return _PyStatus_OK(); |
| 997 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 998 | } |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 999 | |
| 1000 | /* In the last resort, use an empty string */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1001 | pathconfig->program_full_path = _PyMem_RawWcsdup(L""); |
| 1002 | if (pathconfig->program_full_path == NULL) { |
| 1003 | return _PyStatus_NO_MEMORY(); |
| 1004 | } |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1005 | return _PyStatus_OK(); |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | /* Calculate pathconfig->program_full_path */ |
| 1010 | static PyStatus |
| 1011 | calculate_program(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
| 1012 | { |
| 1013 | PyStatus status; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1014 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1015 | status = calculate_program_impl(calculate, pathconfig); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1016 | if (_PyStatus_EXCEPTION(status)) { |
| 1017 | return status; |
| 1018 | } |
| 1019 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1020 | if (pathconfig->program_full_path[0] != '\0') { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1021 | /* program_full_path is not empty */ |
| 1022 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1023 | /* Make sure that program_full_path is an absolute path */ |
| 1024 | if (!_Py_isabs(pathconfig->program_full_path)) { |
| 1025 | status = absolutize(&pathconfig->program_full_path); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1026 | if (_PyStatus_EXCEPTION(status)) { |
| 1027 | return status; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | #if defined(__CYGWIN__) || defined(__MINGW32__) |
| 1032 | /* For these platforms it is necessary to ensure that the .exe suffix |
| 1033 | * is appended to the filename, otherwise there is potential for |
| 1034 | * sys.executable to return the name of a directory under the same |
| 1035 | * path (bpo-28441). |
| 1036 | */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1037 | status = add_exe_suffix(&pathconfig->program_full_path); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1038 | if (_PyStatus_EXCEPTION(status)) { |
| 1039 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1040 | } |
E. M. Bray | 7a7693e | 2018-10-05 13:38:50 +0200 | [diff] [blame] | 1041 | #endif |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1042 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1043 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1047 | #if HAVE_READLINK |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1048 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1049 | resolve_symlinks(wchar_t **path_p) |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1050 | { |
| 1051 | wchar_t new_path[MAXPATHLEN + 1]; |
| 1052 | const size_t new_path_len = Py_ARRAY_LENGTH(new_path); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1053 | unsigned int nlink = 0; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1054 | |
| 1055 | while (1) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1056 | int linklen = _Py_wreadlink(*path_p, new_path, new_path_len); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1057 | if (linklen == -1) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1058 | /* not a symbolic link: we are done */ |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | if (_Py_isabs(new_path)) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1063 | PyMem_RawFree(*path_p); |
| 1064 | *path_p = _PyMem_RawWcsdup(new_path); |
| 1065 | if (*path_p == NULL) { |
| 1066 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | else { |
| 1070 | /* new_path is relative to path */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1071 | reduce(*path_p); |
| 1072 | |
| 1073 | wchar_t *abs_path = joinpath2(*path_p, new_path); |
| 1074 | if (abs_path == NULL) { |
| 1075 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1076 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1077 | |
| 1078 | PyMem_RawFree(*path_p); |
| 1079 | *path_p = abs_path; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1082 | nlink++; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1083 | /* 40 is the Linux kernel 4.2 limit */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1084 | if (nlink >= 40) { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1085 | return _PyStatus_ERR("maximum number of symbolic links reached"); |
| 1086 | } |
| 1087 | } |
| 1088 | return _PyStatus_OK(); |
| 1089 | } |
| 1090 | #endif /* HAVE_READLINK */ |
| 1091 | |
| 1092 | |
| 1093 | #ifdef WITH_NEXT_FRAMEWORK |
| 1094 | static PyStatus |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1095 | calculate_argv0_path_framework(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1096 | { |
| 1097 | NSModule pythonModule; |
| 1098 | |
| 1099 | /* On Mac OS X we have a special case if we're running from a framework. |
| 1100 | This is because the python home should be set relative to the library, |
| 1101 | which is in the framework, not relative to the executable, which may |
| 1102 | be outside of the framework. Except when we're in the build |
| 1103 | directory... */ |
| 1104 | pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); |
| 1105 | |
| 1106 | /* Use dylib functions to find out where the framework was loaded from */ |
| 1107 | const char* modPath = NSLibraryNameForModule(pythonModule); |
| 1108 | if (modPath == NULL) { |
| 1109 | return _PyStatus_OK(); |
| 1110 | } |
| 1111 | |
| 1112 | /* We're in a framework. |
| 1113 | See if we might be in the build directory. The framework in the |
| 1114 | build directory is incomplete, it only has the .dylib and a few |
| 1115 | needed symlinks, it doesn't have the Lib directories and such. |
| 1116 | If we're running with the framework from the build directory we must |
| 1117 | be running the interpreter in the build directory, so we use the |
| 1118 | build-directory-specific logic to find Lib and such. */ |
| 1119 | size_t len; |
| 1120 | wchar_t* wbuf = Py_DecodeLocale(modPath, &len); |
| 1121 | if (wbuf == NULL) { |
| 1122 | return DECODE_LOCALE_ERR("framework location", len); |
| 1123 | } |
| 1124 | |
| 1125 | /* Path: reduce(modPath) / lib_python / LANDMARK */ |
| 1126 | PyStatus status; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1127 | |
| 1128 | wchar_t *parent = _PyMem_RawWcsdup(wbuf); |
| 1129 | if (parent == NULL) { |
| 1130 | status = _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1131 | goto done; |
| 1132 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1133 | |
| 1134 | reduce(parent); |
Victor Stinner | b6e0fc7 | 2019-10-10 15:42:30 +0200 | [diff] [blame] | 1135 | wchar_t *lib_python = joinpath2(parent, calculate->lib_python); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1136 | PyMem_RawFree(parent); |
| 1137 | |
| 1138 | if (lib_python == NULL) { |
| 1139 | status = _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1140 | goto done; |
| 1141 | } |
Victor Stinner | f0c8579 | 2019-10-04 03:53:26 +0200 | [diff] [blame] | 1142 | |
| 1143 | int module; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1144 | status = ismodule(lib_python, &module); |
| 1145 | PyMem_RawFree(lib_python); |
| 1146 | |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1147 | if (_PyStatus_EXCEPTION(status)) { |
| 1148 | goto done; |
| 1149 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1150 | if (!module) { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1151 | /* We are in the build directory so use the name of the |
| 1152 | executable - we know that the absolute path is passed */ |
Victor Stinner | b6e0fc7 | 2019-10-10 15:42:30 +0200 | [diff] [blame] | 1153 | PyMem_RawFree(calculate->argv0_path); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1154 | calculate->argv0_path = _PyMem_RawWcsdup(pathconfig->program_full_path); |
| 1155 | if (calculate->argv0_path == NULL) { |
| 1156 | status = _PyStatus_NO_MEMORY(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1157 | goto done; |
| 1158 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1159 | |
| 1160 | status = _PyStatus_OK(); |
| 1161 | goto done; |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1162 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1163 | |
| 1164 | /* Use the location of the library as argv0_path */ |
Victor Stinner | b6e0fc7 | 2019-10-10 15:42:30 +0200 | [diff] [blame] | 1165 | PyMem_RawFree(calculate->argv0_path); |
| 1166 | calculate->argv0_path = wbuf; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1167 | return _PyStatus_OK(); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1168 | |
| 1169 | done: |
| 1170 | PyMem_RawFree(wbuf); |
| 1171 | return status; |
| 1172 | } |
| 1173 | #endif |
| 1174 | |
| 1175 | |
| 1176 | static PyStatus |
| 1177 | calculate_argv0_path(PyCalculatePath *calculate, |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1178 | _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1179 | { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1180 | PyStatus status; |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1181 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1182 | calculate->argv0_path = _PyMem_RawWcsdup(pathconfig->program_full_path); |
| 1183 | if (calculate->argv0_path == NULL) { |
| 1184 | return _PyStatus_NO_MEMORY(); |
Victor Stinner | faddaed | 2019-03-19 02:58:14 +0100 | [diff] [blame] | 1185 | } |
Jack Jansen | 55070f5 | 2001-12-02 23:56:28 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 1187 | #ifdef WITH_NEXT_FRAMEWORK |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1188 | status = calculate_argv0_path_framework(calculate, pathconfig); |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1189 | if (_PyStatus_EXCEPTION(status)) { |
| 1190 | return status; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1191 | } |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 1192 | #endif |
Guido van Rossum | e296ced | 2001-09-28 20:00:29 +0000 | [diff] [blame] | 1193 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1194 | status = resolve_symlinks(&calculate->argv0_path); |
| 1195 | if (_PyStatus_EXCEPTION(status)) { |
| 1196 | return status; |
| 1197 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1198 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1199 | reduce(calculate->argv0_path); |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1200 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1201 | return _PyStatus_OK(); |
| 1202 | } |
| 1203 | |
| 1204 | |
| 1205 | static PyStatus |
| 1206 | calculate_open_pyenv(PyCalculatePath *calculate, FILE **env_file_p) |
| 1207 | { |
| 1208 | *env_file_p = NULL; |
| 1209 | |
| 1210 | const wchar_t *env_cfg = L"pyvenv.cfg"; |
| 1211 | |
| 1212 | /* Filename: <argv0_path> / "pyvenv.cfg" */ |
| 1213 | wchar_t *filename = joinpath2(calculate->argv0_path, env_cfg); |
| 1214 | if (filename == NULL) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1215 | return _PyStatus_NO_MEMORY(); |
| 1216 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1217 | |
| 1218 | *env_file_p = _Py_wfopen(filename, L"r"); |
| 1219 | PyMem_RawFree(filename); |
| 1220 | |
| 1221 | if (*env_file_p != NULL) { |
| 1222 | return _PyStatus_OK(); |
| 1223 | |
| 1224 | } |
| 1225 | |
| 1226 | /* fopen() failed: reset errno */ |
| 1227 | errno = 0; |
| 1228 | |
| 1229 | /* Path: <basename(argv0_path)> / "pyvenv.cfg" */ |
| 1230 | wchar_t *parent = _PyMem_RawWcsdup(calculate->argv0_path); |
| 1231 | if (parent == NULL) { |
| 1232 | return _PyStatus_NO_MEMORY(); |
| 1233 | } |
| 1234 | reduce(parent); |
| 1235 | |
| 1236 | filename = joinpath2(parent, env_cfg); |
| 1237 | PyMem_RawFree(parent); |
| 1238 | if (filename == NULL) { |
| 1239 | return _PyStatus_NO_MEMORY(); |
| 1240 | } |
| 1241 | |
| 1242 | *env_file_p = _Py_wfopen(filename, L"r"); |
| 1243 | PyMem_RawFree(filename); |
| 1244 | |
| 1245 | if (*env_file_p == NULL) { |
| 1246 | /* fopen() failed: reset errno */ |
| 1247 | errno = 0; |
| 1248 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1249 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1250 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1251 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 1252 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1253 | /* Search for an "pyvenv.cfg" environment configuration file, first in the |
| 1254 | executable's directory and then in the parent directory. |
| 1255 | If found, open it for use when searching for prefixes. |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1256 | |
| 1257 | Write the 'home' variable of pyvenv.cfg into calculate->argv0_path. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1258 | static PyStatus |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1259 | calculate_read_pyenv(PyCalculatePath *calculate) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1260 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1261 | PyStatus status; |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1262 | FILE *env_file = NULL; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 1263 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1264 | status = calculate_open_pyenv(calculate, &env_file); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1265 | if (_PyStatus_EXCEPTION(status)) { |
Miss Islington (bot) | 0e9af8c | 2021-06-02 16:50:38 -0700 | [diff] [blame] | 1266 | assert(env_file == NULL); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1267 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1268 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1269 | if (env_file == NULL) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1270 | /* pyvenv.cfg not found */ |
| 1271 | return _PyStatus_OK(); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 1272 | } |
| 1273 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1274 | /* Look for a 'home' variable and set argv0_path to it, if found */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1275 | wchar_t *home = NULL; |
| 1276 | status = _Py_FindEnvConfigValue(env_file, L"home", &home); |
| 1277 | if (_PyStatus_EXCEPTION(status)) { |
| 1278 | fclose(env_file); |
| 1279 | return status; |
| 1280 | } |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 1281 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1282 | if (home) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1283 | PyMem_RawFree(calculate->argv0_path); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1284 | calculate->argv0_path = home; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1285 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1286 | fclose(env_file); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1287 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1288 | } |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 1289 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1290 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1291 | static PyStatus |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1292 | calculate_zip_path(PyCalculatePath *calculate) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1293 | { |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1294 | PyStatus res; |
| 1295 | |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 1296 | /* Path: <platlibdir> / "pythonXY.zip" */ |
| 1297 | wchar_t *path = joinpath2(calculate->platlibdir, |
| 1298 | L"python" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) |
| 1299 | L".zip"); |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1300 | if (path == NULL) { |
| 1301 | return _PyStatus_NO_MEMORY(); |
| 1302 | } |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1303 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1304 | if (calculate->prefix_found > 0) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1305 | /* Use the reduced prefix returned by Py_GetPrefix() |
| 1306 | |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 1307 | Path: <basename(basename(prefix))> / <platlibdir> / "pythonXY.zip" */ |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1308 | wchar_t *parent = _PyMem_RawWcsdup(calculate->prefix); |
| 1309 | if (parent == NULL) { |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1310 | res = _PyStatus_NO_MEMORY(); |
| 1311 | goto done; |
Victor Stinner | bb6bf7d | 2019-09-24 18:21:02 +0200 | [diff] [blame] | 1312 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1313 | reduce(parent); |
| 1314 | reduce(parent); |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1315 | calculate->zip_path = joinpath2(parent, path); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1316 | PyMem_RawFree(parent); |
Victor Stinner | d434110 | 2017-11-23 00:12:09 +0100 | [diff] [blame] | 1317 | } |
| 1318 | else { |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1319 | calculate->zip_path = joinpath2(calculate->prefix_macro, path); |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1320 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1321 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1322 | if (calculate->zip_path == NULL) { |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1323 | res = _PyStatus_NO_MEMORY(); |
| 1324 | goto done; |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1325 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1326 | |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1327 | res = _PyStatus_OK(); |
| 1328 | |
| 1329 | done: |
| 1330 | PyMem_RawFree(path); |
| 1331 | return res; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1335 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1336 | calculate_module_search_path(PyCalculatePath *calculate, |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1337 | _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1338 | { |
| 1339 | /* Calculate size of return buffer */ |
| 1340 | size_t bufsz = 0; |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1341 | if (calculate->pythonpath_env != NULL) { |
| 1342 | bufsz += wcslen(calculate->pythonpath_env) + 1; |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1343 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1344 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1345 | wchar_t *defpath = calculate->pythonpath_macro; |
| 1346 | size_t prefixsz = wcslen(calculate->prefix) + 1; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1347 | while (1) { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1348 | wchar_t *delim = wcschr(defpath, DELIM); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1349 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 1350 | if (!_Py_isabs(defpath)) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1351 | /* Paths are relative to prefix */ |
| 1352 | bufsz += prefixsz; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1353 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1354 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1355 | if (delim) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1356 | bufsz += delim - defpath + 1; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1357 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1358 | else { |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 1359 | bufsz += wcslen(defpath) + 1; |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1360 | break; |
| 1361 | } |
| 1362 | defpath = delim + 1; |
| 1363 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1364 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1365 | bufsz += wcslen(calculate->zip_path) + 1; |
| 1366 | bufsz += wcslen(calculate->exec_prefix) + 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1367 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1368 | /* Allocate the buffer */ |
| 1369 | wchar_t *buf = PyMem_RawMalloc(bufsz * sizeof(wchar_t)); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1370 | if (buf == NULL) { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1371 | return _PyStatus_NO_MEMORY(); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1372 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1373 | buf[0] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1374 | |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1375 | /* Run-time value of $PYTHONPATH goes first */ |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1376 | if (calculate->pythonpath_env) { |
| 1377 | wcscpy(buf, calculate->pythonpath_env); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1378 | wcscat(buf, delimiter); |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1379 | } |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1380 | |
| 1381 | /* Next is the default zip path */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1382 | wcscat(buf, calculate->zip_path); |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1383 | wcscat(buf, delimiter); |
| 1384 | |
| 1385 | /* Next goes merge of compile-time $PYTHONPATH with |
| 1386 | * dynamically located prefix. |
| 1387 | */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1388 | defpath = calculate->pythonpath_macro; |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1389 | while (1) { |
| 1390 | wchar_t *delim = wcschr(defpath, DELIM); |
| 1391 | |
Victor Stinner | 3939c32 | 2019-06-25 15:02:43 +0200 | [diff] [blame] | 1392 | if (!_Py_isabs(defpath)) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1393 | wcscat(buf, calculate->prefix); |
| 1394 | if (prefixsz >= 2 && calculate->prefix[prefixsz - 2] != SEP && |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1395 | defpath[0] != (delim ? DELIM : L'\0')) |
| 1396 | { |
| 1397 | /* not empty */ |
Serhiy Storchaka | 62e32d6 | 2016-11-11 12:05:01 +0200 | [diff] [blame] | 1398 | wcscat(buf, separator); |
| 1399 | } |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | if (delim) { |
| 1403 | size_t len = delim - defpath + 1; |
| 1404 | size_t end = wcslen(buf) + len; |
| 1405 | wcsncat(buf, defpath, len); |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1406 | buf[end] = '\0'; |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1407 | } |
| 1408 | else { |
| 1409 | wcscat(buf, defpath); |
| 1410 | break; |
| 1411 | } |
| 1412 | defpath = delim + 1; |
| 1413 | } |
| 1414 | wcscat(buf, delimiter); |
| 1415 | |
| 1416 | /* Finally, on goes the directory for dynamic-load modules */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1417 | wcscat(buf, calculate->exec_prefix); |
Victor Stinner | 72967a4 | 2013-11-16 01:22:04 +0100 | [diff] [blame] | 1418 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1419 | pathconfig->module_search_path = buf; |
| 1420 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1421 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 1422 | |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1423 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1424 | static PyStatus |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 1425 | calculate_init(PyCalculatePath *calculate, const PyConfig *config) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1426 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1427 | size_t len; |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 1428 | |
| 1429 | calculate->warnings = config->pathconfig_warnings; |
| 1430 | calculate->pythonpath_env = config->pythonpath_env; |
| 1431 | calculate->platlibdir = config->platlibdir; |
| 1432 | |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 1433 | const char *path = getenv("PATH"); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1434 | if (path) { |
| 1435 | calculate->path_env = Py_DecodeLocale(path, &len); |
| 1436 | if (!calculate->path_env) { |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1437 | return DECODE_LOCALE_ERR("PATH environment variable", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1438 | } |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 1439 | } |
Victor Stinner | ae4836d | 2010-11-08 23:49:47 +0000 | [diff] [blame] | 1440 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1441 | /* Decode macros */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1442 | calculate->pythonpath_macro = Py_DecodeLocale(PYTHONPATH, &len); |
| 1443 | if (!calculate->pythonpath_macro) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1444 | return DECODE_LOCALE_ERR("PYTHONPATH macro", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1445 | } |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1446 | calculate->prefix_macro = Py_DecodeLocale(PREFIX, &len); |
| 1447 | if (!calculate->prefix_macro) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1448 | return DECODE_LOCALE_ERR("PREFIX macro", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1449 | } |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1450 | calculate->exec_prefix_macro = Py_DecodeLocale(EXEC_PREFIX, &len); |
| 1451 | if (!calculate->exec_prefix_macro) { |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1452 | return DECODE_LOCALE_ERR("EXEC_PREFIX macro", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1453 | } |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1454 | calculate->vpath_macro = Py_DecodeLocale(VPATH, &len); |
| 1455 | if (!calculate->vpath_macro) { |
| 1456 | return DECODE_LOCALE_ERR("VPATH macro", len); |
| 1457 | } |
| 1458 | |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 1459 | // <platlibdir> / "pythonX.Y" |
| 1460 | wchar_t *pyversion = Py_DecodeLocale("python" VERSION, &len); |
| 1461 | if (!pyversion) { |
Victor Stinner | 8510f43 | 2020-03-10 09:53:09 +0100 | [diff] [blame] | 1462 | return DECODE_LOCALE_ERR("VERSION macro", len); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1463 | } |
Victor Stinner | d72b964 | 2020-06-11 17:28:52 +0200 | [diff] [blame] | 1464 | calculate->lib_python = joinpath2(config->platlibdir, pyversion); |
| 1465 | PyMem_RawFree(pyversion); |
| 1466 | if (calculate->lib_python == NULL) { |
| 1467 | return _PyStatus_NO_MEMORY(); |
| 1468 | } |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1469 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1470 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | static void |
| 1475 | calculate_free(PyCalculatePath *calculate) |
| 1476 | { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1477 | PyMem_RawFree(calculate->pythonpath_macro); |
| 1478 | PyMem_RawFree(calculate->prefix_macro); |
| 1479 | PyMem_RawFree(calculate->exec_prefix_macro); |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1480 | PyMem_RawFree(calculate->vpath_macro); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1481 | PyMem_RawFree(calculate->lib_python); |
| 1482 | PyMem_RawFree(calculate->path_env); |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1483 | PyMem_RawFree(calculate->zip_path); |
| 1484 | PyMem_RawFree(calculate->argv0_path); |
| 1485 | PyMem_RawFree(calculate->prefix); |
| 1486 | PyMem_RawFree(calculate->exec_prefix); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1490 | static PyStatus |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1491 | calculate_path(PyCalculatePath *calculate, _PyPathConfig *pathconfig) |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1492 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1493 | PyStatus status; |
Victor Stinner | 31a8393 | 2017-12-04 13:39:15 +0100 | [diff] [blame] | 1494 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1495 | if (pathconfig->program_full_path == NULL) { |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1496 | status = calculate_program(calculate, pathconfig); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1497 | if (_PyStatus_EXCEPTION(status)) { |
| 1498 | return status; |
| 1499 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1500 | } |
| 1501 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1502 | status = calculate_argv0_path(calculate, pathconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1503 | if (_PyStatus_EXCEPTION(status)) { |
| 1504 | return status; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1505 | } |
| 1506 | |
Victor Stinner | 221fd84 | 2019-09-25 02:54:25 +0200 | [diff] [blame] | 1507 | /* If a pyvenv.cfg configure file is found, |
Miss Islington (bot) | 5afc5bb | 2021-10-07 01:55:18 -0700 | [diff] [blame] | 1508 | argv0_path is overridden with its 'home' variable. */ |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1509 | status = calculate_read_pyenv(calculate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1510 | if (_PyStatus_EXCEPTION(status)) { |
| 1511 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1512 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1513 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1514 | status = calculate_prefix(calculate, pathconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1515 | if (_PyStatus_EXCEPTION(status)) { |
| 1516 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1517 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1518 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1519 | status = calculate_zip_path(calculate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1520 | if (_PyStatus_EXCEPTION(status)) { |
| 1521 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1522 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1523 | |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1524 | status = calculate_exec_prefix(calculate, pathconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1525 | if (_PyStatus_EXCEPTION(status)) { |
| 1526 | return status; |
Victor Stinner | 7b14f0c | 2019-03-18 23:54:59 +0100 | [diff] [blame] | 1527 | } |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1528 | |
Victor Stinner | 03a8a56 | 2019-10-04 02:22:39 +0200 | [diff] [blame] | 1529 | if ((!calculate->prefix_found || !calculate->exec_prefix_found) |
| 1530 | && calculate->warnings) |
Victor Stinner | f04ebe2 | 2017-11-25 00:01:23 +0100 | [diff] [blame] | 1531 | { |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1532 | fprintf(stderr, |
| 1533 | "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); |
| 1534 | } |
| 1535 | |
Victor Stinner | e267793 | 2019-09-21 01:50:16 +0200 | [diff] [blame] | 1536 | if (pathconfig->module_search_path == NULL) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1537 | status = calculate_module_search_path(calculate, pathconfig); |
Victor Stinner | e267793 | 2019-09-21 01:50:16 +0200 | [diff] [blame] | 1538 | if (_PyStatus_EXCEPTION(status)) { |
| 1539 | return status; |
| 1540 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1543 | if (pathconfig->prefix == NULL) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1544 | status = calculate_set_prefix(calculate, pathconfig); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1545 | if (_PyStatus_EXCEPTION(status)) { |
| 1546 | return status; |
| 1547 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1550 | if (pathconfig->exec_prefix == NULL) { |
Victor Stinner | abd7cd8 | 2019-10-04 15:25:50 +0200 | [diff] [blame] | 1551 | status = calculate_set_exec_prefix(calculate, pathconfig); |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1552 | if (_PyStatus_EXCEPTION(status)) { |
| 1553 | return status; |
| 1554 | } |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1555 | } |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1556 | return _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 1560 | /* Calculate the Python path configuration. |
| 1561 | |
| 1562 | Inputs: |
| 1563 | |
| 1564 | - PATH environment variable |
| 1565 | - Macros: PYTHONPATH, PREFIX, EXEC_PREFIX, VERSION (ex: "3.9"). |
| 1566 | PREFIX and EXEC_PREFIX are generated by the configure script. |
| 1567 | PYTHONPATH macro is the default search path. |
| 1568 | - pybuilddir.txt file |
| 1569 | - pyvenv.cfg configuration file |
| 1570 | - PyConfig fields ('config' function argument): |
| 1571 | |
| 1572 | - pathconfig_warnings |
| 1573 | - pythonpath_env (PYTHONPATH environment variable) |
| 1574 | |
| 1575 | - _PyPathConfig fields ('pathconfig' function argument): |
| 1576 | |
| 1577 | - program_name: see config_init_program_name() |
| 1578 | - home: Py_SetPythonHome() or PYTHONHOME environment variable |
| 1579 | |
| 1580 | - current working directory: see copy_absolute() |
| 1581 | |
| 1582 | Outputs, 'pathconfig' fields: |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1583 | |
| 1584 | - program_full_path |
| 1585 | - module_search_path |
| 1586 | - prefix |
| 1587 | - exec_prefix |
| 1588 | |
Victor Stinner | 85ce0a7 | 2019-09-24 00:55:48 +0200 | [diff] [blame] | 1589 | If a field is already set (non NULL), it is left unchanged. */ |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1590 | PyStatus |
| 1591 | _PyPathConfig_Calculate(_PyPathConfig *pathconfig, const PyConfig *config) |
Serhiy Storchaka | 13badcb | 2017-12-02 21:36:00 +0200 | [diff] [blame] | 1592 | { |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1593 | PyStatus status; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1594 | PyCalculatePath calculate; |
| 1595 | memset(&calculate, 0, sizeof(calculate)); |
| 1596 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1597 | status = calculate_init(&calculate, config); |
| 1598 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1599 | goto done; |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1600 | } |
| 1601 | |
Victor Stinner | 9c42f8c | 2019-09-23 18:47:29 +0200 | [diff] [blame] | 1602 | status = calculate_path(&calculate, pathconfig); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1603 | if (_PyStatus_EXCEPTION(status)) { |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1604 | goto done; |
| 1605 | } |
| 1606 | |
Victor Stinner | c02b41b | 2019-10-04 19:53:43 +0200 | [diff] [blame] | 1607 | /* program_full_path must an either an empty string or an absolute path */ |
| 1608 | assert(wcslen(pathconfig->program_full_path) == 0 |
| 1609 | || _Py_isabs(pathconfig->program_full_path)); |
| 1610 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1611 | status = _PyStatus_OK(); |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1612 | |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1613 | done: |
Victor Stinner | 0327bde | 2017-11-23 17:03:20 +0100 | [diff] [blame] | 1614 | calculate_free(&calculate); |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 1615 | return status; |
Victor Stinner | 9316ee4 | 2017-11-25 03:17:57 +0100 | [diff] [blame] | 1616 | } |
Victor Stinner | 46972b7 | 2017-11-24 22:55:40 +0100 | [diff] [blame] | 1617 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1618 | #ifdef __cplusplus |
| 1619 | } |
| 1620 | #endif |