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