From ab9a95686163d59109ad6c7c6c4fe9716750d0af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:29:01 +0100 Subject: [PATCH 1/6] up --- Lib/ctypes/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 2f5a1777fb6284ab03eb561859d6c7bb6d597a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:30:23 +0100 Subject: [PATCH 2/6] test --- Lib/test/test_ctypes/test_dllist.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_ctypes/test_dllist.py b/Lib/test/test_ctypes/test_dllist.py index bff6c0fb95f129..8b5b01136be4d7 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,12 @@ 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): + 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() From a71acaed180c5c03ddbb399518b92e6dc64e43a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:35:46 +0100 Subject: [PATCH 3/6] news --- .../next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst 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..a3de4c98bd9770 --- /dev/null +++ b/Misc/NEWS.d/next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst @@ -0,0 +1,2 @@ +Fix a bug where importing :mod:`ctypes.util` on macOS could load +``libc.dylib`` from the current working directory. From 60cd5f2c77cebcc8e149af0c6abf8ca7207c7f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:39:35 +0100 Subject: [PATCH 4/6] cleanup --- Lib/test/test_ctypes/test_dllist.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_ctypes/test_dllist.py b/Lib/test/test_ctypes/test_dllist.py index 8b5b01136be4d7..d9cecdfc0c19a1 100644 --- a/Lib/test/test_ctypes/test_dllist.py +++ b/Lib/test/test_ctypes/test_dllist.py @@ -57,6 +57,7 @@ def test_lists_updates(self): @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() From ef407437a8bda31a5514cd0708e70726ceee8ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:45:56 +0100 Subject: [PATCH 5/6] make doc happy --- .../next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index a3de4c98bd9770..15905dc2526553 100644 --- 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 @@ -1,2 +1,2 @@ -Fix a bug where importing :mod:`ctypes.util` on macOS could load +Fix a bug where importing :mod:`!ctypes.util` on macOS could load ``libc.dylib`` from the current working directory. From e3cba4d9cec54b2c7f48c5d2656f23ece8ff9d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Sat, 21 Nov 2026 08:48:07 +0100 Subject: [PATCH 6/6] claim credit --- .../next/macOS/2026-11-21-08-35-36.gh-issue-162362.hgX9W3.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 15905dc2526553..3313c32d6d3080 100644 --- 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 @@ -1,2 +1,3 @@ Fix a bug where importing :mod:`!ctypes.util` on macOS could load -``libc.dylib`` from the current working directory. +``libc.dylib`` from the current working directory. Patch by Maurycy +Pawłowski-Wieroński.