Skip to content

Commit 5a1fa7e

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 1f2fc73 commit 5a1fa7e

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

.github/workflows/mingw.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@ jobs:
126126
cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle
127127
cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc
128128
129+
# copy to /
130+
cp -rf "${pkgdir}"/* /
131+
129132
- name: Run Smoke Test (installed)
130133
shell: msys2 {0}
131134
run: |
132-
export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo"
133135
SMOKETESTS="$(pwd)/mingw_smoketests.py"
134-
cd _build
135-
cd python_pkgdir/${MINGW_PREFIX}/bin
136-
./python.exe "$SMOKETESTS"
137-
MSYSTEM= ./python.exe "$SMOKETESTS"
136+
${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
137+
MSYSTEM= ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
138138
139139
- name: Compress
140140
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

Tools/build/check_extension_modules.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
"winsound",
6262
}
6363

64+
if sys.platform == "win32":
65+
# Enable DLL loading from PATH.
66+
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
67+
6468

6569
logger = logging.getLogger(__name__)
6670

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)