Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 1 | |
| 2 | /* Return the initial module search path. */ |
Jesus Cea | f1af705 | 2012-10-05 02:48:46 +0200 | [diff] [blame] | 3 | /* Used by DOS, 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: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | This describes how sys.path is formed on Windows. It describes the |
| 8 | functionality, not the implementation (ie, the order in which these |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | - If we DO have a Python Home: The relevant sub-directories (Lib, |
Georg Brandl | 6e47a33 | 2008-05-17 19:15:58 +0000 | [diff] [blame] | 28 | plat-win, etc) are based on the Python Home |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 29 | - If we DO NOT have a Python Home, the core Python Path is |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 30 | loaded from the registry. This is the main PythonPath key, |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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_ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 35 | we can assume is a good path), a default path with relative entries is |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | * When Python is hosted in another exe (different directory, embedded via |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 46 | COM, etc), the Python Home will not be deduced, so the core path from |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +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 | |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 54 | * An embedding application can use Py_SetPath() to override all of |
| 55 | these authomatic path computations. |
| 56 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 57 | ---------------------------------------------------------------- */ |
| 58 | |
| 59 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 60 | #include "Python.h" |
| 61 | #include "osdefs.h" |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 62 | #include <wchar.h> |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 63 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 64 | #ifdef MS_WINDOWS |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 65 | #include <windows.h> |
| 66 | #endif |
| 67 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 68 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 69 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 70 | #endif /* HAVE_SYS_TYPES_H */ |
| 71 | |
| 72 | #ifdef HAVE_SYS_STAT_H |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 73 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 74 | #endif /* HAVE_SYS_STAT_H */ |
| 75 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 76 | #include <string.h> |
| 77 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 78 | /* Search in some common locations for the associated Python libraries. |
| 79 | * |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 80 | * Py_GetPath() tries to return a sensible Python module search path. |
| 81 | * |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 82 | * The approach is an adaptation for Windows of the strategy used in |
| 83 | * ../Modules/getpath.c; it uses the Windows Registry as one of its |
| 84 | * information sources. |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 85 | * |
| 86 | * Py_SetPath() can be used to override this mechanism. Call Py_SetPath |
| 87 | * with a semicolon separated path prior to calling Py_Initialize. |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 88 | */ |
| 89 | |
| 90 | #ifndef LANDMARK |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 91 | #define LANDMARK L"lib\\os.py" |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 92 | #endif |
| 93 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 94 | static wchar_t prefix[MAXPATHLEN+1]; |
| 95 | static wchar_t progpath[MAXPATHLEN+1]; |
| 96 | static wchar_t dllpath[MAXPATHLEN+1]; |
| 97 | static wchar_t *module_search_path = NULL; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 100 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | is_sep(wchar_t ch) /* determine if "ch" is a separator character */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 102 | { |
| 103 | #ifdef ALTSEP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | return ch == SEP || ch == ALTSEP; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 105 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | return ch == SEP; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 107 | #endif |
| 108 | } |
| 109 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 110 | /* assumes 'dir' null terminated in bounds. Never writes |
| 111 | beyond existing terminator. |
| 112 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 113 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 114 | reduce(wchar_t *dir) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 115 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | size_t i = wcslen(dir); |
| 117 | while (i > 0 && !is_sep(dir[i])) |
| 118 | --i; |
| 119 | dir[i] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 120 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 122 | |
| 123 | static int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 124 | exists(wchar_t *filename) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 125 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | return GetFileAttributesW(filename) != 0xFFFFFFFF; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | /* Assumes 'filename' MAXPATHLEN+1 bytes long - |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 130 | may extend 'filename' by one character. |
| 131 | */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 132 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | ismodule(wchar_t *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | if (exists(filename)) |
| 136 | return 1; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | /* Check for the compiled version of prefix. */ |
| 139 | if (wcslen(filename) < MAXPATHLEN) { |
| 140 | wcscat(filename, Py_OptimizeFlag ? L"o" : L"c"); |
| 141 | if (exists(filename)) |
| 142 | return 1; |
| 143 | } |
| 144 | return 0; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Tim Peters | 8484fbf | 2004-08-07 19:12:27 +0000 | [diff] [blame] | 147 | /* Add a path component, by appending stuff to buffer. |
| 148 | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
| 149 | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
| 150 | the trailing NUL). It's a fatal error if it contains a string longer than |
| 151 | that (callers must be careful!). If these requirements are met, it's |
| 152 | guaranteed that buffer will still be a NUL-terminated string with no more |
| 153 | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
| 154 | stuff as fits will be appended. |
| 155 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 156 | static void |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 157 | join(wchar_t *buffer, wchar_t *stuff) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | size_t n, k; |
| 160 | if (is_sep(stuff[0])) |
| 161 | n = 0; |
| 162 | else { |
| 163 | n = wcslen(buffer); |
| 164 | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
| 165 | buffer[n++] = SEP; |
| 166 | } |
| 167 | if (n > MAXPATHLEN) |
| 168 | Py_FatalError("buffer overflow in getpathp.c's joinpath()"); |
| 169 | k = wcslen(stuff); |
| 170 | if (n + k > MAXPATHLEN) |
| 171 | k = MAXPATHLEN - n; |
| 172 | wcsncpy(buffer+n, stuff, k); |
| 173 | buffer[n+k] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 176 | /* gotlandmark only called by search_for_prefix, which ensures |
| 177 | 'prefix' is null terminated in bounds. join() ensures |
| 178 | 'landmark' can not overflow prefix if too long. |
| 179 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 180 | static int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 181 | gotlandmark(wchar_t *landmark) |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 183 | int ok; |
| 184 | Py_ssize_t n; |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | n = wcslen(prefix); |
| 187 | join(prefix, landmark); |
| 188 | ok = ismodule(prefix); |
| 189 | prefix[n] = '\0'; |
| 190 | return ok; |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 194 | assumption provided by only caller, calculate_path() */ |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 195 | static int |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 196 | search_for_prefix(wchar_t *argv0_path, wchar_t *landmark) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | /* Search from argv0_path, until landmark is found */ |
| 199 | wcscpy(prefix, argv0_path); |
| 200 | do { |
| 201 | if (gotlandmark(landmark)) |
| 202 | return 1; |
| 203 | reduce(prefix); |
| 204 | } while (prefix[0]); |
| 205 | return 0; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 208 | #ifdef MS_WINDOWS |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 209 | #ifdef Py_ENABLE_SHARED |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 211 | /* a string loaded from the DLL at startup.*/ |
| 212 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 214 | |
| 215 | /* Load a PYTHONPATH value from the registry. |
| 216 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 217 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 218 | Works in both Unicode and 8bit environments. Only uses the |
| 219 | Ex family of functions so it also works with Windows CE. |
| 220 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 221 | Returns NULL, or a pointer that should be freed. |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 222 | |
| 223 | XXX - this code is pretty strange, as it used to also |
| 224 | work on Win16, where the buffer sizes werent available |
| 225 | in advance. It could be simplied now Win16/Win32s is dead! |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 226 | */ |
| 227 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 228 | static wchar_t * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 229 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | HKEY newKey = 0; |
| 232 | DWORD dataSize = 0; |
| 233 | DWORD numKeys = 0; |
| 234 | LONG rc; |
| 235 | wchar_t *retval = NULL; |
| 236 | WCHAR *dataBuf = NULL; |
| 237 | static const WCHAR keyPrefix[] = L"Software\\Python\\PythonCore\\"; |
| 238 | static const WCHAR keySuffix[] = L"\\PythonPath"; |
| 239 | size_t versionLen; |
| 240 | DWORD index; |
| 241 | WCHAR *keyBuf = NULL; |
| 242 | WCHAR *keyBufPtr; |
| 243 | WCHAR **ppPaths = NULL; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 246 | versionLen = strlen(PyWin_DLLVersionString); |
| 247 | /* Space for all the chars, plus one \0 */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 248 | keyBuf = keyBufPtr = PyMem_RawMalloc(sizeof(keyPrefix) + |
| 249 | sizeof(WCHAR)*(versionLen-1) + |
| 250 | sizeof(keySuffix)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | if (keyBuf==NULL) goto done; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(WCHAR)); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 254 | keyBufPtr += Py_ARRAY_LENGTH(keyPrefix) - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | mbstowcs(keyBufPtr, PyWin_DLLVersionString, versionLen); |
| 256 | keyBufPtr += versionLen; |
| 257 | /* NULL comes with this one! */ |
| 258 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 259 | /* Open the root Python key */ |
| 260 | rc=RegOpenKeyExW(keyBase, |
| 261 | keyBuf, /* subkey */ |
| 262 | 0, /* reserved */ |
| 263 | KEY_READ, |
| 264 | &newKey); |
| 265 | if (rc!=ERROR_SUCCESS) goto done; |
| 266 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 267 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 268 | NULL, NULL, &dataSize, NULL, NULL); |
| 269 | if (rc!=ERROR_SUCCESS) goto done; |
| 270 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 271 | /* Allocate a temp array of char buffers, so we only need to loop |
| 272 | reading the registry once |
| 273 | */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 274 | ppPaths = PyMem_RawMalloc( sizeof(WCHAR *) * numKeys ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | if (ppPaths==NULL) goto done; |
| 276 | memset(ppPaths, 0, sizeof(WCHAR *) * numKeys); |
| 277 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 278 | for(index=0;index<numKeys;index++) { |
| 279 | WCHAR keyBuf[MAX_PATH+1]; |
| 280 | HKEY subKey = 0; |
| 281 | DWORD reqdSize = MAX_PATH+1; |
| 282 | /* Get the sub-key name */ |
| 283 | DWORD rc = RegEnumKeyExW(newKey, index, keyBuf, &reqdSize, |
| 284 | NULL, NULL, NULL, NULL ); |
| 285 | if (rc!=ERROR_SUCCESS) goto done; |
| 286 | /* Open the sub-key */ |
| 287 | rc=RegOpenKeyExW(newKey, |
| 288 | keyBuf, /* subkey */ |
| 289 | 0, /* reserved */ |
| 290 | KEY_READ, |
| 291 | &subKey); |
| 292 | if (rc!=ERROR_SUCCESS) goto done; |
| 293 | /* Find the value of the buffer size, malloc, then read it */ |
| 294 | RegQueryValueExW(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 295 | if (reqdSize) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 296 | ppPaths[index] = PyMem_RawMalloc(reqdSize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | if (ppPaths[index]) { |
| 298 | RegQueryValueExW(subKey, NULL, 0, NULL, |
| 299 | (LPBYTE)ppPaths[index], |
| 300 | &reqdSize); |
| 301 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
| 302 | } |
| 303 | } |
| 304 | RegCloseKey(subKey); |
| 305 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | /* return null if no path to return */ |
| 308 | if (dataSize == 0) goto done; |
| 309 | |
| 310 | /* original datasize from RegQueryInfo doesn't include the \0 */ |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 311 | dataBuf = PyMem_RawMalloc((dataSize+1) * sizeof(WCHAR)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (dataBuf) { |
| 313 | WCHAR *szCur = dataBuf; |
| 314 | DWORD reqdSize = dataSize; |
| 315 | /* Copy our collected strings */ |
| 316 | for (index=0;index<numKeys;index++) { |
| 317 | if (index > 0) { |
| 318 | *(szCur++) = L';'; |
| 319 | dataSize--; |
| 320 | } |
| 321 | if (ppPaths[index]) { |
| 322 | Py_ssize_t len = wcslen(ppPaths[index]); |
| 323 | wcsncpy(szCur, ppPaths[index], len); |
| 324 | szCur += len; |
| 325 | assert(dataSize > (DWORD)len); |
| 326 | dataSize -= (DWORD)len; |
| 327 | } |
| 328 | } |
| 329 | if (skipcore) |
| 330 | *szCur = '\0'; |
| 331 | else { |
| 332 | /* If we have no values, we dont need a ';' */ |
| 333 | if (numKeys) { |
| 334 | *(szCur++) = L';'; |
| 335 | dataSize--; |
| 336 | } |
| 337 | /* Now append the core path entries - |
| 338 | this will include the NULL |
| 339 | */ |
| 340 | rc = RegQueryValueExW(newKey, NULL, 0, NULL, |
| 341 | (LPBYTE)szCur, &dataSize); |
| 342 | } |
| 343 | /* And set the result - caller must free */ |
| 344 | retval = dataBuf; |
| 345 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 346 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | /* Loop freeing my temp buffers */ |
| 348 | if (ppPaths) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 349 | for(index=0; index<numKeys; index++) |
| 350 | PyMem_RawFree(ppPaths[index]); |
| 351 | PyMem_RawFree(ppPaths); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | } |
| 353 | if (newKey) |
| 354 | RegCloseKey(newKey); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 355 | PyMem_RawFree(keyBuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | return retval; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 357 | } |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 358 | #endif /* Py_ENABLE_SHARED */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 359 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 361 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 362 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | extern wchar_t *Py_GetProgramName(void); |
| 365 | wchar_t *path = _wgetenv(L"PATH"); |
| 366 | wchar_t *prog = Py_GetProgramName(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 367 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 368 | #ifdef MS_WINDOWS |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 369 | #ifdef Py_ENABLE_SHARED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | extern HANDLE PyWin_DLLhModule; |
| 371 | /* static init of progpath ensures final char remains \0 */ |
| 372 | if (PyWin_DLLhModule) |
| 373 | if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) |
| 374 | dllpath[0] = 0; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 375 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | dllpath[0] = 0; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 377 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN)) |
| 379 | return; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 380 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | if (prog == NULL || *prog == '\0') |
| 382 | prog = L"python"; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | /* If there is no slash in the argv0 path, then we have to |
| 385 | * assume python is on the user's $PATH, since there's no |
| 386 | * other way to find a directory to start the search from. If |
| 387 | * $PATH isn't exported, you lose. |
| 388 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 389 | #ifdef ALTSEP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | if (wcschr(prog, SEP) || wcschr(prog, ALTSEP)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 391 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | if (wcschr(prog, SEP)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 393 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | wcsncpy(progpath, prog, MAXPATHLEN); |
| 395 | else if (path) { |
| 396 | while (1) { |
| 397 | wchar_t *delim = wcschr(path, DELIM); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 399 | if (delim) { |
| 400 | size_t len = delim - path; |
| 401 | /* ensure we can't overwrite buffer */ |
| 402 | len = min(MAXPATHLEN,len); |
| 403 | wcsncpy(progpath, path, len); |
| 404 | *(progpath + len) = '\0'; |
| 405 | } |
| 406 | else |
| 407 | wcsncpy(progpath, path, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 409 | /* join() is safe for MAXPATHLEN+1 size buffer */ |
| 410 | join(progpath, prog); |
| 411 | if (exists(progpath)) |
| 412 | break; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | if (!delim) { |
| 415 | progpath[0] = '\0'; |
| 416 | break; |
| 417 | } |
| 418 | path = delim + 1; |
| 419 | } |
| 420 | } |
| 421 | else |
| 422 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 425 | static int |
| 426 | find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) |
| 427 | { |
| 428 | int result = 0; /* meaning not found */ |
| 429 | char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ |
| 430 | |
| 431 | fseek(env_file, 0, SEEK_SET); |
| 432 | while (!feof(env_file)) { |
| 433 | char * p = fgets(buffer, MAXPATHLEN*2, env_file); |
| 434 | wchar_t tmpbuffer[MAXPATHLEN*2+1]; |
| 435 | PyObject * decoded; |
Victor Stinner | 8bda465 | 2013-06-05 00:22:34 +0200 | [diff] [blame] | 436 | size_t n; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 437 | |
| 438 | if (p == NULL) |
| 439 | break; |
| 440 | n = strlen(p); |
| 441 | if (p[n - 1] != '\n') { |
| 442 | /* line has overflowed - bail */ |
| 443 | break; |
| 444 | } |
| 445 | if (p[0] == '#') /* Comment - skip */ |
| 446 | continue; |
| 447 | decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape"); |
| 448 | if (decoded != NULL) { |
| 449 | Py_ssize_t k; |
| 450 | k = PyUnicode_AsWideChar(decoded, |
| 451 | tmpbuffer, MAXPATHLEN * 2); |
| 452 | Py_DECREF(decoded); |
| 453 | if (k >= 0) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 454 | wchar_t * context = NULL; |
| 455 | wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 456 | if ((tok != NULL) && !wcscmp(tok, key)) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 457 | tok = wcstok_s(NULL, L" \t", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 458 | if ((tok != NULL) && !wcscmp(tok, L"=")) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 459 | tok = wcstok_s(NULL, L"\r\n", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 460 | if (tok != NULL) { |
| 461 | wcsncpy(value, tok, MAXPATHLEN); |
| 462 | result = 1; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | return result; |
| 471 | } |
| 472 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 473 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 474 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | wchar_t argv0_path[MAXPATHLEN+1]; |
| 477 | wchar_t *buf; |
| 478 | size_t bufsz; |
| 479 | wchar_t *pythonhome = Py_GetPythonHome(); |
| 480 | wchar_t *envpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 481 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 482 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | int skiphome, skipdefault; |
| 484 | wchar_t *machinepath = NULL; |
| 485 | wchar_t *userpath = NULL; |
| 486 | wchar_t zip_path[MAXPATHLEN+1]; |
| 487 | size_t len; |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | if (!Py_IgnoreEnvironmentFlag) { |
| 490 | envpath = _wgetenv(L"PYTHONPATH"); |
| 491 | } |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 492 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | char *_envpath = Py_GETENV("PYTHONPATH"); |
| 494 | wchar_t wenvpath[MAXPATHLEN+1]; |
| 495 | if (_envpath) { |
| 496 | size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1); |
| 497 | envpath = wenvpath; |
| 498 | if (r == (size_t)-1 || r >= MAXPATHLEN) |
| 499 | envpath = NULL; |
| 500 | } |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 501 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | get_progpath(); |
| 504 | /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ |
| 505 | wcscpy(argv0_path, progpath); |
| 506 | reduce(argv0_path); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 507 | |
| 508 | /* Search for an environment configuration file, first in the |
| 509 | executable's directory and then in the parent directory. |
| 510 | If found, open it for use when searching for prefixes. |
| 511 | */ |
| 512 | |
| 513 | { |
| 514 | wchar_t tmpbuffer[MAXPATHLEN+1]; |
| 515 | wchar_t *env_cfg = L"pyvenv.cfg"; |
| 516 | FILE * env_file = NULL; |
| 517 | |
| 518 | wcscpy(tmpbuffer, argv0_path); |
| 519 | join(tmpbuffer, env_cfg); |
| 520 | env_file = _Py_wfopen(tmpbuffer, L"r"); |
| 521 | if (env_file == NULL) { |
| 522 | errno = 0; |
| 523 | reduce(tmpbuffer); |
| 524 | reduce(tmpbuffer); |
| 525 | join(tmpbuffer, env_cfg); |
| 526 | env_file = _Py_wfopen(tmpbuffer, L"r"); |
| 527 | if (env_file == NULL) { |
| 528 | errno = 0; |
| 529 | } |
| 530 | } |
| 531 | if (env_file != NULL) { |
| 532 | /* Look for a 'home' variable and set argv0_path to it, if found */ |
| 533 | if (find_env_config_value(env_file, L"home", tmpbuffer)) { |
| 534 | wcscpy(argv0_path, tmpbuffer); |
| 535 | } |
| 536 | fclose(env_file); |
| 537 | env_file = NULL; |
| 538 | } |
| 539 | } |
| 540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 542 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 543 | pythonhome = prefix; |
| 544 | else |
| 545 | pythonhome = NULL; |
| 546 | } |
| 547 | else |
| 548 | wcsncpy(prefix, pythonhome, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | if (envpath && *envpath == '\0') |
| 551 | envpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 552 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 553 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 554 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | /* Calculate zip archive path */ |
| 556 | if (dllpath[0]) /* use name of python DLL */ |
| 557 | wcsncpy(zip_path, dllpath, MAXPATHLEN); |
| 558 | else /* use name of executable program */ |
| 559 | wcsncpy(zip_path, progpath, MAXPATHLEN); |
| 560 | zip_path[MAXPATHLEN] = '\0'; |
| 561 | len = wcslen(zip_path); |
| 562 | if (len > 4) { |
| 563 | zip_path[len-3] = 'z'; /* change ending to "zip" */ |
| 564 | zip_path[len-2] = 'i'; |
| 565 | zip_path[len-1] = 'p'; |
| 566 | } |
| 567 | else { |
| 568 | zip_path[0] = 0; |
| 569 | } |
| 570 | |
| 571 | skiphome = pythonhome==NULL ? 0 : 1; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 572 | #ifdef Py_ENABLE_SHARED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 574 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 575 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 576 | /* We only use the default relative PYTHONPATH if we havent |
| 577 | anything better to use! */ |
| 578 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 579 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 580 | #endif |
| 581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | /* We need to construct a path from the following parts. |
| 583 | (1) the PYTHONPATH environment variable, if set; |
| 584 | (2) for Win32, the zip archive file path; |
| 585 | (3) for Win32, the machinepath and userpath, if set; |
| 586 | (4) the PYTHONPATH config macro, with the leading "." |
| 587 | of each component replaced with pythonhome, if set; |
| 588 | (5) the directory containing the executable (argv0_path). |
| 589 | The length calculation calculates #4 first. |
| 590 | Extra rules: |
| 591 | - If PYTHONHOME is set (in any way) item (3) is ignored. |
| 592 | - If registry values are used, (4) and (5) are ignored. |
| 593 | */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | /* Calculate size of return buffer */ |
| 596 | if (pythonhome != NULL) { |
| 597 | wchar_t *p; |
| 598 | bufsz = 1; |
| 599 | for (p = PYTHONPATH; *p; p++) { |
| 600 | if (*p == DELIM) |
| 601 | bufsz++; /* number of DELIM plus one */ |
| 602 | } |
| 603 | bufsz *= wcslen(pythonhome); |
| 604 | } |
| 605 | else |
| 606 | bufsz = 0; |
| 607 | bufsz += wcslen(PYTHONPATH) + 1; |
| 608 | bufsz += wcslen(argv0_path) + 1; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 609 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | if (userpath) |
| 611 | bufsz += wcslen(userpath) + 1; |
| 612 | if (machinepath) |
| 613 | bufsz += wcslen(machinepath) + 1; |
| 614 | bufsz += wcslen(zip_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 615 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | if (envpath != NULL) |
| 617 | bufsz += wcslen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 618 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 619 | module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | if (buf == NULL) { |
| 621 | /* We can't exit, so print a warning and limp along */ |
| 622 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 623 | if (envpath) { |
| 624 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
| 625 | module_search_path = envpath; |
| 626 | } |
| 627 | else { |
| 628 | fprintf(stderr, "Using default static path.\n"); |
| 629 | module_search_path = PYTHONPATH; |
| 630 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 631 | #ifdef MS_WINDOWS |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 632 | PyMem_RawFree(machinepath); |
| 633 | PyMem_RawFree(userpath); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 634 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | return; |
| 636 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | if (envpath) { |
| 639 | wcscpy(buf, envpath); |
| 640 | buf = wcschr(buf, L'\0'); |
| 641 | *buf++ = DELIM; |
| 642 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 643 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | if (zip_path[0]) { |
| 645 | wcscpy(buf, zip_path); |
| 646 | buf = wcschr(buf, L'\0'); |
| 647 | *buf++ = DELIM; |
| 648 | } |
| 649 | if (userpath) { |
| 650 | wcscpy(buf, userpath); |
| 651 | buf = wcschr(buf, L'\0'); |
| 652 | *buf++ = DELIM; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 653 | PyMem_RawFree(userpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | } |
| 655 | if (machinepath) { |
| 656 | wcscpy(buf, machinepath); |
| 657 | buf = wcschr(buf, L'\0'); |
| 658 | *buf++ = DELIM; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 659 | PyMem_RawFree(machinepath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | } |
| 661 | if (pythonhome == NULL) { |
| 662 | if (!skipdefault) { |
| 663 | wcscpy(buf, PYTHONPATH); |
| 664 | buf = wcschr(buf, L'\0'); |
| 665 | } |
| 666 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 667 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | if (pythonhome == NULL) { |
| 669 | wcscpy(buf, PYTHONPATH); |
| 670 | buf = wcschr(buf, L'\0'); |
| 671 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 672 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | else { |
| 674 | wchar_t *p = PYTHONPATH; |
| 675 | wchar_t *q; |
| 676 | size_t n; |
| 677 | for (;;) { |
| 678 | q = wcschr(p, DELIM); |
| 679 | if (q == NULL) |
| 680 | n = wcslen(p); |
| 681 | else |
| 682 | n = q-p; |
| 683 | if (p[0] == '.' && is_sep(p[1])) { |
| 684 | wcscpy(buf, pythonhome); |
| 685 | buf = wcschr(buf, L'\0'); |
| 686 | p++; |
| 687 | n--; |
| 688 | } |
| 689 | wcsncpy(buf, p, n); |
| 690 | buf += n; |
| 691 | if (q == NULL) |
| 692 | break; |
| 693 | *buf++ = DELIM; |
| 694 | p = q+1; |
| 695 | } |
| 696 | } |
| 697 | if (argv0_path) { |
| 698 | *buf++ = DELIM; |
| 699 | wcscpy(buf, argv0_path); |
| 700 | buf = wcschr(buf, L'\0'); |
| 701 | } |
| 702 | *buf = L'\0'; |
| 703 | /* Now to pull one last hack/trick. If sys.prefix is |
| 704 | empty, then try and find it somewhere on the paths |
| 705 | we calculated. We scan backwards, as our general policy |
| 706 | is that Python core directories are at the *end* of |
| 707 | sys.path. We assume that our "lib" directory is |
| 708 | on the path, and that our 'prefix' directory is |
| 709 | the parent of that. |
| 710 | */ |
| 711 | if (*prefix==L'\0') { |
| 712 | wchar_t lookBuf[MAXPATHLEN+1]; |
| 713 | wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */ |
| 714 | while (1) { |
| 715 | Py_ssize_t nchars; |
| 716 | wchar_t *lookEnd = look; |
| 717 | /* 'look' will end up one character before the |
| 718 | start of the path in question - even if this |
| 719 | is one character before the start of the buffer |
| 720 | */ |
| 721 | while (look >= module_search_path && *look != DELIM) |
| 722 | look--; |
| 723 | nchars = lookEnd-look; |
| 724 | wcsncpy(lookBuf, look+1, nchars); |
| 725 | lookBuf[nchars] = L'\0'; |
| 726 | /* Up one level to the parent */ |
| 727 | reduce(lookBuf); |
| 728 | if (search_for_prefix(lookBuf, LANDMARK)) { |
| 729 | break; |
| 730 | } |
| 731 | /* If we are out of paths to search - give up */ |
| 732 | if (look < module_search_path) |
| 733 | break; |
| 734 | look--; |
| 735 | } |
| 736 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
| 740 | /* External interface */ |
| 741 | |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 742 | void |
| 743 | Py_SetPath(const wchar_t *path) |
| 744 | { |
| 745 | if (module_search_path != NULL) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 746 | PyMem_RawFree(module_search_path); |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 747 | module_search_path = NULL; |
| 748 | } |
| 749 | if (path != NULL) { |
| 750 | extern wchar_t *Py_GetProgramName(void); |
| 751 | wchar_t *prog = Py_GetProgramName(); |
| 752 | wcsncpy(progpath, prog, MAXPATHLEN); |
| 753 | prefix[0] = L'\0'; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 754 | module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t)); |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 755 | if (module_search_path != NULL) |
| 756 | wcscpy(module_search_path, path); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 757 | } |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 760 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 761 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 762 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | if (!module_search_path) |
| 764 | calculate_path(); |
| 765 | return module_search_path; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 768 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 769 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (!module_search_path) |
| 772 | calculate_path(); |
| 773 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 776 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 777 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 782 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 783 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | if (!module_search_path) |
| 786 | calculate_path(); |
| 787 | return progpath; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 788 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 789 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 790 | /* Load python3.dll before loading any extension module that might refer |
| 791 | to it. That way, we can be sure that always the python3.dll corresponding |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 792 | to this python DLL is loaded, not a python3.dll that might be on the path |
| 793 | by chance. |
| 794 | Return whether the DLL was found. |
| 795 | */ |
| 796 | static int python3_checked = 0; |
| 797 | static HANDLE hPython3; |
| 798 | int |
| 799 | _Py_CheckPython3() |
| 800 | { |
| 801 | wchar_t py3path[MAXPATHLEN+1]; |
| 802 | wchar_t *s; |
| 803 | if (python3_checked) |
| 804 | return hPython3 != NULL; |
| 805 | python3_checked = 1; |
| 806 | |
| 807 | /* If there is a python3.dll next to the python3y.dll, |
| 808 | assume this is a build tree; use that DLL */ |
| 809 | wcscpy(py3path, dllpath); |
| 810 | s = wcsrchr(py3path, L'\\'); |
| 811 | if (!s) |
| 812 | s = py3path; |
| 813 | wcscpy(s, L"\\python3.dll"); |
| 814 | hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 815 | if (hPython3 != NULL) |
| 816 | return 1; |
| 817 | |
| 818 | /* Check sys.prefix\DLLs\python3.dll */ |
| 819 | wcscpy(py3path, Py_GetPrefix()); |
| 820 | wcscat(py3path, L"\\DLLs\\python3.dll"); |
| 821 | hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 822 | return hPython3 != NULL; |
| 823 | } |