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