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