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