From d059e7b42301856ea006ec4a11b0953a65c2b6c8 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Fri, 12 Jan 2007 20:17:34 +0000 Subject: [PATCH] patch #1610795: BSD version of ctypes.util.find_library, by Martin Kammerhofer. release25-maint backport candidate, but the release manager has to decide. --- Lib/ctypes/util.py | 78 +++++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 2ee2968d910..f7133538bfa 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -46,23 +46,16 @@ elif os.name == "posix": import re, tempfile, errno def _findLib_gcc(name): - expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name + expr = r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) fdout, ccout = tempfile.mkstemp() os.close(fdout) - cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \ + cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \ '$CC -Wl,-t -o ' + ccout + ' 2>&1 -l' + name try: - fdout, outfile = tempfile.mkstemp() - os.close(fdout) - fd = os.popen(cmd) - trace = fd.read() - err = fd.close() + f = os.popen(cmd) + trace = f.read() + f.close() finally: - try: - os.unlink(outfile) - except OSError, e: - if e.errno != errno.ENOENT: - raise try: os.unlink(ccout) except OSError, e: @@ -73,29 +66,58 @@ elif os.name == "posix": return None return res.group(0) - def _findLib_ld(name): - expr = '/[^\(\)\s]*lib%s\.[^\(\)\s]*' % name - res = re.search(expr, os.popen('/sbin/ldconfig -p 2>/dev/null').read()) - if not res: - # Hm, this works only for libs needed by the python executable. - cmd = 'ldd %s 2>/dev/null' % sys.executable - res = re.search(expr, os.popen(cmd).read()) - if not res: - return None - return res.group(0) - def _get_soname(f): + # assuming GNU binutils / ELF + if not f: + return None cmd = "objdump -p -j .dynamic 2>/dev/null " + f res = re.search(r'\sSONAME\s+([^\s]+)', os.popen(cmd).read()) if not res: return None return res.group(1) - def find_library(name): - lib = _findLib_ld(name) or _findLib_gcc(name) - if not lib: - return None - return _get_soname(lib) + if (sys.platform.startswith("freebsd") + or sys.platform.startswith("openbsd") + or sys.platform.startswith("dragonfly")): + + def _num_version(libname): + # "libxyz.so.MAJOR.MINOR" => [ MAJOR, MINOR ] + parts = libname.split(".") + nums = [] + try: + while parts: + nums.insert(0, int(parts.pop())) + except ValueError: + pass + return nums or [ sys.maxint ] + + def find_library(name): + ename = re.escape(name) + expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) + res = re.findall(expr, + os.popen('/sbin/ldconfig -r 2>/dev/null').read()) + if not res: + return _get_soname(_findLib_gcc(name)) + res.sort(cmp= lambda x,y: cmp(_num_version(x), _num_version(y))) + return res[-1] + + else: + + def _findLib_ldconfig(name): + # XXX assuming GLIBC's ldconfig (with option -p) + expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) + res = re.search(expr, + os.popen('/sbin/ldconfig -p 2>/dev/null').read()) + if not res: + # Hm, this works only for libs needed by the python executable. + cmd = 'ldd %s 2>/dev/null' % sys.executable + res = re.search(expr, os.popen(cmd).read()) + if not res: + return None + return res.group(0) + + def find_library(name): + return _get_soname(_findLib_ldconfig(name) or _findLib_gcc(name)) ################################################################ # test code