]> granicus.if.org Git - python/commitdiff
Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 23 Apr 2011 15:51:04 +0000 (17:51 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 23 Apr 2011 15:51:04 +0000 (17:51 +0200)
of 5 to 10.  Initial patch by Jonas H.

Lib/ctypes/util.py
Misc/ACKS
Misc/NEWS

index 1881e89688800a14a6d94c413711694ba7bb31cd..5b5842219252be5264662b8eb2586e7e321d04bc 100644 (file)
@@ -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))
index 33d90fff74d783dad3e7aebd307e5b88c38f4d8a..c3a227a4bf87725d3630362b70a3efd7c3c8a82f 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -338,6 +338,7 @@ Filip Gruszczyński
 Michael Guravage
 Lars Gustäbel
 Thomas Güttler
+Jonas H.
 Barry Haddow
 Paul ten Hagen
 Rasmus Hahn
index 37b48d164d3df23cbeacec2bc5de3378ac2a865f..8aac765c928cb37fd8b01b182fdd37c79965114c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -113,6 +113,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
+  of 5 to 10.  Initial patch by Jonas H.
+
 - Issue #11382: Trivial system calls, such as dup() or pipe(), needn't
   release the GIL.  Patch by Charles-François Natali.