https://github.com/python/cpython/commit/1f8b24ef69896680d6ba6005e75e1cc79a744f9e
commit: 1f8b24ef69896680d6ba6005e75e1cc79a744f9e
branch: main
author: Malcolm Smith <[email protected]>
committer: encukou <[email protected]>
date: 2024-03-21T14:20:57+01:00
summary:
gh-71052: Implement `ctypes.util.find_library` on Android (GH-116379)
files:
A Misc/NEWS.d/next/Library/2024-03-05-19-56-29.gh-issue-71052.PMDK--.rst
M Doc/library/ctypes.rst
M Lib/ctypes/util.py
M Lib/test/test_ctypes/test_find.py
diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst
index 36976470b5a468..9f7d6456e623a2 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -1334,8 +1334,9 @@ Here are some examples::
'libbz2.so.1.0'
>>>
-On macOS, :func:`~ctypes.util.find_library` tries several predefined naming
schemes and paths
-to locate the library, and returns a full pathname if successful::
+On macOS and Android, :func:`~ctypes.util.find_library` uses the system's
+standard naming schemes and paths to locate the library, and returns a full
+pathname if successful::
>>> from ctypes.util import find_library
>>> find_library("c")
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 12d7428fe9a776..117bf06cb01013 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -89,6 +89,15 @@ def find_library(name):
from ctypes._aix import find_library
+elif sys.platform == "android":
+ def find_library(name):
+ directory = "/system/lib"
+ if "64" in os.uname().machine:
+ directory += "64"
+
+ fname = f"{directory}/lib{name}.so"
+ return fname if os.path.isfile(fname) else None
+
elif os.name == "posix":
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
import re, tempfile
diff --git a/Lib/test/test_ctypes/test_find.py
b/Lib/test/test_ctypes/test_find.py
index 7732ff37308848..85b28617d2d754 100644
--- a/Lib/test/test_ctypes/test_find.py
+++ b/Lib/test/test_ctypes/test_find.py
@@ -129,5 +129,28 @@ def test_gh114257(self):
self.assertIsNone(find_library("libc"))
[email protected](sys.platform == 'android', 'Test only valid for Android')
+class FindLibraryAndroid(unittest.TestCase):
+ def test_find(self):
+ for name in [
+ "c", "m", # POSIX
+ "z", # Non-POSIX, but present on Linux
+ "log", # Not present on Linux
+ ]:
+ with self.subTest(name=name):
+ path = find_library(name)
+ self.assertIsInstance(path, str)
+ self.assertEqual(
+ os.path.dirname(path),
+ "/system/lib64" if "64" in os.uname().machine
+ else "/system/lib")
+ self.assertEqual(os.path.basename(path), f"lib{name}.so")
+ self.assertTrue(os.path.isfile(path), path)
+
+ for name in ["libc", "nonexistent"]:
+ with self.subTest(name=name):
+ self.assertIsNone(find_library(name))
+
+
if __name__ == "__main__":
unittest.main()
diff --git
a/Misc/NEWS.d/next/Library/2024-03-05-19-56-29.gh-issue-71052.PMDK--.rst
b/Misc/NEWS.d/next/Library/2024-03-05-19-56-29.gh-issue-71052.PMDK--.rst
new file mode 100644
index 00000000000000..ddca54c7c9ed7b
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-03-05-19-56-29.gh-issue-71052.PMDK--.rst
@@ -0,0 +1 @@
+Implement :func:`ctypes.util.find_library` on Android.
_______________________________________________
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]