| 1 | n/a | /* Return the initial module search path. */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | #include "osdefs.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | #include <sys/types.h> |
|---|
| 7 | n/a | #include <string.h> |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #ifdef __APPLE__ |
|---|
| 10 | n/a | #include <mach-o/dyld.h> |
|---|
| 11 | n/a | #endif |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | /* Search in some common locations for the associated Python libraries. |
|---|
| 14 | n/a | * |
|---|
| 15 | n/a | * Two directories must be found, the platform independent directory |
|---|
| 16 | n/a | * (prefix), containing the common .py and .pyc files, and the platform |
|---|
| 17 | n/a | * dependent directory (exec_prefix), containing the shared library |
|---|
| 18 | n/a | * modules. Note that prefix and exec_prefix can be the same directory, |
|---|
| 19 | n/a | * but for some installations, they are different. |
|---|
| 20 | n/a | * |
|---|
| 21 | n/a | * Py_GetPath() carries out separate searches for prefix and exec_prefix. |
|---|
| 22 | n/a | * Each search tries a number of different locations until a ``landmark'' |
|---|
| 23 | n/a | * file or directory is found. If no prefix or exec_prefix is found, a |
|---|
| 24 | n/a | * warning message is issued and the preprocessor defined PREFIX and |
|---|
| 25 | n/a | * EXEC_PREFIX are used (even though they will not work); python carries on |
|---|
| 26 | n/a | * as best as is possible, but most imports will fail. |
|---|
| 27 | n/a | * |
|---|
| 28 | n/a | * Before any searches are done, the location of the executable is |
|---|
| 29 | n/a | * determined. If argv[0] has one or more slashes in it, it is used |
|---|
| 30 | n/a | * unchanged. Otherwise, it must have been invoked from the shell's path, |
|---|
| 31 | n/a | * so we search $PATH for the named executable and use that. If the |
|---|
| 32 | n/a | * executable was not found on $PATH (or there was no $PATH environment |
|---|
| 33 | n/a | * variable), the original argv[0] string is used. |
|---|
| 34 | n/a | * |
|---|
| 35 | n/a | * Next, the executable location is examined to see if it is a symbolic |
|---|
| 36 | n/a | * link. If so, the link is chased (correctly interpreting a relative |
|---|
| 37 | n/a | * pathname if one is found) and the directory of the link target is used. |
|---|
| 38 | n/a | * |
|---|
| 39 | n/a | * Finally, argv0_path is set to the directory containing the executable |
|---|
| 40 | n/a | * (i.e. the last component is stripped). |
|---|
| 41 | n/a | * |
|---|
| 42 | n/a | * With argv0_path in hand, we perform a number of steps. The same steps |
|---|
| 43 | n/a | * are performed for prefix and for exec_prefix, but with a different |
|---|
| 44 | n/a | * landmark. |
|---|
| 45 | n/a | * |
|---|
| 46 | n/a | * Step 1. Are we running python out of the build directory? This is |
|---|
| 47 | n/a | * checked by looking for a different kind of landmark relative to |
|---|
| 48 | n/a | * argv0_path. For prefix, the landmark's path is derived from the VPATH |
|---|
| 49 | n/a | * preprocessor variable (taking into account that its value is almost, but |
|---|
| 50 | n/a | * not quite, what we need). For exec_prefix, the landmark is |
|---|
| 51 | n/a | * pybuilddir.txt. If the landmark is found, we're done. |
|---|
| 52 | n/a | * |
|---|
| 53 | n/a | * For the remaining steps, the prefix landmark will always be |
|---|
| 54 | n/a | * lib/python$VERSION/os.py and the exec_prefix will always be |
|---|
| 55 | n/a | * lib/python$VERSION/lib-dynload, where $VERSION is Python's version |
|---|
| 56 | n/a | * number as supplied by the Makefile. Note that this means that no more |
|---|
| 57 | n/a | * build directory checking is performed; if the first step did not find |
|---|
| 58 | n/a | * the landmarks, the assumption is that python is running from an |
|---|
| 59 | n/a | * installed setup. |
|---|
| 60 | n/a | * |
|---|
| 61 | n/a | * Step 2. See if the $PYTHONHOME environment variable points to the |
|---|
| 62 | n/a | * installed location of the Python libraries. If $PYTHONHOME is set, then |
|---|
| 63 | n/a | * it points to prefix and exec_prefix. $PYTHONHOME can be a single |
|---|
| 64 | n/a | * directory, which is used for both, or the prefix and exec_prefix |
|---|
| 65 | n/a | * directories separated by a colon. |
|---|
| 66 | n/a | * |
|---|
| 67 | n/a | * Step 3. Try to find prefix and exec_prefix relative to argv0_path, |
|---|
| 68 | n/a | * backtracking up the path until it is exhausted. This is the most common |
|---|
| 69 | n/a | * step to succeed. Note that if prefix and exec_prefix are different, |
|---|
| 70 | n/a | * exec_prefix is more likely to be found; however if exec_prefix is a |
|---|
| 71 | n/a | * subdirectory of prefix, both will be found. |
|---|
| 72 | n/a | * |
|---|
| 73 | n/a | * Step 4. Search the directories pointed to by the preprocessor variables |
|---|
| 74 | n/a | * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be |
|---|
| 75 | n/a | * passed in as options to the configure script. |
|---|
| 76 | n/a | * |
|---|
| 77 | n/a | * That's it! |
|---|
| 78 | n/a | * |
|---|
| 79 | n/a | * Well, almost. Once we have determined prefix and exec_prefix, the |
|---|
| 80 | n/a | * preprocessor variable PYTHONPATH is used to construct a path. Each |
|---|
| 81 | n/a | * relative path on PYTHONPATH is prefixed with prefix. Then the directory |
|---|
| 82 | n/a | * containing the shared library modules is appended. The environment |
|---|
| 83 | n/a | * variable $PYTHONPATH is inserted in front of it all. Finally, the |
|---|
| 84 | n/a | * prefix and exec_prefix globals are tweaked so they reflect the values |
|---|
| 85 | n/a | * expected by other code, by stripping the "lib/python$VERSION/..." stuff |
|---|
| 86 | n/a | * off. If either points to the build directory, the globals are reset to |
|---|
| 87 | n/a | * the corresponding preprocessor variables (so sys.prefix will reflect the |
|---|
| 88 | n/a | * installation location, even though sys.path points into the build |
|---|
| 89 | n/a | * directory). This seems to make more sense given that currently the only |
|---|
| 90 | n/a | * known use of sys.prefix and sys.exec_prefix is for the ILU installation |
|---|
| 91 | n/a | * process to find the installed Python tree. |
|---|
| 92 | n/a | * |
|---|
| 93 | n/a | * An embedding application can use Py_SetPath() to override all of |
|---|
| 94 | n/a | * these authomatic path computations. |
|---|
| 95 | n/a | * |
|---|
| 96 | n/a | * NOTE: Windows MSVC builds use PC/getpathp.c instead! |
|---|
| 97 | n/a | */ |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | #ifdef __cplusplus |
|---|
| 100 | n/a | extern "C" { |
|---|
| 101 | n/a | #endif |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | #if !defined(PREFIX) || !defined(EXEC_PREFIX) || !defined(VERSION) || !defined(VPATH) |
|---|
| 105 | n/a | #error "PREFIX, EXEC_PREFIX, VERSION, and VPATH must be constant defined" |
|---|
| 106 | n/a | #endif |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | #ifndef LANDMARK |
|---|
| 109 | n/a | #define LANDMARK L"os.py" |
|---|
| 110 | n/a | #endif |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | static wchar_t prefix[MAXPATHLEN+1]; |
|---|
| 113 | n/a | static wchar_t exec_prefix[MAXPATHLEN+1]; |
|---|
| 114 | n/a | static wchar_t progpath[MAXPATHLEN+1]; |
|---|
| 115 | n/a | static wchar_t *module_search_path = NULL; |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | /* Get file status. Encode the path to the locale encoding. */ |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | static int |
|---|
| 120 | n/a | _Py_wstat(const wchar_t* path, struct stat *buf) |
|---|
| 121 | n/a | { |
|---|
| 122 | n/a | int err; |
|---|
| 123 | n/a | char *fname; |
|---|
| 124 | n/a | fname = Py_EncodeLocale(path, NULL); |
|---|
| 125 | n/a | if (fname == NULL) { |
|---|
| 126 | n/a | errno = EINVAL; |
|---|
| 127 | n/a | return -1; |
|---|
| 128 | n/a | } |
|---|
| 129 | n/a | err = stat(fname, buf); |
|---|
| 130 | n/a | PyMem_Free(fname); |
|---|
| 131 | n/a | return err; |
|---|
| 132 | n/a | } |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | static void |
|---|
| 135 | n/a | reduce(wchar_t *dir) |
|---|
| 136 | n/a | { |
|---|
| 137 | n/a | size_t i = wcslen(dir); |
|---|
| 138 | n/a | while (i > 0 && dir[i] != SEP) |
|---|
| 139 | n/a | --i; |
|---|
| 140 | n/a | dir[i] = '\0'; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | static int |
|---|
| 144 | n/a | isfile(wchar_t *filename) /* Is file, not directory */ |
|---|
| 145 | n/a | { |
|---|
| 146 | n/a | struct stat buf; |
|---|
| 147 | n/a | if (_Py_wstat(filename, &buf) != 0) |
|---|
| 148 | n/a | return 0; |
|---|
| 149 | n/a | if (!S_ISREG(buf.st_mode)) |
|---|
| 150 | n/a | return 0; |
|---|
| 151 | n/a | return 1; |
|---|
| 152 | n/a | } |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | |
|---|
| 155 | n/a | static int |
|---|
| 156 | n/a | ismodule(wchar_t *filename) /* Is module -- check for .pyc too */ |
|---|
| 157 | n/a | { |
|---|
| 158 | n/a | if (isfile(filename)) |
|---|
| 159 | n/a | return 1; |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | /* Check for the compiled version of prefix. */ |
|---|
| 162 | n/a | if (wcslen(filename) < MAXPATHLEN) { |
|---|
| 163 | n/a | wcscat(filename, L"c"); |
|---|
| 164 | n/a | if (isfile(filename)) |
|---|
| 165 | n/a | return 1; |
|---|
| 166 | n/a | } |
|---|
| 167 | n/a | return 0; |
|---|
| 168 | n/a | } |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | static int |
|---|
| 172 | n/a | isxfile(wchar_t *filename) /* Is executable file */ |
|---|
| 173 | n/a | { |
|---|
| 174 | n/a | struct stat buf; |
|---|
| 175 | n/a | if (_Py_wstat(filename, &buf) != 0) |
|---|
| 176 | n/a | return 0; |
|---|
| 177 | n/a | if (!S_ISREG(buf.st_mode)) |
|---|
| 178 | n/a | return 0; |
|---|
| 179 | n/a | if ((buf.st_mode & 0111) == 0) |
|---|
| 180 | n/a | return 0; |
|---|
| 181 | n/a | return 1; |
|---|
| 182 | n/a | } |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | static int |
|---|
| 186 | n/a | isdir(wchar_t *filename) /* Is directory */ |
|---|
| 187 | n/a | { |
|---|
| 188 | n/a | struct stat buf; |
|---|
| 189 | n/a | if (_Py_wstat(filename, &buf) != 0) |
|---|
| 190 | n/a | return 0; |
|---|
| 191 | n/a | if (!S_ISDIR(buf.st_mode)) |
|---|
| 192 | n/a | return 0; |
|---|
| 193 | n/a | return 1; |
|---|
| 194 | n/a | } |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | /* Add a path component, by appending stuff to buffer. |
|---|
| 198 | n/a | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
|---|
| 199 | n/a | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
|---|
| 200 | n/a | the trailing NUL). It's a fatal error if it contains a string longer than |
|---|
| 201 | n/a | that (callers must be careful!). If these requirements are met, it's |
|---|
| 202 | n/a | guaranteed that buffer will still be a NUL-terminated string with no more |
|---|
| 203 | n/a | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
|---|
| 204 | n/a | stuff as fits will be appended. |
|---|
| 205 | n/a | */ |
|---|
| 206 | n/a | static void |
|---|
| 207 | n/a | joinpath(wchar_t *buffer, wchar_t *stuff) |
|---|
| 208 | n/a | { |
|---|
| 209 | n/a | size_t n, k; |
|---|
| 210 | n/a | if (stuff[0] == SEP) |
|---|
| 211 | n/a | n = 0; |
|---|
| 212 | n/a | else { |
|---|
| 213 | n/a | n = wcslen(buffer); |
|---|
| 214 | n/a | if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) |
|---|
| 215 | n/a | buffer[n++] = SEP; |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | if (n > MAXPATHLEN) |
|---|
| 218 | n/a | Py_FatalError("buffer overflow in getpath.c's joinpath()"); |
|---|
| 219 | n/a | k = wcslen(stuff); |
|---|
| 220 | n/a | if (n + k > MAXPATHLEN) |
|---|
| 221 | n/a | k = MAXPATHLEN - n; |
|---|
| 222 | n/a | wcsncpy(buffer+n, stuff, k); |
|---|
| 223 | n/a | buffer[n+k] = '\0'; |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | /* copy_absolute requires that path be allocated at least |
|---|
| 227 | n/a | MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */ |
|---|
| 228 | n/a | static void |
|---|
| 229 | n/a | copy_absolute(wchar_t *path, wchar_t *p, size_t pathlen) |
|---|
| 230 | n/a | { |
|---|
| 231 | n/a | if (p[0] == SEP) |
|---|
| 232 | n/a | wcscpy(path, p); |
|---|
| 233 | n/a | else { |
|---|
| 234 | n/a | if (!_Py_wgetcwd(path, pathlen)) { |
|---|
| 235 | n/a | /* unable to get the current directory */ |
|---|
| 236 | n/a | wcscpy(path, p); |
|---|
| 237 | n/a | return; |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | if (p[0] == '.' && p[1] == SEP) |
|---|
| 240 | n/a | p += 2; |
|---|
| 241 | n/a | joinpath(path, p); |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | } |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | /* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */ |
|---|
| 246 | n/a | static void |
|---|
| 247 | n/a | absolutize(wchar_t *path) |
|---|
| 248 | n/a | { |
|---|
| 249 | n/a | wchar_t buffer[MAXPATHLEN+1]; |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | if (path[0] == SEP) |
|---|
| 252 | n/a | return; |
|---|
| 253 | n/a | copy_absolute(buffer, path, MAXPATHLEN+1); |
|---|
| 254 | n/a | wcscpy(path, buffer); |
|---|
| 255 | n/a | } |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | /* search for a prefix value in an environment file. If found, copy it |
|---|
| 258 | n/a | to the provided buffer, which is expected to be no more than MAXPATHLEN |
|---|
| 259 | n/a | bytes long. |
|---|
| 260 | n/a | */ |
|---|
| 261 | n/a | |
|---|
| 262 | n/a | static int |
|---|
| 263 | n/a | find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) |
|---|
| 264 | n/a | { |
|---|
| 265 | n/a | int result = 0; /* meaning not found */ |
|---|
| 266 | n/a | char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ |
|---|
| 267 | n/a | |
|---|
| 268 | n/a | fseek(env_file, 0, SEEK_SET); |
|---|
| 269 | n/a | while (!feof(env_file)) { |
|---|
| 270 | n/a | char * p = fgets(buffer, MAXPATHLEN*2, env_file); |
|---|
| 271 | n/a | wchar_t tmpbuffer[MAXPATHLEN*2+1]; |
|---|
| 272 | n/a | PyObject * decoded; |
|---|
| 273 | n/a | int n; |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | if (p == NULL) |
|---|
| 276 | n/a | break; |
|---|
| 277 | n/a | n = strlen(p); |
|---|
| 278 | n/a | if (p[n - 1] != '\n') { |
|---|
| 279 | n/a | /* line has overflowed - bail */ |
|---|
| 280 | n/a | break; |
|---|
| 281 | n/a | } |
|---|
| 282 | n/a | if (p[0] == '#') /* Comment - skip */ |
|---|
| 283 | n/a | continue; |
|---|
| 284 | n/a | decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape"); |
|---|
| 285 | n/a | if (decoded != NULL) { |
|---|
| 286 | n/a | Py_ssize_t k; |
|---|
| 287 | n/a | wchar_t * state; |
|---|
| 288 | n/a | k = PyUnicode_AsWideChar(decoded, |
|---|
| 289 | n/a | tmpbuffer, MAXPATHLEN * 2); |
|---|
| 290 | n/a | Py_DECREF(decoded); |
|---|
| 291 | n/a | if (k >= 0) { |
|---|
| 292 | n/a | wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state); |
|---|
| 293 | n/a | if ((tok != NULL) && !wcscmp(tok, key)) { |
|---|
| 294 | n/a | tok = wcstok(NULL, L" \t", &state); |
|---|
| 295 | n/a | if ((tok != NULL) && !wcscmp(tok, L"=")) { |
|---|
| 296 | n/a | tok = wcstok(NULL, L"\r\n", &state); |
|---|
| 297 | n/a | if (tok != NULL) { |
|---|
| 298 | n/a | wcsncpy(value, tok, MAXPATHLEN); |
|---|
| 299 | n/a | result = 1; |
|---|
| 300 | n/a | break; |
|---|
| 301 | n/a | } |
|---|
| 302 | n/a | } |
|---|
| 303 | n/a | } |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | } |
|---|
| 306 | n/a | } |
|---|
| 307 | n/a | return result; |
|---|
| 308 | n/a | } |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN |
|---|
| 311 | n/a | bytes long. |
|---|
| 312 | n/a | */ |
|---|
| 313 | n/a | static int |
|---|
| 314 | n/a | search_for_prefix(wchar_t *argv0_path, wchar_t *home, wchar_t *_prefix, |
|---|
| 315 | n/a | wchar_t *lib_python) |
|---|
| 316 | n/a | { |
|---|
| 317 | n/a | size_t n; |
|---|
| 318 | n/a | wchar_t *vpath; |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | /* If PYTHONHOME is set, we believe it unconditionally */ |
|---|
| 321 | n/a | if (home) { |
|---|
| 322 | n/a | wchar_t *delim; |
|---|
| 323 | n/a | wcsncpy(prefix, home, MAXPATHLEN); |
|---|
| 324 | n/a | prefix[MAXPATHLEN] = L'\0'; |
|---|
| 325 | n/a | delim = wcschr(prefix, DELIM); |
|---|
| 326 | n/a | if (delim) |
|---|
| 327 | n/a | *delim = L'\0'; |
|---|
| 328 | n/a | joinpath(prefix, lib_python); |
|---|
| 329 | n/a | joinpath(prefix, LANDMARK); |
|---|
| 330 | n/a | return 1; |
|---|
| 331 | n/a | } |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | /* Check to see if argv[0] is in the build directory */ |
|---|
| 334 | n/a | wcsncpy(prefix, argv0_path, MAXPATHLEN); |
|---|
| 335 | n/a | prefix[MAXPATHLEN] = L'\0'; |
|---|
| 336 | n/a | joinpath(prefix, L"Modules/Setup"); |
|---|
| 337 | n/a | if (isfile(prefix)) { |
|---|
| 338 | n/a | /* Check VPATH to see if argv0_path is in the build directory. */ |
|---|
| 339 | n/a | vpath = Py_DecodeLocale(VPATH, NULL); |
|---|
| 340 | n/a | if (vpath != NULL) { |
|---|
| 341 | n/a | wcsncpy(prefix, argv0_path, MAXPATHLEN); |
|---|
| 342 | n/a | prefix[MAXPATHLEN] = L'\0'; |
|---|
| 343 | n/a | joinpath(prefix, vpath); |
|---|
| 344 | n/a | PyMem_RawFree(vpath); |
|---|
| 345 | n/a | joinpath(prefix, L"Lib"); |
|---|
| 346 | n/a | joinpath(prefix, LANDMARK); |
|---|
| 347 | n/a | if (ismodule(prefix)) |
|---|
| 348 | n/a | return -1; |
|---|
| 349 | n/a | } |
|---|
| 350 | n/a | } |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | /* Search from argv0_path, until root is found */ |
|---|
| 353 | n/a | copy_absolute(prefix, argv0_path, MAXPATHLEN+1); |
|---|
| 354 | n/a | do { |
|---|
| 355 | n/a | n = wcslen(prefix); |
|---|
| 356 | n/a | joinpath(prefix, lib_python); |
|---|
| 357 | n/a | joinpath(prefix, LANDMARK); |
|---|
| 358 | n/a | if (ismodule(prefix)) |
|---|
| 359 | n/a | return 1; |
|---|
| 360 | n/a | prefix[n] = L'\0'; |
|---|
| 361 | n/a | reduce(prefix); |
|---|
| 362 | n/a | } while (prefix[0]); |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | /* Look at configure's PREFIX */ |
|---|
| 365 | n/a | wcsncpy(prefix, _prefix, MAXPATHLEN); |
|---|
| 366 | n/a | prefix[MAXPATHLEN] = L'\0'; |
|---|
| 367 | n/a | joinpath(prefix, lib_python); |
|---|
| 368 | n/a | joinpath(prefix, LANDMARK); |
|---|
| 369 | n/a | if (ismodule(prefix)) |
|---|
| 370 | n/a | return 1; |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | /* Fail */ |
|---|
| 373 | n/a | return 0; |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | |
|---|
| 377 | n/a | /* search_for_exec_prefix requires that argv0_path be no more than |
|---|
| 378 | n/a | MAXPATHLEN bytes long. |
|---|
| 379 | n/a | */ |
|---|
| 380 | n/a | static int |
|---|
| 381 | n/a | search_for_exec_prefix(wchar_t *argv0_path, wchar_t *home, |
|---|
| 382 | n/a | wchar_t *_exec_prefix, wchar_t *lib_python) |
|---|
| 383 | n/a | { |
|---|
| 384 | n/a | size_t n; |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | /* If PYTHONHOME is set, we believe it unconditionally */ |
|---|
| 387 | n/a | if (home) { |
|---|
| 388 | n/a | wchar_t *delim; |
|---|
| 389 | n/a | delim = wcschr(home, DELIM); |
|---|
| 390 | n/a | if (delim) |
|---|
| 391 | n/a | wcsncpy(exec_prefix, delim+1, MAXPATHLEN); |
|---|
| 392 | n/a | else |
|---|
| 393 | n/a | wcsncpy(exec_prefix, home, MAXPATHLEN); |
|---|
| 394 | n/a | exec_prefix[MAXPATHLEN] = L'\0'; |
|---|
| 395 | n/a | joinpath(exec_prefix, lib_python); |
|---|
| 396 | n/a | joinpath(exec_prefix, L"lib-dynload"); |
|---|
| 397 | n/a | return 1; |
|---|
| 398 | n/a | } |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | /* Check to see if argv[0] is in the build directory. "pybuilddir.txt" |
|---|
| 401 | n/a | is written by setup.py and contains the relative path to the location |
|---|
| 402 | n/a | of shared library modules. */ |
|---|
| 403 | n/a | wcsncpy(exec_prefix, argv0_path, MAXPATHLEN); |
|---|
| 404 | n/a | exec_prefix[MAXPATHLEN] = L'\0'; |
|---|
| 405 | n/a | joinpath(exec_prefix, L"pybuilddir.txt"); |
|---|
| 406 | n/a | if (isfile(exec_prefix)) { |
|---|
| 407 | n/a | FILE *f = _Py_wfopen(exec_prefix, L"rb"); |
|---|
| 408 | n/a | if (f == NULL) |
|---|
| 409 | n/a | errno = 0; |
|---|
| 410 | n/a | else { |
|---|
| 411 | n/a | char buf[MAXPATHLEN+1]; |
|---|
| 412 | n/a | PyObject *decoded; |
|---|
| 413 | n/a | wchar_t rel_builddir_path[MAXPATHLEN+1]; |
|---|
| 414 | n/a | n = fread(buf, 1, MAXPATHLEN, f); |
|---|
| 415 | n/a | buf[n] = '\0'; |
|---|
| 416 | n/a | fclose(f); |
|---|
| 417 | n/a | decoded = PyUnicode_DecodeUTF8(buf, n, "surrogateescape"); |
|---|
| 418 | n/a | if (decoded != NULL) { |
|---|
| 419 | n/a | Py_ssize_t k; |
|---|
| 420 | n/a | k = PyUnicode_AsWideChar(decoded, |
|---|
| 421 | n/a | rel_builddir_path, MAXPATHLEN); |
|---|
| 422 | n/a | Py_DECREF(decoded); |
|---|
| 423 | n/a | if (k >= 0) { |
|---|
| 424 | n/a | rel_builddir_path[k] = L'\0'; |
|---|
| 425 | n/a | wcsncpy(exec_prefix, argv0_path, MAXPATHLEN); |
|---|
| 426 | n/a | exec_prefix[MAXPATHLEN] = L'\0'; |
|---|
| 427 | n/a | joinpath(exec_prefix, rel_builddir_path); |
|---|
| 428 | n/a | return -1; |
|---|
| 429 | n/a | } |
|---|
| 430 | n/a | } |
|---|
| 431 | n/a | } |
|---|
| 432 | n/a | } |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | /* Search from argv0_path, until root is found */ |
|---|
| 435 | n/a | copy_absolute(exec_prefix, argv0_path, MAXPATHLEN+1); |
|---|
| 436 | n/a | do { |
|---|
| 437 | n/a | n = wcslen(exec_prefix); |
|---|
| 438 | n/a | joinpath(exec_prefix, lib_python); |
|---|
| 439 | n/a | joinpath(exec_prefix, L"lib-dynload"); |
|---|
| 440 | n/a | if (isdir(exec_prefix)) |
|---|
| 441 | n/a | return 1; |
|---|
| 442 | n/a | exec_prefix[n] = L'\0'; |
|---|
| 443 | n/a | reduce(exec_prefix); |
|---|
| 444 | n/a | } while (exec_prefix[0]); |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | /* Look at configure's EXEC_PREFIX */ |
|---|
| 447 | n/a | wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); |
|---|
| 448 | n/a | exec_prefix[MAXPATHLEN] = L'\0'; |
|---|
| 449 | n/a | joinpath(exec_prefix, lib_python); |
|---|
| 450 | n/a | joinpath(exec_prefix, L"lib-dynload"); |
|---|
| 451 | n/a | if (isdir(exec_prefix)) |
|---|
| 452 | n/a | return 1; |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | /* Fail */ |
|---|
| 455 | n/a | return 0; |
|---|
| 456 | n/a | } |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | static void |
|---|
| 459 | n/a | calculate_path(void) |
|---|
| 460 | n/a | { |
|---|
| 461 | n/a | extern wchar_t *Py_GetProgramName(void); |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | static const wchar_t delimiter[2] = {DELIM, '\0'}; |
|---|
| 464 | n/a | static const wchar_t separator[2] = {SEP, '\0'}; |
|---|
| 465 | n/a | char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */ |
|---|
| 466 | n/a | wchar_t *rtpypath = NULL; |
|---|
| 467 | n/a | wchar_t *home = Py_GetPythonHome(); |
|---|
| 468 | n/a | char *_path = getenv("PATH"); |
|---|
| 469 | n/a | wchar_t *path_buffer = NULL; |
|---|
| 470 | n/a | wchar_t *path = NULL; |
|---|
| 471 | n/a | wchar_t *prog = Py_GetProgramName(); |
|---|
| 472 | n/a | wchar_t argv0_path[MAXPATHLEN+1]; |
|---|
| 473 | n/a | wchar_t zip_path[MAXPATHLEN+1]; |
|---|
| 474 | n/a | int pfound, efound; /* 1 if found; -1 if found build directory */ |
|---|
| 475 | n/a | wchar_t *buf; |
|---|
| 476 | n/a | size_t bufsz; |
|---|
| 477 | n/a | size_t prefixsz; |
|---|
| 478 | n/a | wchar_t *defpath; |
|---|
| 479 | n/a | #ifdef WITH_NEXT_FRAMEWORK |
|---|
| 480 | n/a | NSModule pythonModule; |
|---|
| 481 | n/a | const char* modPath; |
|---|
| 482 | n/a | #endif |
|---|
| 483 | n/a | #ifdef __APPLE__ |
|---|
| 484 | n/a | #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 |
|---|
| 485 | n/a | uint32_t nsexeclength = MAXPATHLEN; |
|---|
| 486 | n/a | #else |
|---|
| 487 | n/a | unsigned long nsexeclength = MAXPATHLEN; |
|---|
| 488 | n/a | #endif |
|---|
| 489 | n/a | char execpath[MAXPATHLEN+1]; |
|---|
| 490 | n/a | #endif |
|---|
| 491 | n/a | wchar_t *_pythonpath, *_prefix, *_exec_prefix; |
|---|
| 492 | n/a | wchar_t *lib_python; |
|---|
| 493 | n/a | |
|---|
| 494 | n/a | _pythonpath = Py_DecodeLocale(PYTHONPATH, NULL); |
|---|
| 495 | n/a | _prefix = Py_DecodeLocale(PREFIX, NULL); |
|---|
| 496 | n/a | _exec_prefix = Py_DecodeLocale(EXEC_PREFIX, NULL); |
|---|
| 497 | n/a | lib_python = Py_DecodeLocale("lib/python" VERSION, NULL); |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | if (!_pythonpath || !_prefix || !_exec_prefix || !lib_python) { |
|---|
| 500 | n/a | Py_FatalError( |
|---|
| 501 | n/a | "Unable to decode path variables in getpath.c: " |
|---|
| 502 | n/a | "memory error"); |
|---|
| 503 | n/a | } |
|---|
| 504 | n/a | |
|---|
| 505 | n/a | if (_path) { |
|---|
| 506 | n/a | path_buffer = Py_DecodeLocale(_path, NULL); |
|---|
| 507 | n/a | path = path_buffer; |
|---|
| 508 | n/a | } |
|---|
| 509 | n/a | |
|---|
| 510 | n/a | /* If there is no slash in the argv0 path, then we have to |
|---|
| 511 | n/a | * assume python is on the user's $PATH, since there's no |
|---|
| 512 | n/a | * other way to find a directory to start the search from. If |
|---|
| 513 | n/a | * $PATH isn't exported, you lose. |
|---|
| 514 | n/a | */ |
|---|
| 515 | n/a | if (wcschr(prog, SEP)) |
|---|
| 516 | n/a | wcsncpy(progpath, prog, MAXPATHLEN); |
|---|
| 517 | n/a | #ifdef __APPLE__ |
|---|
| 518 | n/a | /* On Mac OS X, if a script uses an interpreter of the form |
|---|
| 519 | n/a | * "#!/opt/python2.3/bin/python", the kernel only passes "python" |
|---|
| 520 | n/a | * as argv[0], which falls through to the $PATH search below. |
|---|
| 521 | n/a | * If /opt/python2.3/bin isn't in your path, or is near the end, |
|---|
| 522 | n/a | * this algorithm may incorrectly find /usr/bin/python. To work |
|---|
| 523 | n/a | * around this, we can use _NSGetExecutablePath to get a better |
|---|
| 524 | n/a | * hint of what the intended interpreter was, although this |
|---|
| 525 | n/a | * will fail if a relative path was used. but in that case, |
|---|
| 526 | n/a | * absolutize() should help us out below |
|---|
| 527 | n/a | */ |
|---|
| 528 | n/a | else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) { |
|---|
| 529 | n/a | size_t r = mbstowcs(progpath, execpath, MAXPATHLEN+1); |
|---|
| 530 | n/a | if (r == (size_t)-1 || r > MAXPATHLEN) { |
|---|
| 531 | n/a | /* Could not convert execpath, or it's too long. */ |
|---|
| 532 | n/a | progpath[0] = '\0'; |
|---|
| 533 | n/a | } |
|---|
| 534 | n/a | } |
|---|
| 535 | n/a | #endif /* __APPLE__ */ |
|---|
| 536 | n/a | else if (path) { |
|---|
| 537 | n/a | while (1) { |
|---|
| 538 | n/a | wchar_t *delim = wcschr(path, DELIM); |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | if (delim) { |
|---|
| 541 | n/a | size_t len = delim - path; |
|---|
| 542 | n/a | if (len > MAXPATHLEN) |
|---|
| 543 | n/a | len = MAXPATHLEN; |
|---|
| 544 | n/a | wcsncpy(progpath, path, len); |
|---|
| 545 | n/a | *(progpath + len) = '\0'; |
|---|
| 546 | n/a | } |
|---|
| 547 | n/a | else |
|---|
| 548 | n/a | wcsncpy(progpath, path, MAXPATHLEN); |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | joinpath(progpath, prog); |
|---|
| 551 | n/a | if (isxfile(progpath)) |
|---|
| 552 | n/a | break; |
|---|
| 553 | n/a | |
|---|
| 554 | n/a | if (!delim) { |
|---|
| 555 | n/a | progpath[0] = L'\0'; |
|---|
| 556 | n/a | break; |
|---|
| 557 | n/a | } |
|---|
| 558 | n/a | path = delim + 1; |
|---|
| 559 | n/a | } |
|---|
| 560 | n/a | } |
|---|
| 561 | n/a | else |
|---|
| 562 | n/a | progpath[0] = '\0'; |
|---|
| 563 | n/a | PyMem_RawFree(path_buffer); |
|---|
| 564 | n/a | if (progpath[0] != SEP && progpath[0] != '\0') |
|---|
| 565 | n/a | absolutize(progpath); |
|---|
| 566 | n/a | wcsncpy(argv0_path, progpath, MAXPATHLEN); |
|---|
| 567 | n/a | argv0_path[MAXPATHLEN] = '\0'; |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | #ifdef WITH_NEXT_FRAMEWORK |
|---|
| 570 | n/a | /* On Mac OS X we have a special case if we're running from a framework. |
|---|
| 571 | n/a | ** This is because the python home should be set relative to the library, |
|---|
| 572 | n/a | ** which is in the framework, not relative to the executable, which may |
|---|
| 573 | n/a | ** be outside of the framework. Except when we're in the build directory... |
|---|
| 574 | n/a | */ |
|---|
| 575 | n/a | pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); |
|---|
| 576 | n/a | /* Use dylib functions to find out where the framework was loaded from */ |
|---|
| 577 | n/a | modPath = NSLibraryNameForModule(pythonModule); |
|---|
| 578 | n/a | if (modPath != NULL) { |
|---|
| 579 | n/a | /* We're in a framework. */ |
|---|
| 580 | n/a | /* See if we might be in the build directory. The framework in the |
|---|
| 581 | n/a | ** build directory is incomplete, it only has the .dylib and a few |
|---|
| 582 | n/a | ** needed symlinks, it doesn't have the Lib directories and such. |
|---|
| 583 | n/a | ** If we're running with the framework from the build directory we must |
|---|
| 584 | n/a | ** be running the interpreter in the build directory, so we use the |
|---|
| 585 | n/a | ** build-directory-specific logic to find Lib and such. |
|---|
| 586 | n/a | */ |
|---|
| 587 | n/a | wchar_t* wbuf = Py_DecodeLocale(modPath, NULL); |
|---|
| 588 | n/a | if (wbuf == NULL) { |
|---|
| 589 | n/a | Py_FatalError("Cannot decode framework location"); |
|---|
| 590 | n/a | } |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | wcsncpy(argv0_path, wbuf, MAXPATHLEN); |
|---|
| 593 | n/a | reduce(argv0_path); |
|---|
| 594 | n/a | joinpath(argv0_path, lib_python); |
|---|
| 595 | n/a | joinpath(argv0_path, LANDMARK); |
|---|
| 596 | n/a | if (!ismodule(argv0_path)) { |
|---|
| 597 | n/a | /* We are in the build directory so use the name of the |
|---|
| 598 | n/a | executable - we know that the absolute path is passed */ |
|---|
| 599 | n/a | wcsncpy(argv0_path, progpath, MAXPATHLEN); |
|---|
| 600 | n/a | } |
|---|
| 601 | n/a | else { |
|---|
| 602 | n/a | /* Use the location of the library as the progpath */ |
|---|
| 603 | n/a | wcsncpy(argv0_path, wbuf, MAXPATHLEN); |
|---|
| 604 | n/a | } |
|---|
| 605 | n/a | PyMem_RawFree(wbuf); |
|---|
| 606 | n/a | } |
|---|
| 607 | n/a | #endif |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | #if HAVE_READLINK |
|---|
| 610 | n/a | { |
|---|
| 611 | n/a | wchar_t tmpbuffer[MAXPATHLEN+1]; |
|---|
| 612 | n/a | int linklen = _Py_wreadlink(progpath, tmpbuffer, MAXPATHLEN); |
|---|
| 613 | n/a | while (linklen != -1) { |
|---|
| 614 | n/a | if (tmpbuffer[0] == SEP) |
|---|
| 615 | n/a | /* tmpbuffer should never be longer than MAXPATHLEN, |
|---|
| 616 | n/a | but extra check does not hurt */ |
|---|
| 617 | n/a | wcsncpy(argv0_path, tmpbuffer, MAXPATHLEN); |
|---|
| 618 | n/a | else { |
|---|
| 619 | n/a | /* Interpret relative to progpath */ |
|---|
| 620 | n/a | reduce(argv0_path); |
|---|
| 621 | n/a | joinpath(argv0_path, tmpbuffer); |
|---|
| 622 | n/a | } |
|---|
| 623 | n/a | linklen = _Py_wreadlink(argv0_path, tmpbuffer, MAXPATHLEN); |
|---|
| 624 | n/a | } |
|---|
| 625 | n/a | } |
|---|
| 626 | n/a | #endif /* HAVE_READLINK */ |
|---|
| 627 | n/a | |
|---|
| 628 | n/a | reduce(argv0_path); |
|---|
| 629 | n/a | /* At this point, argv0_path is guaranteed to be less than |
|---|
| 630 | n/a | MAXPATHLEN bytes long. |
|---|
| 631 | n/a | */ |
|---|
| 632 | n/a | |
|---|
| 633 | n/a | /* Search for an environment configuration file, first in the |
|---|
| 634 | n/a | executable's directory and then in the parent directory. |
|---|
| 635 | n/a | If found, open it for use when searching for prefixes. |
|---|
| 636 | n/a | */ |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | { |
|---|
| 639 | n/a | wchar_t tmpbuffer[MAXPATHLEN+1]; |
|---|
| 640 | n/a | wchar_t *env_cfg = L"pyvenv.cfg"; |
|---|
| 641 | n/a | FILE * env_file = NULL; |
|---|
| 642 | n/a | |
|---|
| 643 | n/a | wcscpy(tmpbuffer, argv0_path); |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | joinpath(tmpbuffer, env_cfg); |
|---|
| 646 | n/a | env_file = _Py_wfopen(tmpbuffer, L"r"); |
|---|
| 647 | n/a | if (env_file == NULL) { |
|---|
| 648 | n/a | errno = 0; |
|---|
| 649 | n/a | reduce(tmpbuffer); |
|---|
| 650 | n/a | reduce(tmpbuffer); |
|---|
| 651 | n/a | joinpath(tmpbuffer, env_cfg); |
|---|
| 652 | n/a | env_file = _Py_wfopen(tmpbuffer, L"r"); |
|---|
| 653 | n/a | if (env_file == NULL) { |
|---|
| 654 | n/a | errno = 0; |
|---|
| 655 | n/a | } |
|---|
| 656 | n/a | } |
|---|
| 657 | n/a | if (env_file != NULL) { |
|---|
| 658 | n/a | /* Look for a 'home' variable and set argv0_path to it, if found */ |
|---|
| 659 | n/a | if (find_env_config_value(env_file, L"home", tmpbuffer)) { |
|---|
| 660 | n/a | wcscpy(argv0_path, tmpbuffer); |
|---|
| 661 | n/a | } |
|---|
| 662 | n/a | fclose(env_file); |
|---|
| 663 | n/a | env_file = NULL; |
|---|
| 664 | n/a | } |
|---|
| 665 | n/a | } |
|---|
| 666 | n/a | |
|---|
| 667 | n/a | pfound = search_for_prefix(argv0_path, home, _prefix, lib_python); |
|---|
| 668 | n/a | if (!pfound) { |
|---|
| 669 | n/a | if (!Py_FrozenFlag) |
|---|
| 670 | n/a | fprintf(stderr, |
|---|
| 671 | n/a | "Could not find platform independent libraries <prefix>\n"); |
|---|
| 672 | n/a | wcsncpy(prefix, _prefix, MAXPATHLEN); |
|---|
| 673 | n/a | joinpath(prefix, lib_python); |
|---|
| 674 | n/a | } |
|---|
| 675 | n/a | else |
|---|
| 676 | n/a | reduce(prefix); |
|---|
| 677 | n/a | |
|---|
| 678 | n/a | wcsncpy(zip_path, prefix, MAXPATHLEN); |
|---|
| 679 | n/a | zip_path[MAXPATHLEN] = L'\0'; |
|---|
| 680 | n/a | if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ |
|---|
| 681 | n/a | reduce(zip_path); |
|---|
| 682 | n/a | reduce(zip_path); |
|---|
| 683 | n/a | } |
|---|
| 684 | n/a | else |
|---|
| 685 | n/a | wcsncpy(zip_path, _prefix, MAXPATHLEN); |
|---|
| 686 | n/a | joinpath(zip_path, L"lib/python00.zip"); |
|---|
| 687 | n/a | bufsz = wcslen(zip_path); /* Replace "00" with version */ |
|---|
| 688 | n/a | zip_path[bufsz - 6] = VERSION[0]; |
|---|
| 689 | n/a | zip_path[bufsz - 5] = VERSION[2]; |
|---|
| 690 | n/a | |
|---|
| 691 | n/a | efound = search_for_exec_prefix(argv0_path, home, |
|---|
| 692 | n/a | _exec_prefix, lib_python); |
|---|
| 693 | n/a | if (!efound) { |
|---|
| 694 | n/a | if (!Py_FrozenFlag) |
|---|
| 695 | n/a | fprintf(stderr, |
|---|
| 696 | n/a | "Could not find platform dependent libraries <exec_prefix>\n"); |
|---|
| 697 | n/a | wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); |
|---|
| 698 | n/a | joinpath(exec_prefix, L"lib/lib-dynload"); |
|---|
| 699 | n/a | } |
|---|
| 700 | n/a | /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ |
|---|
| 701 | n/a | |
|---|
| 702 | n/a | if ((!pfound || !efound) && !Py_FrozenFlag) |
|---|
| 703 | n/a | fprintf(stderr, |
|---|
| 704 | n/a | "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | /* Calculate size of return buffer. |
|---|
| 707 | n/a | */ |
|---|
| 708 | n/a | bufsz = 0; |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | if (_rtpypath && _rtpypath[0] != '\0') { |
|---|
| 711 | n/a | size_t rtpypath_len; |
|---|
| 712 | n/a | rtpypath = Py_DecodeLocale(_rtpypath, &rtpypath_len); |
|---|
| 713 | n/a | if (rtpypath != NULL) |
|---|
| 714 | n/a | bufsz += rtpypath_len + 1; |
|---|
| 715 | n/a | } |
|---|
| 716 | n/a | |
|---|
| 717 | n/a | defpath = _pythonpath; |
|---|
| 718 | n/a | prefixsz = wcslen(prefix) + 1; |
|---|
| 719 | n/a | while (1) { |
|---|
| 720 | n/a | wchar_t *delim = wcschr(defpath, DELIM); |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | if (defpath[0] != SEP) |
|---|
| 723 | n/a | /* Paths are relative to prefix */ |
|---|
| 724 | n/a | bufsz += prefixsz; |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | if (delim) |
|---|
| 727 | n/a | bufsz += delim - defpath + 1; |
|---|
| 728 | n/a | else { |
|---|
| 729 | n/a | bufsz += wcslen(defpath) + 1; |
|---|
| 730 | n/a | break; |
|---|
| 731 | n/a | } |
|---|
| 732 | n/a | defpath = delim + 1; |
|---|
| 733 | n/a | } |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | bufsz += wcslen(zip_path) + 1; |
|---|
| 736 | n/a | bufsz += wcslen(exec_prefix) + 1; |
|---|
| 737 | n/a | |
|---|
| 738 | n/a | buf = PyMem_New(wchar_t, bufsz); |
|---|
| 739 | n/a | if (buf == NULL) { |
|---|
| 740 | n/a | Py_FatalError( |
|---|
| 741 | n/a | "Not enough memory for dynamic PYTHONPATH"); |
|---|
| 742 | n/a | } |
|---|
| 743 | n/a | |
|---|
| 744 | n/a | /* Run-time value of $PYTHONPATH goes first */ |
|---|
| 745 | n/a | if (rtpypath) { |
|---|
| 746 | n/a | wcscpy(buf, rtpypath); |
|---|
| 747 | n/a | wcscat(buf, delimiter); |
|---|
| 748 | n/a | } |
|---|
| 749 | n/a | else |
|---|
| 750 | n/a | buf[0] = '\0'; |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | /* Next is the default zip path */ |
|---|
| 753 | n/a | wcscat(buf, zip_path); |
|---|
| 754 | n/a | wcscat(buf, delimiter); |
|---|
| 755 | n/a | |
|---|
| 756 | n/a | /* Next goes merge of compile-time $PYTHONPATH with |
|---|
| 757 | n/a | * dynamically located prefix. |
|---|
| 758 | n/a | */ |
|---|
| 759 | n/a | defpath = _pythonpath; |
|---|
| 760 | n/a | while (1) { |
|---|
| 761 | n/a | wchar_t *delim = wcschr(defpath, DELIM); |
|---|
| 762 | n/a | |
|---|
| 763 | n/a | if (defpath[0] != SEP) { |
|---|
| 764 | n/a | wcscat(buf, prefix); |
|---|
| 765 | n/a | if (prefixsz >= 2 && prefix[prefixsz - 2] != SEP && |
|---|
| 766 | n/a | defpath[0] != (delim ? DELIM : L'\0')) { /* not empty */ |
|---|
| 767 | n/a | wcscat(buf, separator); |
|---|
| 768 | n/a | } |
|---|
| 769 | n/a | } |
|---|
| 770 | n/a | |
|---|
| 771 | n/a | if (delim) { |
|---|
| 772 | n/a | size_t len = delim - defpath + 1; |
|---|
| 773 | n/a | size_t end = wcslen(buf) + len; |
|---|
| 774 | n/a | wcsncat(buf, defpath, len); |
|---|
| 775 | n/a | *(buf + end) = '\0'; |
|---|
| 776 | n/a | } |
|---|
| 777 | n/a | else { |
|---|
| 778 | n/a | wcscat(buf, defpath); |
|---|
| 779 | n/a | break; |
|---|
| 780 | n/a | } |
|---|
| 781 | n/a | defpath = delim + 1; |
|---|
| 782 | n/a | } |
|---|
| 783 | n/a | wcscat(buf, delimiter); |
|---|
| 784 | n/a | |
|---|
| 785 | n/a | /* Finally, on goes the directory for dynamic-load modules */ |
|---|
| 786 | n/a | wcscat(buf, exec_prefix); |
|---|
| 787 | n/a | |
|---|
| 788 | n/a | /* And publish the results */ |
|---|
| 789 | n/a | module_search_path = buf; |
|---|
| 790 | n/a | |
|---|
| 791 | n/a | /* Reduce prefix and exec_prefix to their essence, |
|---|
| 792 | n/a | * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. |
|---|
| 793 | n/a | * If we're loading relative to the build directory, |
|---|
| 794 | n/a | * return the compiled-in defaults instead. |
|---|
| 795 | n/a | */ |
|---|
| 796 | n/a | if (pfound > 0) { |
|---|
| 797 | n/a | reduce(prefix); |
|---|
| 798 | n/a | reduce(prefix); |
|---|
| 799 | n/a | /* The prefix is the root directory, but reduce() chopped |
|---|
| 800 | n/a | * off the "/". */ |
|---|
| 801 | n/a | if (!prefix[0]) |
|---|
| 802 | n/a | wcscpy(prefix, separator); |
|---|
| 803 | n/a | } |
|---|
| 804 | n/a | else |
|---|
| 805 | n/a | wcsncpy(prefix, _prefix, MAXPATHLEN); |
|---|
| 806 | n/a | |
|---|
| 807 | n/a | if (efound > 0) { |
|---|
| 808 | n/a | reduce(exec_prefix); |
|---|
| 809 | n/a | reduce(exec_prefix); |
|---|
| 810 | n/a | reduce(exec_prefix); |
|---|
| 811 | n/a | if (!exec_prefix[0]) |
|---|
| 812 | n/a | wcscpy(exec_prefix, separator); |
|---|
| 813 | n/a | } |
|---|
| 814 | n/a | else |
|---|
| 815 | n/a | wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); |
|---|
| 816 | n/a | |
|---|
| 817 | n/a | PyMem_RawFree(_pythonpath); |
|---|
| 818 | n/a | PyMem_RawFree(_prefix); |
|---|
| 819 | n/a | PyMem_RawFree(_exec_prefix); |
|---|
| 820 | n/a | PyMem_RawFree(lib_python); |
|---|
| 821 | n/a | PyMem_RawFree(rtpypath); |
|---|
| 822 | n/a | } |
|---|
| 823 | n/a | |
|---|
| 824 | n/a | |
|---|
| 825 | n/a | /* External interface */ |
|---|
| 826 | n/a | void |
|---|
| 827 | n/a | Py_SetPath(const wchar_t *path) |
|---|
| 828 | n/a | { |
|---|
| 829 | n/a | if (module_search_path != NULL) { |
|---|
| 830 | n/a | PyMem_RawFree(module_search_path); |
|---|
| 831 | n/a | module_search_path = NULL; |
|---|
| 832 | n/a | } |
|---|
| 833 | n/a | if (path != NULL) { |
|---|
| 834 | n/a | extern wchar_t *Py_GetProgramName(void); |
|---|
| 835 | n/a | wchar_t *prog = Py_GetProgramName(); |
|---|
| 836 | n/a | wcsncpy(progpath, prog, MAXPATHLEN); |
|---|
| 837 | n/a | exec_prefix[0] = prefix[0] = L'\0'; |
|---|
| 838 | n/a | module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t)); |
|---|
| 839 | n/a | if (module_search_path != NULL) |
|---|
| 840 | n/a | wcscpy(module_search_path, path); |
|---|
| 841 | n/a | } |
|---|
| 842 | n/a | } |
|---|
| 843 | n/a | |
|---|
| 844 | n/a | wchar_t * |
|---|
| 845 | n/a | Py_GetPath(void) |
|---|
| 846 | n/a | { |
|---|
| 847 | n/a | if (!module_search_path) |
|---|
| 848 | n/a | calculate_path(); |
|---|
| 849 | n/a | return module_search_path; |
|---|
| 850 | n/a | } |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | wchar_t * |
|---|
| 853 | n/a | Py_GetPrefix(void) |
|---|
| 854 | n/a | { |
|---|
| 855 | n/a | if (!module_search_path) |
|---|
| 856 | n/a | calculate_path(); |
|---|
| 857 | n/a | return prefix; |
|---|
| 858 | n/a | } |
|---|
| 859 | n/a | |
|---|
| 860 | n/a | wchar_t * |
|---|
| 861 | n/a | Py_GetExecPrefix(void) |
|---|
| 862 | n/a | { |
|---|
| 863 | n/a | if (!module_search_path) |
|---|
| 864 | n/a | calculate_path(); |
|---|
| 865 | n/a | return exec_prefix; |
|---|
| 866 | n/a | } |
|---|
| 867 | n/a | |
|---|
| 868 | n/a | wchar_t * |
|---|
| 869 | n/a | Py_GetProgramFullPath(void) |
|---|
| 870 | n/a | { |
|---|
| 871 | n/a | if (!module_search_path) |
|---|
| 872 | n/a | calculate_path(); |
|---|
| 873 | n/a | return progpath; |
|---|
| 874 | n/a | } |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | |
|---|
| 877 | n/a | #ifdef __cplusplus |
|---|
| 878 | n/a | } |
|---|
| 879 | n/a | #endif |
|---|