Skip to content

Search DLLs only on paths added using add_dll_directory(). #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ jobs:
cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle
cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc

# copy to /
cp -rf "${pkgdir}"/* /

- name: Run Smoke Test (installed)
shell: msys2 {0}
run: |
export SETUPTOOLS_USE_DISTUTILS=stdlib
export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo"
SMOKETESTS="$(pwd)/mingw_smoketests.py"
cd _build
cd python_pkgdir/${MINGW_PREFIX}/bin
./python.exe "$SMOKETESTS"
MSYSTEM= ./python.exe "$SMOKETESTS"
${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"
MSYSTEM= ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS"

- name: Compress
if: always()
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()
16 changes: 14 additions & 2 deletions Python/dynload_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_fileutils.h" // _Py_add_relfile()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_initconfig.h"

#ifdef HAVE_DIRECT_H
#include <direct.h>
Expand Down Expand Up @@ -223,6 +224,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
dl_funcptr p;
char funcname[258], *import_python;

int use_legacy = 0;
DWORD load_library_flags = 0;

_Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING");

if (use_legacy) {
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 +262,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 load DLLs from PATH
# This is needed while building core extensions of Python
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