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