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 | |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 14 | * If the PYTHONPATH env. var. exists, its entries are added next. |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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 |
Mark Hammond | 19fdbfb | 2001-09-07 14:08:01 +0000 | [diff] [blame] | 47 | the registry is used. Other "application paths" in the registry are |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 60 | #ifdef MS_WINDOWS |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 65 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 66 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 67 | #endif /* HAVE_SYS_TYPES_H */ |
| 68 | |
| 69 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 70 | #include <sys/stat.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 71 | #endif /* HAVE_SYS_STAT_H */ |
| 72 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 73 | #include <string.h> |
| 74 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 75 | /* Search in some common locations for the associated Python libraries. |
| 76 | * |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 77 | * Py_GetPath() tries to return a sensible Python module search path. |
| 78 | * |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 79 | * The approach is an adaptation for Windows of the strategy used in |
| 80 | * ../Modules/getpath.c; it uses the Windows Registry as one of its |
| 81 | * information sources. |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
| 84 | #ifndef LANDMARK |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 85 | #define LANDMARK "lib\\os.py" |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | static char prefix[MAXPATHLEN+1]; |
| 89 | static char progpath[MAXPATHLEN+1]; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 90 | static char dllpath[MAXPATHLEN+1]; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 91 | static char *module_search_path = NULL; |
| 92 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 94 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 95 | is_sep(char ch) /* determine if "ch" is a separator character */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 96 | { |
| 97 | #ifdef ALTSEP |
| 98 | return ch == SEP || ch == ALTSEP; |
| 99 | #else |
| 100 | return ch == SEP; |
| 101 | #endif |
| 102 | } |
| 103 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 104 | /* assumes 'dir' null terminated in bounds. Never writes |
| 105 | beyond existing terminator. |
| 106 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 107 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 108 | reduce(char *dir) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 109 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 110 | size_t i = strlen(dir); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 111 | while (i > 0 && !is_sep(dir[i])) |
| 112 | --i; |
| 113 | dir[i] = '\0'; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 118 | exists(char *filename) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 119 | { |
| 120 | struct stat buf; |
| 121 | return stat(filename, &buf) == 0; |
| 122 | } |
| 123 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 124 | /* Assumes 'filename' MAXPATHLEN+1 bytes long - |
| 125 | may extend 'filename' by one character. |
| 126 | */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 127 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 128 | ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 129 | { |
| 130 | if (exists(filename)) |
| 131 | return 1; |
| 132 | |
| 133 | /* Check for the compiled version of prefix. */ |
| 134 | if (strlen(filename) < MAXPATHLEN) { |
| 135 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 136 | if (exists(filename)) |
| 137 | return 1; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | |
Tim Peters | 8484fbf | 2004-08-07 19:12:27 +0000 | [diff] [blame] | 142 | /* Add a path component, by appending stuff to buffer. |
| 143 | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
| 144 | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
| 145 | the trailing NUL). It's a fatal error if it contains a string longer than |
| 146 | that (callers must be careful!). If these requirements are met, it's |
| 147 | guaranteed that buffer will still be a NUL-terminated string with no more |
| 148 | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
| 149 | stuff as fits will be appended. |
| 150 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 151 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 152 | join(char *buffer, char *stuff) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 153 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 154 | size_t n, k; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 155 | if (is_sep(stuff[0])) |
| 156 | n = 0; |
| 157 | else { |
| 158 | n = strlen(buffer); |
| 159 | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
| 160 | buffer[n++] = SEP; |
| 161 | } |
Tim Peters | 8484fbf | 2004-08-07 19:12:27 +0000 | [diff] [blame] | 162 | if (n > MAXPATHLEN) |
| 163 | Py_FatalError("buffer overflow in getpathp.c's joinpath()"); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 164 | k = strlen(stuff); |
| 165 | if (n + k > MAXPATHLEN) |
| 166 | k = MAXPATHLEN - n; |
| 167 | strncpy(buffer+n, stuff, k); |
| 168 | buffer[n+k] = '\0'; |
| 169 | } |
| 170 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 171 | /* gotlandmark only called by search_for_prefix, which ensures |
| 172 | 'prefix' is null terminated in bounds. join() ensures |
| 173 | 'landmark' can not overflow prefix if too long. |
| 174 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 175 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 176 | gotlandmark(char *landmark) |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 177 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 178 | int ok; |
| 179 | Py_ssize_t n; |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 180 | |
| 181 | n = strlen(prefix); |
| 182 | join(prefix, landmark); |
| 183 | ok = ismodule(prefix); |
| 184 | prefix[n] = '\0'; |
| 185 | return ok; |
| 186 | } |
| 187 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 188 | /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. |
| 189 | assumption provided by only caller, calculate_path() */ |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 190 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 191 | search_for_prefix(char *argv0_path, char *landmark) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 192 | { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 193 | /* Search from argv0_path, until landmark is found */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 194 | strcpy(prefix, argv0_path); |
| 195 | do { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 196 | if (gotlandmark(landmark)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 197 | return 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 198 | reduce(prefix); |
| 199 | } while (prefix[0]); |
| 200 | return 0; |
| 201 | } |
| 202 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 203 | #ifdef MS_WINDOWS |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 205 | /* a string loaded from the DLL at startup.*/ |
| 206 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 208 | |
| 209 | /* Load a PYTHONPATH value from the registry. |
| 210 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 211 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 212 | Works in both Unicode and 8bit environments. Only uses the |
| 213 | Ex family of functions so it also works with Windows CE. |
| 214 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 215 | Returns NULL, or a pointer that should be freed. |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 216 | |
| 217 | XXX - this code is pretty strange, as it used to also |
| 218 | work on Win16, where the buffer sizes werent available |
| 219 | in advance. It could be simplied now Win16/Win32s is dead! |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 220 | */ |
| 221 | |
| 222 | static char * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 223 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 224 | { |
| 225 | HKEY newKey = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 226 | DWORD dataSize = 0; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 227 | DWORD numKeys = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 228 | LONG rc; |
| 229 | char *retval = NULL; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 230 | TCHAR *dataBuf = NULL; |
| 231 | static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\"); |
| 232 | static const TCHAR keySuffix[] = _T("\\PythonPath"); |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 233 | size_t versionLen; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 234 | DWORD index; |
| 235 | TCHAR *keyBuf = NULL; |
| 236 | TCHAR *keyBufPtr; |
| 237 | TCHAR **ppPaths = NULL; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 239 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 240 | versionLen = _tcslen(PyWin_DLLVersionString); |
| 241 | /* Space for all the chars, plus one \0 */ |
| 242 | keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) + |
| 243 | sizeof(TCHAR)*(versionLen-1) + |
| 244 | sizeof(keySuffix)); |
| 245 | if (keyBuf==NULL) goto done; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 247 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR)); |
| 248 | keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1; |
| 249 | memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR)); |
| 250 | keyBufPtr += versionLen; |
| 251 | /* NULL comes with this one! */ |
| 252 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 253 | /* Open the root Python key */ |
| 254 | rc=RegOpenKeyEx(keyBase, |
| 255 | keyBuf, /* subkey */ |
| 256 | 0, /* reserved */ |
| 257 | KEY_READ, |
| 258 | &newKey); |
| 259 | if (rc!=ERROR_SUCCESS) goto done; |
| 260 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 261 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 262 | NULL, NULL, &dataSize, NULL, NULL); |
| 263 | if (rc!=ERROR_SUCCESS) goto done; |
| 264 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 265 | /* Allocate a temp array of char buffers, so we only need to loop |
| 266 | reading the registry once |
| 267 | */ |
| 268 | ppPaths = malloc( sizeof(TCHAR *) * numKeys ); |
| 269 | if (ppPaths==NULL) goto done; |
| 270 | memset(ppPaths, 0, sizeof(TCHAR *) * numKeys); |
| 271 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 272 | for(index=0;index<numKeys;index++) { |
| 273 | TCHAR keyBuf[MAX_PATH+1]; |
| 274 | HKEY subKey = 0; |
| 275 | DWORD reqdSize = MAX_PATH+1; |
| 276 | /* Get the sub-key name */ |
| 277 | DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize, |
| 278 | NULL, NULL, NULL, NULL ); |
| 279 | if (rc!=ERROR_SUCCESS) goto done; |
| 280 | /* Open the sub-key */ |
| 281 | rc=RegOpenKeyEx(newKey, |
| 282 | keyBuf, /* subkey */ |
| 283 | 0, /* reserved */ |
| 284 | KEY_READ, |
| 285 | &subKey); |
| 286 | if (rc!=ERROR_SUCCESS) goto done; |
| 287 | /* Find the value of the buffer size, malloc, then read it */ |
| 288 | RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 289 | if (reqdSize) { |
| 290 | ppPaths[index] = malloc(reqdSize); |
| 291 | if (ppPaths[index]) { |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 292 | RegQueryValueEx(subKey, NULL, 0, NULL, |
| 293 | (LPBYTE)ppPaths[index], |
| 294 | &reqdSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 295 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 298 | RegCloseKey(subKey); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 299 | } |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 300 | /* original datasize from RegQueryInfo doesn't include the \0 */ |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 301 | dataBuf = malloc((dataSize+1) * sizeof(TCHAR)); |
| 302 | if (dataBuf) { |
| 303 | TCHAR *szCur = dataBuf; |
| 304 | DWORD reqdSize = dataSize; |
| 305 | /* Copy our collected strings */ |
| 306 | for (index=0;index<numKeys;index++) { |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 307 | if (index > 0) { |
| 308 | *(szCur++) = _T(';'); |
| 309 | dataSize--; |
| 310 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 311 | if (ppPaths[index]) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 312 | Py_ssize_t len = _tcslen(ppPaths[index]); |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 313 | _tcsncpy(szCur, ppPaths[index], len); |
| 314 | szCur += len; |
Tim Peters | c7f6cf6 | 2006-02-16 00:35:06 +0000 | [diff] [blame] | 315 | assert(dataSize > (DWORD)len); |
| 316 | dataSize -= (DWORD)len; |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 317 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 318 | } |
| 319 | if (skipcore) |
| 320 | *szCur = '\0'; |
| 321 | else { |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 322 | /* If we have no values, we dont need a ';' */ |
| 323 | if (numKeys) { |
| 324 | *(szCur++) = _T(';'); |
| 325 | dataSize--; |
| 326 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 327 | /* Now append the core path entries - |
| 328 | this will include the NULL |
| 329 | */ |
| 330 | rc = RegQueryValueEx(newKey, NULL, 0, NULL, |
| 331 | (LPBYTE)szCur, &dataSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 332 | } |
| 333 | /* And set the result - caller must free |
| 334 | If MBCS, it is fine as is. If Unicode, allocate new |
| 335 | buffer and convert. |
| 336 | */ |
| 337 | #ifdef UNICODE |
| 338 | retval = (char *)malloc(reqdSize+1); |
| 339 | if (retval) |
| 340 | WideCharToMultiByte(CP_ACP, 0, |
| 341 | dataBuf, -1, /* source */ |
Mark Hammond | 61bb35f | 2003-01-29 22:38:29 +0000 | [diff] [blame] | 342 | retval, reqdSize+1, /* dest */ |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 343 | NULL, NULL); |
| 344 | free(dataBuf); |
| 345 | #else |
| 346 | retval = dataBuf; |
| 347 | #endif |
| 348 | } |
| 349 | done: |
| 350 | /* Loop freeing my temp buffers */ |
| 351 | if (ppPaths) { |
| 352 | for(index=0;index<numKeys;index++) |
| 353 | if (ppPaths[index]) free(ppPaths[index]); |
| 354 | free(ppPaths); |
| 355 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 356 | if (newKey) |
| 357 | RegCloseKey(newKey); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 358 | if (keyBuf) |
| 359 | free(keyBuf); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 360 | return retval; |
| 361 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 362 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 364 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 365 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 366 | { |
Thomas Wouters | a534594 | 2000-07-22 23:59:33 +0000 | [diff] [blame] | 367 | extern char *Py_GetProgramName(void); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 368 | char *path = getenv("PATH"); |
| 369 | char *prog = Py_GetProgramName(); |
| 370 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 371 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 372 | extern HANDLE PyWin_DLLhModule; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 373 | #ifdef UNICODE |
| 374 | WCHAR wprogpath[MAXPATHLEN+1]; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 375 | /* Windows documents that GetModuleFileName() will "truncate", |
| 376 | but makes no mention of the null terminator. Play it safe. |
| 377 | PLUS Windows itself defines MAX_PATH as the same, but anyway... |
| 378 | */ |
Mark Hammond | 2795dae | 2002-07-22 13:28:21 +0000 | [diff] [blame] | 379 | wprogpath[MAXPATHLEN]=_T('\0'); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 380 | if (PyWin_DLLhModule && |
| 381 | GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) { |
| 382 | WideCharToMultiByte(CP_ACP, 0, |
| 383 | wprogpath, -1, |
| 384 | dllpath, MAXPATHLEN+1, |
| 385 | NULL, NULL); |
| 386 | } |
Thomas Heller | 6019f9a | 2003-08-18 17:53:33 +0000 | [diff] [blame] | 387 | wprogpath[MAXPATHLEN]=_T('\0'); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 388 | if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 389 | WideCharToMultiByte(CP_ACP, 0, |
| 390 | wprogpath, -1, |
| 391 | progpath, MAXPATHLEN+1, |
| 392 | NULL, NULL); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | #else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 396 | /* static init of progpath ensures final char remains \0 */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 397 | if (PyWin_DLLhModule) |
| 398 | if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN)) |
| 399 | dllpath[0] = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 400 | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
| 401 | return; |
| 402 | #endif |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 403 | #endif |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 404 | if (prog == NULL || *prog == '\0') |
| 405 | prog = "python"; |
| 406 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 407 | /* If there is no slash in the argv0 path, then we have to |
| 408 | * assume python is on the user's $PATH, since there's no |
| 409 | * other way to find a directory to start the search from. If |
| 410 | * $PATH isn't exported, you lose. |
| 411 | */ |
| 412 | #ifdef ALTSEP |
| 413 | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
| 414 | #else |
| 415 | if (strchr(prog, SEP)) |
| 416 | #endif |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 417 | strncpy(progpath, prog, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 418 | else if (path) { |
| 419 | while (1) { |
| 420 | char *delim = strchr(path, DELIM); |
| 421 | |
| 422 | if (delim) { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 423 | size_t len = delim - path; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 424 | /* ensure we can't overwrite buffer */ |
| 425 | len = min(MAXPATHLEN,len); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 426 | strncpy(progpath, path, len); |
| 427 | *(progpath + len) = '\0'; |
| 428 | } |
| 429 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 430 | strncpy(progpath, path, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 431 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 432 | /* join() is safe for MAXPATHLEN+1 size buffer */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 433 | join(progpath, prog); |
| 434 | if (exists(progpath)) |
| 435 | break; |
| 436 | |
| 437 | if (!delim) { |
| 438 | progpath[0] = '\0'; |
| 439 | break; |
| 440 | } |
| 441 | path = delim + 1; |
| 442 | } |
| 443 | } |
| 444 | else |
| 445 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 449 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 450 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 451 | char argv0_path[MAXPATHLEN+1]; |
| 452 | char *buf; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 453 | size_t bufsz; |
Guido van Rossum | 8b2b3ce | 1998-07-27 13:48:07 +0000 | [diff] [blame] | 454 | char *pythonhome = Py_GetPythonHome(); |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 455 | char *envpath = Py_GETENV("PYTHONPATH"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 456 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 457 | #ifdef MS_WINDOWS |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 458 | int skiphome, skipdefault; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 459 | char *machinepath = NULL; |
| 460 | char *userpath = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 461 | char zip_path[MAXPATHLEN+1]; |
| 462 | size_t len; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 463 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 464 | |
| 465 | get_progpath(); |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 466 | /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 467 | strcpy(argv0_path, progpath); |
| 468 | reduce(argv0_path); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 469 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 470 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 471 | pythonhome = prefix; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 472 | else |
| 473 | pythonhome = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 474 | } |
| 475 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 476 | strncpy(prefix, pythonhome, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 478 | if (envpath && *envpath == '\0') |
| 479 | envpath = NULL; |
| 480 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 481 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 482 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 483 | /* Calculate zip archive path */ |
| 484 | if (dllpath[0]) /* use name of python DLL */ |
| 485 | strncpy(zip_path, dllpath, MAXPATHLEN); |
| 486 | else /* use name of executable program */ |
| 487 | strncpy(zip_path, progpath, MAXPATHLEN); |
Neal Norwitz | a8aed02 | 2002-12-31 12:35:41 +0000 | [diff] [blame] | 488 | zip_path[MAXPATHLEN] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 489 | len = strlen(zip_path); |
| 490 | if (len > 4) { |
| 491 | zip_path[len-3] = 'z'; /* change ending to "zip" */ |
| 492 | zip_path[len-2] = 'i'; |
| 493 | zip_path[len-1] = 'p'; |
| 494 | } |
| 495 | else { |
| 496 | zip_path[0] = 0; |
| 497 | } |
| 498 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 499 | skiphome = pythonhome==NULL ? 0 : 1; |
| 500 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 501 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 502 | /* We only use the default relative PYTHONPATH if we havent |
| 503 | anything better to use! */ |
| 504 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 505 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 506 | #endif |
| 507 | |
| 508 | /* We need to construct a path from the following parts. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 509 | (1) the PYTHONPATH environment variable, if set; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 510 | (2) for Win32, the zip archive file path; |
| 511 | (3) for Win32, the machinepath and userpath, if set; |
| 512 | (4) the PYTHONPATH config macro, with the leading "." |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 513 | of each component replaced with pythonhome, if set; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 514 | (5) the directory containing the executable (argv0_path). |
| 515 | The length calculation calculates #4 first. |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 516 | Extra rules: |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 517 | - If PYTHONHOME is set (in any way) item (3) is ignored. |
| 518 | - If registry values are used, (4) and (5) are ignored. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 519 | */ |
| 520 | |
| 521 | /* Calculate size of return buffer */ |
| 522 | if (pythonhome != NULL) { |
| 523 | char *p; |
| 524 | bufsz = 1; |
| 525 | for (p = PYTHONPATH; *p; p++) { |
| 526 | if (*p == DELIM) |
| 527 | bufsz++; /* number of DELIM plus one */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 528 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 529 | bufsz *= strlen(pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 530 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 531 | else |
| 532 | bufsz = 0; |
Guido van Rossum | 691d2ad | 1997-12-11 02:32:43 +0000 | [diff] [blame] | 533 | bufsz += strlen(PYTHONPATH) + 1; |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 534 | bufsz += strlen(argv0_path) + 1; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 535 | #ifdef MS_WINDOWS |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 536 | if (userpath) |
| 537 | bufsz += strlen(userpath) + 1; |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 538 | if (machinepath) |
| 539 | bufsz += strlen(machinepath) + 1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 540 | bufsz += strlen(zip_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 541 | #endif |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 542 | if (envpath != NULL) |
| 543 | bufsz += strlen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 544 | |
| 545 | module_search_path = buf = malloc(bufsz); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 546 | if (buf == NULL) { |
| 547 | /* We can't exit, so print a warning and limp along */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 548 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 549 | if (envpath) { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 550 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 551 | module_search_path = envpath; |
| 552 | } |
| 553 | else { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 554 | fprintf(stderr, "Using default static path.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 555 | module_search_path = PYTHONPATH; |
| 556 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 557 | #ifdef MS_WINDOWS |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 558 | if (machinepath) |
| 559 | free(machinepath); |
| 560 | if (userpath) |
| 561 | free(userpath); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 562 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 565 | |
| 566 | if (envpath) { |
| 567 | strcpy(buf, envpath); |
| 568 | buf = strchr(buf, '\0'); |
| 569 | *buf++ = DELIM; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 570 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 571 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 572 | if (zip_path[0]) { |
| 573 | strcpy(buf, zip_path); |
| 574 | buf = strchr(buf, '\0'); |
| 575 | *buf++ = DELIM; |
| 576 | } |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 577 | if (userpath) { |
| 578 | strcpy(buf, userpath); |
| 579 | buf = strchr(buf, '\0'); |
| 580 | *buf++ = DELIM; |
| 581 | free(userpath); |
| 582 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 583 | if (machinepath) { |
| 584 | strcpy(buf, machinepath); |
| 585 | buf = strchr(buf, '\0'); |
| 586 | *buf++ = DELIM; |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 587 | free(machinepath); |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 588 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 589 | if (pythonhome == NULL) { |
| 590 | if (!skipdefault) { |
| 591 | strcpy(buf, PYTHONPATH); |
| 592 | buf = strchr(buf, '\0'); |
| 593 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 594 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 595 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 596 | if (pythonhome == NULL) { |
| 597 | strcpy(buf, PYTHONPATH); |
| 598 | buf = strchr(buf, '\0'); |
| 599 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 600 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 601 | else { |
| 602 | char *p = PYTHONPATH; |
| 603 | char *q; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 604 | size_t n; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 605 | for (;;) { |
| 606 | q = strchr(p, DELIM); |
| 607 | if (q == NULL) |
| 608 | n = strlen(p); |
| 609 | else |
| 610 | n = q-p; |
| 611 | if (p[0] == '.' && is_sep(p[1])) { |
| 612 | strcpy(buf, pythonhome); |
| 613 | buf = strchr(buf, '\0'); |
| 614 | p++; |
| 615 | n--; |
| 616 | } |
| 617 | strncpy(buf, p, n); |
| 618 | buf += n; |
| 619 | if (q == NULL) |
| 620 | break; |
| 621 | *buf++ = DELIM; |
| 622 | p = q+1; |
| 623 | } |
| 624 | } |
| 625 | if (argv0_path) { |
| 626 | *buf++ = DELIM; |
| 627 | strcpy(buf, argv0_path); |
| 628 | buf = strchr(buf, '\0'); |
| 629 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 630 | *buf = '\0'; |
Mark Hammond | 19fdbfb | 2001-09-07 14:08:01 +0000 | [diff] [blame] | 631 | /* Now to pull one last hack/trick. If sys.prefix is |
| 632 | empty, then try and find it somewhere on the paths |
| 633 | we calculated. We scan backwards, as our general policy |
| 634 | is that Python core directories are at the *end* of |
| 635 | sys.path. We assume that our "lib" directory is |
| 636 | on the path, and that our 'prefix' directory is |
| 637 | the parent of that. |
| 638 | */ |
| 639 | if (*prefix=='\0') { |
| 640 | char lookBuf[MAXPATHLEN+1]; |
| 641 | char *look = buf - 1; /* 'buf' is at the end of the buffer */ |
| 642 | while (1) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 643 | Py_ssize_t nchars; |
Mark Hammond | 19fdbfb | 2001-09-07 14:08:01 +0000 | [diff] [blame] | 644 | char *lookEnd = look; |
| 645 | /* 'look' will end up one character before the |
| 646 | start of the path in question - even if this |
| 647 | is one character before the start of the buffer |
| 648 | */ |
| 649 | while (*look != DELIM && look >= module_search_path) |
| 650 | look--; |
| 651 | nchars = lookEnd-look; |
| 652 | strncpy(lookBuf, look+1, nchars); |
| 653 | lookBuf[nchars] = '\0'; |
| 654 | /* Up one level to the parent */ |
| 655 | reduce(lookBuf); |
| 656 | if (search_for_prefix(lookBuf, LANDMARK)) { |
| 657 | break; |
| 658 | } |
| 659 | /* If we are out of paths to search - give up */ |
| 660 | if (look < module_search_path) |
| 661 | break; |
| 662 | look--; |
| 663 | } |
| 664 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | |
| 668 | /* External interface */ |
| 669 | |
| 670 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 671 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 672 | { |
| 673 | if (!module_search_path) |
| 674 | calculate_path(); |
| 675 | return module_search_path; |
| 676 | } |
| 677 | |
| 678 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 679 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 680 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 681 | if (!module_search_path) |
| 682 | calculate_path(); |
| 683 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 687 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 688 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 689 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 693 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 694 | { |
| 695 | if (!module_search_path) |
| 696 | calculate_path(); |
| 697 | return progpath; |
| 698 | } |