PageRenderTime 191ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/kbe/src/lib/python/PC/os2emx/getpathp.c

https://bitbucket.org/kbengine/kbengine
C | 418 lines | 278 code | 45 blank | 95 comment | 73 complexity | 42da7e55ff4d3c3c2ccec628be42329c MD5 | raw file
  1. /* Return the initial module search path. */
  2. /* This version used by OS/2+EMX */
  3. /* ----------------------------------------------------------------
  4. PATH RULES FOR OS/2+EMX:
  5. This describes how sys.path is formed on OS/2+EMX. 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 attempt to locate the "Python Home" - if the PYTHONHOME env var
  12. is set, we believe it. Otherwise, we use the path of our host .EXE's
  13. to try and locate our "landmark" (lib\\os.py) and deduce our home.
  14. - If we DO have a Python Home: The relevant sub-directories (Lib,
  15. plat-win, etc) are based on the Python Home
  16. - If we DO NOT have a Python Home, the core Python Path is
  17. loaded from the registry. This is the main PythonPath key,
  18. and both HKLM and HKCU are combined to form the path)
  19. * Iff - we can not locate the Python Home, and have not had a PYTHONPATH
  20. specified (ie, we have _nothing_ we can assume is a good path), a
  21. default path with relative entries is used (eg. .\Lib;.\plat-win, etc)
  22. The end result of all this is:
  23. * When running python.exe, or any other .exe in the main Python directory
  24. (either an installed version, or directly from the PCbuild directory),
  25. the core path is deduced.
  26. * When Python is hosted in another exe (different directory, embedded via
  27. COM, etc), the Python Home will not be deduced, so the core path from
  28. the registry is used. Other "application paths "in the registry are
  29. always read.
  30. * If Python can't find its home and there is no registry (eg, frozen
  31. exe, some very strange installation setup) you get a path with
  32. some default, but relative, paths.
  33. ---------------------------------------------------------------- */
  34. #include "Python.h"
  35. #include "osdefs.h"
  36. #ifndef PYOS_OS2
  37. #error This file only compilable on OS/2
  38. #endif
  39. #define INCL_DOS
  40. #include <os2.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <string.h>
  44. #if HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif /* HAVE_UNISTD_H */
  47. /* Search in some common locations for the associated Python libraries.
  48. *
  49. * Py_GetPath() tries to return a sensible Python module search path.
  50. *
  51. * The approach is an adaptation for Windows of the strategy used in
  52. * ../Modules/getpath.c; it uses the Windows Registry as one of its
  53. * information sources.
  54. */
  55. #ifndef LANDMARK
  56. #if defined(PYCC_GCC)
  57. #define LANDMARK "lib/os.py"
  58. #else
  59. #define LANDMARK "lib\\os.py"
  60. #endif
  61. #endif
  62. static char prefix[MAXPATHLEN+1];
  63. static char progpath[MAXPATHLEN+1];
  64. static char *module_search_path = NULL;
  65. static int
  66. is_sep(char ch) /* determine if "ch" is a separator character */
  67. {
  68. #ifdef ALTSEP
  69. return ch == SEP || ch == ALTSEP;
  70. #else
  71. return ch == SEP;
  72. #endif
  73. }
  74. /* assumes 'dir' null terminated in bounds.
  75. * Never writes beyond existing terminator.
  76. */
  77. static void
  78. reduce(char *dir)
  79. {
  80. size_t i = strlen(dir);
  81. while (i > 0 && !is_sep(dir[i]))
  82. --i;
  83. dir[i] = '\0';
  84. }
  85. static int
  86. exists(char *filename)
  87. {
  88. struct stat buf;
  89. return stat(filename, &buf) == 0;
  90. }
  91. /* Is module (check for .pyc/.pyo too)
  92. * Assumes 'filename' MAXPATHLEN+1 bytes long -
  93. * may extend 'filename' by one character.
  94. */
  95. static int
  96. ismodule(char *filename)
  97. {
  98. if (exists(filename))
  99. return 1;
  100. /* Check for the compiled version of prefix. */
  101. if (strlen(filename) < MAXPATHLEN) {
  102. strcat(filename, Py_OptimizeFlag ? "o" : "c");
  103. if (exists(filename))
  104. return 1;
  105. }
  106. return 0;
  107. }
  108. /* Add a path component, by appending stuff to buffer.
  109. buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
  110. NUL-terminated string with no more than MAXPATHLEN characters (not counting
  111. the trailing NUL). It's a fatal error if it contains a string longer than
  112. that (callers must be careful!). If these requirements are met, it's
  113. guaranteed that buffer will still be a NUL-terminated string with no more
  114. than MAXPATHLEN characters at exit. If stuff is too long, only as much of
  115. stuff as fits will be appended.
  116. */
  117. static void
  118. join(char *buffer, char *stuff)
  119. {
  120. size_t n, k;
  121. if (is_sep(stuff[0]))
  122. n = 0;
  123. else {
  124. n = strlen(buffer);
  125. if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
  126. buffer[n++] = SEP;
  127. }
  128. if (n > MAXPATHLEN)
  129. Py_FatalError("buffer overflow in getpathp.c's joinpath()");
  130. k = strlen(stuff);
  131. if (n + k > MAXPATHLEN)
  132. k = MAXPATHLEN - n;
  133. strncpy(buffer+n, stuff, k);
  134. buffer[n+k] = '\0';
  135. }
  136. /* gotlandmark only called by search_for_prefix, which ensures
  137. * 'prefix' is null terminated in bounds. join() ensures
  138. * 'landmark' can not overflow prefix if too long.
  139. */
  140. static int
  141. gotlandmark(char *landmark)
  142. {
  143. int n, ok;
  144. n = strlen(prefix);
  145. join(prefix, landmark);
  146. ok = ismodule(prefix);
  147. prefix[n] = '\0';
  148. return ok;
  149. }
  150. /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
  151. * assumption provided by only caller, calculate_path()
  152. */
  153. static int
  154. search_for_prefix(char *argv0_path, char *landmark)
  155. {
  156. /* Search from argv0_path, until landmark is found */
  157. strcpy(prefix, argv0_path);
  158. do {
  159. if (gotlandmark(landmark))
  160. return 1;
  161. reduce(prefix);
  162. } while (prefix[0]);
  163. return 0;
  164. }
  165. static void
  166. get_progpath(void)
  167. {
  168. extern char *Py_GetProgramName(void);
  169. char *path = getenv("PATH");
  170. char *prog = Py_GetProgramName();
  171. PPIB pib;
  172. if ((DosGetInfoBlocks(NULL, &pib) == 0) &&
  173. (DosQueryModuleName(pib->pib_hmte, sizeof(progpath), progpath) == 0))
  174. return;
  175. if (prog == NULL || *prog == '\0')
  176. prog = "python";
  177. /* If there is no slash in the argv0 path, then we have to
  178. * assume python is on the user's $PATH, since there's no
  179. * other way to find a directory to start the search from. If
  180. * $PATH isn't exported, you lose.
  181. */
  182. #ifdef ALTSEP
  183. if (strchr(prog, SEP) || strchr(prog, ALTSEP))
  184. #else
  185. if (strchr(prog, SEP))
  186. #endif
  187. strncpy(progpath, prog, MAXPATHLEN);
  188. else if (path) {
  189. while (1) {
  190. char *delim = strchr(path, DELIM);
  191. if (delim) {
  192. size_t len = delim - path;
  193. /* ensure we can't overwrite buffer */
  194. #if !defined(PYCC_GCC)
  195. len = min(MAXPATHLEN,len);
  196. #else
  197. len = MAXPATHLEN < len ? MAXPATHLEN : len;
  198. #endif
  199. strncpy(progpath, path, len);
  200. *(progpath + len) = '\0';
  201. }
  202. else
  203. strncpy(progpath, path, MAXPATHLEN);
  204. /* join() is safe for MAXPATHLEN+1 size buffer */
  205. join(progpath, prog);
  206. if (exists(progpath))
  207. break;
  208. if (!delim) {
  209. progpath[0] = '\0';
  210. break;
  211. }
  212. path = delim + 1;
  213. }
  214. }
  215. else
  216. progpath[0] = '\0';
  217. }
  218. static void
  219. calculate_path(void)
  220. {
  221. char argv0_path[MAXPATHLEN+1];
  222. char *buf;
  223. size_t bufsz;
  224. char *pythonhome = Py_GetPythonHome();
  225. char *envpath = getenv("PYTHONPATH");
  226. char zip_path[MAXPATHLEN+1];
  227. size_t len;
  228. get_progpath();
  229. /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
  230. strcpy(argv0_path, progpath);
  231. reduce(argv0_path);
  232. if (pythonhome == NULL || *pythonhome == '\0') {
  233. if (search_for_prefix(argv0_path, LANDMARK))
  234. pythonhome = prefix;
  235. else
  236. pythonhome = NULL;
  237. }
  238. else
  239. strncpy(prefix, pythonhome, MAXPATHLEN);
  240. if (envpath && *envpath == '\0')
  241. envpath = NULL;
  242. /* Calculate zip archive path */
  243. strncpy(zip_path, progpath, MAXPATHLEN);
  244. zip_path[MAXPATHLEN] = '\0';
  245. len = strlen(zip_path);
  246. if (len > 4) {
  247. zip_path[len-3] = 'z'; /* change ending to "zip" */
  248. zip_path[len-2] = 'i';
  249. zip_path[len-1] = 'p';
  250. }
  251. else {
  252. zip_path[0] = 0;
  253. }
  254. /* We need to construct a path from the following parts.
  255. * (1) the PYTHONPATH environment variable, if set;
  256. * (2) the zip archive file path;
  257. * (3) the PYTHONPATH config macro, with the leading "."
  258. * of each component replaced with pythonhome, if set;
  259. * (4) the directory containing the executable (argv0_path).
  260. * The length calculation calculates #3 first.
  261. */
  262. /* Calculate size of return buffer */
  263. if (pythonhome != NULL) {
  264. char *p;
  265. bufsz = 1;
  266. for (p = PYTHONPATH; *p; p++) {
  267. if (*p == DELIM)
  268. bufsz++; /* number of DELIM plus one */
  269. }
  270. bufsz *= strlen(pythonhome);
  271. }
  272. else
  273. bufsz = 0;
  274. bufsz += strlen(PYTHONPATH) + 1;
  275. bufsz += strlen(argv0_path) + 1;
  276. bufsz += strlen(zip_path) + 1;
  277. if (envpath != NULL)
  278. bufsz += strlen(envpath) + 1;
  279. module_search_path = buf = malloc(bufsz);
  280. if (buf == NULL) {
  281. /* We can't exit, so print a warning and limp along */
  282. fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
  283. if (envpath) {
  284. fprintf(stderr, "Using environment $PYTHONPATH.\n");
  285. module_search_path = envpath;
  286. }
  287. else {
  288. fprintf(stderr, "Using default static path.\n");
  289. module_search_path = PYTHONPATH;
  290. }
  291. return;
  292. }
  293. if (envpath) {
  294. strcpy(buf, envpath);
  295. buf = strchr(buf, '\0');
  296. *buf++ = DELIM;
  297. }
  298. if (zip_path[0]) {
  299. strcpy(buf, zip_path);
  300. buf = strchr(buf, '\0');
  301. *buf++ = DELIM;
  302. }
  303. if (pythonhome == NULL) {
  304. strcpy(buf, PYTHONPATH);
  305. buf = strchr(buf, '\0');
  306. }
  307. else {
  308. char *p = PYTHONPATH;
  309. char *q;
  310. size_t n;
  311. for (;;) {
  312. q = strchr(p, DELIM);
  313. if (q == NULL)
  314. n = strlen(p);
  315. else
  316. n = q-p;
  317. if (p[0] == '.' && is_sep(p[1])) {
  318. strcpy(buf, pythonhome);
  319. buf = strchr(buf, '\0');
  320. p++;
  321. n--;
  322. }
  323. strncpy(buf, p, n);
  324. buf += n;
  325. if (q == NULL)
  326. break;
  327. *buf++ = DELIM;
  328. p = q+1;
  329. }
  330. }
  331. if (argv0_path) {
  332. *buf++ = DELIM;
  333. strcpy(buf, argv0_path);
  334. buf = strchr(buf, '\0');
  335. }
  336. *buf = '\0';
  337. }
  338. /* External interface */
  339. char *
  340. Py_GetPath(void)
  341. {
  342. if (!module_search_path)
  343. calculate_path();
  344. return module_search_path;
  345. }
  346. char *
  347. Py_GetPrefix(void)
  348. {
  349. if (!module_search_path)
  350. calculate_path();
  351. return prefix;
  352. }
  353. char *
  354. Py_GetExecPrefix(void)
  355. {
  356. return Py_GetPrefix();
  357. }
  358. char *
  359. Py_GetProgramFullPath(void)
  360. {
  361. if (!module_search_path)
  362. calculate_path();
  363. return progpath;
  364. }