You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

698 lines
21 KiB

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