Skip to content

Commit a4ac5fa

Browse files
bpo-41976: Fix the fallback to gcc of ctypes.util.find_library when using gcc>9 (GH-22598)
(cherry picked from commit 27ac19c) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
1 parent 15e091f commit a4ac5fa

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

Lib/ctypes/test/test_find.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import unittest.mock
23
import os.path
34
import sys
45
import test.support
@@ -72,7 +73,7 @@ def test_shell_injection(self):
7273

7374
@unittest.skipUnless(sys.platform.startswith('linux'),
7475
'Test only valid for Linux')
75-
class LibPathFindTest(unittest.TestCase):
76+
class FindLibraryLinux(unittest.TestCase):
7677
def test_find_on_libpath(self):
7778
import subprocess
7879
import tempfile
@@ -111,6 +112,15 @@ def test_find_on_libpath(self):
111112
# LD_LIBRARY_PATH)
112113
self.assertEqual(find_library(libname), 'lib%s.so' % libname)
113114

115+
def test_find_library_with_gcc(self):
116+
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None):
117+
self.assertNotEqual(find_library('c'), None)
118+
119+
def test_find_library_with_ld(self):
120+
with unittest.mock.patch("ctypes.util._findSoname_ldconfig", lambda *args: None), \
121+
unittest.mock.patch("ctypes.util._findLib_gcc", lambda *args: None):
122+
self.assertNotEqual(find_library('c'), None)
123+
114124

115125
if __name__ == "__main__":
116126
unittest.main()

Lib/ctypes/util.py

+24-6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ def find_library(name):
9393
# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump
9494
import re, tempfile
9595

96+
def _is_elf(filename):
97+
"Return True if the given file is an ELF file"
98+
elf_header = b'\x7fELF'
99+
with open(filename, 'br') as thefile:
100+
return thefile.read(4) == elf_header
101+
96102
def _findLib_gcc(name):
97103
# Run GCC's linker with the -t (aka --trace) option and examine the
98104
# library name it prints out. The GCC command will fail because we
@@ -130,10 +136,17 @@ def _findLib_gcc(name):
130136
# Raised if the file was already removed, which is the normal
131137
# behaviour of GCC if linking fails
132138
pass
133-
res = re.search(expr, trace)
139+
res = re.findall(expr, trace)
134140
if not res:
135141
return None
136-
return os.fsdecode(res.group(0))
142+
143+
for file in res:
144+
# Check if the given file is an elf file: gcc can report
145+
# some files that are linker scripts and not actual
146+
# shared objects. See bpo-41976 for more details
147+
if not _is_elf(file):
148+
continue
149+
return os.fsdecode(file)
137150

138151

139152
if sys.platform == "sunos5":
@@ -299,17 +312,22 @@ def _findLib_ld(name):
299312
stderr=subprocess.PIPE,
300313
universal_newlines=True)
301314
out, _ = p.communicate()
302-
res = re.search(expr, os.fsdecode(out))
303-
if res:
304-
result = res.group(0)
315+
res = re.findall(expr, os.fsdecode(out))
316+
for file in res:
317+
# Check if the given file is an elf file: gcc can report
318+
# some files that are linker scripts and not actual
319+
# shared objects. See bpo-41976 for more details
320+
if not _is_elf(file):
321+
continue
322+
return os.fsdecode(file)
305323
except Exception:
306324
pass # result will be None
307325
return result
308326

309327
def find_library(name):
310328
# See issue #9998
311329
return _findSoname_ldconfig(name) or \
312-
_get_soname(_findLib_gcc(name) or _findLib_ld(name))
330+
_get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))
313331

314332
################################################################
315333
# test code
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a bug that was causing :func:`ctypes.util.find_library` to return
2+
``None`` when triying to locate a library in an environment when gcc>=9 is
3+
available and ``ldconfig`` is not. Patch by Pablo Galindo

0 commit comments

Comments
 (0)