diff --git a/Lib/test/test_ctypes/test_find.py b/Lib/test/test_ctypes/test_find.py index 8bc84c3d2ef9f85..875d99ae3cf2ca6 100644 --- a/Lib/test/test_ctypes/test_find.py +++ b/Lib/test/test_ctypes/test_find.py @@ -78,17 +78,30 @@ def test_shell_injection(self): @unittest.skipUnless(sys.platform.startswith('linux'), 'Test only valid for Linux') class FindLibraryLinux(unittest.TestCase): + @classmethod + def setUpClass(cls): + import subprocess + + try: + subprocess.check_output(['gcc', '--version']) + cls.has_gcc = True + except (FileNotFoundError, subprocess.CalledProcessError): + cls.has_gcc = False + + try: + subprocess.check_output(['ld', '--version']) + cls.has_ld = True + except (FileNotFoundError, subprocess.CalledProcessError): + cls.has_ld = False + @thread_unsafe('uses setenv') def test_find_on_libpath(self): + if not self.has_gcc: + self.skipTest("gcc not available") + import subprocess import tempfile - try: - p = subprocess.Popen(['gcc', '--version'], stdout=subprocess.PIPE, - stderr=subprocess.DEVNULL) - out, _ = p.communicate() - except OSError: - raise unittest.SkipTest('gcc, needed for test, not available') with tempfile.TemporaryDirectory() as d: # create an empty temporary file srcname = os.path.join(d, 'dummy.c') @@ -118,10 +131,17 @@ def test_find_on_libpath(self): self.assertEqual(find_library(libname), 'lib%s.so' % libname) def test_find_library_with_gcc(self): - with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None): + if not self.has_gcc: + self.skipTest("gcc not available") + + with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \ + unittest.mock.patch("ctypes.util._findLib_ld", lambda *args: None): self.assertNotEqual(find_library('c'), None) def test_find_library_with_ld(self): + if not self.has_ld: + self.skipTest("ld not available") + with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \ unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None): self.assertNotEqual(find_library('c'), None) diff --git a/Misc/NEWS.d/next/Tests/2026-06-10-12-14-32.gh-issue-151089.grvjfj.rst b/Misc/NEWS.d/next/Tests/2026-06-10-12-14-32.gh-issue-151089.grvjfj.rst new file mode 100644 index 000000000000000..0bc7b7610613739 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-06-10-12-14-32.gh-issue-151089.grvjfj.rst @@ -0,0 +1,3 @@ +Fix the mocking in test_ctypes/test_find.py:test_find_library_with_gcc() where +it will call ld if gcc isn't found, and skip the tests if ld or gcc isn't +available.