Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffa1a77

Browse files
committed
Issue #11258: Speed up ctypes.util.find_library() under Linux a lot. Patch
by Jonas H.
1 parent ec5a2d5 commit ffa1a77

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

Lib/ctypes/test/test_loading.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,12 @@ def test_1703286_B(self):
102102
# This is the real test: call the function via 'call_function'
103103
self.assertEqual(0, call_function(proc, (None,)))
104104

105+
if os.name != "nt":
106+
def test_libc_exists(self):
107+
# A basic test that the libc is found by find_library()
108+
# XXX Can this fail on some non-Windows systems?
109+
self.assertTrue(libc_name)
110+
self.assertTrue(os.path.exists(libc_name))
111+
105112
if __name__ == "__main__":
106113
unittest.main()

Lib/ctypes/util.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,18 @@ def _findSoname_ldconfig(name):
203203
abi_type = mach_map.get(machine, 'libc6')
204204

205205
# XXX assuming GLIBC's ldconfig (with option -p)
206-
expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \
207-
% (abi_type, re.escape(name))
206+
name = 'lib%s' % name
207+
pat = re.compile('\s*(/[^\(\)\s]*%s\.[^\(\)\s]*)' % re.escape(name))
208208
with contextlib.closing(os.popen('LC_ALL=C LANG=C /sbin/ldconfig -p 2>/dev/null')) as f:
209-
data = f.read()
210-
res = re.search(expr, data)
211-
if not res:
212-
return None
213-
return res.group(1)
209+
for line in f:
210+
if not '=>' in line:
211+
continue
212+
path = line.rsplit('=>', 1)[1]
213+
if not name+'.' in path:
214+
continue
215+
res = pat.search(path)
216+
if res:
217+
return res.group(1)
214218

215219
def find_library(name):
216220
return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Dag Gruneau
329329
Michael Guravage
330330
Lars Gustäbel
331331
Thomas Güttler
332+
Jonas H.
332333
Barry Haddow
333334
Paul ten Hagen
334335
Rasmus Hahn

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Core and Builtins
3535
Library
3636
-------
3737

38+
- Issue #11258: Speed up ctypes.util.find_library() under Linux a lot. Patch
39+
by Jonas H.
40+
3841
- Issue #11297: Add collections.ChainMap().
3942

4043
- Issue #10755: Add the posix.fdlistdir() function. Patch by Ross Lagerwall.

0 commit comments

Comments
 (0)