Skip to content

Commit

Permalink
Merge pull request #18277 from lazka/update-python
Browse files Browse the repository at this point in the history
Update Python
  • Loading branch information
lazka authored Aug 27, 2023
2 parents 88c0232 + cc8cb3f commit bfeef00
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
74 changes: 74 additions & 0 deletions mingw-w64-python/0146-Port-GetPythonImport-to-mingw.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
From a6cef77b4664e68982438130164b4cf120eca13f Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Sun, 27 Aug 2023 15:00:32 +0200
Subject: [PATCH 146/N] Port GetPythonImport() to mingw

This looks for DLL names in the import table but while with MSVC the DLL
is named python311.dll in our case it is named libpython3.11.dll.
Adjust the strings and lengths accordingly.
---
Python/dynload_win.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index fcb7321..8a3ef4d 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -56,6 +56,16 @@ const char *_PyImport_DynLoadFiletab[] = {
#define DWORD_AT(mem) (*(DWORD *)(mem))
#define WORD_AT(mem) (*(WORD *)(mem))

+#ifdef __MINGW32__
+#define DLL_PREFIX "libpython"
+#define DLL_PREFIX_LEN 9
+#define DLL_NAME_FORMAT "libpython%d.%d"
+#else
+#define DLL_PREFIX "python"
+#define DLL_PREFIX_LEN 6
+#define DLL_NAME_FORMAT "python%d%d"
+#endif
+
static char *GetPythonImport (HINSTANCE hModule)
{
unsigned char *dllbase, *import_data, *import_name;
@@ -122,15 +132,15 @@ static char *GetPythonImport (HINSTANCE hModule)
import_off);
while (DWORD_AT(import_data)) {
import_name = dllbase + DWORD_AT(import_data+12);
- if (strlen(import_name) >= 6 &&
- !strncmp(import_name,"python",6)) {
+ if (strlen(import_name) >= DLL_PREFIX_LEN &&
+ !strncmp(import_name, DLL_PREFIX, DLL_PREFIX_LEN)) {
char *pch;

/* Don't claim that python3.dll is a Python DLL. */
#ifdef _DEBUG
- if (strcmp(import_name, "python3_d.dll") == 0) {
+ if (strcmp(import_name, DLL_PREFIX "3_d.dll") == 0) {
#else
- if (strcmp(import_name, "python3.dll") == 0) {
+ if (strcmp(import_name, DLL_PREFIX "3.dll") == 0) {
#endif
import_data += 20;
continue;
@@ -138,7 +148,7 @@ static char *GetPythonImport (HINSTANCE hModule)

/* Ensure python prefix is followed only
by numbers to the end of the basename */
- pch = import_name + 6;
+ pch = import_name + DLL_PREFIX_LEN;
#ifdef _DEBUG
while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
#else
@@ -329,9 +339,9 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,

PyOS_snprintf(buffer, sizeof(buffer),
#ifdef _DEBUG
- "python%d%d_d.dll",
+ DLL_NAME_FORMAT "_d.dll",
#else
- "python%d%d.dll",
+ DLL_NAME_FORMAT ".dll",
#endif
PY_MAJOR_VERSION,PY_MINOR_VERSION);
import_python = GetPythonImport(hDLL);
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
From 7a3aacf3cba0c35f8621a78fc2d239fe2c878c63 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Sun, 27 Aug 2023 16:04:50 +0200
Subject: [PATCH 147/N] LoadLibraryExW: make sure to only use backslashes for
paths

It seems like in case the path passed to it is absolute, but contains
forward slashes then LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR does not work
and DLLs in the same directory as the extension are not considered.

This occurs in our fork because in MSYS2-mode the extension loader will
normalize to forward slashes before.

Normalize everything to backslashes again before passing it to LoadLibraryExW.

Fixes #151
---
Python/dynload_win.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/Python/dynload_win.c b/Python/dynload_win.c
index 8a3ef4d..cd15e4a 100644
--- a/Python/dynload_win.c
+++ b/Python/dynload_win.c
@@ -250,6 +250,9 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
_Py_CheckPython3();
#endif

+// So we can adjust the separators in the path below
+#define USE_UNICODE_WCHAR_CACHE 0
+
#if USE_UNICODE_WCHAR_CACHE
const wchar_t *wpathname = _PyUnicode_AsUnicode(pathname);
#else /* USE_UNICODE_WCHAR_CACHE */
@@ -258,6 +261,15 @@ dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
if (wpathname == NULL)
return NULL;

+ // LoadLibraryExW only considers paths using backslashes as "fully qualified",
+ // and for example LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR doesn't work with forward slashes.
+ // https://github.com/msys2-contrib/cpython-mingw/issues/151
+ for (size_t i = 0; wpathname[i] != L'\0'; ++i) {
+ if (wpathname[i] == L'/') {
+ wpathname[i] = L'\\';
+ }
+ }
+
PyOS_snprintf(funcname, sizeof(funcname), "%.20s_%.200s", prefix, shortname);

{
14 changes: 10 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}.5
pkgrel=1
pkgrel=2
pkgdesc="A high-level scripting language (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
Expand Down Expand Up @@ -192,7 +192,9 @@ source=("https://www.python.org/ftp/python/${pkgver%rc?}/Python-${pkgver}.tar.xz
0142-Allow-picking-up-include-lib-dirs-from-CFLAGS-LDFLAG.patch
0143-Build-and-install-libpython3.dll.patch
0144-setup.py-don-t-prepend-the-system-library-directorie.patch
0145-tests-skip-a-broken-test.patch)
0145-tests-skip-a-broken-test.patch
0146-Port-GetPythonImport-to-mingw.patch
0147-LoadLibraryExW-make-sure-to-only-use-backslashes-for.patch)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
Expand Down Expand Up @@ -350,7 +352,9 @@ prepare() {
0142-Allow-picking-up-include-lib-dirs-from-CFLAGS-LDFLAG.patch \
0143-Build-and-install-libpython3.dll.patch \
0144-setup.py-don-t-prepend-the-system-library-directorie.patch \
0145-tests-skip-a-broken-test.patch
0145-tests-skip-a-broken-test.patch \
0146-Port-GetPythonImport-to-mingw.patch \
0147-LoadLibraryExW-make-sure-to-only-use-backslashes-for.patch

autoreconf -vfi
}
Expand Down Expand Up @@ -596,4 +600,6 @@ sha256sums=('85cd12e9cf1d6d5a45f17f7afe1cebe7ee628d3282281c492e86adf636defa3f'
'69cf576d5083088b1239b3bcbe30d18e6cb85e1dab8738f8b04007ef35fd40bd'
'8df3f5bee7856ee12ea705d6d31358f5ee5ff2c8eb99ab1aad9101afe92893b9'
'6e0d29ee44c5d5ab8605a980c7cee246acf5ccce638e6a7cee4c4e7a8ec865cd'
'3b6bf12600203d45ff9b0ecde0bb9959c4fde923c9609e4c1e18b6d7877e3f1b')
'3b6bf12600203d45ff9b0ecde0bb9959c4fde923c9609e4c1e18b6d7877e3f1b'
'd9a54181024f0b538d8903bdf772a0dd754811aab339e6f553c47e1198ab04cc'
'71fd408d8bb8ca40584f287adea583032d1b7150f011abe2165c44d468428f0f')

0 comments on commit bfeef00

Please sign in to comment.