summaryrefslogtreecommitdiffstats
path: root/Lib/ctypes
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-04-23 15:51:04 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-04-23 15:51:04 (GMT)
commit8c52027e2d8c4268d0d0e376948d3107576a4aa4 (patch)
tree9e3ddd4d7e6823a127e99d7a0d46fb238e4d10a5 /Lib/ctypes
parent877509aef43034fec002eeafc9982e4b6c1c0b3d (diff)
downloadcpython-8c52027e2d8c4268d0d0e376948d3107576a4aa4.zip
cpython-8c52027e2d8c4268d0d0e376948d3107576a4aa4.tar.gz
cpython-8c52027e2d8c4268d0d0e376948d3107576a4aa4.tar.bz2
Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
of 5 to 10. Initial patch by Jonas H.
Diffstat (limited to 'Lib/ctypes')
-rw-r--r--Lib/ctypes/util.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
index 1881e89..5b58422 100644
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -1,5 +1,6 @@
import sys, os
import contextlib
+import subprocess
# find_library(name) returns the pathname of a library, or None.
if os.name == "nt":
@@ -203,14 +204,19 @@ elif os.name == "posix":
abi_type = mach_map.get(machine, 'libc6')
# XXX assuming GLIBC's ldconfig (with option -p)
- expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \
- % (abi_type, re.escape(name))
- with contextlib.closing(os.popen('LC_ALL=C LANG=C /sbin/ldconfig -p 2>/dev/null')) as f:
- data = f.read()
- res = re.search(expr, data)
- if not res:
- return None
- return res.group(1)
+ regex = os.fsencode(
+ '\s+(lib%s\.[^\s]+)\s+\(%s' % (re.escape(name), abi_type))
+ try:
+ with subprocess.Popen(['/sbin/ldconfig', '-p'],
+ stdin=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ stdout=subprocess.PIPE,
+ env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
+ res = re.search(regex, p.stdout.read())
+ if res:
+ return os.fsdecode(res.group(1))
+ except OSError:
+ pass
def find_library(name):
return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))