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 #48
Also fixes #141

Signed-off-by: Naveen M K <naveen521kk@gmail.com>
  • Loading branch information
naveen521kk committed Aug 5, 2024
1 parent f2d1898 commit cd29270
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/mingw.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ 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 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.main import main

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

main(_add_python_opts=True)
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 @@ -225,6 +226,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 /* _MSC_VER */
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
PyMem_Free(wpathname);

Expand Down
4 changes: 4 additions & 0 deletions Tools/build/check_extension_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"winsound",
}

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


logger = logging.getLogger(__name__)

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

0 comments on commit cd29270

Please sign in to comment.