2 /* Return the initial module search path. */
3 /* Used by DOS, OS/2, Windows 3.1. Works on NT too. */
10 extern BOOL
PyWin_IsWin32s(void);
13 #include <sys/types.h>
19 #endif /* HAVE_UNISTD_H */
21 /* Search in some common locations for the associated Python libraries.
23 * Two directories must be found, the platform independent directory
24 * (prefix), containing the common .py and .pyc files, and the platform
25 * dependent directory (exec_prefix), containing the shared library
26 * modules. Note that prefix and exec_prefix can be the same directory,
27 * but for some installations, they are different.
29 * Py_GetPath() tries to return a sensible Python module search path.
31 * First, we look to see if the executable is in a subdirectory of
32 * the Python build directory. We calculate the full path of the
33 * directory containing the executable as progpath. We work backwards
34 * along progpath and look for $dir/Modules/Setup.in, a distinctive
35 * landmark. If found, we use $dir/Lib as $root. The returned
36 * Python path is the compiled #define PYTHONPATH with all the initial
37 * "./lib" replaced by $root.
39 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
41 * Otherwise we try to find $progpath/lib/os.py, and if found, then
42 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
43 * with all "./lib" replaced by $root (as above).
48 #define LANDMARK "lib\\os.py"
51 static char prefix
[MAXPATHLEN
+1];
52 static char exec_prefix
[MAXPATHLEN
+1];
53 static char progpath
[MAXPATHLEN
+1];
54 static char *module_search_path
= NULL
;
58 is_sep(char ch
) /* determine if "ch" is a separator character */
61 return ch
== SEP
|| ch
== ALTSEP
;
72 while (i
> 0 && !is_sep(dir
[i
]))
79 exists(char *filename
)
82 return stat(filename
, &buf
) == 0;
87 join(char *buffer
, char *stuff
)
94 if (n
> 0 && !is_sep(buffer
[n
-1]) && n
< MAXPATHLEN
)
98 if (n
+ k
> MAXPATHLEN
)
100 strncpy(buffer
+n
, stuff
, k
);
106 search_for_prefix(char *argv0_path
, char *landmark
)
110 /* Search from argv0_path, until root is found */
111 strcpy(prefix
, argv0_path
);
114 join(prefix
, landmark
);
115 if (exists(prefix
)) {
126 #include "malloc.h" // for alloca - see comments below!
127 extern const char *PyWin_DLLVersionString
; // a string loaded from the DLL at startup.
130 /* Load a PYTHONPATH value from the registry.
131 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
133 Returns NULL, or a pointer that should be freed.
137 getpythonregpath(HKEY keyBase
, BOOL bWin32s
)
142 DWORD numEntries
= 0;
146 const char keyPrefix
[] = "Software\\Python\\PythonCore\\";
147 const char keySuffix
[] = "\\PythonPath";
151 // Tried to use sysget("winver") but here is too early :-(
152 versionLen
= strlen(PyWin_DLLVersionString
);
153 // alloca == no free required, but memory only local to fn.
154 // also no heap fragmentation! Am I being silly?
155 keyBuf
= alloca(sizeof(keyPrefix
)-1 + versionLen
+ sizeof(keySuffix
)); // chars only, plus 1 NULL.
156 // lots of constants here for the compiler to optimize away :-)
157 memcpy(keyBuf
, keyPrefix
, sizeof(keyPrefix
)-1);
158 memcpy(keyBuf
+sizeof(keyPrefix
)-1, PyWin_DLLVersionString
, versionLen
);
159 memcpy(keyBuf
+sizeof(keyPrefix
)-1+versionLen
, keySuffix
, sizeof(keySuffix
)); // NULL comes with this one!
161 rc
=RegOpenKey(keyBase
,
164 if (rc
==ERROR_SUCCESS
) {
165 RegQueryInfoKey(newKey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
166 &numEntries
, &nameSize
, &dataSize
, NULL
, NULL
);
168 if (bWin32s
&& numEntries
==0 && dataSize
==0) {
169 /* must hardcode for Win32s */
174 /* Loop over all subkeys. */
175 /* Win32s doesnt know how many subkeys, so we do
177 char keyBuf
[MAX_PATH
+1];
180 for(index
=0;;index
++) {
182 DWORD rc
= RegEnumKey(newKey
,
183 index
, keyBuf
, MAX_PATH
+1);
185 rc
= RegQueryValue(newKey
, keyBuf
, NULL
, &reqdSize
);
187 if (bWin32s
&& reqdSize
==0) reqdSize
= 512;
188 dataSize
+= reqdSize
+ 1; /* 1 for the ";" */
190 dataBuf
= malloc(dataSize
+1);
192 return NULL
; /* pretty serious? Raise error? */
193 /* Now loop over, grabbing the paths.
194 Subkeys before main library */
195 for(index
=0;;index
++) {
197 long reqdSize
= dataSize
;
198 DWORD rc
= RegEnumKey(newKey
,
199 index
, keyBuf
,MAX_PATH
+1);
201 rc
= RegQueryValue(newKey
,
202 keyBuf
, dataBuf
+off
, &reqdSize
);
205 /* If Nothing, or only '\0' copied. */
206 adjust
= strlen(dataBuf
+off
);
209 dataBuf
[off
++] = ';';
214 /* Additionally, win32s doesnt work as expected, so
215 the specific strlen() is required for 3.1. */
216 rc
= RegQueryValue(newKey
, "", dataBuf
+off
, &dataSize
);
217 if (rc
==ERROR_SUCCESS
) {
218 if (strlen(dataBuf
)==0)
221 retval
= dataBuf
; /* caller will free */
231 #endif /* MS_WIN32 */
236 extern char *Py_GetProgramName(void);
237 char *path
= getenv("PATH");
238 char *prog
= Py_GetProgramName();
241 if (GetModuleFileName(NULL
, progpath
, MAXPATHLEN
))
244 if (prog
== NULL
|| *prog
== '\0')
247 /* If there is no slash in the argv0 path, then we have to
248 * assume python is on the user's $PATH, since there's no
249 * other way to find a directory to start the search from. If
250 * $PATH isn't exported, you lose.
253 if (strchr(prog
, SEP
) || strchr(prog
, ALTSEP
))
255 if (strchr(prog
, SEP
))
257 strcpy(progpath
, prog
);
260 char *delim
= strchr(path
, DELIM
);
263 int len
= delim
- path
;
264 strncpy(progpath
, path
, len
);
265 *(progpath
+ len
) = '\0';
268 strcpy(progpath
, path
);
270 join(progpath
, prog
);
271 if (exists(progpath
))
288 char argv0_path
[MAXPATHLEN
+1];
291 char *pythonhome
= Py_GetPythonHome();
292 char *envpath
= Py_GETENV("PYTHONPATH");
294 char *machinepath
, *userpath
;
296 /* Are we running under Windows 3.1(1) Win32s? */
297 if (PyWin_IsWin32s()) {
298 /* Only CLASSES_ROOT is supported */
299 machinepath
= getpythonregpath(HKEY_CLASSES_ROOT
, TRUE
);
302 machinepath
= getpythonregpath(HKEY_LOCAL_MACHINE
, FALSE
);
303 userpath
= getpythonregpath(HKEY_CURRENT_USER
, FALSE
);
308 strcpy(argv0_path
, progpath
);
310 if (pythonhome
== NULL
|| *pythonhome
== '\0') {
311 if (search_for_prefix(argv0_path
, LANDMARK
))
319 strcpy(prefix
, pythonhome
);
321 /* Extract Any Optional Trailing EXEC_PREFIX */
322 /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
323 delim
= strchr(prefix
, DELIM
);
326 strcpy(exec_prefix
, delim
+1);
328 strcpy(exec_prefix
, EXEC_PREFIX
);
331 if (envpath
&& *envpath
== '\0')
334 /* We need to construct a path from the following parts:
335 (1) the PYTHONPATH environment variable, if set;
336 (2) for Win32, the machinepath and userpath, if set;
337 (3) the PYTHONPATH config macro, with the leading "."
338 of each component replaced with pythonhome, if set;
339 (4) the directory containing the executable (argv0_path).
340 The length calculation calculates #3 first.
343 /* Calculate size of return buffer */
344 if (pythonhome
!= NULL
) {
347 for (p
= PYTHONPATH
; *p
; p
++) {
349 bufsz
++; /* number of DELIM plus one */
351 bufsz
*= strlen(pythonhome
);
355 bufsz
+= strlen(PYTHONPATH
) + 1;
357 bufsz
+= strlen(envpath
) + 1;
358 bufsz
+= strlen(argv0_path
) + 1;
361 bufsz
+= strlen(machinepath
) + 1;
363 bufsz
+= strlen(userpath
) + 1;
366 module_search_path
= buf
= malloc(bufsz
);
368 /* We can't exit, so print a warning and limp along */
369 fprintf(stderr
, "Can't malloc dynamic PYTHONPATH.\n");
371 fprintf(stderr
, "Using default static $PYTHONPATH.\n");
372 module_search_path
= envpath
;
375 fprintf(stderr
, "Using environment $PYTHONPATH.\n");
376 module_search_path
= PYTHONPATH
;
382 strcpy(buf
, envpath
);
383 buf
= strchr(buf
, '\0');
388 strcpy(buf
, machinepath
);
389 buf
= strchr(buf
, '\0');
393 strcpy(buf
, userpath
);
394 buf
= strchr(buf
, '\0');
398 if (pythonhome
== NULL
) {
399 strcpy(buf
, PYTHONPATH
);
400 buf
= strchr(buf
, '\0');
403 char *p
= PYTHONPATH
;
407 q
= strchr(p
, DELIM
);
412 if (p
[0] == '.' && is_sep(p
[1])) {
413 strcpy(buf
, pythonhome
);
414 buf
= strchr(buf
, '\0');
428 strcpy(buf
, argv0_path
);
429 buf
= strchr(buf
, '\0');
435 /* External interface */
440 if (!module_search_path
)
443 return module_search_path
;
449 if (!module_search_path
)
456 Py_GetExecPrefix(void)
458 if (!module_search_path
)
465 Py_GetProgramFullPath(void)
467 if (!module_search_path
)