Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions Lib/test/test_ctypes/test_find.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading