1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Return the initial module search path. */
12 /* Used by DOS, OS/2, Windows 3.1. Works on NT too. */
19 extern BOOL
PyWin_IsWin32s(void);
22 #include <sys/types.h>
28 #endif /* HAVE_UNISTD_H */
30 /* Search in some common locations for the associated Python libraries.
32 * Two directories must be found, the platform independent directory
33 * (prefix), containing the common .py and .pyc files, and the platform
34 * dependent directory (exec_prefix), containing the shared library
35 * modules. Note that prefix and exec_prefix can be the same directory,
36 * but for some installations, they are different.
38 * Py_GetPath() tries to return a sensible Python module search path.
40 * First, we look to see if the executable is in a subdirectory of
41 * the Python build directory. We calculate the full path of the
42 * directory containing the executable as progpath. We work backwards
43 * along progpath and look for $dir/Modules/Setup.in, a distinctive
44 * landmark. If found, we use $dir/Lib as $root. The returned
45 * Python path is the compiled #define PYTHONPATH with all the initial
46 * "./lib" replaced by $root.
48 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
50 * Otherwise we try to find $progpath/lib/os.py, and if found, then
51 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
52 * with all "./lib" replaced by $root (as above).
57 #define LANDMARK "lib\\os.py"
60 static char prefix
[MAXPATHLEN
+1];
61 static char exec_prefix
[MAXPATHLEN
+1];
62 static char progpath
[MAXPATHLEN
+1];
63 static char *module_search_path
= NULL
;
67 is_sep(char ch
) /* determine if "ch" is a separator character */
70 return ch
== SEP
|| ch
== ALTSEP
;
81 while (i
> 0 && !is_sep(dir
[i
]))
88 exists(char *filename
)
91 return stat(filename
, &buf
) == 0;
96 join(char *buffer
, char *stuff
)
103 if (n
> 0 && !is_sep(buffer
[n
-1]) && n
< MAXPATHLEN
)
107 if (n
+ k
> MAXPATHLEN
)
109 strncpy(buffer
+n
, stuff
, k
);
115 search_for_prefix(char *argv0_path
, char *landmark
)
119 /* Search from argv0_path, until root is found */
120 strcpy(prefix
, argv0_path
);
123 join(prefix
, landmark
);
124 if (exists(prefix
)) {
135 #include "malloc.h" // for alloca - see comments below!
136 extern const char *PyWin_DLLVersionString
; // a string loaded from the DLL at startup.
139 /* Load a PYTHONPATH value from the registry.
140 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
142 Returns NULL, or a pointer that should be freed.
146 getpythonregpath(HKEY keyBase
, BOOL bWin32s
)
151 DWORD numEntries
= 0;
155 const char keyPrefix
[] = "Software\\Python\\PythonCore\\";
156 const char keySuffix
[] = "\\PythonPath";
160 // Tried to use sysget("winver") but here is too early :-(
161 versionLen
= strlen(PyWin_DLLVersionString
);
162 // alloca == no free required, but memory only local to fn.
163 // also no heap fragmentation! Am I being silly?
164 keyBuf
= alloca(sizeof(keyPrefix
)-1 + versionLen
+ sizeof(keySuffix
)); // chars only, plus 1 NULL.
165 // lots of constants here for the compiler to optimize away :-)
166 memcpy(keyBuf
, keyPrefix
, sizeof(keyPrefix
)-1);
167 memcpy(keyBuf
+sizeof(keyPrefix
)-1, PyWin_DLLVersionString
, versionLen
);
168 memcpy(keyBuf
+sizeof(keyPrefix
)-1+versionLen
, keySuffix
, sizeof(keySuffix
)); // NULL comes with this one!
170 rc
=RegOpenKey(keyBase
,
173 if (rc
==ERROR_SUCCESS
) {
174 RegQueryInfoKey(newKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
175 &numEntries
, &nameSize
, &dataSize
, NULL
, NULL
);
177 if (bWin32s
&& numEntries
==0 && dataSize
==0) {
178 /* must hardcode for Win32s */
183 /* Loop over all subkeys. */
184 /* Win32s doesnt know how many subkeys, so we do
186 char keyBuf
[MAX_PATH
+1];
189 for(index
=0;;index
++) {
191 DWORD rc
= RegEnumKey(newKey
,
192 index
, keyBuf
, MAX_PATH
+1);
194 rc
= RegQueryValue(newKey
, keyBuf
, NULL
, &reqdSize
);
196 if (bWin32s
&& reqdSize
==0) reqdSize
= 512;
197 dataSize
+= reqdSize
+ 1; /* 1 for the ";" */
199 dataBuf
= malloc(dataSize
+1);
201 return NULL
; /* pretty serious? Raise error? */
202 /* Now loop over, grabbing the paths.
203 Subkeys before main library */
204 for(index
=0;;index
++) {
206 long reqdSize
= dataSize
;
207 DWORD rc
= RegEnumKey(newKey
,
208 index
, keyBuf
,MAX_PATH
+1);
210 rc
= RegQueryValue(newKey
,
211 keyBuf
, dataBuf
+off
, &reqdSize
);
214 /* If Nothing, or only '\0' copied. */
215 adjust
= strlen(dataBuf
+off
);
218 dataBuf
[off
++] = ';';
223 /* Additionally, win32s doesnt work as expected, so
224 the specific strlen() is required for 3.1. */
225 rc
= RegQueryValue(newKey
, "", dataBuf
+off
, &dataSize
);
226 if (rc
==ERROR_SUCCESS
) {
227 if (strlen(dataBuf
)==0)
230 retval
= dataBuf
; /* caller will free */
240 #endif /* MS_WIN32 */
245 extern char *Py_GetProgramName(void);
246 char *path
= getenv("PATH");
247 char *prog
= Py_GetProgramName();
250 if (GetModuleFileName(NULL
, progpath
, MAXPATHLEN
))
253 if (prog
== NULL
|| *prog
== '\0')
256 /* If there is no slash in the argv0 path, then we have to
257 * assume python is on the user's $PATH, since there's no
258 * other way to find a directory to start the search from. If
259 * $PATH isn't exported, you lose.
262 if (strchr(prog
, SEP
) || strchr(prog
, ALTSEP
))
264 if (strchr(prog
, SEP
))
266 strcpy(progpath
, prog
);
269 char *delim
= strchr(path
, DELIM
);
272 int len
= delim
- path
;
273 strncpy(progpath
, path
, len
);
274 *(progpath
+ len
) = '\0';
277 strcpy(progpath
, path
);
279 join(progpath
, prog
);
280 if (exists(progpath
))
297 char argv0_path
[MAXPATHLEN
+1];
300 char *pythonhome
= Py_GetPythonHome();
301 char *envpath
= getenv("PYTHONPATH");
303 char *machinepath
, *userpath
;
305 /* Are we running under Windows 3.1(1) Win32s? */
306 if (PyWin_IsWin32s()) {
307 /* Only CLASSES_ROOT is supported */
308 machinepath
= getpythonregpath(HKEY_CLASSES_ROOT
, TRUE
);
311 machinepath
= getpythonregpath(HKEY_LOCAL_MACHINE
, FALSE
);
312 userpath
= getpythonregpath(HKEY_CURRENT_USER
, FALSE
);
317 strcpy(argv0_path
, progpath
);
319 if (pythonhome
== NULL
|| *pythonhome
== '\0') {
320 if (search_for_prefix(argv0_path
, LANDMARK
))
328 strcpy(prefix
, pythonhome
);
330 /* Extract Any Optional Trailing EXEC_PREFIX */
331 /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
332 delim
= strchr(prefix
, DELIM
);
335 strcpy(exec_prefix
, delim
+1);
337 strcpy(exec_prefix
, EXEC_PREFIX
);
340 if (envpath
&& *envpath
== '\0')
343 /* We need to construct a path from the following parts:
344 (1) the PYTHONPATH environment variable, if set;
345 (2) for Win32, the machinepath and userpath, if set;
346 (3) the PYTHONPATH config macro, with the leading "."
347 of each component replaced with pythonhome, if set;
348 (4) the directory containing the executable (argv0_path).
349 The length calculation calculates #3 first.
352 /* Calculate size of return buffer */
353 if (pythonhome
!= NULL
) {
356 for (p
= PYTHONPATH
; *p
; p
++) {
358 bufsz
++; /* number of DELIM plus one */
360 bufsz
*= strlen(pythonhome
);
364 bufsz
+= strlen(PYTHONPATH
) + 1;
366 bufsz
+= strlen(envpath
) + 1;
367 bufsz
+= strlen(argv0_path
) + 1;
370 bufsz
+= strlen(machinepath
) + 1;
372 bufsz
+= strlen(userpath
) + 1;
375 module_search_path
= buf
= malloc(bufsz
);
377 /* We can't exit, so print a warning and limp along */
378 fprintf(stderr
, "Can't malloc dynamic PYTHONPATH.\n");
380 fprintf(stderr
, "Using default static $PYTHONPATH.\n");
381 module_search_path
= envpath
;
384 fprintf(stderr
, "Using environment $PYTHONPATH.\n");
385 module_search_path
= PYTHONPATH
;
391 strcpy(buf
, envpath
);
392 buf
= strchr(buf
, '\0');
397 strcpy(buf
, machinepath
);
398 buf
= strchr(buf
, '\0');
402 strcpy(buf
, userpath
);
403 buf
= strchr(buf
, '\0');
407 if (pythonhome
== NULL
) {
408 strcpy(buf
, PYTHONPATH
);
409 buf
= strchr(buf
, '\0');
412 char *p
= PYTHONPATH
;
416 q
= strchr(p
, DELIM
);
421 if (p
[0] == '.' && is_sep(p
[1])) {
422 strcpy(buf
, pythonhome
);
423 buf
= strchr(buf
, '\0');
437 strcpy(buf
, argv0_path
);
438 buf
= strchr(buf
, '\0');
444 /* External interface */
449 if (!module_search_path
)
452 return module_search_path
;
458 if (!module_search_path
)
465 Py_GetExecPrefix(void)
467 if (!module_search_path
)
474 Py_GetProgramFullPath(void)
476 if (!module_search_path
)