/PC/getpathp.c

http://unladen-swallow.googlecode.com/ · C · 714 lines · 495 code · 54 blank · 165 comment · 120 complexity · 224416e8d3fe2d668437aa82bf664775 MD5 · raw file

  1. /* Return the initial module search path. */
  2. /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
  3. /* ----------------------------------------------------------------
  4. PATH RULES FOR WINDOWS:
  5. This describes how sys.path is formed on Windows. It describes the
  6. functionality, not the implementation (ie, the order in which these
  7. are actually fetched is different)
  8. * Python always adds an empty entry at the start, which corresponds
  9. to the current directory.
  10. * If the PYTHONPATH env. var. exists, its entries are added next.
  11. * We look in the registry for "application paths" - that is, sub-keys
  12. under the main PythonPath registry key. These are added next (the
  13. order of sub-key processing is undefined).
  14. HKEY_CURRENT_USER is searched and added first.
  15. HKEY_LOCAL_MACHINE is searched and added next.
  16. (Note that all known installers only use HKLM, so HKCU is typically
  17. empty)
  18. * We attempt to locate the "Python Home" - if the PYTHONHOME env var
  19. is set, we believe it. Otherwise, we use the path of our host .EXE's
  20. to try and locate our "landmark" (lib\\os.py) and deduce our home.
  21. - If we DO have a Python Home: The relevant sub-directories (Lib,
  22. plat-win, lib-tk, etc) are based on the Python Home
  23. - If we DO NOT have a Python Home, the core Python Path is
  24. loaded from the registry. This is the main PythonPath key,
  25. and both HKLM and HKCU are combined to form the path)
  26. * Iff - we can not locate the Python Home, have not had a PYTHONPATH
  27. specified, and can't locate any Registry entries (ie, we have _nothing_
  28. we can assume is a good path), a default path with relative entries is
  29. used (eg. .\Lib;.\plat-win, etc)
  30. The end result of all this is:
  31. * When running python.exe, or any other .exe in the main Python directory
  32. (either an installed version, or directly from the PCbuild directory),
  33. the core path is deduced, and the core paths in the registry are
  34. ignored. Other "application paths" in the registry are always read.
  35. * When Python is hosted in another exe (different directory, embedded via
  36. COM, etc), the Python Home will not be deduced, so the core path from
  37. the registry is used. Other "application paths" in the registry are
  38. always read.
  39. * If Python can't find its home and there is no registry (eg, frozen
  40. exe, some very strange installation setup) you get a path with
  41. some default, but relative, paths.
  42. ---------------------------------------------------------------- */
  43. #include "Python.h"
  44. #include "osdefs.h"
  45. #ifdef MS_WINDOWS
  46. #include <windows.h>
  47. #include <tchar.h>
  48. #endif
  49. #ifdef HAVE_SYS_TYPES_H
  50. #include <sys/types.h>
  51. #endif /* HAVE_SYS_TYPES_H */
  52. #ifdef HAVE_SYS_STAT_H
  53. #include <sys/stat.h>
  54. #endif /* HAVE_SYS_STAT_H */
  55. #include <string.h>
  56. /* Search in some common locations for the associated Python libraries.
  57. *
  58. * Py_GetPath() tries to return a sensible Python module search path.
  59. *
  60. * The approach is an adaptation for Windows of the strategy used in
  61. * ../Modules/getpath.c; it uses the Windows Registry as one of its
  62. * information sources.
  63. */
  64. #ifndef LANDMARK
  65. #define LANDMARK "lib\\os.py"
  66. #endif
  67. static char prefix[MAXPATHLEN+1];
  68. static char progpath[MAXPATHLEN+1];
  69. static char dllpath[MAXPATHLEN+1];
  70. static char *module_search_path = NULL;
  71. static int
  72. is_sep(char ch) /* determine if "ch" is a separator character */
  73. {
  74. #ifdef ALTSEP
  75. return ch == SEP || ch == ALTSEP;
  76. #else
  77. return ch == SEP;
  78. #endif
  79. }
  80. /* assumes 'dir' null terminated in bounds. Never writes
  81. beyond existing terminator.
  82. */
  83. static void
  84. reduce(char *dir)
  85. {
  86. size_t i = strlen(dir);
  87. while (i > 0 && !is_sep(dir[i]))
  88. --i;
  89. dir[i] = '\0';
  90. }
  91. static int
  92. exists(char *filename)
  93. {
  94. struct stat buf;
  95. return stat(filename, &buf) == 0;
  96. }
  97. /* Assumes 'filename' MAXPATHLEN+1 bytes long -
  98. may extend 'filename' by one character.
  99. */
  100. static int
  101. ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
  102. {
  103. if (exists(filename))
  104. return 1;
  105. /* Check for the compiled version of prefix. */
  106. if (strlen(filename) < MAXPATHLEN) {
  107. strcat(filename, Py_OptimizeFlag ? "o" : "c");
  108. if (exists(filename))
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. /* Add a path component, by appending stuff to buffer.
  114. buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
  115. NUL-terminated string with no more than MAXPATHLEN characters (not counting
  116. the trailing NUL). It's a fatal error if it contains a string longer than
  117. that (callers must be careful!). If these requirements are met, it's
  118. guaranteed that buffer will still be a NUL-terminated string with no more
  119. than MAXPATHLEN characters at exit. If stuff is too long, only as much of
  120. stuff as fits will be appended.
  121. */
  122. static void
  123. join(char *buffer, char *stuff)
  124. {
  125. size_t n, k;
  126. if (is_sep(stuff[0]))
  127. n = 0;
  128. else {
  129. n = strlen(buffer);
  130. if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
  131. buffer[n++] = SEP;
  132. }
  133. if (n > MAXPATHLEN)
  134. Py_FatalError("buffer overflow in getpathp.c's joinpath()");
  135. k = strlen(stuff);
  136. if (n + k > MAXPATHLEN)
  137. k = MAXPATHLEN - n;
  138. strncpy(buffer+n, stuff, k);
  139. buffer[n+k] = '\0';
  140. }
  141. /* gotlandmark only called by search_for_prefix, which ensures
  142. 'prefix' is null terminated in bounds. join() ensures
  143. 'landmark' can not overflow prefix if too long.
  144. */
  145. static int
  146. gotlandmark(char *landmark)
  147. {
  148. int ok;
  149. Py_ssize_t n;
  150. n = strlen(prefix);
  151. join(prefix, landmark);
  152. ok = ismodule(prefix);
  153. prefix[n] = '\0';
  154. return ok;
  155. }
  156. /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
  157. assumption provided by only caller, calculate_path() */
  158. static int
  159. search_for_prefix(char *argv0_path, char *landmark)
  160. {
  161. /* Search from argv0_path, until landmark is found */
  162. strcpy(prefix, argv0_path);
  163. do {
  164. if (gotlandmark(landmark))
  165. return 1;
  166. reduce(prefix);
  167. } while (prefix[0]);
  168. return 0;
  169. }
  170. #ifdef MS_WINDOWS
  171. #ifdef Py_ENABLE_SHARED
  172. /* a string loaded from the DLL at startup.*/
  173. extern const char *PyWin_DLLVersionString;
  174. /* Load a PYTHONPATH value from the registry.
  175. Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
  176. Works in both Unicode and 8bit environments. Only uses the
  177. Ex family of functions so it also works with Windows CE.
  178. Returns NULL, or a pointer that should be freed.
  179. XXX - this code is pretty strange, as it used to also
  180. work on Win16, where the buffer sizes werent available
  181. in advance. It could be simplied now Win16/Win32s is dead!
  182. */
  183. static char *
  184. getpythonregpath(HKEY keyBase, int skipcore)
  185. {
  186. HKEY newKey = 0;
  187. DWORD dataSize = 0;
  188. DWORD numKeys = 0;
  189. LONG rc;
  190. char *retval = NULL;
  191. TCHAR *dataBuf = NULL;
  192. static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
  193. static const TCHAR keySuffix[] = _T("\\PythonPath");
  194. size_t versionLen;
  195. DWORD index;
  196. TCHAR *keyBuf = NULL;
  197. TCHAR *keyBufPtr;
  198. TCHAR **ppPaths = NULL;
  199. /* Tried to use sysget("winver") but here is too early :-( */
  200. versionLen = _tcslen(PyWin_DLLVersionString);
  201. /* Space for all the chars, plus one \0 */
  202. keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
  203. sizeof(TCHAR)*(versionLen-1) +
  204. sizeof(keySuffix));
  205. if (keyBuf==NULL) goto done;
  206. memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
  207. keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
  208. memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
  209. keyBufPtr += versionLen;
  210. /* NULL comes with this one! */
  211. memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
  212. /* Open the root Python key */
  213. rc=RegOpenKeyEx(keyBase,
  214. keyBuf, /* subkey */
  215. 0, /* reserved */
  216. KEY_READ,
  217. &newKey);
  218. if (rc!=ERROR_SUCCESS) goto done;
  219. /* Find out how big our core buffer is, and how many subkeys we have */
  220. rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
  221. NULL, NULL, &dataSize, NULL, NULL);
  222. if (rc!=ERROR_SUCCESS) goto done;
  223. if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
  224. /* Allocate a temp array of char buffers, so we only need to loop
  225. reading the registry once
  226. */
  227. ppPaths = malloc( sizeof(TCHAR *) * numKeys );
  228. if (ppPaths==NULL) goto done;
  229. memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
  230. /* Loop over all subkeys, allocating a temp sub-buffer. */
  231. for(index=0;index<numKeys;index++) {
  232. TCHAR keyBuf[MAX_PATH+1];
  233. HKEY subKey = 0;
  234. DWORD reqdSize = MAX_PATH+1;
  235. /* Get the sub-key name */
  236. DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
  237. NULL, NULL, NULL, NULL );
  238. if (rc!=ERROR_SUCCESS) goto done;
  239. /* Open the sub-key */
  240. rc=RegOpenKeyEx(newKey,
  241. keyBuf, /* subkey */
  242. 0, /* reserved */
  243. KEY_READ,
  244. &subKey);
  245. if (rc!=ERROR_SUCCESS) goto done;
  246. /* Find the value of the buffer size, malloc, then read it */
  247. RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
  248. if (reqdSize) {
  249. ppPaths[index] = malloc(reqdSize);
  250. if (ppPaths[index]) {
  251. RegQueryValueEx(subKey, NULL, 0, NULL,
  252. (LPBYTE)ppPaths[index],
  253. &reqdSize);
  254. dataSize += reqdSize + 1; /* 1 for the ";" */
  255. }
  256. }
  257. RegCloseKey(subKey);
  258. }
  259. /* return null if no path to return */
  260. if (dataSize == 0) goto done;
  261. /* original datasize from RegQueryInfo doesn't include the \0 */
  262. dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
  263. if (dataBuf) {
  264. TCHAR *szCur = dataBuf;
  265. DWORD reqdSize = dataSize;
  266. /* Copy our collected strings */
  267. for (index=0;index<numKeys;index++) {
  268. if (index > 0) {
  269. *(szCur++) = _T(';');
  270. dataSize--;
  271. }
  272. if (ppPaths[index]) {
  273. Py_ssize_t len = _tcslen(ppPaths[index]);
  274. _tcsncpy(szCur, ppPaths[index], len);
  275. szCur += len;
  276. assert(dataSize > (DWORD)len);
  277. dataSize -= (DWORD)len;
  278. }
  279. }
  280. if (skipcore)
  281. *szCur = '\0';
  282. else {
  283. /* If we have no values, we dont need a ';' */
  284. if (numKeys) {
  285. *(szCur++) = _T(';');
  286. dataSize--;
  287. }
  288. /* Now append the core path entries -
  289. this will include the NULL
  290. */
  291. rc = RegQueryValueEx(newKey, NULL, 0, NULL,
  292. (LPBYTE)szCur, &dataSize);
  293. }
  294. /* And set the result - caller must free
  295. If MBCS, it is fine as is. If Unicode, allocate new
  296. buffer and convert.
  297. */
  298. #ifdef UNICODE
  299. retval = (char *)malloc(reqdSize+1);
  300. if (retval)
  301. WideCharToMultiByte(CP_ACP, 0,
  302. dataBuf, -1, /* source */
  303. retval, reqdSize+1, /* dest */
  304. NULL, NULL);
  305. free(dataBuf);
  306. #else
  307. retval = dataBuf;
  308. #endif
  309. }
  310. done:
  311. /* Loop freeing my temp buffers */
  312. if (ppPaths) {
  313. for(index=0;index<numKeys;index++)
  314. if (ppPaths[index]) free(ppPaths[index]);
  315. free(ppPaths);
  316. }
  317. if (newKey)
  318. RegCloseKey(newKey);
  319. if (keyBuf)
  320. free(keyBuf);
  321. return retval;
  322. }
  323. #endif /* Py_ENABLE_SHARED */
  324. #endif /* MS_WINDOWS */
  325. static void
  326. get_progpath(void)
  327. {
  328. extern char *Py_GetProgramName(void);
  329. char *path = getenv("PATH");
  330. char *prog = Py_GetProgramName();
  331. #ifdef MS_WINDOWS
  332. extern HANDLE PyWin_DLLhModule;
  333. #ifdef UNICODE
  334. WCHAR wprogpath[MAXPATHLEN+1];
  335. /* Windows documents that GetModuleFileName() will "truncate",
  336. but makes no mention of the null terminator. Play it safe.
  337. PLUS Windows itself defines MAX_PATH as the same, but anyway...
  338. */
  339. #ifdef Py_ENABLE_SHARED
  340. wprogpath[MAXPATHLEN]=_T('\0');
  341. if (PyWin_DLLhModule &&
  342. GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) {
  343. WideCharToMultiByte(CP_ACP, 0,
  344. wprogpath, -1,
  345. dllpath, MAXPATHLEN+1,
  346. NULL, NULL);
  347. }
  348. #else
  349. dllpath[0] = 0;
  350. #endif
  351. wprogpath[MAXPATHLEN]=_T('\0');
  352. if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
  353. WideCharToMultiByte(CP_ACP, 0,
  354. wprogpath, -1,
  355. progpath, MAXPATHLEN+1,
  356. NULL, NULL);
  357. return;
  358. }
  359. #else
  360. /* static init of progpath ensures final char remains \0 */
  361. #ifdef Py_ENABLE_SHARED
  362. if (PyWin_DLLhModule)
  363. if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN))
  364. dllpath[0] = 0;
  365. #else
  366. dllpath[0] = 0;
  367. #endif
  368. if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
  369. return;
  370. #endif
  371. #endif
  372. if (prog == NULL || *prog == '\0')
  373. prog = "python";
  374. /* If there is no slash in the argv0 path, then we have to
  375. * assume python is on the user's $PATH, since there's no
  376. * other way to find a directory to start the search from. If
  377. * $PATH isn't exported, you lose.
  378. */
  379. #ifdef ALTSEP
  380. if (strchr(prog, SEP) || strchr(prog, ALTSEP))
  381. #else
  382. if (strchr(prog, SEP))
  383. #endif
  384. strncpy(progpath, prog, MAXPATHLEN);
  385. else if (path) {
  386. while (1) {
  387. char *delim = strchr(path, DELIM);
  388. if (delim) {
  389. size_t len = delim - path;
  390. /* ensure we can't overwrite buffer */
  391. len = min(MAXPATHLEN,len);
  392. strncpy(progpath, path, len);
  393. *(progpath + len) = '\0';
  394. }
  395. else
  396. strncpy(progpath, path, MAXPATHLEN);
  397. /* join() is safe for MAXPATHLEN+1 size buffer */
  398. join(progpath, prog);
  399. if (exists(progpath))
  400. break;
  401. if (!delim) {
  402. progpath[0] = '\0';
  403. break;
  404. }
  405. path = delim + 1;
  406. }
  407. }
  408. else
  409. progpath[0] = '\0';
  410. }
  411. static void
  412. calculate_path(void)
  413. {
  414. char argv0_path[MAXPATHLEN+1];
  415. char *buf;
  416. size_t bufsz;
  417. char *pythonhome = Py_GetPythonHome();
  418. char *envpath = Py_GETENV("PYTHONPATH");
  419. #ifdef MS_WINDOWS
  420. int skiphome, skipdefault;
  421. char *machinepath = NULL;
  422. char *userpath = NULL;
  423. char zip_path[MAXPATHLEN+1];
  424. size_t len;
  425. #endif
  426. get_progpath();
  427. /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
  428. strcpy(argv0_path, progpath);
  429. reduce(argv0_path);
  430. if (pythonhome == NULL || *pythonhome == '\0') {
  431. if (search_for_prefix(argv0_path, LANDMARK))
  432. pythonhome = prefix;
  433. else
  434. pythonhome = NULL;
  435. }
  436. else
  437. strncpy(prefix, pythonhome, MAXPATHLEN);
  438. if (envpath && *envpath == '\0')
  439. envpath = NULL;
  440. #ifdef MS_WINDOWS
  441. /* Calculate zip archive path */
  442. if (dllpath[0]) /* use name of python DLL */
  443. strncpy(zip_path, dllpath, MAXPATHLEN);
  444. else /* use name of executable program */
  445. strncpy(zip_path, progpath, MAXPATHLEN);
  446. zip_path[MAXPATHLEN] = '\0';
  447. len = strlen(zip_path);
  448. if (len > 4) {
  449. zip_path[len-3] = 'z'; /* change ending to "zip" */
  450. zip_path[len-2] = 'i';
  451. zip_path[len-1] = 'p';
  452. }
  453. else {
  454. zip_path[0] = 0;
  455. }
  456. skiphome = pythonhome==NULL ? 0 : 1;
  457. #ifdef Py_ENABLE_SHARED
  458. machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
  459. userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
  460. #endif
  461. /* We only use the default relative PYTHONPATH if we havent
  462. anything better to use! */
  463. skipdefault = envpath!=NULL || pythonhome!=NULL || \
  464. machinepath!=NULL || userpath!=NULL;
  465. #endif
  466. /* We need to construct a path from the following parts.
  467. (1) the PYTHONPATH environment variable, if set;
  468. (2) for Win32, the zip archive file path;
  469. (3) for Win32, the machinepath and userpath, if set;
  470. (4) the PYTHONPATH config macro, with the leading "."
  471. of each component replaced with pythonhome, if set;
  472. (5) the directory containing the executable (argv0_path).
  473. The length calculation calculates #4 first.
  474. Extra rules:
  475. - If PYTHONHOME is set (in any way) item (3) is ignored.
  476. - If registry values are used, (4) and (5) are ignored.
  477. */
  478. /* Calculate size of return buffer */
  479. if (pythonhome != NULL) {
  480. char *p;
  481. bufsz = 1;
  482. for (p = PYTHONPATH; *p; p++) {
  483. if (*p == DELIM)
  484. bufsz++; /* number of DELIM plus one */
  485. }
  486. bufsz *= strlen(pythonhome);
  487. }
  488. else
  489. bufsz = 0;
  490. bufsz += strlen(PYTHONPATH) + 1;
  491. bufsz += strlen(argv0_path) + 1;
  492. #ifdef MS_WINDOWS
  493. if (userpath)
  494. bufsz += strlen(userpath) + 1;
  495. if (machinepath)
  496. bufsz += strlen(machinepath) + 1;
  497. bufsz += strlen(zip_path) + 1;
  498. #endif
  499. if (envpath != NULL)
  500. bufsz += strlen(envpath) + 1;
  501. module_search_path = buf = malloc(bufsz);
  502. if (buf == NULL) {
  503. /* We can't exit, so print a warning and limp along */
  504. fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
  505. if (envpath) {
  506. fprintf(stderr, "Using environment $PYTHONPATH.\n");
  507. module_search_path = envpath;
  508. }
  509. else {
  510. fprintf(stderr, "Using default static path.\n");
  511. module_search_path = PYTHONPATH;
  512. }
  513. #ifdef MS_WINDOWS
  514. if (machinepath)
  515. free(machinepath);
  516. if (userpath)
  517. free(userpath);
  518. #endif /* MS_WINDOWS */
  519. return;
  520. }
  521. if (envpath) {
  522. strcpy(buf, envpath);
  523. buf = strchr(buf, '\0');
  524. *buf++ = DELIM;
  525. }
  526. #ifdef MS_WINDOWS
  527. if (zip_path[0]) {
  528. strcpy(buf, zip_path);
  529. buf = strchr(buf, '\0');
  530. *buf++ = DELIM;
  531. }
  532. if (userpath) {
  533. strcpy(buf, userpath);
  534. buf = strchr(buf, '\0');
  535. *buf++ = DELIM;
  536. free(userpath);
  537. }
  538. if (machinepath) {
  539. strcpy(buf, machinepath);
  540. buf = strchr(buf, '\0');
  541. *buf++ = DELIM;
  542. free(machinepath);
  543. }
  544. if (pythonhome == NULL) {
  545. if (!skipdefault) {
  546. strcpy(buf, PYTHONPATH);
  547. buf = strchr(buf, '\0');
  548. }
  549. }
  550. #else
  551. if (pythonhome == NULL) {
  552. strcpy(buf, PYTHONPATH);
  553. buf = strchr(buf, '\0');
  554. }
  555. #endif /* MS_WINDOWS */
  556. else {
  557. char *p = PYTHONPATH;
  558. char *q;
  559. size_t n;
  560. for (;;) {
  561. q = strchr(p, DELIM);
  562. if (q == NULL)
  563. n = strlen(p);
  564. else
  565. n = q-p;
  566. if (p[0] == '.' && is_sep(p[1])) {
  567. strcpy(buf, pythonhome);
  568. buf = strchr(buf, '\0');
  569. p++;
  570. n--;
  571. }
  572. strncpy(buf, p, n);
  573. buf += n;
  574. if (q == NULL)
  575. break;
  576. *buf++ = DELIM;
  577. p = q+1;
  578. }
  579. }
  580. if (argv0_path) {
  581. *buf++ = DELIM;
  582. strcpy(buf, argv0_path);
  583. buf = strchr(buf, '\0');
  584. }
  585. *buf = '\0';
  586. /* Now to pull one last hack/trick. If sys.prefix is
  587. empty, then try and find it somewhere on the paths
  588. we calculated. We scan backwards, as our general policy
  589. is that Python core directories are at the *end* of
  590. sys.path. We assume that our "lib" directory is
  591. on the path, and that our 'prefix' directory is
  592. the parent of that.
  593. */
  594. if (*prefix=='\0') {
  595. char lookBuf[MAXPATHLEN+1];
  596. char *look = buf - 1; /* 'buf' is at the end of the buffer */
  597. while (1) {
  598. Py_ssize_t nchars;
  599. char *lookEnd = look;
  600. /* 'look' will end up one character before the
  601. start of the path in question - even if this
  602. is one character before the start of the buffer
  603. */
  604. while (look >= module_search_path && *look != DELIM)
  605. look--;
  606. nchars = lookEnd-look;
  607. strncpy(lookBuf, look+1, nchars);
  608. lookBuf[nchars] = '\0';
  609. /* Up one level to the parent */
  610. reduce(lookBuf);
  611. if (search_for_prefix(lookBuf, LANDMARK)) {
  612. break;
  613. }
  614. /* If we are out of paths to search - give up */
  615. if (look < module_search_path)
  616. break;
  617. look--;
  618. }
  619. }
  620. }
  621. /* External interface */
  622. char *
  623. Py_GetPath(void)
  624. {
  625. if (!module_search_path)
  626. calculate_path();
  627. return module_search_path;
  628. }
  629. char *
  630. Py_GetPrefix(void)
  631. {
  632. if (!module_search_path)
  633. calculate_path();
  634. return prefix;
  635. }
  636. char *
  637. Py_GetExecPrefix(void)
  638. {
  639. return Py_GetPrefix();
  640. }
  641. char *
  642. Py_GetProgramFullPath(void)
  643. {
  644. if (!module_search_path)
  645. calculate_path();
  646. return progpath;
  647. }