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 | { |
Victor Stinner | ccb1f8c | 2016-03-23 11:31:58 +0100 | [diff] [blame] | 138 | size_t n; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 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; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | /* Copy our collected strings */ |
| 325 | for (index=0;index<numKeys;index++) { |
| 326 | if (index > 0) { |
| 327 | *(szCur++) = L';'; |
| 328 | dataSize--; |
| 329 | } |
| 330 | if (ppPaths[index]) { |
| 331 | Py_ssize_t len = wcslen(ppPaths[index]); |
| 332 | wcsncpy(szCur, ppPaths[index], len); |
| 333 | szCur += len; |
| 334 | assert(dataSize > (DWORD)len); |
| 335 | dataSize -= (DWORD)len; |
| 336 | } |
| 337 | } |
| 338 | if (skipcore) |
| 339 | *szCur = '\0'; |
| 340 | else { |
| 341 | /* If we have no values, we dont need a ';' */ |
| 342 | if (numKeys) { |
| 343 | *(szCur++) = L';'; |
| 344 | dataSize--; |
| 345 | } |
| 346 | /* Now append the core path entries - |
| 347 | this will include the NULL |
| 348 | */ |
| 349 | rc = RegQueryValueExW(newKey, NULL, 0, NULL, |
| 350 | (LPBYTE)szCur, &dataSize); |
Serhiy Storchaka | e0cb9da | 2015-12-18 09:54:19 +0200 | [diff] [blame] | 351 | if (rc != ERROR_SUCCESS) { |
| 352 | PyMem_RawFree(dataBuf); |
| 353 | goto done; |
| 354 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | } |
| 356 | /* And set the result - caller must free */ |
| 357 | retval = dataBuf; |
| 358 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 359 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | /* Loop freeing my temp buffers */ |
| 361 | if (ppPaths) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 362 | for(index=0; index<numKeys; index++) |
| 363 | PyMem_RawFree(ppPaths[index]); |
| 364 | PyMem_RawFree(ppPaths); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | } |
| 366 | if (newKey) |
| 367 | RegCloseKey(newKey); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 368 | PyMem_RawFree(keyBuf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | return retval; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 370 | } |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 371 | #endif /* Py_ENABLE_SHARED */ |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 372 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 374 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 375 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 376 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | extern wchar_t *Py_GetProgramName(void); |
| 378 | wchar_t *path = _wgetenv(L"PATH"); |
| 379 | wchar_t *prog = Py_GetProgramName(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 380 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 381 | #ifdef MS_WINDOWS |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 382 | #ifdef Py_ENABLE_SHARED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | extern HANDLE PyWin_DLLhModule; |
| 384 | /* static init of progpath ensures final char remains \0 */ |
| 385 | if (PyWin_DLLhModule) |
| 386 | if (!GetModuleFileNameW(PyWin_DLLhModule, dllpath, MAXPATHLEN)) |
| 387 | dllpath[0] = 0; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 388 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | dllpath[0] = 0; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 390 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | if (GetModuleFileNameW(NULL, progpath, MAXPATHLEN)) |
| 392 | return; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 393 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | if (prog == NULL || *prog == '\0') |
| 395 | prog = L"python"; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | /* If there is no slash in the argv0 path, then we have to |
| 398 | * assume python is on the user's $PATH, since there's no |
| 399 | * other way to find a directory to start the search from. If |
| 400 | * $PATH isn't exported, you lose. |
| 401 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 402 | #ifdef ALTSEP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | if (wcschr(prog, SEP) || wcschr(prog, ALTSEP)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 404 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | if (wcschr(prog, SEP)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 406 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | wcsncpy(progpath, prog, MAXPATHLEN); |
| 408 | else if (path) { |
| 409 | while (1) { |
| 410 | wchar_t *delim = wcschr(path, DELIM); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | if (delim) { |
| 413 | size_t len = delim - path; |
| 414 | /* ensure we can't overwrite buffer */ |
| 415 | len = min(MAXPATHLEN,len); |
| 416 | wcsncpy(progpath, path, len); |
| 417 | *(progpath + len) = '\0'; |
| 418 | } |
| 419 | else |
| 420 | wcsncpy(progpath, path, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | /* join() is safe for MAXPATHLEN+1 size buffer */ |
| 423 | join(progpath, prog); |
| 424 | if (exists(progpath)) |
| 425 | break; |
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 | progpath[0] = '\0'; |
| 429 | break; |
| 430 | } |
| 431 | path = delim + 1; |
| 432 | } |
| 433 | } |
| 434 | else |
| 435 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 438 | static int |
| 439 | find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value) |
| 440 | { |
| 441 | int result = 0; /* meaning not found */ |
| 442 | char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ |
| 443 | |
| 444 | fseek(env_file, 0, SEEK_SET); |
| 445 | while (!feof(env_file)) { |
| 446 | char * p = fgets(buffer, MAXPATHLEN*2, env_file); |
| 447 | wchar_t tmpbuffer[MAXPATHLEN*2+1]; |
| 448 | PyObject * decoded; |
Victor Stinner | 8bda465 | 2013-06-05 00:22:34 +0200 | [diff] [blame] | 449 | size_t n; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 450 | |
| 451 | if (p == NULL) |
| 452 | break; |
| 453 | n = strlen(p); |
| 454 | if (p[n - 1] != '\n') { |
| 455 | /* line has overflowed - bail */ |
| 456 | break; |
| 457 | } |
| 458 | if (p[0] == '#') /* Comment - skip */ |
| 459 | continue; |
| 460 | decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape"); |
| 461 | if (decoded != NULL) { |
| 462 | Py_ssize_t k; |
| 463 | k = PyUnicode_AsWideChar(decoded, |
| 464 | tmpbuffer, MAXPATHLEN * 2); |
| 465 | Py_DECREF(decoded); |
| 466 | if (k >= 0) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 467 | wchar_t * context = NULL; |
| 468 | wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 469 | if ((tok != NULL) && !wcscmp(tok, key)) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 470 | tok = wcstok_s(NULL, L" \t", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 471 | if ((tok != NULL) && !wcscmp(tok, L"=")) { |
Steve Dower | f63dab5 | 2015-02-25 20:48:01 -0800 | [diff] [blame] | 472 | tok = wcstok_s(NULL, L"\r\n", &context); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 473 | if (tok != NULL) { |
| 474 | wcsncpy(value, tok, MAXPATHLEN); |
| 475 | result = 1; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | return result; |
| 484 | } |
| 485 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 486 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 487 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | wchar_t argv0_path[MAXPATHLEN+1]; |
| 490 | wchar_t *buf; |
| 491 | size_t bufsz; |
| 492 | wchar_t *pythonhome = Py_GetPythonHome(); |
| 493 | wchar_t *envpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 494 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 495 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | int skiphome, skipdefault; |
| 497 | wchar_t *machinepath = NULL; |
| 498 | wchar_t *userpath = NULL; |
| 499 | wchar_t zip_path[MAXPATHLEN+1]; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 500 | int applocal = 0; |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | if (!Py_IgnoreEnvironmentFlag) { |
| 503 | envpath = _wgetenv(L"PYTHONPATH"); |
| 504 | } |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 505 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | char *_envpath = Py_GETENV("PYTHONPATH"); |
| 507 | wchar_t wenvpath[MAXPATHLEN+1]; |
| 508 | if (_envpath) { |
| 509 | size_t r = mbstowcs(wenvpath, _envpath, MAXPATHLEN+1); |
| 510 | envpath = wenvpath; |
| 511 | if (r == (size_t)-1 || r >= MAXPATHLEN) |
| 512 | envpath = NULL; |
| 513 | } |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 514 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | get_progpath(); |
| 517 | /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 518 | wcscpy_s(argv0_path, MAXPATHLEN+1, progpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | reduce(argv0_path); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 520 | |
| 521 | /* Search for an environment configuration file, first in the |
| 522 | executable's directory and then in the parent directory. |
| 523 | If found, open it for use when searching for prefixes. |
| 524 | */ |
| 525 | |
| 526 | { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 527 | wchar_t envbuffer[MAXPATHLEN+1]; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 528 | wchar_t tmpbuffer[MAXPATHLEN+1]; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 529 | const wchar_t *env_cfg = L"pyvenv.cfg"; |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 530 | FILE * env_file = NULL; |
| 531 | |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 532 | wcscpy_s(envbuffer, MAXPATHLEN+1, argv0_path); |
| 533 | join(envbuffer, env_cfg); |
| 534 | env_file = _Py_wfopen(envbuffer, L"r"); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 535 | if (env_file == NULL) { |
| 536 | errno = 0; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 537 | reduce(envbuffer); |
| 538 | reduce(envbuffer); |
| 539 | join(envbuffer, env_cfg); |
| 540 | env_file = _Py_wfopen(envbuffer, L"r"); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 541 | if (env_file == NULL) { |
| 542 | errno = 0; |
| 543 | } |
| 544 | } |
| 545 | if (env_file != NULL) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 546 | /* Look for an 'applocal' variable and, if true, ignore all registry |
| 547 | * keys and environment variables, but retain the default paths |
| 548 | * (DLLs, Lib) and the zip file. Setting pythonhome here suppresses |
| 549 | * the search for LANDMARK below and overrides %PYTHONHOME%. |
| 550 | */ |
| 551 | if (find_env_config_value(env_file, L"applocal", tmpbuffer) && |
| 552 | (applocal = (wcsicmp(tmpbuffer, L"true") == 0))) { |
| 553 | envpath = NULL; |
| 554 | pythonhome = argv0_path; |
| 555 | } |
Victor Stinner | ccb1f8c | 2016-03-23 11:31:58 +0100 | [diff] [blame] | 556 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 557 | /* Look for a 'home' variable and set argv0_path to it, if found */ |
| 558 | if (find_env_config_value(env_file, L"home", tmpbuffer)) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 559 | wcscpy_s(argv0_path, MAXPATHLEN+1, tmpbuffer); |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 560 | } |
| 561 | fclose(env_file); |
| 562 | env_file = NULL; |
| 563 | } |
| 564 | } |
| 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 567 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 568 | pythonhome = prefix; |
| 569 | else |
| 570 | pythonhome = NULL; |
| 571 | } |
| 572 | else |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 573 | wcscpy_s(prefix, MAXPATHLEN+1, pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | if (envpath && *envpath == '\0') |
| 576 | envpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 578 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 579 | #ifdef MS_WINDOWS |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 580 | /* Calculate zip archive path from DLL or exe path */ |
| 581 | if (wcscpy_s(zip_path, MAXPATHLEN+1, dllpath[0] ? dllpath : progpath)) |
| 582 | /* exceeded buffer length - ignore zip_path */ |
| 583 | zip_path[0] = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | else { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 585 | wchar_t *dot = wcsrchr(zip_path, '.'); |
| 586 | if (!dot || wcscpy_s(dot, MAXPATHLEN+1 - (dot - zip_path), L".zip")) |
| 587 | /* exceeded buffer length - ignore zip_path */ |
| 588 | zip_path[0] = L'\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | skiphome = pythonhome==NULL ? 0 : 1; |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 592 | #ifdef Py_ENABLE_SHARED |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 593 | if (!applocal) { |
| 594 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 595 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 596 | } |
Martin v. Löwis | bc186a8 | 2009-02-02 15:32:22 +0000 | [diff] [blame] | 597 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | /* We only use the default relative PYTHONPATH if we havent |
| 599 | anything better to use! */ |
| 600 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 601 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 602 | #endif |
| 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | /* We need to construct a path from the following parts. |
| 605 | (1) the PYTHONPATH environment variable, if set; |
| 606 | (2) for Win32, the zip archive file path; |
| 607 | (3) for Win32, the machinepath and userpath, if set; |
| 608 | (4) the PYTHONPATH config macro, with the leading "." |
| 609 | of each component replaced with pythonhome, if set; |
| 610 | (5) the directory containing the executable (argv0_path). |
| 611 | The length calculation calculates #4 first. |
| 612 | Extra rules: |
| 613 | - If PYTHONHOME is set (in any way) item (3) is ignored. |
| 614 | - If registry values are used, (4) and (5) are ignored. |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 615 | - If applocal is set, (1), (3), and registry values are ignored |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | /* Calculate size of return buffer */ |
| 619 | if (pythonhome != NULL) { |
| 620 | wchar_t *p; |
| 621 | bufsz = 1; |
| 622 | for (p = PYTHONPATH; *p; p++) { |
| 623 | if (*p == DELIM) |
| 624 | bufsz++; /* number of DELIM plus one */ |
| 625 | } |
Steve Dower | f64b9d5 | 2015-05-23 17:34:50 -0700 | [diff] [blame] | 626 | bufsz *= wcslen(pythonhome); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | } |
| 628 | else |
| 629 | bufsz = 0; |
Steve Dower | f64b9d5 | 2015-05-23 17:34:50 -0700 | [diff] [blame] | 630 | bufsz += wcslen(PYTHONPATH) + 1; |
| 631 | bufsz += wcslen(argv0_path) + 1; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 632 | #ifdef MS_WINDOWS |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 633 | if (!applocal && userpath) |
Steve Dower | f64b9d5 | 2015-05-23 17:34:50 -0700 | [diff] [blame] | 634 | bufsz += wcslen(userpath) + 1; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 635 | if (!applocal && machinepath) |
Steve Dower | f64b9d5 | 2015-05-23 17:34:50 -0700 | [diff] [blame] | 636 | bufsz += wcslen(machinepath) + 1; |
| 637 | bufsz += wcslen(zip_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 638 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | if (envpath != NULL) |
Steve Dower | f64b9d5 | 2015-05-23 17:34:50 -0700 | [diff] [blame] | 640 | bufsz += wcslen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 641 | |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 642 | module_search_path = buf = PyMem_RawMalloc(bufsz*sizeof(wchar_t)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | if (buf == NULL) { |
| 644 | /* We can't exit, so print a warning and limp along */ |
| 645 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 646 | if (envpath) { |
| 647 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
| 648 | module_search_path = envpath; |
| 649 | } |
| 650 | else { |
| 651 | fprintf(stderr, "Using default static path.\n"); |
| 652 | module_search_path = PYTHONPATH; |
| 653 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 654 | #ifdef MS_WINDOWS |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 655 | PyMem_RawFree(machinepath); |
| 656 | PyMem_RawFree(userpath); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 657 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | return; |
| 659 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 661 | if (envpath) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 662 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), envpath)) |
| 663 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | buf = wcschr(buf, L'\0'); |
| 665 | *buf++ = DELIM; |
| 666 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 667 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | if (zip_path[0]) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 669 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), zip_path)) |
| 670 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | buf = wcschr(buf, L'\0'); |
| 672 | *buf++ = DELIM; |
| 673 | } |
| 674 | if (userpath) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 675 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), userpath)) |
| 676 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 677 | buf = wcschr(buf, L'\0'); |
| 678 | *buf++ = DELIM; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 679 | PyMem_RawFree(userpath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | } |
| 681 | if (machinepath) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 682 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), machinepath)) |
| 683 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 684 | buf = wcschr(buf, L'\0'); |
| 685 | *buf++ = DELIM; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 686 | PyMem_RawFree(machinepath); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | } |
| 688 | if (pythonhome == NULL) { |
| 689 | if (!skipdefault) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 690 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), PYTHONPATH)) |
| 691 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | buf = wcschr(buf, L'\0'); |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 693 | *buf++ = DELIM; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 696 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | if (pythonhome == NULL) { |
| 698 | wcscpy(buf, PYTHONPATH); |
| 699 | buf = wcschr(buf, L'\0'); |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 700 | *buf++ = DELIM; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 702 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | else { |
| 704 | wchar_t *p = PYTHONPATH; |
| 705 | wchar_t *q; |
| 706 | size_t n; |
| 707 | for (;;) { |
| 708 | q = wcschr(p, DELIM); |
| 709 | if (q == NULL) |
| 710 | n = wcslen(p); |
| 711 | else |
| 712 | n = q-p; |
| 713 | if (p[0] == '.' && is_sep(p[1])) { |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 714 | if (wcscpy_s(buf, bufsz - (buf - module_search_path), pythonhome)) |
| 715 | Py_FatalError("buffer overflow in getpathp.c's calculate_path()"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 716 | buf = wcschr(buf, L'\0'); |
| 717 | p++; |
| 718 | n--; |
| 719 | } |
| 720 | wcsncpy(buf, p, n); |
| 721 | buf += n; |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 722 | *buf++ = DELIM; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | if (q == NULL) |
| 724 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | p = q+1; |
| 726 | } |
| 727 | } |
| 728 | if (argv0_path) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | wcscpy(buf, argv0_path); |
| 730 | buf = wcschr(buf, L'\0'); |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 731 | *buf++ = DELIM; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | } |
Steve Dower | 4a7fe7e | 2015-05-22 15:10:10 -0700 | [diff] [blame] | 733 | *(buf - 1) = L'\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | /* Now to pull one last hack/trick. If sys.prefix is |
| 735 | empty, then try and find it somewhere on the paths |
| 736 | we calculated. We scan backwards, as our general policy |
| 737 | is that Python core directories are at the *end* of |
| 738 | sys.path. We assume that our "lib" directory is |
| 739 | on the path, and that our 'prefix' directory is |
| 740 | the parent of that. |
| 741 | */ |
| 742 | if (*prefix==L'\0') { |
| 743 | wchar_t lookBuf[MAXPATHLEN+1]; |
| 744 | wchar_t *look = buf - 1; /* 'buf' is at the end of the buffer */ |
| 745 | while (1) { |
| 746 | Py_ssize_t nchars; |
| 747 | wchar_t *lookEnd = look; |
| 748 | /* 'look' will end up one character before the |
| 749 | start of the path in question - even if this |
| 750 | is one character before the start of the buffer |
| 751 | */ |
| 752 | while (look >= module_search_path && *look != DELIM) |
| 753 | look--; |
| 754 | nchars = lookEnd-look; |
| 755 | wcsncpy(lookBuf, look+1, nchars); |
| 756 | lookBuf[nchars] = L'\0'; |
| 757 | /* Up one level to the parent */ |
| 758 | reduce(lookBuf); |
| 759 | if (search_for_prefix(lookBuf, LANDMARK)) { |
| 760 | break; |
| 761 | } |
| 762 | /* If we are out of paths to search - give up */ |
| 763 | if (look < module_search_path) |
| 764 | break; |
| 765 | look--; |
| 766 | } |
| 767 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | |
| 771 | /* External interface */ |
| 772 | |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 773 | void |
| 774 | Py_SetPath(const wchar_t *path) |
| 775 | { |
| 776 | if (module_search_path != NULL) { |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 777 | PyMem_RawFree(module_search_path); |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 778 | module_search_path = NULL; |
| 779 | } |
| 780 | if (path != NULL) { |
| 781 | extern wchar_t *Py_GetProgramName(void); |
| 782 | wchar_t *prog = Py_GetProgramName(); |
| 783 | wcsncpy(progpath, prog, MAXPATHLEN); |
| 784 | prefix[0] = L'\0'; |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 785 | 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] | 786 | if (module_search_path != NULL) |
| 787 | wcscpy(module_search_path, path); |
Victor Stinner | 1a7425f | 2013-07-07 16:25:15 +0200 | [diff] [blame] | 788 | } |
Kristján Valur Jónsson | 3b69db2 | 2010-09-27 05:32:54 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 791 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 792 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | if (!module_search_path) |
| 795 | calculate_path(); |
| 796 | return module_search_path; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 799 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 800 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | if (!module_search_path) |
| 803 | calculate_path(); |
| 804 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 807 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 808 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 809 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 813 | wchar_t * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 814 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | if (!module_search_path) |
| 817 | calculate_path(); |
| 818 | return progpath; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 819 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 820 | |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 821 | /* Load python3.dll before loading any extension module that might refer |
| 822 | 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] | 823 | to this python DLL is loaded, not a python3.dll that might be on the path |
| 824 | by chance. |
| 825 | Return whether the DLL was found. |
| 826 | */ |
| 827 | static int python3_checked = 0; |
| 828 | static HANDLE hPython3; |
| 829 | int |
| 830 | _Py_CheckPython3() |
| 831 | { |
| 832 | wchar_t py3path[MAXPATHLEN+1]; |
| 833 | wchar_t *s; |
| 834 | if (python3_checked) |
| 835 | return hPython3 != NULL; |
| 836 | python3_checked = 1; |
| 837 | |
| 838 | /* If there is a python3.dll next to the python3y.dll, |
| 839 | assume this is a build tree; use that DLL */ |
| 840 | wcscpy(py3path, dllpath); |
| 841 | s = wcsrchr(py3path, L'\\'); |
| 842 | if (!s) |
| 843 | s = py3path; |
| 844 | wcscpy(s, L"\\python3.dll"); |
| 845 | hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 846 | if (hPython3 != NULL) |
| 847 | return 1; |
| 848 | |
| 849 | /* Check sys.prefix\DLLs\python3.dll */ |
| 850 | wcscpy(py3path, Py_GetPrefix()); |
| 851 | wcscat(py3path, L"\\DLLs\\python3.dll"); |
| 852 | hPython3 = LoadLibraryExW(py3path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 853 | return hPython3 != NULL; |
| 854 | } |