[Python-checkins] r88639 - in python/branches/py3k: Lib/ctypes/test/test_loading.py Lib/ctypes/util.py Misc/ACKS Misc/NEWS

antoine.pitrou python-checkins at python.org
Sat Feb 26 09:45:20 CET 2011


Author: antoine.pitrou
Date: Sat Feb 26 09:45:20 2011
New Revision: 88639

Log:
Issue #11258: Speed up ctypes.util.find_library() under Linux a lot.  Patch
by Jonas H.



Modified:
   python/branches/py3k/Lib/ctypes/test/test_loading.py
   python/branches/py3k/Lib/ctypes/util.py
   python/branches/py3k/Misc/ACKS
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/ctypes/test/test_loading.py
==============================================================================
--- python/branches/py3k/Lib/ctypes/test/test_loading.py	(original)
+++ python/branches/py3k/Lib/ctypes/test/test_loading.py	Sat Feb 26 09:45:20 2011
@@ -102,5 +102,12 @@
             # This is the real test: call the function via 'call_function'
             self.assertEqual(0, call_function(proc, (None,)))
 
+    if os.name != "nt":
+        def test_libc_exists(self):
+            # A basic test that the libc is found by find_library()
+            # XXX Can this fail on some non-Windows systems?
+            self.assertTrue(libc_name)
+            self.assertTrue(os.path.exists(libc_name))
+
 if __name__ == "__main__":
     unittest.main()

Modified: python/branches/py3k/Lib/ctypes/util.py
==============================================================================
--- python/branches/py3k/Lib/ctypes/util.py	(original)
+++ python/branches/py3k/Lib/ctypes/util.py	Sat Feb 26 09:45:20 2011
@@ -203,14 +203,18 @@
             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))
+            name = 'lib%s' % name
+            pat = re.compile('\s*(/[^\(\)\s]*%s\.[^\(\)\s]*)' % 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)
+                for line in f:
+                    if not '=>' in line:
+                        continue
+                    path = line.rsplit('=>', 1)[1]
+                    if not name+'.' in path:
+                        continue
+                    res = pat.search(path)
+                    if res:
+                        return res.group(1)
 
         def find_library(name):
             return _findSoname_ldconfig(name) or _get_soname(_findLib_gcc(name))

Modified: python/branches/py3k/Misc/ACKS
==============================================================================
--- python/branches/py3k/Misc/ACKS	(original)
+++ python/branches/py3k/Misc/ACKS	Sat Feb 26 09:45:20 2011
@@ -329,6 +329,7 @@
 Michael Guravage
 Lars Gustäbel
 Thomas Güttler
+Jonas H.
 Barry Haddow
 Paul ten Hagen
 Rasmus Hahn

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sat Feb 26 09:45:20 2011
@@ -35,6 +35,9 @@
 Library
 -------
 
+- Issue #11258: Speed up ctypes.util.find_library() under Linux a lot.  Patch
+  by Jonas H.
+
 - Issue #11297: Add collections.ChainMap().
 
 - Issue #10755: Add the posix.fdlistdir() function.  Patch by Ross Lagerwall.


More information about the Python-checkins mailing list