diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-01-22 18:10:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-22 18:10:41 (GMT) |
commit | ed567c1e1f27df507ca45478bdf9143b21ed06e1 (patch) | |
tree | d69e63f35a85eb7b80c62f64a2ee647716594f95 | |
parent | 4bbb9f6f29e08d88ebc4965165b8aa1aec47f427 (diff) | |
download | cpython-ed567c1e1f27df507ca45478bdf9143b21ed06e1.zip cpython-ed567c1e1f27df507ca45478bdf9143b21ed06e1.tar.gz cpython-ed567c1e1f27df507ca45478bdf9143b21ed06e1.tar.bz2 |
[3.12] gh-114257: Ignore the FileNotFound error in ctypes.util._is_elf() (GH-114394) (GH-114444)
(cherry picked from commit 7fc51c3f6b7b13f88480557ff14bdb1c049f9a37)
Co-authored-by: AN Long <aisk@users.noreply.github.com>
-rw-r--r-- | Lib/ctypes/util.py | 7 | ||||
-rw-r--r-- | Lib/test/test_ctypes/test_find.py | 3 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2024-01-21-16-32-55.gh-issue-114257.bCFld5.rst | 2 |
3 files changed, 10 insertions, 2 deletions
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 0c2510e..c550883 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -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/Lib/test/test_ctypes/test_find.py b/Lib/test/test_ctypes/test_find.py index 1ff9d01..a41e949 100644 --- a/Lib/test/test_ctypes/test_find.py +++ b/Lib/test/test_ctypes/test_find.py @@ -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() 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 0000000..6f02ff9 --- /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. |