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 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 101 | /* assumes 'dir' null terminated in bounds. Never writes |
| 102 | beyond existing terminator. |
| 103 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 104 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 105 | reduce(char *dir) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 106 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 107 | size_t i = strlen(dir); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 108 | while (i > 0 && !is_sep(dir[i])) |
| 109 | --i; |
| 110 | dir[i] = '\0'; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 115 | exists(char *filename) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 116 | { |
| 117 | struct stat buf; |
| 118 | return stat(filename, &buf) == 0; |
| 119 | } |
| 120 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 121 | /* Assumes 'filename' MAXPATHLEN+1 bytes long - |
| 122 | may extend 'filename' by one character. |
| 123 | */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 124 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 125 | ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 126 | { |
| 127 | if (exists(filename)) |
| 128 | return 1; |
| 129 | |
| 130 | /* Check for the compiled version of prefix. */ |
| 131 | if (strlen(filename) < MAXPATHLEN) { |
| 132 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 133 | if (exists(filename)) |
| 134 | return 1; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 139 | /* guarantees buffer will never overflow MAXPATHLEN+1 bytes */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 140 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 141 | join(char *buffer, char *stuff) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 142 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 143 | size_t n, k; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 144 | if (is_sep(stuff[0])) |
| 145 | n = 0; |
| 146 | else { |
| 147 | n = strlen(buffer); |
| 148 | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
| 149 | buffer[n++] = SEP; |
| 150 | } |
| 151 | k = strlen(stuff); |
| 152 | if (n + k > MAXPATHLEN) |
| 153 | k = MAXPATHLEN - n; |
| 154 | strncpy(buffer+n, stuff, k); |
| 155 | buffer[n+k] = '\0'; |
| 156 | } |
| 157 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 158 | /* gotlandmark only called by search_for_prefix, which ensures |
| 159 | 'prefix' is null terminated in bounds. join() ensures |
| 160 | 'landmark' can not overflow prefix if too long. |
| 161 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 162 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 163 | gotlandmark(char *landmark) |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 164 | { |
| 165 | int n, ok; |
| 166 | |
| 167 | n = strlen(prefix); |
| 168 | join(prefix, landmark); |
| 169 | ok = ismodule(prefix); |
| 170 | prefix[n] = '\0'; |
| 171 | return ok; |
| 172 | } |
| 173 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 174 | /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. |
| 175 | assumption provided by only caller, calculate_path() */ |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 176 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 177 | search_for_prefix(char *argv0_path, char *landmark) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 178 | { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 179 | /* Search from argv0_path, until landmark is found */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 180 | strcpy(prefix, argv0_path); |
| 181 | do { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 182 | if (gotlandmark(landmark)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 183 | return 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 184 | reduce(prefix); |
| 185 | } while (prefix[0]); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 189 | #ifdef MS_WIN32 |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 191 | /* a string loaded from the DLL at startup.*/ |
| 192 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 194 | |
| 195 | /* Load a PYTHONPATH value from the registry. |
| 196 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 197 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 198 | Works in both Unicode and 8bit environments. Only uses the |
| 199 | Ex family of functions so it also works with Windows CE. |
| 200 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 201 | Returns NULL, or a pointer that should be freed. |
| 202 | */ |
| 203 | |
| 204 | static char * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 205 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 206 | { |
| 207 | HKEY newKey = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 208 | DWORD dataSize = 0; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 209 | DWORD numKeys = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 210 | LONG rc; |
| 211 | char *retval = NULL; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 212 | TCHAR *dataBuf = NULL; |
| 213 | static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\"); |
| 214 | static const TCHAR keySuffix[] = _T("\\PythonPath"); |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 215 | size_t versionLen; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 216 | DWORD index; |
| 217 | TCHAR *keyBuf = NULL; |
| 218 | TCHAR *keyBufPtr; |
| 219 | TCHAR **ppPaths = NULL; |
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 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 222 | versionLen = _tcslen(PyWin_DLLVersionString); |
| 223 | /* Space for all the chars, plus one \0 */ |
| 224 | keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) + |
| 225 | sizeof(TCHAR)*(versionLen-1) + |
| 226 | sizeof(keySuffix)); |
| 227 | if (keyBuf==NULL) goto done; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 228 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 229 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR)); |
| 230 | keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1; |
| 231 | memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR)); |
| 232 | keyBufPtr += versionLen; |
| 233 | /* NULL comes with this one! */ |
| 234 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 235 | /* Open the root Python key */ |
| 236 | rc=RegOpenKeyEx(keyBase, |
| 237 | keyBuf, /* subkey */ |
| 238 | 0, /* reserved */ |
| 239 | KEY_READ, |
| 240 | &newKey); |
| 241 | if (rc!=ERROR_SUCCESS) goto done; |
| 242 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 243 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 244 | NULL, NULL, &dataSize, NULL, NULL); |
| 245 | if (rc!=ERROR_SUCCESS) goto done; |
| 246 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 247 | /* Allocate a temp array of char buffers, so we only need to loop |
| 248 | reading the registry once |
| 249 | */ |
| 250 | ppPaths = malloc( sizeof(TCHAR *) * numKeys ); |
| 251 | if (ppPaths==NULL) goto done; |
| 252 | memset(ppPaths, 0, sizeof(TCHAR *) * numKeys); |
| 253 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 254 | for(index=0;index<numKeys;index++) { |
| 255 | TCHAR keyBuf[MAX_PATH+1]; |
| 256 | HKEY subKey = 0; |
| 257 | DWORD reqdSize = MAX_PATH+1; |
| 258 | /* Get the sub-key name */ |
| 259 | DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize, |
| 260 | NULL, NULL, NULL, NULL ); |
| 261 | if (rc!=ERROR_SUCCESS) goto done; |
| 262 | /* Open the sub-key */ |
| 263 | rc=RegOpenKeyEx(newKey, |
| 264 | keyBuf, /* subkey */ |
| 265 | 0, /* reserved */ |
| 266 | KEY_READ, |
| 267 | &subKey); |
| 268 | if (rc!=ERROR_SUCCESS) goto done; |
| 269 | /* Find the value of the buffer size, malloc, then read it */ |
| 270 | RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 271 | if (reqdSize) { |
| 272 | ppPaths[index] = malloc(reqdSize); |
| 273 | if (ppPaths[index]) { |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 274 | RegQueryValueEx(subKey, NULL, 0, NULL, |
| 275 | (LPBYTE)ppPaths[index], |
| 276 | &reqdSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 277 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 280 | RegCloseKey(subKey); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 281 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 282 | dataBuf = malloc((dataSize+1) * sizeof(TCHAR)); |
| 283 | if (dataBuf) { |
| 284 | TCHAR *szCur = dataBuf; |
| 285 | DWORD reqdSize = dataSize; |
| 286 | /* Copy our collected strings */ |
| 287 | for (index=0;index<numKeys;index++) { |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 288 | if (index > 0) { |
| 289 | *(szCur++) = _T(';'); |
| 290 | dataSize--; |
| 291 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 292 | if (ppPaths[index]) { |
| 293 | int len = _tcslen(ppPaths[index]); |
| 294 | _tcsncpy(szCur, ppPaths[index], len); |
| 295 | szCur += len; |
| 296 | dataSize -= len; |
| 297 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 298 | } |
| 299 | if (skipcore) |
| 300 | *szCur = '\0'; |
| 301 | else { |
| 302 | *(szCur++) = _T(';'); |
| 303 | dataSize--; |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 304 | /* Now append the core path entries - |
| 305 | this will include the NULL |
| 306 | */ |
| 307 | rc = RegQueryValueEx(newKey, NULL, 0, NULL, |
| 308 | (LPBYTE)szCur, &dataSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 309 | } |
| 310 | /* And set the result - caller must free |
| 311 | If MBCS, it is fine as is. If Unicode, allocate new |
| 312 | buffer and convert. |
| 313 | */ |
| 314 | #ifdef UNICODE |
| 315 | retval = (char *)malloc(reqdSize+1); |
| 316 | if (retval) |
| 317 | WideCharToMultiByte(CP_ACP, 0, |
| 318 | dataBuf, -1, /* source */ |
| 319 | retval, dataSize+1, /* dest */ |
| 320 | NULL, NULL); |
| 321 | free(dataBuf); |
| 322 | #else |
| 323 | retval = dataBuf; |
| 324 | #endif |
| 325 | } |
| 326 | done: |
| 327 | /* Loop freeing my temp buffers */ |
| 328 | if (ppPaths) { |
| 329 | for(index=0;index<numKeys;index++) |
| 330 | if (ppPaths[index]) free(ppPaths[index]); |
| 331 | free(ppPaths); |
| 332 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 333 | if (newKey) |
| 334 | RegCloseKey(newKey); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 335 | if (keyBuf) |
| 336 | free(keyBuf); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 337 | return retval; |
| 338 | } |
| 339 | #endif /* MS_WIN32 */ |
| 340 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 341 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 342 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 343 | { |
Thomas Wouters | a534594 | 2000-07-22 23:59:33 +0000 | [diff] [blame] | 344 | extern char *Py_GetProgramName(void); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 345 | char *path = getenv("PATH"); |
| 346 | char *prog = Py_GetProgramName(); |
| 347 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 348 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 349 | #ifdef UNICODE |
| 350 | WCHAR wprogpath[MAXPATHLEN+1]; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 351 | /* Windows documents that GetModuleFileName() will "truncate", |
| 352 | but makes no mention of the null terminator. Play it safe. |
| 353 | PLUS Windows itself defines MAX_PATH as the same, but anyway... |
| 354 | */ |
| 355 | wprogpath[MAXPATHLEN]=_T('\0')'; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 356 | if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 357 | WideCharToMultiByte(CP_ACP, 0, |
| 358 | wprogpath, -1, |
| 359 | progpath, MAXPATHLEN+1, |
| 360 | NULL, NULL); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | #else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 364 | /* static init of progpath ensures final char remains \0 */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 365 | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
| 366 | return; |
| 367 | #endif |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 368 | #endif |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 369 | if (prog == NULL || *prog == '\0') |
| 370 | prog = "python"; |
| 371 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 372 | /* If there is no slash in the argv0 path, then we have to |
| 373 | * assume python is on the user's $PATH, since there's no |
| 374 | * other way to find a directory to start the search from. If |
| 375 | * $PATH isn't exported, you lose. |
| 376 | */ |
| 377 | #ifdef ALTSEP |
| 378 | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
| 379 | #else |
| 380 | if (strchr(prog, SEP)) |
| 381 | #endif |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 382 | strncpy(progpath, prog, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 383 | else if (path) { |
| 384 | while (1) { |
| 385 | char *delim = strchr(path, DELIM); |
| 386 | |
| 387 | if (delim) { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 388 | size_t len = delim - path; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 389 | /* ensure we can't overwrite buffer */ |
| 390 | len = min(MAXPATHLEN,len); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 391 | strncpy(progpath, path, len); |
| 392 | *(progpath + len) = '\0'; |
| 393 | } |
| 394 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 395 | strncpy(progpath, path, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 396 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 397 | /* join() is safe for MAXPATHLEN+1 size buffer */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 398 | join(progpath, prog); |
| 399 | if (exists(progpath)) |
| 400 | break; |
| 401 | |
| 402 | if (!delim) { |
| 403 | progpath[0] = '\0'; |
| 404 | break; |
| 405 | } |
| 406 | path = delim + 1; |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 414 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 415 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 416 | char argv0_path[MAXPATHLEN+1]; |
| 417 | char *buf; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 418 | size_t bufsz; |
Guido van Rossum | 8b2b3ce | 1998-07-27 13:48:07 +0000 | [diff] [blame] | 419 | char *pythonhome = Py_GetPythonHome(); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 420 | char *envpath = getenv("PYTHONPATH"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 422 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 423 | int skiphome, skipdefault; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 424 | char *machinepath = NULL; |
| 425 | char *userpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 426 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 427 | |
| 428 | get_progpath(); |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 429 | /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 430 | strcpy(argv0_path, progpath); |
| 431 | reduce(argv0_path); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 432 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 433 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 434 | pythonhome = prefix; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 435 | else |
| 436 | pythonhome = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 437 | } |
| 438 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame^] | 439 | strncpy(prefix, pythonhome, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 440 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 441 | if (envpath && *envpath == '\0') |
| 442 | envpath = NULL; |
| 443 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 445 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 446 | skiphome = pythonhome==NULL ? 0 : 1; |
| 447 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 448 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 449 | /* We only use the default relative PYTHONPATH if we havent |
| 450 | anything better to use! */ |
| 451 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 452 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 453 | #endif |
| 454 | |
| 455 | /* We need to construct a path from the following parts. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 456 | (1) the PYTHONPATH environment variable, if set; |
| 457 | (2) for Win32, the machinepath and userpath, if set; |
| 458 | (3) the PYTHONPATH config macro, with the leading "." |
| 459 | of each component replaced with pythonhome, if set; |
| 460 | (4) the directory containing the executable (argv0_path). |
| 461 | The length calculation calculates #3 first. |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 462 | Extra rules: |
| 463 | - If PYTHONHOME is set (in any way) item (2) is ignored. |
| 464 | - If registry values are used, (3) and (4) are ignored. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 465 | */ |
| 466 | |
| 467 | /* Calculate size of return buffer */ |
| 468 | if (pythonhome != NULL) { |
| 469 | char *p; |
| 470 | bufsz = 1; |
| 471 | for (p = PYTHONPATH; *p; p++) { |
| 472 | if (*p == DELIM) |
| 473 | bufsz++; /* number of DELIM plus one */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 474 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 475 | bufsz *= strlen(pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 476 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 477 | else |
| 478 | bufsz = 0; |
Guido van Rossum | 691d2ad | 1997-12-11 02:32:43 +0000 | [diff] [blame] | 479 | bufsz += strlen(PYTHONPATH) + 1; |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 480 | bufsz += strlen(argv0_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 481 | #ifdef MS_WIN32 |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 482 | if (userpath) |
| 483 | bufsz += strlen(userpath) + 1; |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 484 | if (machinepath) |
| 485 | bufsz += strlen(machinepath) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 486 | #endif |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 487 | if (envpath != NULL) |
| 488 | bufsz += strlen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 489 | |
| 490 | module_search_path = buf = malloc(bufsz); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 491 | if (buf == NULL) { |
| 492 | /* We can't exit, so print a warning and limp along */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 493 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 494 | if (envpath) { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 495 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 496 | module_search_path = envpath; |
| 497 | } |
| 498 | else { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 499 | fprintf(stderr, "Using default static path.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 500 | module_search_path = PYTHONPATH; |
| 501 | } |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 502 | #ifdef MS_WIN32 |
| 503 | if (machinepath) |
| 504 | free(machinepath); |
| 505 | if (userpath) |
| 506 | free(userpath); |
| 507 | #endif /* MS_WIN32 */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 508 | return; |
| 509 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 510 | |
| 511 | if (envpath) { |
| 512 | strcpy(buf, envpath); |
| 513 | buf = strchr(buf, '\0'); |
| 514 | *buf++ = DELIM; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 516 | #ifdef MS_WIN32 |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 517 | if (userpath) { |
| 518 | strcpy(buf, userpath); |
| 519 | buf = strchr(buf, '\0'); |
| 520 | *buf++ = DELIM; |
| 521 | free(userpath); |
| 522 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 523 | if (machinepath) { |
| 524 | strcpy(buf, machinepath); |
| 525 | buf = strchr(buf, '\0'); |
| 526 | *buf++ = DELIM; |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 527 | free(machinepath); |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 528 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 529 | if (pythonhome == NULL) { |
| 530 | if (!skipdefault) { |
| 531 | strcpy(buf, PYTHONPATH); |
| 532 | buf = strchr(buf, '\0'); |
| 533 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 534 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 535 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 536 | if (pythonhome == NULL) { |
| 537 | strcpy(buf, PYTHONPATH); |
| 538 | buf = strchr(buf, '\0'); |
| 539 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 540 | #endif /* MS_WIN32 */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 541 | else { |
| 542 | char *p = PYTHONPATH; |
| 543 | char *q; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 544 | size_t n; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 545 | for (;;) { |
| 546 | q = strchr(p, DELIM); |
| 547 | if (q == NULL) |
| 548 | n = strlen(p); |
| 549 | else |
| 550 | n = q-p; |
| 551 | if (p[0] == '.' && is_sep(p[1])) { |
| 552 | strcpy(buf, pythonhome); |
| 553 | buf = strchr(buf, '\0'); |
| 554 | p++; |
| 555 | n--; |
| 556 | } |
| 557 | strncpy(buf, p, n); |
| 558 | buf += n; |
| 559 | if (q == NULL) |
| 560 | break; |
| 561 | *buf++ = DELIM; |
| 562 | p = q+1; |
| 563 | } |
| 564 | } |
| 565 | if (argv0_path) { |
| 566 | *buf++ = DELIM; |
| 567 | strcpy(buf, argv0_path); |
| 568 | buf = strchr(buf, '\0'); |
| 569 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 570 | *buf = '\0'; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /* External interface */ |
| 575 | |
| 576 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 577 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 578 | { |
| 579 | if (!module_search_path) |
| 580 | calculate_path(); |
| 581 | return module_search_path; |
| 582 | } |
| 583 | |
| 584 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 585 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 586 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 587 | if (!module_search_path) |
| 588 | calculate_path(); |
| 589 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 593 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 594 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 595 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 599 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 600 | { |
| 601 | if (!module_search_path) |
| 602 | calculate_path(); |
| 603 | return progpath; |
| 604 | } |