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