Skip to content

Commit f09a6a2

Browse files
[3.13] gh-140041: Fix import of ctypes on Android and Cygwin when ABI flags are present (GH-140178) (#140181)
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 7797676 commit f09a6a2

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"
@@ -484,10 +486,9 @@ def LoadLibrary(self, name):
484486

485487
if _os.name == "nt":
486488
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
487-
elif _sys.platform == "android":
488-
pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
489-
elif _sys.platform == "cygwin":
490-
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
489+
elif _sys.platform in ["android", "cygwin"]:
490+
# These are Unix-like platforms which use a dynamically-linked libpython.
491+
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
491492
else:
492493
pythonapi = PyDLL(None)
493494

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)