Skip to content

Commit 9296a4a

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 c41019c commit 9296a4a

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
@@ -124,15 +124,15 @@ jobs:
124124
cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle
125125
cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc
126126
127+
# copy to /
128+
cp -rf "${pkgdir}"/* /
129+
127130
- name: Run Smoke Test (installed)
128131
shell: msys2 {0}
129132
run: |
130-
export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo"
131133
SMOKETESTS="$(pwd)/mingw_smoketests.py"
132-
cd _build
133-
cd python_pkgdir/${MINGW_PREFIX}/bin
134-
./python.exe "$SMOKETESTS"
135-
MSYSTEM= ./python.exe "$SMOKETESTS"
134+
${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
135+
MSYSTEM= ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
136136
137137
- name: Compress
138138
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
#include "pycore_importdl.h" // dl_funcptr
910
#include "patchlevel.h" // PY_MAJOR_VERSION
@@ -206,6 +207,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
206207
dl_funcptr p;
207208
char funcname[258], *import_python;
208209

210+
int use_legacy = 0;
211+
DWORD load_library_flags = 0;
212+
213+
_Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING");
214+
215+
if (use_legacy) {
216+
load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
217+
} else {
218+
load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
219+
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
220+
}
221+
209222
#ifdef _MSC_VER
210223
_Py_CheckPython3();
211224
#endif /* _MSC_VER */
@@ -230,8 +243,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
230243
AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
231244
ensure DLLs adjacent to the PYD are preferred. */
232245
Py_BEGIN_ALLOW_THREADS
233-
hDLL = LoadLibraryExW(wpathname, NULL,
234-
LOAD_WITH_ALTERED_SEARCH_PATH);
246+
hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags);
235247
Py_END_ALLOW_THREADS
236248
PyMem_Free(wpathname);
237249

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)