Skip to content

Commit 2b2d1a5

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 #48 Also fixes #141 Signed-off-by: Naveen M K <[email protected]>
1 parent a2f2087 commit 2b2d1a5

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
@@ -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 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: 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>
@@ -223,6 +224,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
223224
dl_funcptr p;
224225
char funcname[258], *import_python;
225226

227+
int use_legacy = 0;
228+
DWORD load_library_flags = 0;
229+
230+
_Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING");
231+
232+
if (use_legacy) {
233+
load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
234+
} else {
235+
load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
236+
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
237+
}
238+
226239
#ifdef _MSC_VER
227240
_Py_CheckPython3();
228241
#endif
@@ -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
#if !USE_UNICODE_WCHAR_CACHE
256268
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)