From c5b58a47f342e067d7de44eb4aeb3385552314e8 Mon Sep 17 00:00:00 2001
From: Stefano Rivera <stefano@rivera.za.net>
Date: Sun, 11 Aug 2024 10:43:45 +0200
Subject: [PATCH 1/3] gh-122917 Allow stable API extensions to include a
 multiarch tuple in the filename

This permits stable ABI extensions for multiple architectures to be
co-installed into the same directory, without clashing with each other,
the same way (non-stable ABI) regular extensions can.

It is listed below the current .abi3 suffix because setuptools will
select the first suffix containing .abi3, as the target filename.
We do this to protect older Python versions predating this patch.

Forwarded: https://github.com/python/cpython/pull/122917
Bug-Upstream: https://github.com/python/cpython/issues/122931
---
 Makefile.pre.in                                                | 1 +
 .../next/C_API/2024-08-12-09-48-04.gh-issue-122931.QwHc2l.rst  | 2 ++
 Python/dynload_shlib.c                                         | 3 +++
 3 files changed, 6 insertions(+)
 create mode 100644 Misc/NEWS.d/next/C_API/2024-08-12-09-48-04.gh-issue-122931.QwHc2l.rst

--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1961,6 +1961,7 @@
 	$(CC) -c $(PY_CORE_CFLAGS) \
 		$(if $(MULTIARCH),-DMULTIARCH='"$(MULTIARCH)"') \
 		-DSOABI='"$(SOABI)"' \
+		$(MULTIARCH_CPPFLAGS) \
 		-o $@ $(srcdir)/Python/dynload_shlib.c
 
 Python/dynload_hpux.o: $(srcdir)/Python/dynload_hpux.c Makefile
--- /dev/null
+++ b/Misc/NEWS.d/next/C_API/2024-08-12-09-48-04.gh-issue-122931.QwHc2l.rst
@@ -0,0 +1,2 @@
+Allow importing stable ABI C extensions that include a multiarch tuple
+in their filename, e.g. ``foo.abi3-x86-64-linux-gnu.so``.
--- a/Python/dynload_shlib.c
+++ b/Python/dynload_shlib.c
@@ -45,6 +45,9 @@
     "." ALT_SOABI ".so",
 #endif
     ".abi" PYTHON_ABI_STRING ".so",
+#ifdef MULTIARCH
+    ".abi" PYTHON_ABI_STRING "-" MULTIARCH ".so",
+#endif
     ".so",
 #endif  /* __CYGWIN__ */
     NULL,
--- a/Doc/c-api/stable.rst
+++ b/Doc/c-api/stable.rst
@@ -114,7 +114,8 @@
 ``python39.dll``.
 
 On some platforms, Python will look for and load shared library files named
-with the ``abi3`` tag (e.g. ``mymodule.abi3.so``).
+with the ``abi3`` tag (e.g. ``mymodule.abi3.so`` or
+``mymodule.abi3-x86-64-linux-gnu.so``).
 It does not check if such extensions conform to a Stable ABI.
 The user (or their packaging tools) need to ensure that, for example,
 extensions built with the 3.10+ Limited API are not installed for lower
