blob: 010fbaabef6173972063e9079e1aa852517cc646 [file] [log] [blame]
Guido van Rossum582646a1996-05-28 22:30:17 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum582646a1996-05-28 22:30:17 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum582646a1996-05-28 22:30:17 +00009******************************************************************/
10
11/* Return the initial module search path. */
12
Guido van Rossum667d7041995-08-04 04:20:48 +000013#include "Python.h"
14#include "osdefs.h"
15
Guido van Rossum305e5d01997-04-11 17:18:45 +000016#include <sys/types.h>
17#include <sys/stat.h>
Guido van Rossum21f84971997-06-02 22:18:31 +000018#include <string.h>
Guido van Rossum667d7041995-08-04 04:20:48 +000019
Guido van Rossum305e5d01997-04-11 17:18:45 +000020#if HAVE_UNISTD_H
21#include <unistd.h>
22#endif /* HAVE_UNISTD_H */
23
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000024#ifdef WITH_NEXT_FRAMEWORK
25#include <mach-o/dyld.h>
26#endif
27
Guido van Rossum305e5d01997-04-11 17:18:45 +000028/* Search in some common locations for the associated Python libraries.
29 *
30 * Two directories must be found, the platform independent directory
Barry Warsaw90126031997-04-11 20:27:03 +000031 * (prefix), containing the common .py and .pyc files, and the platform
32 * dependent directory (exec_prefix), containing the shared library
33 * modules. Note that prefix and exec_prefix can be the same directory,
34 * but for some installations, they are different.
Guido van Rossum305e5d01997-04-11 17:18:45 +000035 *
Barry Warsaw90126031997-04-11 20:27:03 +000036 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
37 * Each search tries a number of different locations until a ``landmark''
38 * file or directory is found. If no prefix or exec_prefix is found, a
39 * warning message is issued and the preprocessor defined PREFIX and
40 * EXEC_PREFIX are used (even though they will not work); python carries on
41 * as best as is possible, but most imports will fail.
Guido van Rossum305e5d01997-04-11 17:18:45 +000042 *
43 * Before any searches are done, the location of the executable is
Barry Warsaw90126031997-04-11 20:27:03 +000044 * determined. If argv[0] has one or more slashs in it, it is used
45 * unchanged. Otherwise, it must have been invoked from the shell's path,
46 * so we search $PATH for the named executable and use that. If the
47 * executable was not found on $PATH (or there was no $PATH environment
48 * variable), the original argv[0] string is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000049 *
Barry Warsaw90126031997-04-11 20:27:03 +000050 * Next, the executable location is examined to see if it is a symbolic
51 * link. If so, the link is chased (correctly interpreting a relative
52 * pathname if one is found) and the directory of the link target is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000053 *
Barry Warsaw90126031997-04-11 20:27:03 +000054 * Finally, argv0_path is set to the directory containing the executable
55 * (i.e. the last component is stripped).
Guido van Rossum305e5d01997-04-11 17:18:45 +000056 *
Barry Warsaw90126031997-04-11 20:27:03 +000057 * With argv0_path in hand, we perform a number of steps. The same steps
58 * are performed for prefix and for exec_prefix, but with a different
59 * landmark.
Guido van Rossum305e5d01997-04-11 17:18:45 +000060 *
61 * Step 1. Are we running python out of the build directory? This is
62 * checked by looking for a different kind of landmark relative to
Barry Warsaw90126031997-04-11 20:27:03 +000063 * argv0_path. For prefix, the landmark's path is derived from the VPATH
64 * preprocessor variable (taking into account that its value is almost, but
65 * not quite, what we need). For exec_prefix, the landmark is
66 * Modules/Setup. If the landmark is found, we're done.
Guido van Rossum305e5d01997-04-11 17:18:45 +000067 *
68 * For the remaining steps, the prefix landmark will always be
Jeremy Hylton847a9962000-05-26 21:49:07 +000069 * lib/python$VERSION/os.py and the exec_prefix will always be
Guido van Rossum266033e1997-10-20 23:20:32 +000070 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
Barry Warsaw90126031997-04-11 20:27:03 +000071 * number as supplied by the Makefile. Note that this means that no more
72 * build directory checking is performed; if the first step did not find
73 * the landmarks, the assumption is that python is running from an
74 * installed setup.
Guido van Rossum305e5d01997-04-11 17:18:45 +000075 *
76 * Step 2. See if the $PYTHONHOME environment variable points to the
Barry Warsaw90126031997-04-11 20:27:03 +000077 * installed location of the Python libraries. If $PYTHONHOME is set, then
78 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
79 * directory, which is used for both, or the prefix and exec_prefix
80 * directories separated by a colon.
Guido van Rossum305e5d01997-04-11 17:18:45 +000081 *
82 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
Barry Warsaw90126031997-04-11 20:27:03 +000083 * backtracking up the path until it is exhausted. This is the most common
84 * step to succeed. Note that if prefix and exec_prefix are different,
85 * exec_prefix is more likely to be found; however if exec_prefix is a
86 * subdirectory of prefix, both will be found.
Guido van Rossum305e5d01997-04-11 17:18:45 +000087 *
Barry Warsaw90126031997-04-11 20:27:03 +000088 * Step 4. Search the directories pointed to by the preprocessor variables
89 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
90 * passed in as options to the configure script.
Guido van Rossum305e5d01997-04-11 17:18:45 +000091 *
Barry Warsaw90126031997-04-11 20:27:03 +000092 * That's it!
93 *
94 * Well, almost. Once we have determined prefix and exec_prefix, the
95 * preprocesor variable PYTHONPATH is used to construct a path. Each
96 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
97 * containing the shared library modules is appended. The environment
98 * variable $PYTHONPATH is inserted in front of it all. Finally, the
99 * prefix and exec_prefix globals are tweaked so they reflect the values
100 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
101 * off. If either points to the build directory, the globals are reset to
102 * the corresponding preprocessor variables (so sys.prefix will reflect the
103 * installation location, even though sys.path points into the build
104 * directory). This seems to make more sense given that currently the only
105 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
106 * process to find the installed Python tree.
107 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000108
109#ifndef VERSION
110#define VERSION "1.5"
111#endif
112
113#ifndef VPATH
114#define VPATH "."
Guido van Rossum667d7041995-08-04 04:20:48 +0000115#endif
116
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000117#ifndef PREFIX
118#define PREFIX "/usr/local"
119#endif
120
121#ifndef EXEC_PREFIX
Guido van Rossum6e12d561996-07-30 20:36:12 +0000122#define EXEC_PREFIX PREFIX
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000123#endif
124
Guido van Rossum305e5d01997-04-11 17:18:45 +0000125#ifndef PYTHONPATH
126/* I know this isn't K&R C, but the Makefile specifies it anyway */
127#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
Guido van Rossum266033e1997-10-20 23:20:32 +0000128 EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
Guido van Rossum305e5d01997-04-11 17:18:45 +0000129#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000130
Guido van Rossum305e5d01997-04-11 17:18:45 +0000131#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +0000132#define LANDMARK "os.py"
Guido van Rossum305e5d01997-04-11 17:18:45 +0000133#endif
134
Guido van Rossum305e5d01997-04-11 17:18:45 +0000135static char prefix[MAXPATHLEN+1];
136static char exec_prefix[MAXPATHLEN+1];
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000137static char progpath[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000138static char *module_search_path = NULL;
139static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
140
141static void
142reduce(dir)
143 char *dir;
144{
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000145 size_t i = strlen(dir);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000146 while (i > 0 && dir[i] != SEP)
147 --i;
148 dir[i] = '\0';
149}
Guido van Rossumd29806c1998-01-19 22:06:22 +0000150
151
152#ifndef S_ISREG
153#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
154#endif
155
156#ifndef S_ISDIR
157#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
158#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000159
160static int
Guido van Rossumd29806c1998-01-19 22:06:22 +0000161isfile(filename) /* Is file, not directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000162 char *filename;
163{
164 struct stat buf;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000165 if (stat(filename, &buf) != 0)
166 return 0;
167 if (!S_ISREG(buf.st_mode))
168 return 0;
169 return 1;
170}
171
172
173static int
174ismodule(filename) /* Is module -- check for .pyc/.pyo too */
175 char *filename;
176{
177 if (isfile(filename))
178 return 1;
179
180 /* Check for the compiled version of prefix. */
181 if (strlen(filename) < MAXPATHLEN) {
182 strcat(filename, Py_OptimizeFlag ? "o" : "c");
183 if (isfile(filename))
184 return 1;
185 }
186 return 0;
187}
188
189
190static int
191isxfile(filename) /* Is executable file */
192 char *filename;
193{
194 struct stat buf;
195 if (stat(filename, &buf) != 0)
196 return 0;
197 if (!S_ISREG(buf.st_mode))
198 return 0;
199 if ((buf.st_mode & 0111) == 0)
200 return 0;
201 return 1;
202}
203
204
205static int
206isdir(filename) /* Is directory */
207 char *filename;
208{
209 struct stat buf;
210 if (stat(filename, &buf) != 0)
211 return 0;
212 if (!S_ISDIR(buf.st_mode))
213 return 0;
214 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000215}
216
217
218static void
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000219joinpath(buffer, stuff)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000220 char *buffer;
221 char *stuff;
222{
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000223 size_t n, k;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000224 if (stuff[0] == SEP)
225 n = 0;
226 else {
227 n = strlen(buffer);
228 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
229 buffer[n++] = SEP;
230 }
231 k = strlen(stuff);
232 if (n + k > MAXPATHLEN)
233 k = MAXPATHLEN - n;
234 strncpy(buffer+n, stuff, k);
235 buffer[n+k] = '\0';
236}
237
238
Guido van Rossum305e5d01997-04-11 17:18:45 +0000239static int
240search_for_prefix(argv0_path, home)
241 char *argv0_path;
242 char *home;
243{
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000244 size_t n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000245 char *vpath;
246
Jeremy Hylton847a9962000-05-26 21:49:07 +0000247 /* If PYTHONHOME is set, we believe it unconditionally */
248 if (home) {
249 char *delim;
250 strcpy(prefix, home);
251 delim = strchr(prefix, DELIM);
252 if (delim)
253 *delim = '\0';
254 joinpath(prefix, lib_python);
255 joinpath(prefix, LANDMARK);
256 return 1;
257 }
258
Guido van Rossum573a24a1997-05-12 20:49:39 +0000259 /* Check to see if argv[0] is in the build directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000260 strcpy(prefix, argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000261 joinpath(prefix, "Modules/Setup");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000262 if (isfile(prefix)) {
Guido van Rossum573a24a1997-05-12 20:49:39 +0000263 /* Check VPATH to see if argv0_path is in the build directory.
264 * Complication: the VPATH passed in is relative to the
265 * Modules build directory and points to the Modules source
266 * directory; we need it relative to the build tree and
267 * pointing to the source tree. Solution: chop off a leading
268 * ".." (but only if it's there -- it could be an absolute
269 * path) and chop off the final component (assuming it's
270 * "Modules").
271 */
272 vpath = VPATH;
273 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
274 vpath += 3;
275 strcpy(prefix, argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000276 joinpath(prefix, vpath);
Guido van Rossum573a24a1997-05-12 20:49:39 +0000277 reduce(prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000278 joinpath(prefix, "Lib");
279 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000280 if (ismodule(prefix))
Guido van Rossum573a24a1997-05-12 20:49:39 +0000281 return -1;
282 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000283
Guido van Rossum305e5d01997-04-11 17:18:45 +0000284 /* Search from argv0_path, until root is found */
285 strcpy(prefix, argv0_path);
286 do {
287 n = strlen(prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000288 joinpath(prefix, lib_python);
289 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000290 if (ismodule(prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000291 return 1;
292 prefix[n] = '\0';
293 reduce(prefix);
294 } while (prefix[0]);
295
296 /* Look at configure's PREFIX */
297 strcpy(prefix, PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000298 joinpath(prefix, lib_python);
299 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000300 if (ismodule(prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000301 return 1;
302
Guido van Rossumd7763621997-05-12 20:53:23 +0000303 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000304 return 0;
305}
306
307
308static int
309search_for_exec_prefix(argv0_path, home)
310 char *argv0_path;
311 char *home;
312{
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000313 size_t n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000314
Jeremy Hylton847a9962000-05-26 21:49:07 +0000315 /* If PYTHONHOME is set, we believe it unconditionally */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000316 if (home) {
Guido van Rossum305e5d01997-04-11 17:18:45 +0000317 char *delim;
318 delim = strchr(home, DELIM);
319 if (delim)
320 strcpy(exec_prefix, delim+1);
321 else
322 strcpy(exec_prefix, home);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000323 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000324 joinpath(exec_prefix, "lib-dynload");
Guido van Rossum305e5d01997-04-11 17:18:45 +0000325 return 1;
326 }
327
Jeremy Hylton847a9962000-05-26 21:49:07 +0000328 /* Check to see if argv[0] is in the build directory */
329 strcpy(exec_prefix, argv0_path);
330 joinpath(exec_prefix, "Modules/Setup");
331 if (isfile(exec_prefix)) {
332 reduce(exec_prefix);
333 return -1;
334 }
335
Guido van Rossum305e5d01997-04-11 17:18:45 +0000336 /* Search from argv0_path, until root is found */
337 strcpy(exec_prefix, argv0_path);
338 do {
339 n = strlen(exec_prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000340 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000341 joinpath(exec_prefix, "lib-dynload");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000342 if (isdir(exec_prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000343 return 1;
344 exec_prefix[n] = '\0';
345 reduce(exec_prefix);
346 } while (exec_prefix[0]);
347
348 /* Look at configure's EXEC_PREFIX */
349 strcpy(exec_prefix, EXEC_PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000350 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000351 joinpath(exec_prefix, "lib-dynload");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000352 if (isdir(exec_prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000353 return 1;
354
Guido van Rossumd7763621997-05-12 20:53:23 +0000355 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000356 return 0;
357}
358
359
360static void
361calculate_path()
362{
363 extern char *Py_GetProgramName();
364
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000365 static char delimiter[2] = {DELIM, '\0'};
366 static char separator[2] = {SEP, '\0'};
Guido van Rossum305e5d01997-04-11 17:18:45 +0000367 char *pythonpath = PYTHONPATH;
368 char *rtpypath = getenv("PYTHONPATH");
Guido van Rossum131c92c1998-02-06 22:29:30 +0000369 char *home = Py_GetPythonHome();
Guido van Rossum305e5d01997-04-11 17:18:45 +0000370 char *path = getenv("PATH");
371 char *prog = Py_GetProgramName();
372 char argv0_path[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000373 int pfound, efound; /* 1 if found; -1 if found build directory */
374 char *buf;
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000375 size_t bufsz;
376 size_t prefixsz;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000377 char *defpath = pythonpath;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000378#ifdef WITH_NEXT_FRAMEWORK
379 NSModule pythonModule;
380#endif
381
382#ifdef WITH_NEXT_FRAMEWORK
383 pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
384 /* Use dylib functions to find out where the framework was loaded from */
385 buf = NSLibraryNameForModule(pythonModule);
386 if (buf != NULL) {
387 /* We're in a framework. */
388 strcpy(progpath, buf);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000389
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000390 /* Frameworks have support for versioning */
391 strcpy(lib_python, "lib");
392 } else {
393 /* If we're not in a framework, fall back to the old way (even though NSNameOfModule() probably does the same thing.) */
394#endif
395
Guido van Rossum305e5d01997-04-11 17:18:45 +0000396 /* Initialize this dynamically for K&R C */
397 sprintf(lib_python, "lib/python%s", VERSION);
398
399 /* If there is no slash in the argv0 path, then we have to
400 * assume python is on the user's $PATH, since there's no
401 * other way to find a directory to start the search from. If
402 * $PATH isn't exported, you lose.
403 */
404 if (strchr(prog, SEP))
405 strcpy(progpath, prog);
406 else if (path) {
407 while (1) {
408 char *delim = strchr(path, DELIM);
409
410 if (delim) {
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000411 size_t len = delim - path;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000412 strncpy(progpath, path, len);
413 *(progpath + len) = '\0';
414 }
415 else
416 strcpy(progpath, path);
417
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000418 joinpath(progpath, prog);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000419 if (isxfile(progpath))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000420 break;
421
422 if (!delim) {
423 progpath[0] = '\0';
424 break;
425 }
426 path = delim + 1;
427 }
428 }
429 else
430 progpath[0] = '\0';
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000431#ifdef WITH_NEXT_FRAMEWORK
432 }
433#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000434
435 strcpy(argv0_path, progpath);
436
437#if HAVE_READLINK
438 {
439 char tmpbuffer[MAXPATHLEN+1];
440 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
Guido van Rossum302be441998-04-29 21:07:06 +0000441 while (linklen != -1) {
Guido van Rossum305e5d01997-04-11 17:18:45 +0000442 /* It's not null terminated! */
443 tmpbuffer[linklen] = '\0';
444 if (tmpbuffer[0] == SEP)
445 strcpy(argv0_path, tmpbuffer);
446 else {
447 /* Interpret relative to progpath */
448 reduce(argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000449 joinpath(argv0_path, tmpbuffer);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000450 }
Guido van Rossum302be441998-04-29 21:07:06 +0000451 linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000452 }
453 }
454#endif /* HAVE_READLINK */
455
456 reduce(argv0_path);
457
458 if (!(pfound = search_for_prefix(argv0_path, home))) {
Guido van Rossum131c92c1998-02-06 22:29:30 +0000459 if (!Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000460 fprintf(stderr,
461 "Could not find platform independent libraries <prefix>\n");
462 strcpy(prefix, PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000463 joinpath(prefix, lib_python);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000464 }
465 else
466 reduce(prefix);
467
468 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
Guido van Rossum131c92c1998-02-06 22:29:30 +0000469 if (!Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000470 fprintf(stderr,
471 "Could not find platform dependent libraries <exec_prefix>\n");
472 strcpy(exec_prefix, EXEC_PREFIX);
Guido van Rossum266033e1997-10-20 23:20:32 +0000473 joinpath(exec_prefix, "lib/lib-dynload");
Guido van Rossum305e5d01997-04-11 17:18:45 +0000474 }
475 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
476
Guido van Rossum131c92c1998-02-06 22:29:30 +0000477 if ((!pfound || !efound) && !Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000478 fprintf(stderr,
479 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
480
481 /* Calculate size of return buffer.
482 */
483 bufsz = 0;
484
485 if (rtpypath)
486 bufsz += strlen(rtpypath) + 1;
487
488 prefixsz = strlen(prefix) + 1;
489
490 while (1) {
491 char *delim = strchr(defpath, DELIM);
492
493 if (defpath[0] != SEP)
494 /* Paths are relative to prefix */
495 bufsz += prefixsz;
496
497 if (delim)
498 bufsz += delim - defpath + 1;
499 else {
500 bufsz += strlen(defpath) + 1;
501 break;
502 }
503 defpath = delim + 1;
504 }
505
506 bufsz += strlen(exec_prefix) + 1;
507
508 /* This is the only malloc call in this file */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000509 buf = PyMem_Malloc(bufsz);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000510
511 if (buf == NULL) {
512 /* We can't exit, so print a warning and limp along */
513 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
514 fprintf(stderr, "Using default static PYTHONPATH.\n");
515 module_search_path = PYTHONPATH;
516 }
517 else {
518 /* Run-time value of $PYTHONPATH goes first */
519 if (rtpypath) {
520 strcpy(buf, rtpypath);
521 strcat(buf, delimiter);
522 }
Guido van Rossum573a24a1997-05-12 20:49:39 +0000523 else
524 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000525
526 /* Next goes merge of compile-time $PYTHONPATH with
527 * dynamically located prefix.
528 */
529 defpath = pythonpath;
530 while (1) {
531 char *delim = strchr(defpath, DELIM);
532
533 if (defpath[0] != SEP) {
534 strcat(buf, prefix);
535 strcat(buf, separator);
536 }
537
538 if (delim) {
Guido van Rossumb6f657c2000-06-28 21:29:03 +0000539 size_t len = delim - defpath + 1;
540 size_t end = strlen(buf) + len;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000541 strncat(buf, defpath, len);
542 *(buf + end) = '\0';
543 }
544 else {
545 strcat(buf, defpath);
546 break;
547 }
548 defpath = delim + 1;
549 }
550 strcat(buf, delimiter);
551
552 /* Finally, on goes the directory for dynamic-load modules */
553 strcat(buf, exec_prefix);
554
555 /* And publish the results */
556 module_search_path = buf;
557 }
558
559 /* Reduce prefix and exec_prefix to their essence,
560 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
561 * If we're loading relative to the build directory,
562 * return the compiled-in defaults instead.
563 */
564 if (pfound > 0) {
565 reduce(prefix);
566 reduce(prefix);
567 }
568 else
569 strcpy(prefix, PREFIX);
570
571 if (efound > 0) {
572 reduce(exec_prefix);
573 reduce(exec_prefix);
574 reduce(exec_prefix);
575 }
576 else
577 strcpy(exec_prefix, EXEC_PREFIX);
578}
579
580
581/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000582
583char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000584Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000585{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000586 if (!module_search_path)
587 calculate_path();
588 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000589}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000590
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000591char *
592Py_GetPrefix()
593{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000594 if (!module_search_path)
595 calculate_path();
596 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000597}
598
599char *
600Py_GetExecPrefix()
601{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000602 if (!module_search_path)
603 calculate_path();
604 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000605}
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000606
607char *
608Py_GetProgramFullPath()
609{
610 if (!module_search_path)
611 calculate_path();
612 return progpath;
613}