Skip to content

Commit eb863f4

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 f4c52b9 commit eb863f4

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

.github/workflows/mingw.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ 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: |
136139
export SETUPTOOLS_USE_DISTUTILS=stdlib
137-
export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo"
138140
SMOKETESTS="$(pwd)/mingw_smoketests.py"
139-
cd _build
140-
cd python_pkgdir/${MINGW_PREFIX}/bin
141-
./python.exe "$SMOKETESTS"
142-
MSYSTEM= ./python.exe "$SMOKETESTS"
141+
${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
142+
MSYSTEM= ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
143143
144144
- name: Compress
145145
if: always()

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.main import main
5+
6+
if sys.platform == "win32":
7+
# Enable DLL loading from PATH.
8+
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
9+
210
main(_add_python_opts=True)

Python/dynload_win.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Python.h"
55
#include "pycore_fileutils.h" // _Py_add_relfile()
66
#include "pycore_pystate.h" // _PyInterpreterState_GET()
7+
#include "pycore_initconfig.h"
78

89
#ifdef HAVE_DIRECT_H
910
#include <direct.h>
@@ -225,6 +226,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
225226
dl_funcptr p;
226227
char funcname[258], *import_python;
227228

229+
int use_legacy = 0;
230+
DWORD load_library_flags = 0;
231+
232+
_Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING");
233+
234+
if (use_legacy) {
235+
load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
236+
} else {
237+
load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
238+
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
239+
}
240+
228241
#ifdef _MSC_VER
229242
_Py_CheckPython3();
230243
#endif /* _MSC_VER */
@@ -249,8 +262,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
249262
AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
250263
ensure DLLs adjacent to the PYD are preferred. */
251264
Py_BEGIN_ALLOW_THREADS
252-
hDLL = LoadLibraryExW(wpathname, NULL,
253-
LOAD_WITH_ALTERED_SEARCH_PATH);
265+
hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags);
254266
Py_END_ALLOW_THREADS
255267
PyMem_Free(wpathname);
256268

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

0 commit comments

Comments
 (0)