Thread: [ctypes-commit] ctypes/ctypes util.py,1.5,1.6 __init__.py,1.84,1.85
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2006-04-13 18:41:18
|
Update of /cvsroot/ctypes/ctypes/ctypes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29848 Modified Files: __init__.py Added Files: util.py Log Message: Merged in changes made in the LoadLibrary_branch: Restore the old way to load libraries again: cdll/windll have a LoadLibrary method, and no other public attributes. CDLL/WinDLL accept library pathname in the constructor, that's the only public attribute: all the find(), load(), and load_library() methods are gone. In other words, use these ways to load shared libraries: CDLL("path_to_lib") CDLL("path_to_lib", mode=RTLD_GLOBAL) cdll.path_to_lib cdll.LoadLibrary("path_to_lib") cdll.LoadLibrary("path_to_lib", mode=RTLD_GLOBAL) The CDLL/WinDLL base class has been renamed to LibraryLoader, in case anyone wants to subclass it. The ctypes._loader module has been removed completely. ctypes.util has been revived, it implements a find_library() function which should be useful to find libraries from the linker name (on platforms where this makes sense): find_library("c") -> "/usr/lib/libc.so.6" (Linux) find_library("c") -> "/usr/lib/libc.dylib" (OS X) find_library("msvcr71") -> "c:\windows\system32\msvcr71.dll" (Windows) If the library is not found, None is returned. --- NEW FILE: util.py --- import sys, os import ctypes # find_library(name) returns the pathname of a library, or None. if os.name in ("nt", "ce"): def find_library(name): # See MSDN for the REAL search order. for directory in os.environ['PATH'].split(os.pathsep): fname = os.path.join(directory, name) if os.path.exists(fname): return fname if fname.lower().endswith(".dll"): continue fname = fname + ".dll" if os.path.exists(fname): return fname return None if os.name == "posix" and sys.platform == "darwin": from ctypes.macholib.dyld import dyld_find as _dyld_find def find_library(name): possible = ['lib%s.dylib' % name, '%s.dylib' % name, '%s.framework/%s' % (name, name)] for name in possible: try: return _dyld_find(name) except ValueError: continue return None elif os.name == "posix": # Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdump import re, tempfile def _findLib_gcc(name): expr = '[^\(\)\s]*lib%s\.[^\(\)\s]*' % name cmd = 'if type gcc &>/dev/null; then CC=gcc; else CC=cc; fi;' \ '$CC -Wl,-t -o /dev/null 2>&1 -l' + name try: fdout, outfile = tempfile.mkstemp() fd = os.popen(cmd) trace = fd.read() err = fd.close() finally: try: os.unlink(outfile) except OSError, e: if e.errno != errno.ENOENT: raise res = re.search(expr, trace) if not res: 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): 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) ################################################################ # test code def test(): from ctypes import cdll if os.name == "nt": print cdll.msvcrt print cdll.load("msvcrt") print find_library("msvcrt") if os.name == "posix": # find and load_version print find_library("m") print find_library("c") print find_library("bz2") # getattr ## print cdll.m ## print cdll.bz2 # load if sys.platform == "darwin": print cdll.LoadLibrary("libm.dylib") print cdll.LoadLibrary("libcrypto.dylib") print cdll.LoadLibrary("libSystem.dylib") print cdll.LoadLibrary("System.framework/System") else: print cdll.LoadLibrary("libm.so") print cdll.LoadLibrary("libcrypt.so") print find_library("crypt") if __name__ == "__main__": test() Index: __init__.py =================================================================== RCS file: /cvsroot/ctypes/ctypes/ctypes/__init__.py,v retrieving revision 1.84 retrieving revision 1.85 diff -C2 -d -r1.84 -r1.85 *** __init__.py 13 Apr 2006 18:10:50 -0000 1.84 --- __init__.py 13 Apr 2006 18:41:15 -0000 1.85 *************** *** 32,37 **** FUNCFLAG_PYTHONAPI as _FUNCFLAG_PYTHONAPI - from ctypes._loader import LibraryLoader - """ WINOLEAPI -> HRESULT --- 32,35 ---- *************** *** 361,364 **** --- 359,379 ---- _restype_ = HRESULT + class LibraryLoader(object): + def __init__(self, dlltype): + self._dlltype = dlltype + + def __getattr__(self, name): + if name[0] == '_': + raise AttributeError(name) + dll = self._dlltype(name) + setattr(self, name, dll) + return dll + + def __getitem__(self, name): + return getattr(self, name) + + def LoadLibrary(self, name): + return self._dlltype(name) + cdll = LibraryLoader(CDLL) pydll = LibraryLoader(PyDLL) |