[Python-checkins] cpython: Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor

antoine.pitrou python-checkins at python.org
Sat Apr 23 17:56:24 CEST 2011


http://hg.python.org/cpython/rev/19d9f0a177de
changeset:   69533:19d9f0a177de
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Apr 23 17:51:04 2011 +0200
summary:
  Issue #11258: Speed up ctypes.util.find_library() under Linux by a factor
of 5 to 10.  Initial patch by Jonas H.

files:
  Lib/ctypes/util.py |  22 ++++++++++++++--------
  Misc/ACKS          |   1 +
  Misc/NEWS          |   3 +++
  3 files changed, 18 insertions(+), 8 deletions(-)


diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
--- 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 @@
             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))
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -338,6 +338,7 @@
 Michael Guravage
 Lars Gustäbel
 Thomas Güttler
+Jonas H.
 Barry Haddow
 Paul ten Hagen
 Rasmus Hahn
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -113,6 +113,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list