File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import sys , os
22import contextlib
3+ import subprocess
34
45# find_library(name) returns the pathname of a library, or None.
56if 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 ))
Original file line number Diff line number Diff line change @@ -338,6 +338,7 @@ Filip Gruszczyński
338338Michael Guravage
339339Lars Gustäbel
340340Thomas Güttler
341+ Jonas H.
341342Barry Haddow
342343Paul ten Hagen
343344Rasmus Hahn
Original file line number Diff line number Diff line change @@ -113,6 +113,9 @@ Core and Builtins
113113Library
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
You can’t perform that action at this time.
0 commit comments