https://github.com/python/cpython/commit/f5d9980217b2f36d095aa58a9b949b828f113082
commit: f5d9980217b2f36d095aa58a9b949b828f113082
branch: 3.11
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2024-01-22T18:10:44Z
summary:

[3.11] gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() 
(GH-114394) (GH-114445)

(cherry picked from commit 7fc51c3f6b7b13f88480557ff14bdb1c049f9a37)

Co-authored-by: AN Long <[email protected]>

files:
A Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst
M Lib/ctypes/test/test_find.py
M Lib/ctypes/util.py

diff --git a/Lib/ctypes/test/test_find.py b/Lib/ctypes/test/test_find.py
index 1ff9d019b138a4..a41e94971dfbab 100644
--- a/Lib/ctypes/test/test_find.py
+++ b/Lib/ctypes/test/test_find.py
@@ -122,6 +122,9 @@ def test_find_library_with_ld(self):
              unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: 
None):
             self.assertNotEqual(find_library('c'), None)
 
+    def test_gh114257(self):
+        self.assertIsNone(find_library("libc"))
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 0c2510e1619c8e..c550883e7c7d4b 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -96,8 +96,11 @@ def find_library(name):
     def _is_elf(filename):
         "Return True if the given file is an ELF file"
         elf_header = b'\x7fELF'
-        with open(filename, 'br') as thefile:
-            return thefile.read(4) == elf_header
+        try:
+            with open(filename, 'br') as thefile:
+                return thefile.read(4) == elf_header
+        except FileNotFoundError:
+            return False
 
     def _findLib_gcc(name):
         # Run GCC's linker with the -t (aka --trace) option and examine the
diff --git 
a/Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst 
b/Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst
new file mode 100644
index 00000000000000..6f02ff9e62617d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst
@@ -0,0 +1,2 @@
+Dismiss the :exc:`FileNotFound` error in :func:`ctypes.util.find_library` and
+just return ``None`` on Linux.

_______________________________________________
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]

Reply via email to