Skip to content

Commit 7fc51c3

Browse files
authored
gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() (GH-114394)
1 parent ed30a3c commit 7fc51c3

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

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

Lib/test/test_ctypes/test_find.py

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

128+
def test_gh114257(self):
129+
self.assertIsNone(find_library("libc"))
130+
128131

129132
if __name__ == "__main__":
130133
unittest.main()
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)