Skip to content

Commit edd105c

Browse files
committed
1 parent 87d3a63 commit edd105c

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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')

mingw-w64-python/PKGBUILD

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ else
2222
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}${_pybasever}")
2323
fi
2424
pkgver=${_pybasever}.4
25-
pkgrel=4
25+
pkgrel=5
2626
pkgdesc="A high-level scripting language (mingw-w64)"
2727
arch=('any')
2828
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
@@ -187,7 +187,8 @@ source=("https://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz
187187
0137-gh-105821-Use-a-raw-f-string-in-test_httpservers.py-.patch
188188
0138-getpath-use-normpath-on-all-generated-paths.patch
189189
0139-pathconfig-normpath-sys.path-0.patch
190-
0140-smoketests-add-some-tests-for-sys-site-paths.patch)
190+
0140-smoketests-add-some-tests-for-sys-site-paths.patch
191+
0141-Search-DLLs-only-on-paths-added-using-add_dll_direct.patch)
191192

192193
# Helper macros to help make tasks easier #
193194
apply_patch_with_msg() {
@@ -340,7 +341,8 @@ prepare() {
340341
0137-gh-105821-Use-a-raw-f-string-in-test_httpservers.py-.patch \
341342
0138-getpath-use-normpath-on-all-generated-paths.patch \
342343
0139-pathconfig-normpath-sys.path-0.patch \
343-
0140-smoketests-add-some-tests-for-sys-site-paths.patch
344+
0140-smoketests-add-some-tests-for-sys-site-paths.patch \
345+
0141-Search-DLLs-only-on-paths-added-using-add_dll_direct.patch
344346

345347
autoreconf -vfi
346348
}
@@ -581,4 +583,5 @@ sha256sums=('2f0e409df2ab57aa9fc4cbddfb976af44e4e55bf6f619eee6bc5c2297264a7f6'
581583
'e1902d363b5c4ddf8c94a1892d13709423bd72c08423351b2c2e655fdad72a5a'
582584
'87c6d54604d60bb097a21723e89fbc7c0ebaf9f0de169613af79f09cd02b1554'
583585
'66adf06f722cd36780b066a561c804f99202d2d0a83c877e7ddeda8d47ce1c64'
584-
'630de3ac36b8eb96d556d6ce418629f0834530a801d7c72c7565d642f5208447')
586+
'630de3ac36b8eb96d556d6ce418629f0834530a801d7c72c7565d642f5208447'
587+
'8b579b27e350e06b28112163c0996a8d4da8f5f57210141581c2adf2501f0d2f')

0 commit comments

Comments
 (0)