Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 1 | |
| 2 | /* Return the initial module search path. */ |
Guido van Rossum | c499572 | 1998-08-08 20:05:31 +0000 | [diff] [blame] | 3 | /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 5 | /* ---------------------------------------------------------------- |
| 6 | PATH RULES FOR WINDOWS: |
| 7 | This describes how sys.path is formed on Windows. It describes the |
| 8 | functionality, not the implementation (ie, the order in which these |
| 9 | are actually fetched is different) |
| 10 | |
| 11 | * Python always adds an empty entry at the start, which corresponds |
| 12 | to the current directory. |
| 13 | |
| 14 | * If the PYTHONPATH env. var. exists, it's entries are added next. |
| 15 | |
| 16 | * We look in the registry for "application paths" - that is, sub-keys |
| 17 | under the main PythonPath registry key. These are added next (the |
| 18 | order of sub-key processing is undefined). |
| 19 | HKEY_CURRENT_USER is searched and added first. |
| 20 | HKEY_LOCAL_MACHINE is searched and added next. |
| 21 | (Note that all known installers only use HKLM, so HKCU is typically |
| 22 | empty) |
| 23 | |
| 24 | * We attempt to locate the "Python Home" - if the PYTHONHOME env var |
| 25 | is set, we believe it. Otherwise, we use the path of our host .EXE's |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 26 | to try and locate our "landmark" (lib\\os.py) and deduce our home. |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 27 | - If we DO have a Python Home: The relevant sub-directories (Lib, |
| 28 | plat-win, lib-tk, etc) are based on the Python Home |
| 29 | - If we DO NOT have a Python Home, the core Python Path is |
| 30 | loaded from the registry. This is the main PythonPath key, |
| 31 | and both HKLM and HKCU are combined to form the path) |
| 32 | |
| 33 | * Iff - we can not locate the Python Home, have not had a PYTHONPATH |
| 34 | specified, and can't locate any Registry entries (ie, we have _nothing_ |
| 35 | we can assume is a good path), a default path with relative entries is |
| 36 | used (eg. .\Lib;.\plat-win, etc) |
| 37 | |
| 38 | |
| 39 | The end result of all this is: |
| 40 | * When running python.exe, or any other .exe in the main Python directory |
| 41 | (either an installed version, or directly from the PCbuild directory), |
| 42 | the core path is deduced, and the core paths in the registry are |
| 43 | ignored. Other "application paths" in the registry are always read. |
| 44 | |
| 45 | * When Python is hosted in another exe (different directory, embedded via |
| 46 | COM, etc), the Python Home will not be deduced, so the core path from |
| 47 | the registry is used. Other "application paths "in the registry are |
| 48 | always read. |
| 49 | |
| 50 | * If Python can't find its home and there is no registry (eg, frozen |
| 51 | exe, some very strange installation setup) you get a path with |
| 52 | some default, but relative, paths. |
| 53 | |
| 54 | ---------------------------------------------------------------- */ |
| 55 | |
| 56 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 57 | #include "Python.h" |
| 58 | #include "osdefs.h" |
| 59 | |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 60 | #ifdef MS_WIN32 |
| 61 | #include <windows.h> |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 62 | #include <tchar.h> |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 65 | #include <sys/types.h> |
| 66 | #include <sys/stat.h> |
| 67 | #include <string.h> |
| 68 | |
| 69 | #if HAVE_UNISTD_H |
| 70 | #include <unistd.h> |
| 71 | #endif /* HAVE_UNISTD_H */ |
| 72 | |
| 73 | /* Search in some common locations for the associated Python libraries. |
| 74 | * |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 75 | * Py_GetPath() tries to return a sensible Python module search path. |
| 76 | * |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 77 | * The approach is an adaptation for Windows of the strategy used in |
| 78 | * ../Modules/getpath.c; it uses the Windows Registry as one of its |
| 79 | * information sources. |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 80 | */ |
| 81 | |
| 82 | #ifndef LANDMARK |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 83 | #define LANDMARK "lib\\os.py" |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | static char prefix[MAXPATHLEN+1]; |
| 87 | static char progpath[MAXPATHLEN+1]; |
| 88 | static char *module_search_path = NULL; |
| 89 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 91 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 92 | is_sep(char ch) /* determine if "ch" is a separator character */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 93 | { |
| 94 | #ifdef ALTSEP |
| 95 | return ch == SEP || ch == ALTSEP; |
| 96 | #else |
| 97 | return ch == SEP; |
| 98 | #endif |
| 99 | } |
| 100 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 101 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 102 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 103 | reduce(char *dir) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 104 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 105 | size_t i = strlen(dir); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 106 | while (i > 0 && !is_sep(dir[i])) |
| 107 | --i; |
| 108 | dir[i] = '\0'; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 113 | exists(char *filename) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 114 | { |
| 115 | struct stat buf; |
| 116 | return stat(filename, &buf) == 0; |
| 117 | } |
| 118 | |
| 119 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 120 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 121 | ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 122 | { |
| 123 | if (exists(filename)) |
| 124 | return 1; |
| 125 | |
| 126 | /* Check for the compiled version of prefix. */ |
| 127 | if (strlen(filename) < MAXPATHLEN) { |
| 128 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 129 | if (exists(filename)) |
| 130 | return 1; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 136 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 137 | join(char *buffer, char *stuff) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 138 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 139 | size_t n, k; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 140 | if (is_sep(stuff[0])) |
| 141 | n = 0; |
| 142 | else { |
| 143 | n = strlen(buffer); |
| 144 | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
| 145 | buffer[n++] = SEP; |
| 146 | } |
| 147 | k = strlen(stuff); |
| 148 | if (n + k > MAXPATHLEN) |
| 149 | k = MAXPATHLEN - n; |
| 150 | strncpy(buffer+n, stuff, k); |
| 151 | buffer[n+k] = '\0'; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 156 | gotlandmark(char *landmark) |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 157 | { |
| 158 | int n, ok; |
| 159 | |
| 160 | n = strlen(prefix); |
| 161 | join(prefix, landmark); |
| 162 | ok = ismodule(prefix); |
| 163 | prefix[n] = '\0'; |
| 164 | return ok; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 169 | search_for_prefix(char *argv0_path, char *landmark) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 170 | { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 171 | /* Search from argv0_path, until landmark is found */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 172 | strcpy(prefix, argv0_path); |
| 173 | do { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 174 | if (gotlandmark(landmark)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 175 | return 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 176 | reduce(prefix); |
| 177 | } while (prefix[0]); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 181 | #ifdef MS_WIN32 |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 183 | /* a string loaded from the DLL at startup.*/ |
| 184 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 186 | |
| 187 | /* Load a PYTHONPATH value from the registry. |
| 188 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 189 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 190 | Works in both Unicode and 8bit environments. Only uses the |
| 191 | Ex family of functions so it also works with Windows CE. |
| 192 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 193 | Returns NULL, or a pointer that should be freed. |
| 194 | */ |
| 195 | |
| 196 | static char * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 197 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 198 | { |
| 199 | HKEY newKey = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 200 | DWORD dataSize = 0; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 201 | DWORD numKeys = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 202 | LONG rc; |
| 203 | char *retval = NULL; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 204 | TCHAR *dataBuf = NULL; |
| 205 | static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\"); |
| 206 | static const TCHAR keySuffix[] = _T("\\PythonPath"); |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 207 | size_t versionLen; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 208 | DWORD index; |
| 209 | TCHAR *keyBuf = NULL; |
| 210 | TCHAR *keyBufPtr; |
| 211 | TCHAR **ppPaths = NULL; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 213 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 214 | versionLen = _tcslen(PyWin_DLLVersionString); |
| 215 | /* Space for all the chars, plus one \0 */ |
| 216 | keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) + |
| 217 | sizeof(TCHAR)*(versionLen-1) + |
| 218 | sizeof(keySuffix)); |
| 219 | if (keyBuf==NULL) goto done; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 221 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR)); |
| 222 | keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1; |
| 223 | memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR)); |
| 224 | keyBufPtr += versionLen; |
| 225 | /* NULL comes with this one! */ |
| 226 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 227 | /* Open the root Python key */ |
| 228 | rc=RegOpenKeyEx(keyBase, |
| 229 | keyBuf, /* subkey */ |
| 230 | 0, /* reserved */ |
| 231 | KEY_READ, |
| 232 | &newKey); |
| 233 | if (rc!=ERROR_SUCCESS) goto done; |
| 234 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 235 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 236 | NULL, NULL, &dataSize, NULL, NULL); |
| 237 | if (rc!=ERROR_SUCCESS) goto done; |
| 238 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 239 | /* Allocate a temp array of char buffers, so we only need to loop |
| 240 | reading the registry once |
| 241 | */ |
| 242 | ppPaths = malloc( sizeof(TCHAR *) * numKeys ); |
| 243 | if (ppPaths==NULL) goto done; |
| 244 | memset(ppPaths, 0, sizeof(TCHAR *) * numKeys); |
| 245 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 246 | for(index=0;index<numKeys;index++) { |
| 247 | TCHAR keyBuf[MAX_PATH+1]; |
| 248 | HKEY subKey = 0; |
| 249 | DWORD reqdSize = MAX_PATH+1; |
| 250 | /* Get the sub-key name */ |
| 251 | DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize, |
| 252 | NULL, NULL, NULL, NULL ); |
| 253 | if (rc!=ERROR_SUCCESS) goto done; |
| 254 | /* Open the sub-key */ |
| 255 | rc=RegOpenKeyEx(newKey, |
| 256 | keyBuf, /* subkey */ |
| 257 | 0, /* reserved */ |
| 258 | KEY_READ, |
| 259 | &subKey); |
| 260 | if (rc!=ERROR_SUCCESS) goto done; |
| 261 | /* Find the value of the buffer size, malloc, then read it */ |
| 262 | RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 263 | if (reqdSize) { |
| 264 | ppPaths[index] = malloc(reqdSize); |
| 265 | if (ppPaths[index]) { |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 266 | RegQueryValueEx(subKey, NULL, 0, NULL, |
| 267 | (LPBYTE)ppPaths[index], |
| 268 | &reqdSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 269 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 272 | RegCloseKey(subKey); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 273 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 274 | dataBuf = malloc((dataSize+1) * sizeof(TCHAR)); |
| 275 | if (dataBuf) { |
| 276 | TCHAR *szCur = dataBuf; |
| 277 | DWORD reqdSize = dataSize; |
| 278 | /* Copy our collected strings */ |
| 279 | for (index=0;index<numKeys;index++) { |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 280 | if (index > 0) { |
| 281 | *(szCur++) = _T(';'); |
| 282 | dataSize--; |
| 283 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 284 | if (ppPaths[index]) { |
| 285 | int len = _tcslen(ppPaths[index]); |
| 286 | _tcsncpy(szCur, ppPaths[index], len); |
| 287 | szCur += len; |
| 288 | dataSize -= len; |
| 289 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 290 | } |
| 291 | if (skipcore) |
| 292 | *szCur = '\0'; |
| 293 | else { |
| 294 | *(szCur++) = _T(';'); |
| 295 | dataSize--; |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 296 | /* Now append the core path entries - |
| 297 | this will include the NULL |
| 298 | */ |
| 299 | rc = RegQueryValueEx(newKey, NULL, 0, NULL, |
| 300 | (LPBYTE)szCur, &dataSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 301 | } |
| 302 | /* And set the result - caller must free |
| 303 | If MBCS, it is fine as is. If Unicode, allocate new |
| 304 | buffer and convert. |
| 305 | */ |
| 306 | #ifdef UNICODE |
| 307 | retval = (char *)malloc(reqdSize+1); |
| 308 | if (retval) |
| 309 | WideCharToMultiByte(CP_ACP, 0, |
| 310 | dataBuf, -1, /* source */ |
| 311 | retval, dataSize+1, /* dest */ |
| 312 | NULL, NULL); |
| 313 | free(dataBuf); |
| 314 | #else |
| 315 | retval = dataBuf; |
| 316 | #endif |
| 317 | } |
| 318 | done: |
| 319 | /* Loop freeing my temp buffers */ |
| 320 | if (ppPaths) { |
| 321 | for(index=0;index<numKeys;index++) |
| 322 | if (ppPaths[index]) free(ppPaths[index]); |
| 323 | free(ppPaths); |
| 324 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 325 | if (newKey) |
| 326 | RegCloseKey(newKey); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 327 | if (keyBuf) |
| 328 | free(keyBuf); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 329 | return retval; |
| 330 | } |
| 331 | #endif /* MS_WIN32 */ |
| 332 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 333 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 334 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 335 | { |
Thomas Wouters | a534594 | 2000-07-22 23:59:33 +0000 | [diff] [blame] | 336 | extern char *Py_GetProgramName(void); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 337 | char *path = getenv("PATH"); |
| 338 | char *prog = Py_GetProgramName(); |
| 339 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 340 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 341 | #ifdef UNICODE |
| 342 | WCHAR wprogpath[MAXPATHLEN+1]; |
| 343 | if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { |
| 344 | WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL); |
| 345 | return; |
| 346 | } |
| 347 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 348 | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
| 349 | return; |
| 350 | #endif |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 351 | #endif |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 352 | if (prog == NULL || *prog == '\0') |
| 353 | prog = "python"; |
| 354 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 355 | /* If there is no slash in the argv0 path, then we have to |
| 356 | * assume python is on the user's $PATH, since there's no |
| 357 | * other way to find a directory to start the search from. If |
| 358 | * $PATH isn't exported, you lose. |
| 359 | */ |
| 360 | #ifdef ALTSEP |
| 361 | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
| 362 | #else |
| 363 | if (strchr(prog, SEP)) |
| 364 | #endif |
| 365 | strcpy(progpath, prog); |
| 366 | else if (path) { |
| 367 | while (1) { |
| 368 | char *delim = strchr(path, DELIM); |
| 369 | |
| 370 | if (delim) { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 371 | size_t len = delim - path; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 372 | strncpy(progpath, path, len); |
| 373 | *(progpath + len) = '\0'; |
| 374 | } |
| 375 | else |
| 376 | strcpy(progpath, path); |
| 377 | |
| 378 | join(progpath, prog); |
| 379 | if (exists(progpath)) |
| 380 | break; |
| 381 | |
| 382 | if (!delim) { |
| 383 | progpath[0] = '\0'; |
| 384 | break; |
| 385 | } |
| 386 | path = delim + 1; |
| 387 | } |
| 388 | } |
| 389 | else |
| 390 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 394 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 395 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 396 | char argv0_path[MAXPATHLEN+1]; |
| 397 | char *buf; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 398 | size_t bufsz; |
Guido van Rossum | 8b2b3ce | 1998-07-27 13:48:07 +0000 | [diff] [blame] | 399 | char *pythonhome = Py_GetPythonHome(); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 400 | char *envpath = getenv("PYTHONPATH"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 402 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 403 | int skiphome, skipdefault; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 404 | char *machinepath = NULL; |
| 405 | char *userpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 406 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 407 | |
| 408 | get_progpath(); |
| 409 | strcpy(argv0_path, progpath); |
| 410 | reduce(argv0_path); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 411 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 412 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 413 | pythonhome = prefix; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 414 | else |
| 415 | pythonhome = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | strcpy(prefix, pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 420 | if (envpath && *envpath == '\0') |
| 421 | envpath = NULL; |
| 422 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 424 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 425 | skiphome = pythonhome==NULL ? 0 : 1; |
| 426 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 427 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 428 | /* We only use the default relative PYTHONPATH if we havent |
| 429 | anything better to use! */ |
| 430 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 431 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 432 | #endif |
| 433 | |
| 434 | /* We need to construct a path from the following parts. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 435 | (1) the PYTHONPATH environment variable, if set; |
| 436 | (2) for Win32, the machinepath and userpath, if set; |
| 437 | (3) the PYTHONPATH config macro, with the leading "." |
| 438 | of each component replaced with pythonhome, if set; |
| 439 | (4) the directory containing the executable (argv0_path). |
| 440 | The length calculation calculates #3 first. |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 441 | Extra rules: |
| 442 | - If PYTHONHOME is set (in any way) item (2) is ignored. |
| 443 | - If registry values are used, (3) and (4) are ignored. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 444 | */ |
| 445 | |
| 446 | /* Calculate size of return buffer */ |
| 447 | if (pythonhome != NULL) { |
| 448 | char *p; |
| 449 | bufsz = 1; |
| 450 | for (p = PYTHONPATH; *p; p++) { |
| 451 | if (*p == DELIM) |
| 452 | bufsz++; /* number of DELIM plus one */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 453 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 454 | bufsz *= strlen(pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 455 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 456 | else |
| 457 | bufsz = 0; |
Guido van Rossum | 691d2ad | 1997-12-11 02:32:43 +0000 | [diff] [blame] | 458 | bufsz += strlen(PYTHONPATH) + 1; |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 459 | bufsz += strlen(argv0_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 460 | #ifdef MS_WIN32 |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 461 | if (userpath) |
| 462 | bufsz += strlen(userpath) + 1; |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 463 | if (machinepath) |
| 464 | bufsz += strlen(machinepath) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 465 | #endif |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 466 | if (envpath != NULL) |
| 467 | bufsz += strlen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 468 | |
| 469 | module_search_path = buf = malloc(bufsz); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 470 | if (buf == NULL) { |
| 471 | /* We can't exit, so print a warning and limp along */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 472 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 473 | if (envpath) { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 474 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 475 | module_search_path = envpath; |
| 476 | } |
| 477 | else { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 478 | fprintf(stderr, "Using default static path.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 479 | module_search_path = PYTHONPATH; |
| 480 | } |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 481 | #ifdef MS_WIN32 |
| 482 | if (machinepath) |
| 483 | free(machinepath); |
| 484 | if (userpath) |
| 485 | free(userpath); |
| 486 | #endif /* MS_WIN32 */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 487 | return; |
| 488 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 489 | |
| 490 | if (envpath) { |
| 491 | strcpy(buf, envpath); |
| 492 | buf = strchr(buf, '\0'); |
| 493 | *buf++ = DELIM; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 494 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 495 | #ifdef MS_WIN32 |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 496 | if (userpath) { |
| 497 | strcpy(buf, userpath); |
| 498 | buf = strchr(buf, '\0'); |
| 499 | *buf++ = DELIM; |
| 500 | free(userpath); |
| 501 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 502 | if (machinepath) { |
| 503 | strcpy(buf, machinepath); |
| 504 | buf = strchr(buf, '\0'); |
| 505 | *buf++ = DELIM; |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 506 | free(machinepath); |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 507 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 508 | if (pythonhome == NULL) { |
| 509 | if (!skipdefault) { |
| 510 | strcpy(buf, PYTHONPATH); |
| 511 | buf = strchr(buf, '\0'); |
| 512 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 513 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 514 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 515 | if (pythonhome == NULL) { |
| 516 | strcpy(buf, PYTHONPATH); |
| 517 | buf = strchr(buf, '\0'); |
| 518 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 519 | #endif /* MS_WIN32 */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 520 | else { |
| 521 | char *p = PYTHONPATH; |
| 522 | char *q; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 523 | size_t n; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 524 | for (;;) { |
| 525 | q = strchr(p, DELIM); |
| 526 | if (q == NULL) |
| 527 | n = strlen(p); |
| 528 | else |
| 529 | n = q-p; |
| 530 | if (p[0] == '.' && is_sep(p[1])) { |
| 531 | strcpy(buf, pythonhome); |
| 532 | buf = strchr(buf, '\0'); |
| 533 | p++; |
| 534 | n--; |
| 535 | } |
| 536 | strncpy(buf, p, n); |
| 537 | buf += n; |
| 538 | if (q == NULL) |
| 539 | break; |
| 540 | *buf++ = DELIM; |
| 541 | p = q+1; |
| 542 | } |
| 543 | } |
| 544 | if (argv0_path) { |
| 545 | *buf++ = DELIM; |
| 546 | strcpy(buf, argv0_path); |
| 547 | buf = strchr(buf, '\0'); |
| 548 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 549 | *buf = '\0'; |
| 550 | } |
| 551 | |
| 552 | |
| 553 | /* External interface */ |
| 554 | |
| 555 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 556 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 557 | { |
| 558 | if (!module_search_path) |
| 559 | calculate_path(); |
| 560 | return module_search_path; |
| 561 | } |
| 562 | |
| 563 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 564 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 565 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 566 | if (!module_search_path) |
| 567 | calculate_path(); |
| 568 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 572 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 573 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 574 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 578 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 579 | { |
| 580 | if (!module_search_path) |
| 581 | calculate_path(); |
| 582 | return progpath; |
| 583 | } |