From 1ce773d283b699e58915317cd3beb742181dffe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Tue, 1 Dec 2026 01:55:15 +0100 Subject: [PATCH] gh-162362: Resolve dyld functions from the process on `import ctypes.util` on macOS (GH-162363) (cherry picked from commit 6a663aae4dc61fbdbebc443451fb461c0b3837b5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maurycy Pawłowski-Wieroński --- Lib/ctypes/util.py | 2 +- Lib/test/test_ctypes/test_dllist.py | 8 ++++++++ .../macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 015c62b31a1bd78..40b270f08aa45d1 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -150,7 +150,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 15603dc3d779726..689c81484d4495f 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 000000000000000..3313c32d6d30808 --- /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.