Created
August 27, 2017 01:48
-
-
Save Lyude/9909fe7995360b0914b25efca65064a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
from ctypes.util import find_library | |
class LibClang(CDLL): | |
def __getitem__(self, name): | |
return super().__getitem__('clang_' + name) | |
libclang = LibClang(find_library('clang')) | |
class ClangString(): | |
def __init__(self, ptr): | |
self._as_parameter_ = c_void_p(ptr) | |
def __del__(self): | |
libclang.disposeString(self) | |
def __str__(self): | |
def value(self): | |
libclang.getCString.restype = c_char_p | |
class CFFI(): | |
""" | |
Allows us to call functions for a certain class in a libclang C class | |
without having to do anywhere as much typing. Also caches values looked | |
up through ctypes. | |
""" | |
def __init__(self, type_name): | |
self.__type_name = type_name | |
def __getattr__(self, name): | |
# Cache an entry for the function so we don't have to look it up again | |
symbol = libclang['%s_%s' % (self.__type_name, name)] | |
setattr(self, name, symbol) | |
return symbol | |
def __getitem__(self, item): | |
return getattr(self, item) | |
def __name__(self): | |
return self.__type_name | |
class ClangObject(): | |
def __init__(self, ptr): | |
self._as_parameter_ = c_void_p(ptr) | |
def __init_subclass__(cls, **kwargs): | |
super().__init_subclass__(**kwargs) | |
cls._cffi = CFFI(cls.__name__) | |
def __del__(self): | |
try: | |
self._cffi.dispose(self) | |
except AttributeError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment