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

Skip to content

Commit 8c52027

Browse files
committed
Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
of 5 to 10. Initial patch by Jonas H.
1 parent 877509a commit 8c52027

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

Lib/ctypes/util.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys, os
22
import contextlib
3+
import subprocess
34

45
# find_library(name) returns the pathname of a library, or None.
56
if os.name == "nt":
@@ -203,14 +204,19 @@ def _findSoname_ldconfig(name):
203204
abi_type = mach_map.get(machine, 'libc6')
204205

205206
# 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))
208-
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)
207+
regex = os.fsencode(
208+
'\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type))
209+
try:
210+
with subprocess.Popen(['/sbin/ldconfig', '-p'],
211+
stdin=subprocess.DEVNULL,
212+
stderr=subprocess.DEVNULL,
213+
stdout=subprocess.PIPE,
214+
env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
215+
res = re.search(regex, p.stdout.read())
216+
if res:
217+
return os.fsdecode(res.group(1))
218+
except OSError:
219+
pass
214220

215221
def find_library(name):
216222
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
@@ -338,6 +338,7 @@ Filip Gruszczyński
338338
Michael Guravage
339339
Lars Gustäbel
340340
Thomas Güttler
341+
Jonas H.
341342
Barry Haddow
342343
Paul ten Hagen
343344
Rasmus Hahn

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ Core and Builtins
113113
Library
114114
-------
115115

116+
- Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
117+
of 5 to 10. Initial patch by Jonas H.
118+
116119
- Issue #11382: Trivial system calls, such as dup() or pipe(), needn't
117120
release the GIL. Patch by Charles-François Natali.
118121

0 commit comments

Comments
 (0)