Skip to content

Commit 504bf76

Browse files
[3.14] gh-140041: Fix import of ctypes on Android and Cygwin when ABI flags are present (GH-140178) (#140180)
Use sysconfig to determine the full name of libpython, rather than hardcoding a library name that doesn't have ABI flags. (cherry picked from commit 7f371ed) Co-authored-by: Malcolm Smith <[email protected]>
1 parent 5caf643 commit 504bf76

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

Android/testbed/app/build.gradle.kts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ for ((i, prefix) in prefixes.withIndex()) {
4747
val libDir = file("$prefix/lib")
4848
val version = run {
4949
for (filename in libDir.list()!!) {
50-
"""python(\d+\.\d+)""".toRegex().matchEntire(filename)?.let {
50+
"""python(\d+\.\d+[a-z]*)""".toRegex().matchEntire(filename)?.let {
5151
return@run it.groupValues[1]
5252
}
5353
}
@@ -64,9 +64,10 @@ for ((i, prefix) in prefixes.withIndex()) {
6464
val libPythonDir = file("$libDir/python$pythonVersion")
6565
val triplet = run {
6666
for (filename in libPythonDir.list()!!) {
67-
"""_sysconfigdata__android_(.+).py""".toRegex().matchEntire(filename)?.let {
68-
return@run it.groupValues[1]
69-
}
67+
"""_sysconfigdata_[a-z]*_android_(.+).py""".toRegex()
68+
.matchEntire(filename)?.let {
69+
return@run it.groupValues[1]
70+
}
7071
}
7172
throw GradleException("Failed to find Python triplet in $libPythonDir")
7273
}

Lib/ctypes/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""create and manipulate C data types in Python"""
22

3-
import os as _os, sys as _sys
3+
import os as _os
4+
import sys as _sys
5+
import sysconfig as _sysconfig
46
import types as _types
57

68
__version__ = "1.1.0"
@@ -556,10 +558,9 @@ def LoadLibrary(self, name):
556558

557559
if _os.name == "nt":
558560
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
559-
elif _sys.platform == "android":
560-
pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
561-
elif _sys.platform == "cygwin":
562-
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
561+
elif _sys.platform in ["android", "cygwin"]:
562+
# These are Unix-like platforms which use a dynamically-linked libpython.
563+
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
563564
else:
564565
pythonapi = PyDLL(None)
565566

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix import of :mod:`ctypes` on Android and Cygwin when ABI flags are present.

0 commit comments

Comments
 (0)