Skip to content

Commit

Permalink
Merge pull request msys2#17969 from naveen521kk/py-update-dll
Browse files Browse the repository at this point in the history
python: update
  • Loading branch information
lazka authored Aug 7, 2023
2 parents e12afcd + edd105c commit 748578f
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
From 2b2d1a5985b12c4c053e99d65809e1ff7dbd07ec Mon Sep 17 00:00:00 2001
From: Naveen M K <naveen521kk@gmail.com>
Date: Sat, 5 Aug 2023 14:10:27 +0530
Subject: [PATCH 141/N] Search DLLs only on paths added using
`add_dll_directory()`.

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 https://github.com/msys2-contrib/cpython-mingw/issues/48
Also fixes https://github.com/msys2-contrib/cpython-mingw/issues/141

Signed-off-by: Naveen M K <naveen521kk@gmail.com>
---
.github/workflows/mingw.yml | 10 +++++-----
Lib/test/__main__.py | 8 ++++++++
Python/dynload_win.c | 16 ++++++++++++++--
mingw_smoketests.py | 3 +++
setup.py | 4 ++++
5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/.github/workflows/mingw.yml b/.github/workflows/mingw.yml
index 4d1cdcb..cbaf801 100644
--- a/.github/workflows/mingw.yml
+++ b/.github/workflows/mingw.yml
@@ -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()
diff --git a/Lib/test/__main__.py b/Lib/test/__main__.py
index 19a6b2b..7641b32 100644
--- a/Lib/test/__main__.py
+++ b/Lib/test/__main__.py
@@ -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()
diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 79b60be..fcb7321 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -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>
@@ -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
@@ -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);
diff --git a/mingw_smoketests.py b/mingw_smoketests.py
index 516005c..ca1f652 100644
--- a/mingw_smoketests.py
+++ b/mingw_smoketests.py
@@ -34,6 +34,9 @@ if os.environ.get("MSYSTEM", ""):
else:
SEP = "\\"

+if sysconfig.is_python_build():
+ os.environ["PYTHONLEGACYWINDOWSDLLLOADING"] = "1"
+
_UCRT = sysconfig.get_platform() not in ('mingw_x86_64', 'mingw_i686')


diff --git a/setup.py b/setup.py
index 8d2e251..99ef3ed 100644
--- a/setup.py
+++ b/setup.py
@@ -88,6 +88,10 @@ if sys.platform == "win32" and os.environ.get("MSYSTEM", ""):
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')
11 changes: 7 additions & 4 deletions mingw-w64-python/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ else
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}${_pybasever}")
fi
pkgver=${_pybasever}.4
pkgrel=4
pkgrel=5
pkgdesc="A high-level scripting language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
Expand Down Expand Up @@ -187,7 +187,8 @@ source=("https://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz
0137-gh-105821-Use-a-raw-f-string-in-test_httpservers.py-.patch
0138-getpath-use-normpath-on-all-generated-paths.patch
0139-pathconfig-normpath-sys.path-0.patch
0140-smoketests-add-some-tests-for-sys-site-paths.patch)
0140-smoketests-add-some-tests-for-sys-site-paths.patch
0141-Search-DLLs-only-on-paths-added-using-add_dll_direct.patch)

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

autoreconf -vfi
}
Expand Down Expand Up @@ -581,4 +583,5 @@ sha256sums=('2f0e409df2ab57aa9fc4cbddfb976af44e4e55bf6f619eee6bc5c2297264a7f6'
'e1902d363b5c4ddf8c94a1892d13709423bd72c08423351b2c2e655fdad72a5a'
'87c6d54604d60bb097a21723e89fbc7c0ebaf9f0de169613af79f09cd02b1554'
'66adf06f722cd36780b066a561c804f99202d2d0a83c877e7ddeda8d47ce1c64'
'630de3ac36b8eb96d556d6ce418629f0834530a801d7c72c7565d642f5208447')
'630de3ac36b8eb96d556d6ce418629f0834530a801d7c72c7565d642f5208447'
'8b579b27e350e06b28112163c0996a8d4da8f5f57210141581c2adf2501f0d2f')

0 comments on commit 748578f

Please sign in to comment.