Skip to content

Commit 7f371ed

Browse files
authored
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.
1 parent 65d1a14 commit 7f371ed

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"
@@ -550,10 +552,9 @@ def LoadLibrary(self, name):
550552

551553
if _os.name == "nt":
552554
pythonapi = PyDLL("python dll", None, _sys.dllhandle)
553-
elif _sys.platform == "android":
554-
pythonapi = PyDLL("libpython%d.%d.so" % _sys.version_info[:2])
555-
elif _sys.platform == "cygwin":
556-
pythonapi = PyDLL("libpython%d.%d.dll" % _sys.version_info[:2])
555+
elif _sys.platform in ["android", "cygwin"]:
556+
# These are Unix-like platforms which use a dynamically-linked libpython.
557+
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
557558
else:
558559
pythonapi = PyDLL(None)
559560

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)