Skip to content

Commit f5d9980

Browse files
miss-islingtonaisk
andauthored
[3.11] gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() (GH-114394) (GH-114445)
(cherry picked from commit 7fc51c3) Co-authored-by: AN Long <aisk@users.noreply.github.com>
1 parent acea9d8 commit f5d9980

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/ctypes/test/test_find.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def test_find_library_with_ld(self):
122122
unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None):
123123
self.assertNotEqual(find_library('c'), None)
124124

125+
def test_gh114257(self):
126+
self.assertIsNone(find_library("libc"))
127+
125128

126129
if __name__ == "__main__":
127130
unittest.main()

Lib/ctypes/util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ def find_library(name):
9696
def _is_elf(filename):
9797
"Return True if the given file is an ELF file"
9898
elf_header = b'\x7fELF'
99-
with open(filename, 'br') as thefile:
100-
return thefile.read(4) == elf_header
99+
try:
100+
with open(filename, 'br') as thefile:
101+
return thefile.read(4) == elf_header
102+
except FileNotFoundError:
103+
return False
101104

102105
def _findLib_gcc(name):
103106
# Run GCC's linker with the -t (aka --trace) option and examine the
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Dismiss the :exc:`FileNotFound` error in :func:`ctypes.util.find_library` and
2+
just return ``None`` on Linux.

0 commit comments

Comments
 (0)