https://github.com/python/cpython/commit/7f371ed84ba471bb1b11e79b502f244a9c17ac84
commit: 7f371ed84ba471bb1b11e79b502f244a9c17ac84
branch: main
author: Malcolm Smith <[email protected]>
committer: freakboy3742 <[email protected]>
date: 2025-10-16T05:40:39+08:00
summary:

gh-140041: Fix import of `ctypes` on Android and Cygwin when ABI flags are 
present (#140178)

Use sysconfig to determine the full name of libpython, rather than hardcoding
a library name that doesn't have ABI flags.

files:
A Misc/NEWS.d/next/Library/2025-10-15-21-42-13.gh-issue-140041._Fka2j.rst
M Android/testbed/app/build.gradle.kts
M Lib/ctypes/__init__.py

diff --git a/Android/testbed/app/build.gradle.kts 
b/Android/testbed/app/build.gradle.kts
index 92cffd61f86876..4de628a279ca3f 100644
--- a/Android/testbed/app/build.gradle.kts
+++ b/Android/testbed/app/build.gradle.kts
@@ -47,7 +47,7 @@ for ((i, prefix) in prefixes.withIndex()) {
     val libDir = file("$prefix/lib")
     val version = run {
         for (filename in libDir.list()!!) {
-            """python(\d+\.\d+)""".toRegex().matchEntire(filename)?.let {
+            """python(\d+\.\d+[a-z]*)""".toRegex().matchEntire(filename)?.let {
                 return@run it.groupValues[1]
             }
         }
@@ -64,9 +64,10 @@ for ((i, prefix) in prefixes.withIndex()) {
     val libPythonDir = file("$libDir/python$pythonVersion")
     val triplet = run {
         for (filename in libPythonDir.list()!!) {
-            
"""_sysconfigdata__android_(.+).py""".toRegex().matchEntire(filename)?.let {
-                return@run it.groupValues[1]
-            }
+            """_sysconfigdata_[a-z]*_android_(.+).py""".toRegex()
+                .matchEntire(filename)?.let {
+                    return@run it.groupValues[1]
+                }
         }
         throw GradleException("Failed to find Python triplet in $libPythonDir")
     }
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 768cbada894613..ab5b656e6e5d6c 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -1,6 +1,8 @@
 """create and manipulate C data types in Python"""
 
-import os as _os, sys as _sys
+import os as _os
+import sys as _sys
+import sysconfig as _sysconfig
 import types as _types
 
 __version__ = "1.1.0"
@@ -550,10 +552,9 @@ def LoadLibrary(self, name):
 
 if _os.name == "nt":
     pythonapi = PyDLL("python dll", None, _sys.dllhandle)
-elif _sys.platform == "android":
-    pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
-elif _sys.platform == "cygwin":
-    pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
+elif _sys.platform in ["android", "cygwin"]:
+    # These are Unix-like platforms which use a dynamically-linked libpython.
+    pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
 else:
     pythonapi = PyDLL(None)
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-10-15-21-42-13.gh-issue-140041._Fka2j.rst 
b/Misc/NEWS.d/next/Library/2025-10-15-21-42-13.gh-issue-140041._Fka2j.rst
new file mode 100644
index 00000000000000..243ff39311cf06
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-10-15-21-42-13.gh-issue-140041._Fka2j.rst
@@ -0,0 +1 @@
+Fix import of :mod:`ctypes` on Android and Cygwin when ABI flags are present.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to