8000 gh-71052: Implement `ctypes.util.find_library` on Android (GH-116379) · python/cpython@1f8b24e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f8b24e

Browse files
authored
gh-71052: Implement ctypes.util.find_library on Android (GH-116379)
1 parent d16c9d1 commit 1f8b24e

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

Doc/library/ctypes.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,8 +1334,9 @@ Here are 8000 some examples::
13341334
'libbz2.so.1.0'
13351335
>>>
13361336

1337-
On macOS, :func:`~ctypes.util.find_library` tries several predefined naming schemes and paths
1338-
to locate the library, and returns a full pathname if successful::
1337+
On macOS and Android, :func:`~ctypes.util.find_library` uses the system's
1338+
standard naming schemes and paths to locate the library, and returns a full
1339+
pathname if successful::
13391340

13401341
>>> from ctypes.util import find_library
13411342
>>> find_library("c")

Lib/ctypes/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ def find_library(name):
8989

9090
from ctypes._aix import find_library
9191

92+
elif sys.platform == "android":
93+
def find_library(name):
94+
directory = "/system/lib"
95+
if "64" in os.uname().machine:
96+
directory += "64"
97+
98+
fname = f"{directory}/lib{name}.so"
99+
return fname if os.path.isfile(fname) else None
100+
92101
elif os.name == "posix":
93102
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
94103
import re, tempfile

Lib/test/test_ctypes/test_find.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,28 @@ def test_gh114257(self):
129129
self.assertIsNone(find_library("libc"))
130130

131131

132+
@unittest.skipUnless(sys.platform == 'android', 'Test only valid for Android')
133+
class FindLibraryAndroid(unittest.TestCase):
134+
def test_find(self):
135+
for name in [
136+
"c", "m", # POSIX
137+
"z", # Non-POSIX, but present on Linux
138+
"log", # Not present on Linux
139+
]:
140+
with self.subTest(name=name):
141+
path = find_library(name)
142+
self.assertIsInstance(path, str)
143+
self.assertEqual(
144+
os.path.dirname(path),
145+
"/system/lib64" if "64" in os.uname().machine
146+
else "/system/lib")
147+
self.assertEqual(os.path.basename(path), f"lib{name}.so")
148+
self.assertTrue(os.path.isfile(path), path)
149+
150+
for name in ["libc", "nonexistent"]:
151+
with self.subTest(name=name):
152+
self.assertIsNone(find_library(name))
153+
154+
132155
if __name__ == "__main__":
133156
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Implement :func:`ctypes.util.find_library` on Android.

0 commit comments

Comments
 (0)
0