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