blob: 3c418d6c875996ff2a62a7a290d2066dcd1a08d9 [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
Thomas Wouters7e474022000-07-16 12:04:32 +000095 * preprocessor variable PYTHONPATH is used to construct a path. Each
Barry Warsaw90126031997-04-11 20:27:03 +000096 * 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
Fred Drakeedabdc12000-07-08 06:16:37 +0000142reduce(char *dir)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000143{
Fred Drakeedabdc12000-07-08 06:16:37 +0000144 size_t i = strlen(dir);
145 while (i > 0 && dir[i] != SEP)
146 --i;
147 dir[i] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000148}
Guido van Rossumd29806c1998-01-19 22:06:22 +0000149
150
151#ifndef S_ISREG
152#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
153#endif
154
155#ifndef S_ISDIR
156#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
157#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000158
159static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000160isfile(char *filename) /* Is file, not directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000161{
Fred Drakeedabdc12000-07-08 06:16:37 +0000162 struct stat buf;
163 if (stat(filename, &buf) != 0)
164 return 0;
165 if (!S_ISREG(buf.st_mode))
166 return 0;
167 return 1;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000168}
169
170
171static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000172ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000173{
Fred Drakeedabdc12000-07-08 06:16:37 +0000174 if (isfile(filename))
175 return 1;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000176
Fred Drakeedabdc12000-07-08 06:16:37 +0000177 /* Check for the compiled version of prefix. */
178 if (strlen(filename) < MAXPATHLEN) {
179 strcat(filename, Py_OptimizeFlag ? "o" : "c");
180 if (isfile(filename))
181 return 1;
182 }
183 return 0;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000184}
185
186
187static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000188isxfile(char *filename) /* Is executable file */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000189{
Fred Drakeedabdc12000-07-08 06:16:37 +0000190 struct stat buf;
191 if (stat(filename, &buf) != 0)
192 return 0;
193 if (!S_ISREG(buf.st_mode))
194 return 0;
195 if ((buf.st_mode & 0111) == 0)
196 return 0;
197 return 1;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000198}
199
200
201static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000202isdir(char *filename) /* Is directory */
Guido van Rossumd29806c1998-01-19 22:06:22 +0000203{
Fred Drakeedabdc12000-07-08 06:16:37 +0000204 struct stat buf;
205 if (stat(filename, &buf) != 0)
206 return 0;
207 if (!S_ISDIR(buf.st_mode))
208 return 0;
209 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000210}
211
212
213static void
Fred Drakeedabdc12000-07-08 06:16:37 +0000214joinpath(char *buffer, char *stuff)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000215{
Fred Drakeedabdc12000-07-08 06:16:37 +0000216 size_t n, k;
217 if (stuff[0] == SEP)
218 n = 0;
219 else {
220 n = strlen(buffer);
221 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
222 buffer[n++] = SEP;
223 }
224 k = strlen(stuff);
225 if (n + k > MAXPATHLEN)
226 k = MAXPATHLEN - n;
227 strncpy(buffer+n, stuff, k);
228 buffer[n+k] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000229}
230
231
Guido van Rossum305e5d01997-04-11 17:18:45 +0000232static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000233search_for_prefix(char *argv0_path, char *home)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000234{
Fred Drakeedabdc12000-07-08 06:16:37 +0000235 size_t n;
236 char *vpath;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000237
Fred Drakeedabdc12000-07-08 06:16:37 +0000238 /* If PYTHONHOME is set, we believe it unconditionally */
239 if (home) {
240 char *delim;
241 strcpy(prefix, home);
242 delim = strchr(prefix, DELIM);
243 if (delim)
244 *delim = '\0';
245 joinpath(prefix, lib_python);
246 joinpath(prefix, LANDMARK);
247 return 1;
248 }
Jeremy Hylton847a9962000-05-26 21:49:07 +0000249
Fred Drakeedabdc12000-07-08 06:16:37 +0000250 /* Check to see if argv[0] is in the build directory */
251 strcpy(prefix, argv0_path);
252 joinpath(prefix, "Modules/Setup");
253 if (isfile(prefix)) {
254 /* Check VPATH to see if argv0_path is in the build directory.
255 * Complication: the VPATH passed in is relative to the
256 * Modules build directory and points to the Modules source
257 * directory; we need it relative to the build tree and
258 * pointing to the source tree. Solution: chop off a leading
259 * ".." (but only if it's there -- it could be an absolute
260 * path) and chop off the final component (assuming it's
261 * "Modules").
262 */
263 vpath = VPATH;
264 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
265 vpath += 3;
266 strcpy(prefix, argv0_path);
267 joinpath(prefix, vpath);
268 reduce(prefix);
269 joinpath(prefix, "Lib");
270 joinpath(prefix, LANDMARK);
271 if (ismodule(prefix))
272 return -1;
273 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000274
Fred Drakeedabdc12000-07-08 06:16:37 +0000275 /* Search from argv0_path, until root is found */
276 strcpy(prefix, argv0_path);
277 do {
278 n = strlen(prefix);
279 joinpath(prefix, lib_python);
280 joinpath(prefix, LANDMARK);
281 if (ismodule(prefix))
282 return 1;
283 prefix[n] = '\0';
284 reduce(prefix);
285 } while (prefix[0]);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000286
Fred Drakeedabdc12000-07-08 06:16:37 +0000287 /* Look at configure's PREFIX */
288 strcpy(prefix, PREFIX);
289 joinpath(prefix, lib_python);
290 joinpath(prefix, LANDMARK);
291 if (ismodule(prefix))
292 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000293
Fred Drakeedabdc12000-07-08 06:16:37 +0000294 /* Fail */
295 return 0;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000296}
297
298
299static int
Fred Drakeedabdc12000-07-08 06:16:37 +0000300search_for_exec_prefix(char *argv0_path, char *home)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000301{
Fred Drakeedabdc12000-07-08 06:16:37 +0000302 size_t n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000303
Fred Drakeedabdc12000-07-08 06:16:37 +0000304 /* If PYTHONHOME is set, we believe it unconditionally */
305 if (home) {
306 char *delim;
307 delim = strchr(home, DELIM);
308 if (delim)
309 strcpy(exec_prefix, delim+1);
310 else
311 strcpy(exec_prefix, home);
312 joinpath(exec_prefix, lib_python);
313 joinpath(exec_prefix, "lib-dynload");
314 return 1;
315 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000316
Fred Drakeedabdc12000-07-08 06:16:37 +0000317 /* Check to see if argv[0] is in the build directory */
318 strcpy(exec_prefix, argv0_path);
319 joinpath(exec_prefix, "Modules/Setup");
320 if (isfile(exec_prefix)) {
321 reduce(exec_prefix);
322 return -1;
323 }
Jeremy Hylton847a9962000-05-26 21:49:07 +0000324
Fred Drakeedabdc12000-07-08 06:16:37 +0000325 /* Search from argv0_path, until root is found */
326 strcpy(exec_prefix, argv0_path);
327 do {
328 n = strlen(exec_prefix);
329 joinpath(exec_prefix, lib_python);
330 joinpath(exec_prefix, "lib-dynload");
331 if (isdir(exec_prefix))
332 return 1;
333 exec_prefix[n] = '\0';
334 reduce(exec_prefix);
335 } while (exec_prefix[0]);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000336
Fred Drakeedabdc12000-07-08 06:16:37 +0000337 /* Look at configure's EXEC_PREFIX */
338 strcpy(exec_prefix, EXEC_PREFIX);
339 joinpath(exec_prefix, lib_python);
340 joinpath(exec_prefix, "lib-dynload");
341 if (isdir(exec_prefix))
342 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000343
Fred Drakeedabdc12000-07-08 06:16:37 +0000344 /* Fail */
345 return 0;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000346}
347
348
349static void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000350calculate_path(void)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000351{
Fred Drakeedabdc12000-07-08 06:16:37 +0000352 extern char *Py_GetProgramName();
Guido van Rossum305e5d01997-04-11 17:18:45 +0000353
Fred Drakeedabdc12000-07-08 06:16:37 +0000354 static char delimiter[2] = {DELIM, '\0'};
355 static char separator[2] = {SEP, '\0'};
356 char *pythonpath = PYTHONPATH;
357 char *rtpypath = getenv("PYTHONPATH");
358 char *home = Py_GetPythonHome();
359 char *path = getenv("PATH");
360 char *prog = Py_GetProgramName();
361 char argv0_path[MAXPATHLEN+1];
362 int pfound, efound; /* 1 if found; -1 if found build directory */
363 char *buf;
364 size_t bufsz;
365 size_t prefixsz;
366 char *defpath = pythonpath;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000367#ifdef WITH_NEXT_FRAMEWORK
Fred Drakeedabdc12000-07-08 06:16:37 +0000368 NSModule pythonModule;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000369#endif
370
371#ifdef WITH_NEXT_FRAMEWORK
Fred Drakeedabdc12000-07-08 06:16:37 +0000372 pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
373 /* Use dylib functions to find out where the framework was loaded from */
374 buf = NSLibraryNameForModule(pythonModule);
375 if (buf != NULL) {
376 /* We're in a framework. */
377 strcpy(progpath, buf);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000378
Fred Drakeedabdc12000-07-08 06:16:37 +0000379 /* Frameworks have support for versioning */
380 strcpy(lib_python, "lib");
381 }
382 else {
383 /* If we're not in a framework, fall back to the old way
384 (even though NSNameOfModule() probably does the same thing.) */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000385#endif
386
Guido van Rossum305e5d01997-04-11 17:18:45 +0000387 /* Initialize this dynamically for K&R C */
388 sprintf(lib_python, "lib/python%s", VERSION);
389
390 /* If there is no slash in the argv0 path, then we have to
391 * assume python is on the user's $PATH, since there's no
392 * other way to find a directory to start the search from. If
393 * $PATH isn't exported, you lose.
394 */
395 if (strchr(prog, SEP))
Fred Drakeedabdc12000-07-08 06:16:37 +0000396 strcpy(progpath, prog);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000397 else if (path) {
Fred Drakeedabdc12000-07-08 06:16:37 +0000398 while (1) {
399 char *delim = strchr(path, DELIM);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000400
Fred Drakeedabdc12000-07-08 06:16:37 +0000401 if (delim) {
402 size_t len = delim - path;
403 strncpy(progpath, path, len);
404 *(progpath + len) = '\0';
405 }
406 else
407 strcpy(progpath, path);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000408
Fred Drakeedabdc12000-07-08 06:16:37 +0000409 joinpath(progpath, prog);
410 if (isxfile(progpath))
411 break;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000412
Fred Drakeedabdc12000-07-08 06:16:37 +0000413 if (!delim) {
414 progpath[0] = '\0';
415 break;
416 }
417 path = delim + 1;
418 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000419 }
420 else
Fred Drakeedabdc12000-07-08 06:16:37 +0000421 progpath[0] = '\0';
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000422#ifdef WITH_NEXT_FRAMEWORK
Fred Drakeedabdc12000-07-08 06:16:37 +0000423 }
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000424#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000425
Fred Drakeedabdc12000-07-08 06:16:37 +0000426 strcpy(argv0_path, progpath);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000427
428#if HAVE_READLINK
Fred Drakeedabdc12000-07-08 06:16:37 +0000429 {
430 char tmpbuffer[MAXPATHLEN+1];
431 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
432 while (linklen != -1) {
433 /* It's not null terminated! */
434 tmpbuffer[linklen] = '\0';
435 if (tmpbuffer[0] == SEP)
436 strcpy(argv0_path, tmpbuffer);
437 else {
438 /* Interpret relative to progpath */
439 reduce(argv0_path);
440 joinpath(argv0_path, tmpbuffer);
441 }
442 linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
443 }
444 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000445#endif /* HAVE_READLINK */
446
Fred Drakeedabdc12000-07-08 06:16:37 +0000447 reduce(argv0_path);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000448
Fred Drakeedabdc12000-07-08 06:16:37 +0000449 if (!(pfound = search_for_prefix(argv0_path, home))) {
450 if (!Py_FrozenFlag)
451 fprintf(stderr,
452 "Could not find platform independent libraries <prefix>\n");
453 strcpy(prefix, PREFIX);
454 joinpath(prefix, lib_python);
455 }
456 else
457 reduce(prefix);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000458
Fred Drakeedabdc12000-07-08 06:16:37 +0000459 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
460 if (!Py_FrozenFlag)
461 fprintf(stderr,
462 "Could not find platform dependent libraries <exec_prefix>\n");
463 strcpy(exec_prefix, EXEC_PREFIX);
464 joinpath(exec_prefix, "lib/lib-dynload");
465 }
466 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000467
Fred Drakeedabdc12000-07-08 06:16:37 +0000468 if ((!pfound || !efound) && !Py_FrozenFlag)
469 fprintf(stderr,
470 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
Guido van Rossum305e5d01997-04-11 17:18:45 +0000471
Fred Drakeedabdc12000-07-08 06:16:37 +0000472 /* Calculate size of return buffer.
473 */
474 bufsz = 0;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000475
Fred Drakeedabdc12000-07-08 06:16:37 +0000476 if (rtpypath)
477 bufsz += strlen(rtpypath) + 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000478
Fred Drakeedabdc12000-07-08 06:16:37 +0000479 prefixsz = strlen(prefix) + 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000480
Fred Drakeedabdc12000-07-08 06:16:37 +0000481 while (1) {
482 char *delim = strchr(defpath, DELIM);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000483
Fred Drakeedabdc12000-07-08 06:16:37 +0000484 if (defpath[0] != SEP)
485 /* Paths are relative to prefix */
486 bufsz += prefixsz;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000487
Fred Drakeedabdc12000-07-08 06:16:37 +0000488 if (delim)
489 bufsz += delim - defpath + 1;
490 else {
491 bufsz += strlen(defpath) + 1;
492 break;
493 }
494 defpath = delim + 1;
495 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000496
Fred Drakeedabdc12000-07-08 06:16:37 +0000497 bufsz += strlen(exec_prefix) + 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000498
Fred Drakeedabdc12000-07-08 06:16:37 +0000499 /* This is the only malloc call in this file */
500 buf = PyMem_Malloc(bufsz);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000501
Fred Drakeedabdc12000-07-08 06:16:37 +0000502 if (buf == NULL) {
503 /* We can't exit, so print a warning and limp along */
504 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
505 fprintf(stderr, "Using default static PYTHONPATH.\n");
506 module_search_path = PYTHONPATH;
507 }
508 else {
509 /* Run-time value of $PYTHONPATH goes first */
510 if (rtpypath) {
511 strcpy(buf, rtpypath);
512 strcat(buf, delimiter);
513 }
514 else
515 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000516
Fred Drakeedabdc12000-07-08 06:16:37 +0000517 /* Next goes merge of compile-time $PYTHONPATH with
518 * dynamically located prefix.
519 */
520 defpath = pythonpath;
521 while (1) {
522 char *delim = strchr(defpath, DELIM);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000523
Fred Drakeedabdc12000-07-08 06:16:37 +0000524 if (defpath[0] != SEP) {
525 strcat(buf, prefix);
526 strcat(buf, separator);
527 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000528
Fred Drakeedabdc12000-07-08 06:16:37 +0000529 if (delim) {
530 size_t len = delim - defpath + 1;
531 size_t end = strlen(buf) + len;
532 strncat(buf, defpath, len);
533 *(buf + end) = '\0';
534 }
535 else {
536 strcat(buf, defpath);
537 break;
538 }
539 defpath = delim + 1;
540 }
541 strcat(buf, delimiter);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000542
Fred Drakeedabdc12000-07-08 06:16:37 +0000543 /* Finally, on goes the directory for dynamic-load modules */
544 strcat(buf, exec_prefix);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000545
Fred Drakeedabdc12000-07-08 06:16:37 +0000546 /* And publish the results */
547 module_search_path = buf;
548 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000549
Fred Drakeedabdc12000-07-08 06:16:37 +0000550 /* Reduce prefix and exec_prefix to their essence,
551 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
552 * If we're loading relative to the build directory,
553 * return the compiled-in defaults instead.
554 */
555 if (pfound > 0) {
556 reduce(prefix);
557 reduce(prefix);
558 }
559 else
560 strcpy(prefix, PREFIX);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000561
Fred Drakeedabdc12000-07-08 06:16:37 +0000562 if (efound > 0) {
563 reduce(exec_prefix);
564 reduce(exec_prefix);
565 reduce(exec_prefix);
566 }
567 else
568 strcpy(exec_prefix, EXEC_PREFIX);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000569}
570
571
572/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000573
574char *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000575Py_GetPath(void)
Guido van Rossum667d7041995-08-04 04:20:48 +0000576{
Fred Drakeedabdc12000-07-08 06:16:37 +0000577 if (!module_search_path)
578 calculate_path();
579 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000580}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000581
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000582char *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000583Py_GetPrefix(void)
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000584{
Fred Drakeedabdc12000-07-08 06:16:37 +0000585 if (!module_search_path)
586 calculate_path();
587 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000588}
589
590char *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000591Py_GetExecPrefix(void)
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000592{
Fred Drakeedabdc12000-07-08 06:16:37 +0000593 if (!module_search_path)
594 calculate_path();
595 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000596}
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000597
598char *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000599Py_GetProgramFullPath(void)
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000600{
Fred Drakeedabdc12000-07-08 06:16:37 +0000601 if (!module_search_path)
602 calculate_path();
603 return progpath;
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000604}