|
| 1 | +From 2b2d1a5985b12c4c053e99d65809e1ff7dbd07ec Mon Sep 17 00:00:00 2001 |
| 2 | +From: Naveen M K < [email protected]> |
| 3 | +Date: Sat, 5 Aug 2023 14:10:27 +0530 |
| 4 | +Subject: [PATCH 141/N] Search DLLs only on paths added using |
| 5 | + `add_dll_directory()`. |
| 6 | + |
| 7 | +This is the default behavior on upstream Python. We previously |
| 8 | +reverted this behavior because it broke some use cases. |
| 9 | + |
| 10 | +The old behavior can be restored by setting the environment variable |
| 11 | +PYTHONLEGACYWINDOWSDLLLOADING to 1. |
| 12 | + |
| 13 | +Fixes https://github.com/msys2-contrib/cpython-mingw/issues/48 |
| 14 | +Also fixes https://github.com/msys2-contrib/cpython-mingw/issues/141 |
| 15 | + |
| 16 | +Signed-off-by: Naveen M K < [email protected]> |
| 17 | +--- |
| 18 | + .github/workflows/mingw.yml | 10 +++++----- |
| 19 | + Lib/test/__main__.py | 8 ++++++++ |
| 20 | + Python/dynload_win.c | 16 ++++++++++++++-- |
| 21 | + mingw_smoketests.py | 3 +++ |
| 22 | + setup.py | 4 ++++ |
| 23 | + 5 files changed, 34 insertions(+), 7 deletions(-) |
| 24 | + |
| 25 | +diff --git a/.github/workflows/mingw.yml b/.github/workflows/mingw.yml |
| 26 | +index 4d1cdcb..cbaf801 100644 |
| 27 | +--- a/.github/workflows/mingw.yml |
| 28 | ++++ b/.github/workflows/mingw.yml |
| 29 | +@@ -130,16 +130,16 @@ jobs: |
| 30 | + cp "${pkgdir}${MINGW_PREFIX}"/bin/idle3 "${pkgdir}${MINGW_PREFIX}"/bin/idle |
| 31 | + cp "${pkgdir}${MINGW_PREFIX}"/bin/pydoc3 "${pkgdir}${MINGW_PREFIX}"/bin/pydoc |
| 32 | + |
| 33 | ++ # copy to / |
| 34 | ++ cp -rf "${pkgdir}"/* / |
| 35 | ++ |
| 36 | + - name: Run Smoke Test (installed) |
| 37 | + shell: msys2 {0} |
| 38 | + run: | |
| 39 | + export SETUPTOOLS_USE_DISTUTILS=stdlib |
| 40 | +- export PYTHONTZPATH="${MINGW_PREFIX}/share/zoneinfo" |
| 41 | + SMOKETESTS="$(pwd)/mingw_smoketests.py" |
| 42 | +- cd _build |
| 43 | +- cd python_pkgdir/${MINGW_PREFIX}/bin |
| 44 | +- ./python.exe "$SMOKETESTS" |
| 45 | +- MSYSTEM= ./python.exe "$SMOKETESTS" |
| 46 | ++ ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS" |
| 47 | ++ MSYSTEM= ${MINGW_PREFIX}/bin/python.exe "$SMOKETESTS" |
| 48 | + |
| 49 | + - name: Compress |
| 50 | + if: always() |
| 51 | +diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py |
| 52 | +index 19a6b2b..7641b32 100644 |
| 53 | +--- a/Lib/test/__main__.py |
| 54 | ++++ b/Lib/test/__main__.py |
| 55 | +@@ -1,2 +1,10 @@ |
| 56 | ++import os |
| 57 | ++import sys |
| 58 | ++ |
| 59 | + from test.libregrtest import main |
| 60 | ++ |
| 61 | ++if sys.platform == "win32": |
| 62 | ++ # Enable DLL loading from PATH. |
| 63 | ++ os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1" |
| 64 | ++ |
| 65 | + main() |
| 66 | +diff --git a/Python/dynload_win.c b/Python/dynload_win.c |
| 67 | +index 79b60be..fcb7321 100644 |
| 68 | +--- a/Python/dynload_win.c |
| 69 | ++++ b/Python/dynload_win.c |
| 70 | +@@ -4,6 +4,7 @@ |
| 71 | + #include "Python.h" |
| 72 | + #include "pycore_fileutils.h" // _Py_add_relfile() |
| 73 | + #include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 74 | ++#include "pycore_initconfig.h" |
| 75 | + |
| 76 | + #ifdef HAVE_DIRECT_H |
| 77 | + #include <direct.h> |
| 78 | +@@ -223,6 +224,18 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, |
| 79 | + dl_funcptr p; |
| 80 | + char funcname[258], *import_python; |
| 81 | + |
| 82 | ++ int use_legacy = 0; |
| 83 | ++ DWORD load_library_flags = 0; |
| 84 | ++ |
| 85 | ++ _Py_get_env_flag(1, &use_legacy, "PYTHONLEGACYWINDOWSDLLLOADING"); |
| 86 | ++ |
| 87 | ++ if (use_legacy) { |
| 88 | ++ load_library_flags = LOAD_WITH_ALTERED_SEARCH_PATH; |
| 89 | ++ } else { |
| 90 | ++ load_library_flags = LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | |
| 91 | ++ LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR; |
| 92 | ++ } |
| 93 | ++ |
| 94 | + #ifdef _MSC_VER |
| 95 | + _Py_CheckPython3(); |
| 96 | + #endif |
| 97 | +@@ -249,8 +262,7 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, |
| 98 | + AddDllDirectory function. We add SEARCH_DLL_LOAD_DIR to |
| 99 | + ensure DLLs adjacent to the PYD are preferred. */ |
| 100 | + Py_BEGIN_ALLOW_THREADS |
| 101 | +- hDLL = LoadLibraryExW(wpathname, NULL, |
| 102 | +- LOAD_WITH_ALTERED_SEARCH_PATH); |
| 103 | ++ hDLL = LoadLibraryExW(wpathname, NULL, load_library_flags); |
| 104 | + Py_END_ALLOW_THREADS |
| 105 | + #if !USE_UNICODE_WCHAR_CACHE |
| 106 | + PyMem_Free(wpathname); |
| 107 | +diff --git a/mingw_smoketests.py b/mingw_smoketests.py |
| 108 | +index 516005c..ca1f652 100644 |
| 109 | +--- a/mingw_smoketests.py |
| 110 | ++++ b/mingw_smoketests.py |
| 111 | +@@ -34,6 +34,9 @@ if os.environ.get("MSYSTEM", ""): |
| 112 | + else: |
| 113 | + SEP = "\\" |
| 114 | + |
| 115 | ++if sysconfig.is_python_build(): |
| 116 | ++ os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1" |
| 117 | ++ |
| 118 | + _UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686') |
| 119 | + |
| 120 | + |
| 121 | +diff --git a/setup.py b/setup.py |
| 122 | +index 8d2e251..99ef3ed 100644 |
| 123 | +--- a/setup.py |
| 124 | ++++ b/setup.py |
| 125 | +@@ -88,6 +88,10 @@ if sys.platform == "win32" and os.environ.get("MSYSTEM", ""): |
| 126 | + return os_system(command_in_sh) |
| 127 | + os.system = msys_system |
| 128 | + |
| 129 | ++ # set PYTHONLEGACYWINDOWSDLLLOADING to 1 to load DLLs from PATH |
| 130 | ++ # This is needed while building core extensions of Python |
| 131 | ++ os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1" |
| 132 | ++ |
| 133 | + CROSS_COMPILING = ("_PYTHON_HOST_PLATFORM" in os.environ) |
| 134 | + HOST_PLATFORM = get_platform() |
| 135 | + MS_WINDOWS = (HOST_PLATFORM == 'win32' or HOST_PLATFORM == 'mingw') |
0 commit comments