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