diff options
author | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 04:41:24 +0000 |
---|---|---|
committer | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-10 04:41:24 +0000 |
commit | 61e22cd85cd4c84fff391da67018c92bf21a8e19 (patch) | |
tree | 9f3f6b06d25b5735247ea51b665ed16a858cab3b /bindings/python/llvm/common.py | |
parent | 51cf8661637c114e4b4f178bd2677a6bb246be0d (diff) | |
download | external_llvm-61e22cd85cd4c84fff391da67018c92bf21a8e19.zip external_llvm-61e22cd85cd4c84fff391da67018c92bf21a8e19.tar.gz external_llvm-61e22cd85cd4c84fff391da67018c92bf21a8e19.tar.bz2 |
[llvm.py] Implement interface to object files
It is now possible to load object files and scan over sections, symbols,
and relocations! Includes test code with partial coverage.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152482 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings/python/llvm/common.py')
-rw-r--r-- | bindings/python/llvm/common.py | 72 |
1 files changed, 67 insertions, 5 deletions
diff --git a/bindings/python/llvm/common.py b/bindings/python/llvm/common.py index fe35bf1..9911e69 100644 --- a/bindings/python/llvm/common.py +++ b/bindings/python/llvm/common.py @@ -14,23 +14,85 @@ from ctypes import cdll import ctypes.util __all__ = [ - 'LLVMObject', + 'c_object_p', 'find_library', 'get_library', ] -LLVMObject = POINTER(c_void_p) +c_object_p = POINTER(c_void_p) + +class LLVMObject(object): + """Base class for objects that are backed by an LLVM data structure. + + This class should never be instantiated outside of this package. + """ + def __init__(self, ptr, ownable=True, disposer=None): + assert isinstance(ptr, c_object_p) + + self._ptr = self._as_parameter_ = ptr + + self._self_owned = True + self._ownable = ownable + self._disposer = disposer + + self._owned_objects = [] + + def take_ownership(self, obj): + """Take ownership of another object. + + When you take ownership of another object, you are responsible for + destroying that object. In addition, a reference to that object is + placed inside this object so the Python garbage collector will not + collect the object while it is still alive in libLLVM. + + This method should likely only be called from within modules inside + this package. + """ + assert isinstance(obj, LLVMObject) + + self._owned_objects.append(obj) + obj._self_owned = False + + def from_param(self): + """ctypes function that converts this object to a function parameter.""" + return self._as_parameter_ + + def __del__(self): + if self._self_owned and self._disposer: + self._disposer(self) + +class CachedProperty(object): + """Decorator that caches the result of a property lookup. + + This is a useful replacement for @property. It is recommended to use this + decorator on properties that invoke C API calls for which the result of the + call will be idempotent. + """ + def __init__(self, wrapped): + self.wrapped = wrapped + try: + self.__doc__ = wrapped.__doc__ + except: # pragma: no cover + pass + + def __get__(self, instance, instance_type=None): + if instance is None: + return self + + value = self.wrapped(instance) + setattr(instance, self.wrapped.__name__, value) + + return value def find_library(): # FIXME should probably have build system define absolute path of shared # library at install time. - for lib in ['LLVM-3.1svn', 'LLVM']: + for lib in ['LLVM-3.1svn', 'libLLVM-3.1svn', 'LLVM', 'libLLVM']: result = ctypes.util.find_library(lib) if result: return result - # FIXME This is a local hack to ease development. - return "/usr/local/llvm/lib/libLLVM-3.1svn.so" + return None def get_library(): """Obtain a reference to the llvm library.""" |