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