]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() (GH-114394...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 22 Jan 2024 18:10:44 +0000 (19:10 +0100)
committerGitHub <noreply@github.com>
Mon, 22 Jan 2024 18:10:44 +0000 (18:10 +0000)
(cherry picked from commit 7fc51c3f6b7b13f88480557ff14bdb1c049f9a37)

Co-authored-by: AN Long <aisk@users.noreply.github.com>
Lib/ctypes/test/test_find.py
Lib/ctypes/util.py
Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst [new file with mode: 0644]

index 1ff9d019b138a4f1b723bb0946bba655333a5ac1..a41e94971dfbab0240ab50bdef4624408675ee2f 100644 (file)
@@ -122,6 +122,9 @@ class FindLibraryLinux(unittest.TestCase):
              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()
index 0c2510e1619c8ed33f49aeacfbc72b118ad9d27a..c550883e7c7d4b71b678e0dbec0f2a1474620197 100644 (file)
@@ -96,8 +96,11 @@ elif os.name == "posix":
     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 (file)
index 0000000..6f02ff9
--- /dev/null
@@ -0,0 +1,2 @@
+Dismiss the :exc:`FileNotFound` error in :func:`ctypes.util.find_library` and
+just return ``None`` on Linux.