diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 3ca2458a2a653e..4d10cbf3236325 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -121,7 +121,7 @@ def find_library(name): # https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html import ctypes - _libc = ctypes.CDLL(find_library("c")) + _libc = ctypes.CDLL(None) _dyld_get_image_name = _libc["_dyld_get_image_name"] _dyld_get_image_name.restype = ctypes.c_char_p diff --git a/Lib/test/test_ctypes/test_dllist.py b/Lib/test/test_ctypes/test_dllist.py index bff6c0fb95f129..d9cecdfc0c19a1 100644 --- a/Lib/test/test_ctypes/test_dllist.py +++ b/Lib/test/test_ctypes/test_dllist.py @@ -4,6 +4,7 @@ from ctypes import CDLL import ctypes.util from test.support import import_helper +from unittest.mock import patch WINDOWS = os.name == "nt" @@ -54,6 +55,13 @@ def test_lists_updates(self): self.assertGreater(dlls2, dlls1, f"newly loaded libraries: {dlls2 - dlls1}") self.assertTrue(any("_ctypes_test" in dll for dll in dlls2), f"loaded={dlls2}") + @unittest.skipUnless(APPLE, "requires an Apple platform") + def test_import_does_not_search_for_libc(self): + self.addCleanup(setattr, ctypes, "util", ctypes.util) + with patch("ctypes.macholib.dyld.dyld_find", return_value=None) as find: + import_helper.import_fresh_module("ctypes.util") + find.assert_not_called() + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst b/Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst new file mode 100644 index 00000000000000..3313c32d6d3080 --- /dev/null +++ b/Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst @@ -0,0 +1,3 @@ +Fix a bug where importing :mod:`!ctypes.util` on macOS could load +``libc.dylib`` from the current working directory. Patch by Maurycy +Pawłowski-Wieroński.