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