Skip to content

Commit

Permalink
Search DLLs only on paths added using add_dll_directory().
Browse files Browse the repository at this point in the history
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 <naveen521kk@gmail.com>
  • Loading branch information
naveen521kk committed Aug 5, 2023
1 parent a2f2087 commit bc7b70f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
run: |
export SETUPTOOLS_USE_DISTUTILS=stdlib
export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo"
export PYTHONLEGACYWINDOWSDLLLOADING=1
SMOKETESTS="$(pwd)/mingw_smoketests.py"
cd _build
cd python_pkgdir/${MINGW_PREFIX}/bin
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/__main__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
import os
import sys

from test.libregrtest import main

if sys.platform == "win32":
# Enable DLL loading from PATH.
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"

main()
13 changes: 11 additions & 2 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
dl_funcptr p;
char funcname[258], *import_python;

wchar_t* use_legacy = _wgetenv(L"PYTHONLEGACYWINDOWSDLLLOADING");
DWORD load_library_flags = 0;

if (use_legacy && wcscmp(use_legacy, L"1") == 0) {
load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH;
} else {
load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR;
}

#ifdef _MSC_VER
_Py_CheckPython3();
#endif
Expand All @@ -249,8 +259,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to
ensure DLLs adjacent to the PYD are preferred. */
Py_BEGIN_ALLOW_THREADS
hDLL = LoadLibraryExW(wpathname, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags);
Py_END_ALLOW_THREADS
#if !USE_UNICODE_WCHAR_CACHE
PyMem_Free(wpathname);
Expand Down
3 changes: 3 additions & 0 deletions mingw_smoketests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
else:
SEP = "\\"

if sysconfig.is_python_build():
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"

_UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')


Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def msys_system(command):
return os_system(command_in_sh)
os.system = msys_system

# set PYTHONLEGACYWINDOWSDLLLOADING to 1 to enable the legacy Windows DLL
# loading behavior. This is needed while building Python itself.
os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"

CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ)
HOST_PLATFORM = get_platform()
MS_WINDOWS = (HOST_PLATFORM == 'win32' or HOST_PLATFORM == 'mingw')
Expand Down

0 comments on commit bc7b70f

Please sign in to comment.