Skip to content

Commit d3f985f

Browse files
committed
Search DLLs only on paths added using add_dll_directory().
This is the default behavior on upstream Python. We previously reverted this behavior because it broke some use cases. The old behavior can be restored by setting the environment variable PYTHONLEGACYWINDOWSDLLLOADING to 1. Fixes msys2-contrib#48 Also fixes msys2-contrib#141 Signed-off-by: Naveen M K <[email protected]>
1 parent a2f2087 commit d3f985f

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

.github/workflows/mingw.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ jobs:
130130
cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle
131131
cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc
132132
133+
# copy to /
134+
cp -rf "${pkgdir}"/* /
135+
133136
- name: Run Smoke Test (installed)
134137
shell: msys2 {0}
135138
run: |

Lib/test/__main__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
import os
2+
import sys
3+
14
from test.libregrtest import main
5+
6+
if sys.platform == "win32":
7+
# Enable DLL loading from PATH.
8+
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
9+
210
main()

Python/dynload_win.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
223223
dl_funcptr p;
224224
char funcname[258], *import_python;
225225

226+
wchar_t* use_legacy = _wgetenv(L"PYTHONLEGACYWINDOWSDLLLOADING");
227+
DWORD load_library_flags = 0;
228+
229+
if (use_legacy && wcscmp(use_legacy, L"1") == 0) {
230+
load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
231+
} else {
232+
load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
233+
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
234+
}
235+
226236
#ifdef _MSC_VER
227237
_Py_CheckPython3();
228238
#endif
@@ -249,8 +259,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
249259
AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
250260
ensure DLLs adjacent to the PYD are preferred. */
251261
Py_BEGIN_ALLOW_THREADS
252-
hDLL = LoadLibraryExW(wpathname, NULL,
253-
LOAD_WITH_ALTERED_SEARCH_PATH);
262+
hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags);
254263
Py_END_ALLOW_THREADS
255264
#if !USE_UNICODE_WCHAR_CACHE
256265
PyMem_Free(wpathname);

mingw_smoketests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
else:
3535
SEP = "\\"
3636

37+
if sysconfig.is_python_build():
38+
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
39+
3740
_UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')
3841

3942

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def msys_system(command):
8888
return os_system(command_in_sh)
8989
os.system = msys_system
9090

91+
# set PYTHONLEGACYWINDOWSDLLLOADING to 1 to load DLLs from PATH
92+
# This is needed while building core extensions of Python
93+
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
94+
9195
CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
9296
HOST_PLATFORM = get_platform()
9397
MS_WINDOWS = (HOST_PLATFORM == 'win32' or HOST_PLATFORM == 'mingw')

0 commit comments

Comments
 (0)