| 1 | n/a | |
|---|
| 2 | n/a | /* Return the initial module search path. */ |
|---|
| 3 | n/a | /* Used by DOS, OS/2, Windows 3.1. Works on NT too. */ |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include "Python.h" |
|---|
| 6 | n/a | #include "osdefs.h" |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | #ifdef MS_WIN32 |
|---|
| 9 | n/a | #include <windows.h> |
|---|
| 10 | n/a | extern BOOL PyWin_IsWin32s(void); |
|---|
| 11 | n/a | #endif |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | #include <sys/types.h> |
|---|
| 14 | n/a | #include <sys/stat.h> |
|---|
| 15 | n/a | #include <string.h> |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | #if HAVE_UNISTD_H |
|---|
| 18 | n/a | #include <unistd.h> |
|---|
| 19 | n/a | #endif /* HAVE_UNISTD_H */ |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | /* Search in some common locations for the associated Python libraries. |
|---|
| 22 | n/a | * |
|---|
| 23 | n/a | * Two directories must be found, the platform independent directory |
|---|
| 24 | n/a | * (prefix), containing the common .py and .pyc files, and the platform |
|---|
| 25 | n/a | * dependent directory (exec_prefix), containing the shared library |
|---|
| 26 | n/a | * modules. Note that prefix and exec_prefix can be the same directory, |
|---|
| 27 | n/a | * but for some installations, they are different. |
|---|
| 28 | n/a | * |
|---|
| 29 | n/a | * Py_GetPath() tries to return a sensible Python module search path. |
|---|
| 30 | n/a | * |
|---|
| 31 | n/a | * First, we look to see if the executable is in a subdirectory of |
|---|
| 32 | n/a | * the Python build directory. We calculate the full path of the |
|---|
| 33 | n/a | * directory containing the executable as progpath. We work backwards |
|---|
| 34 | n/a | * along progpath and look for $dir/Modules/Setup.in, a distinctive |
|---|
| 35 | n/a | * landmark. If found, we use $dir/Lib as $root. The returned |
|---|
| 36 | n/a | * Python path is the compiled #define PYTHONPATH with all the initial |
|---|
| 37 | n/a | * "./lib" replaced by $root. |
|---|
| 38 | n/a | * |
|---|
| 39 | n/a | * Otherwise, if there is a PYTHONPATH environment variable, we return that. |
|---|
| 40 | n/a | * |
|---|
| 41 | n/a | * Otherwise we try to find $progpath/lib/os.py, and if found, then |
|---|
| 42 | n/a | * root is $progpath/lib, and we return Python path as compiled PYTHONPATH |
|---|
| 43 | n/a | * with all "./lib" replaced by $root (as above). |
|---|
| 44 | n/a | * |
|---|
| 45 | n/a | */ |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | #ifndef LANDMARK |
|---|
| 48 | n/a | #define LANDMARK "lib\\os.py" |
|---|
| 49 | n/a | #endif |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | static char prefix[MAXPATHLEN+1]; |
|---|
| 52 | n/a | static char exec_prefix[MAXPATHLEN+1]; |
|---|
| 53 | n/a | static char progpath[MAXPATHLEN+1]; |
|---|
| 54 | n/a | static char *module_search_path = NULL; |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | static int |
|---|
| 58 | n/a | is_sep(char ch) /* determine if "ch" is a separator character */ |
|---|
| 59 | n/a | { |
|---|
| 60 | n/a | #ifdef ALTSEP |
|---|
| 61 | n/a | return ch == SEP || ch == ALTSEP; |
|---|
| 62 | n/a | #else |
|---|
| 63 | n/a | return ch == SEP; |
|---|
| 64 | n/a | #endif |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | static void |
|---|
| 69 | n/a | reduce(char *dir) |
|---|
| 70 | n/a | { |
|---|
| 71 | n/a | int i = strlen(dir); |
|---|
| 72 | n/a | while (i > 0 && !is_sep(dir[i])) |
|---|
| 73 | n/a | --i; |
|---|
| 74 | n/a | dir[i] = '\0'; |
|---|
| 75 | n/a | } |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | static int |
|---|
| 79 | n/a | exists(char *filename) |
|---|
| 80 | n/a | { |
|---|
| 81 | n/a | struct stat buf; |
|---|
| 82 | n/a | return stat(filename, &buf) == 0; |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | /* Add a path component, by appending stuff to buffer. |
|---|
| 87 | n/a | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
|---|
| 88 | n/a | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
|---|
| 89 | n/a | the trailing NUL). It's a fatal error if it contains a string longer than |
|---|
| 90 | n/a | that (callers must be careful!). If these requirements are met, it's |
|---|
| 91 | n/a | guaranteed that buffer will still be a NUL-terminated string with no more |
|---|
| 92 | n/a | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
|---|
| 93 | n/a | stuff as fits will be appended. |
|---|
| 94 | n/a | */ |
|---|
| 95 | n/a | static void |
|---|
| 96 | n/a | join(char *buffer, char *stuff) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | int n, k; |
|---|
| 99 | n/a | if (is_sep(stuff[0])) |
|---|
| 100 | n/a | n = 0; |
|---|
| 101 | n/a | else { |
|---|
| 102 | n/a | n = strlen(buffer); |
|---|
| 103 | n/a | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
|---|
| 104 | n/a | buffer[n++] = SEP; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | if (n > MAXPATHLEN) |
|---|
| 107 | n/a | Py_FatalError("buffer overflow in getpathp.c's joinpath()"); |
|---|
| 108 | n/a | k = strlen(stuff); |
|---|
| 109 | n/a | if (n + k > MAXPATHLEN) |
|---|
| 110 | n/a | k = MAXPATHLEN - n; |
|---|
| 111 | n/a | strncpy(buffer+n, stuff, k); |
|---|
| 112 | n/a | buffer[n+k] = '\0'; |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | static int |
|---|
| 117 | n/a | search_for_prefix(char *argv0_path, char *landmark) |
|---|
| 118 | n/a | { |
|---|
| 119 | n/a | int n; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | /* Search from argv0_path, until root is found */ |
|---|
| 122 | n/a | strcpy(prefix, argv0_path); |
|---|
| 123 | n/a | do { |
|---|
| 124 | n/a | n = strlen(prefix); |
|---|
| 125 | n/a | join(prefix, landmark); |
|---|
| 126 | n/a | if (exists(prefix)) { |
|---|
| 127 | n/a | prefix[n] = '\0'; |
|---|
| 128 | n/a | return 1; |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | prefix[n] = '\0'; |
|---|
| 131 | n/a | reduce(prefix); |
|---|
| 132 | n/a | } while (prefix[0]); |
|---|
| 133 | n/a | return 0; |
|---|
| 134 | n/a | } |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | #ifdef MS_WIN32 |
|---|
| 137 | n/a | #include "malloc.h" // for alloca - see comments below! |
|---|
| 138 | n/a | extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup. |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | /* Load a PYTHONPATH value from the registry. |
|---|
| 142 | n/a | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | Returns NULL, or a pointer that should be freed. |
|---|
| 145 | n/a | */ |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | static char * |
|---|
| 148 | n/a | getpythonregpath(HKEY keyBase, BOOL bWin32s) |
|---|
| 149 | n/a | { |
|---|
| 150 | n/a | HKEY newKey = 0; |
|---|
| 151 | n/a | DWORD nameSize = 0; |
|---|
| 152 | n/a | DWORD dataSize = 0; |
|---|
| 153 | n/a | DWORD numEntries = 0; |
|---|
| 154 | n/a | LONG rc; |
|---|
| 155 | n/a | char *retval = NULL; |
|---|
| 156 | n/a | char *dataBuf; |
|---|
| 157 | n/a | const char keyPrefix[] = "Software\\Python\\PythonCore\\"; |
|---|
| 158 | n/a | const char keySuffix[] = "\\PythonPath"; |
|---|
| 159 | n/a | int versionLen; |
|---|
| 160 | n/a | char *keyBuf; |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | // Tried to use sysget("winver") but here is too early :-( |
|---|
| 163 | n/a | versionLen = strlen(PyWin_DLLVersionString); |
|---|
| 164 | n/a | // alloca == no free required, but memory only local to fn. |
|---|
| 165 | n/a | // also no heap fragmentation! Am I being silly? |
|---|
| 166 | n/a | keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL. |
|---|
| 167 | n/a | // lots of constants here for the compiler to optimize away :-) |
|---|
| 168 | n/a | memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1); |
|---|
| 169 | n/a | memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen); |
|---|
| 170 | n/a | memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one! |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | rc=RegOpenKey(keyBase, |
|---|
| 173 | n/a | keyBuf, |
|---|
| 174 | n/a | &newKey); |
|---|
| 175 | n/a | if (rc==ERROR_SUCCESS) { |
|---|
| 176 | n/a | RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL, |
|---|
| 177 | n/a | &numEntries, &nameSize, &dataSize, NULL, NULL); |
|---|
| 178 | n/a | } |
|---|
| 179 | n/a | if (bWin32s && numEntries==0 && dataSize==0) { |
|---|
| 180 | n/a | /* must hardcode for Win32s */ |
|---|
| 181 | n/a | numEntries = 1; |
|---|
| 182 | n/a | dataSize = 511; |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | if (numEntries) { |
|---|
| 185 | n/a | /* Loop over all subkeys. */ |
|---|
| 186 | n/a | /* Win32s doesnt know how many subkeys, so we do |
|---|
| 187 | n/a | it twice */ |
|---|
| 188 | n/a | char keyBuf[MAX_PATH+1]; |
|---|
| 189 | n/a | int index = 0; |
|---|
| 190 | n/a | int off = 0; |
|---|
| 191 | n/a | for(index=0;;index++) { |
|---|
| 192 | n/a | long reqdSize = 0; |
|---|
| 193 | n/a | DWORD rc = RegEnumKey(newKey, |
|---|
| 194 | n/a | index, keyBuf, MAX_PATH+1); |
|---|
| 195 | n/a | if (rc) break; |
|---|
| 196 | n/a | rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize); |
|---|
| 197 | n/a | if (rc) break; |
|---|
| 198 | n/a | if (bWin32s && reqdSize==0) reqdSize = 512; |
|---|
| 199 | n/a | dataSize += reqdSize + 1; /* 1 for the ";" */ |
|---|
| 200 | n/a | } |
|---|
| 201 | n/a | dataBuf = malloc(dataSize+1); |
|---|
| 202 | n/a | if (dataBuf==NULL) |
|---|
| 203 | n/a | return NULL; /* pretty serious? Raise error? */ |
|---|
| 204 | n/a | /* Now loop over, grabbing the paths. |
|---|
| 205 | n/a | Subkeys before main library */ |
|---|
| 206 | n/a | for(index=0;;index++) { |
|---|
| 207 | n/a | int adjust; |
|---|
| 208 | n/a | long reqdSize = dataSize; |
|---|
| 209 | n/a | DWORD rc = RegEnumKey(newKey, |
|---|
| 210 | n/a | index, keyBuf,MAX_PATH+1); |
|---|
| 211 | n/a | if (rc) break; |
|---|
| 212 | n/a | rc = RegQueryValue(newKey, |
|---|
| 213 | n/a | keyBuf, dataBuf+off, &reqdSize); |
|---|
| 214 | n/a | if (rc) break; |
|---|
| 215 | n/a | if (reqdSize>1) { |
|---|
| 216 | n/a | /* If Nothing, or only '\0' copied. */ |
|---|
| 217 | n/a | adjust = strlen(dataBuf+off); |
|---|
| 218 | n/a | dataSize -= adjust; |
|---|
| 219 | n/a | off += adjust; |
|---|
| 220 | n/a | dataBuf[off++] = ';'; |
|---|
| 221 | n/a | dataBuf[off] = '\0'; |
|---|
| 222 | n/a | dataSize--; |
|---|
| 223 | n/a | } |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | /* Additionally, win32s doesnt work as expected, so |
|---|
| 226 | n/a | the specific strlen() is required for 3.1. */ |
|---|
| 227 | n/a | rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize); |
|---|
| 228 | n/a | if (rc==ERROR_SUCCESS) { |
|---|
| 229 | n/a | if (strlen(dataBuf)==0) |
|---|
| 230 | n/a | free(dataBuf); |
|---|
| 231 | n/a | else |
|---|
| 232 | n/a | retval = dataBuf; /* caller will free */ |
|---|
| 233 | n/a | } |
|---|
| 234 | n/a | else |
|---|
| 235 | n/a | free(dataBuf); |
|---|
| 236 | n/a | } |
|---|
| 237 | n/a | |
|---|
| 238 | n/a | if (newKey) |
|---|
| 239 | n/a | RegCloseKey(newKey); |
|---|
| 240 | n/a | return retval; |
|---|
| 241 | n/a | } |
|---|
| 242 | n/a | #endif /* MS_WIN32 */ |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | static void |
|---|
| 245 | n/a | get_progpath(void) |
|---|
| 246 | n/a | { |
|---|
| 247 | n/a | extern char *Py_GetProgramName(void); |
|---|
| 248 | n/a | char *path = getenv("PATH"); |
|---|
| 249 | n/a | char *prog = Py_GetProgramName(); |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | #ifdef MS_WIN32 |
|---|
| 252 | n/a | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
|---|
| 253 | n/a | return; |
|---|
| 254 | n/a | #endif |
|---|
| 255 | n/a | if (prog == NULL || *prog == '\0') |
|---|
| 256 | n/a | prog = "python"; |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | /* If there is no slash in the argv0 path, then we have to |
|---|
| 259 | n/a | * assume python is on the user's $PATH, since there's no |
|---|
| 260 | n/a | * other way to find a directory to start the search from. If |
|---|
| 261 | n/a | * $PATH isn't exported, you lose. |
|---|
| 262 | n/a | */ |
|---|
| 263 | n/a | #ifdef ALTSEP |
|---|
| 264 | n/a | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
|---|
| 265 | n/a | #else |
|---|
| 266 | n/a | if (strchr(prog, SEP)) |
|---|
| 267 | n/a | #endif |
|---|
| 268 | n/a | strcpy(progpath, prog); |
|---|
| 269 | n/a | else if (path) { |
|---|
| 270 | n/a | while (1) { |
|---|
| 271 | n/a | char *delim = strchr(path, DELIM); |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | if (delim) { |
|---|
| 274 | n/a | int len = delim - path; |
|---|
| 275 | n/a | strncpy(progpath, path, len); |
|---|
| 276 | n/a | *(progpath + len) = '\0'; |
|---|
| 277 | n/a | } |
|---|
| 278 | n/a | else |
|---|
| 279 | n/a | strcpy(progpath, path); |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | join(progpath, prog); |
|---|
| 282 | n/a | if (exists(progpath)) |
|---|
| 283 | n/a | break; |
|---|
| 284 | n/a | |
|---|
| 285 | n/a | if (!delim) { |
|---|
| 286 | n/a | progpath[0] = '\0'; |
|---|
| 287 | n/a | break; |
|---|
| 288 | n/a | } |
|---|
| 289 | n/a | path = delim + 1; |
|---|
| 290 | n/a | } |
|---|
| 291 | n/a | } |
|---|
| 292 | n/a | else |
|---|
| 293 | n/a | progpath[0] = '\0'; |
|---|
| 294 | n/a | } |
|---|
| 295 | n/a | |
|---|
| 296 | n/a | static void |
|---|
| 297 | n/a | calculate_path(void) |
|---|
| 298 | n/a | { |
|---|
| 299 | n/a | char argv0_path[MAXPATHLEN+1]; |
|---|
| 300 | n/a | char *buf; |
|---|
| 301 | n/a | int bufsz; |
|---|
| 302 | n/a | char *pythonhome = Py_GetPythonHome(); |
|---|
| 303 | n/a | char *envpath = Py_GETENV("PYTHONPATH"); |
|---|
| 304 | n/a | #ifdef MS_WIN32 |
|---|
| 305 | n/a | char *machinepath, *userpath; |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | /* Are we running under Windows 3.1(1) Win32s? */ |
|---|
| 308 | n/a | if (PyWin_IsWin32s()) { |
|---|
| 309 | n/a | /* Only CLASSES_ROOT is supported */ |
|---|
| 310 | n/a | machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE); |
|---|
| 311 | n/a | userpath = NULL; |
|---|
| 312 | n/a | } else { |
|---|
| 313 | n/a | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE); |
|---|
| 314 | n/a | userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE); |
|---|
| 315 | n/a | } |
|---|
| 316 | n/a | #endif |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | get_progpath(); |
|---|
| 319 | n/a | strcpy(argv0_path, progpath); |
|---|
| 320 | n/a | reduce(argv0_path); |
|---|
| 321 | n/a | if (pythonhome == NULL || *pythonhome == '\0') { |
|---|
| 322 | n/a | if (search_for_prefix(argv0_path, LANDMARK)) |
|---|
| 323 | n/a | pythonhome = prefix; |
|---|
| 324 | n/a | else |
|---|
| 325 | n/a | pythonhome = NULL; |
|---|
| 326 | n/a | } |
|---|
| 327 | n/a | else { |
|---|
| 328 | n/a | char *delim; |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | strcpy(prefix, pythonhome); |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | /* Extract Any Optional Trailing EXEC_PREFIX */ |
|---|
| 333 | n/a | /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */ |
|---|
| 334 | n/a | delim = strchr(prefix, DELIM); |
|---|
| 335 | n/a | if (delim) { |
|---|
| 336 | n/a | *delim = '\0'; |
|---|
| 337 | n/a | strcpy(exec_prefix, delim+1); |
|---|
| 338 | n/a | } else |
|---|
| 339 | n/a | strcpy(exec_prefix, EXEC_PREFIX); |
|---|
| 340 | n/a | } |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | if (envpath && *envpath == '\0') |
|---|
| 343 | n/a | envpath = NULL; |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | /* We need to construct a path from the following parts: |
|---|
| 346 | n/a | (1) the PYTHONPATH environment variable, if set; |
|---|
| 347 | n/a | (2) for Win32, the machinepath and userpath, if set; |
|---|
| 348 | n/a | (3) the PYTHONPATH config macro, with the leading "." |
|---|
| 349 | n/a | of each component replaced with pythonhome, if set; |
|---|
| 350 | n/a | (4) the directory containing the executable (argv0_path). |
|---|
| 351 | n/a | The length calculation calculates #3 first. |
|---|
| 352 | n/a | */ |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | /* Calculate size of return buffer */ |
|---|
| 355 | n/a | if (pythonhome != NULL) { |
|---|
| 356 | n/a | char *p; |
|---|
| 357 | n/a | bufsz = 1; |
|---|
| 358 | n/a | for (p = PYTHONPATH; *p; p++) { |
|---|
| 359 | n/a | if (*p == DELIM) |
|---|
| 360 | n/a | bufsz++; /* number of DELIM plus one */ |
|---|
| 361 | n/a | } |
|---|
| 362 | n/a | bufsz *= strlen(pythonhome); |
|---|
| 363 | n/a | } |
|---|
| 364 | n/a | else |
|---|
| 365 | n/a | bufsz = 0; |
|---|
| 366 | n/a | bufsz += strlen(PYTHONPATH) + 1; |
|---|
| 367 | n/a | if (envpath != NULL) |
|---|
| 368 | n/a | bufsz += strlen(envpath) + 1; |
|---|
| 369 | n/a | bufsz += strlen(argv0_path) + 1; |
|---|
| 370 | n/a | #ifdef MS_WIN32 |
|---|
| 371 | n/a | if (machinepath) |
|---|
| 372 | n/a | bufsz += strlen(machinepath) + 1; |
|---|
| 373 | n/a | if (userpath) |
|---|
| 374 | n/a | bufsz += strlen(userpath) + 1; |
|---|
| 375 | n/a | #endif |
|---|
| 376 | n/a | |
|---|
| 377 | n/a | module_search_path = buf = malloc(bufsz); |
|---|
| 378 | n/a | if (buf == NULL) { |
|---|
| 379 | n/a | /* We can't exit, so print a warning and limp along */ |
|---|
| 380 | n/a | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
|---|
| 381 | n/a | if (envpath) { |
|---|
| 382 | n/a | fprintf(stderr, "Using default static $PYTHONPATH.\n"); |
|---|
| 383 | n/a | module_search_path = envpath; |
|---|
| 384 | n/a | } |
|---|
| 385 | n/a | else { |
|---|
| 386 | n/a | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
|---|
| 387 | n/a | module_search_path = PYTHONPATH; |
|---|
| 388 | n/a | } |
|---|
| 389 | n/a | return; |
|---|
| 390 | n/a | } |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | if (envpath) { |
|---|
| 393 | n/a | strcpy(buf, envpath); |
|---|
| 394 | n/a | buf = strchr(buf, '\0'); |
|---|
| 395 | n/a | *buf++ = DELIM; |
|---|
| 396 | n/a | } |
|---|
| 397 | n/a | #ifdef MS_WIN32 |
|---|
| 398 | n/a | if (machinepath) { |
|---|
| 399 | n/a | strcpy(buf, machinepath); |
|---|
| 400 | n/a | buf = strchr(buf, '\0'); |
|---|
| 401 | n/a | *buf++ = DELIM; |
|---|
| 402 | n/a | } |
|---|
| 403 | n/a | if (userpath) { |
|---|
| 404 | n/a | strcpy(buf, userpath); |
|---|
| 405 | n/a | buf = strchr(buf, '\0'); |
|---|
| 406 | n/a | *buf++ = DELIM; |
|---|
| 407 | n/a | } |
|---|
| 408 | n/a | #endif |
|---|
| 409 | n/a | if (pythonhome == NULL) { |
|---|
| 410 | n/a | strcpy(buf, PYTHONPATH); |
|---|
| 411 | n/a | buf = strchr(buf, '\0'); |
|---|
| 412 | n/a | } |
|---|
| 413 | n/a | else { |
|---|
| 414 | n/a | char *p = PYTHONPATH; |
|---|
| 415 | n/a | char *q; |
|---|
| 416 | n/a | int n; |
|---|
| 417 | n/a | for (;;) { |
|---|
| 418 | n/a | q = strchr(p, DELIM); |
|---|
| 419 | n/a | if (q == NULL) |
|---|
| 420 | n/a | n = strlen(p); |
|---|
| 421 | n/a | else |
|---|
| 422 | n/a | n = q-p; |
|---|
| 423 | n/a | if (p[0] == '.' && is_sep(p[1])) { |
|---|
| 424 | n/a | strcpy(buf, pythonhome); |
|---|
| 425 | n/a | buf = strchr(buf, '\0'); |
|---|
| 426 | n/a | p++; |
|---|
| 427 | n/a | n--; |
|---|
| 428 | n/a | } |
|---|
| 429 | n/a | strncpy(buf, p, n); |
|---|
| 430 | n/a | buf += n; |
|---|
| 431 | n/a | if (q == NULL) |
|---|
| 432 | n/a | break; |
|---|
| 433 | n/a | *buf++ = DELIM; |
|---|
| 434 | n/a | p = q+1; |
|---|
| 435 | n/a | } |
|---|
| 436 | n/a | } |
|---|
| 437 | n/a | if (argv0_path) { |
|---|
| 438 | n/a | *buf++ = DELIM; |
|---|
| 439 | n/a | strcpy(buf, argv0_path); |
|---|
| 440 | n/a | buf = strchr(buf, '\0'); |
|---|
| 441 | n/a | } |
|---|
| 442 | n/a | *buf = '\0'; |
|---|
| 443 | n/a | } |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | /* External interface */ |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | char * |
|---|
| 449 | n/a | Py_GetPath(void) |
|---|
| 450 | n/a | { |
|---|
| 451 | n/a | if (!module_search_path) |
|---|
| 452 | n/a | calculate_path(); |
|---|
| 453 | n/a | |
|---|
| 454 | n/a | return module_search_path; |
|---|
| 455 | n/a | } |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | char * |
|---|
| 458 | n/a | Py_GetPrefix(void) |
|---|
| 459 | n/a | { |
|---|
| 460 | n/a | if (!module_search_path) |
|---|
| 461 | n/a | calculate_path(); |
|---|
| 462 | n/a | |
|---|
| 463 | n/a | return prefix; |
|---|
| 464 | n/a | } |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | char * |
|---|
| 467 | n/a | Py_GetExecPrefix(void) |
|---|
| 468 | n/a | { |
|---|
| 469 | n/a | if (!module_search_path) |
|---|
| 470 | n/a | calculate_path(); |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | return exec_prefix; |
|---|
| 473 | n/a | } |
|---|
| 474 | n/a | |
|---|
| 475 | n/a | char * |
|---|
| 476 | n/a | Py_GetProgramFullPath(void) |
|---|
| 477 | n/a | { |
|---|
| 478 | n/a | if (!module_search_path) |
|---|
| 479 | n/a | calculate_path(); |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | return progpath; |
|---|
| 482 | n/a | } |
|---|