def build_ffi(cdef_source, verify_source, libraries=[], extra_compile_args=[], extra_link_args=[]): ffi = FFI() ffi.cdef(cdef_source) ffi.verifier = Verifier( ffi, verify_source, tmpdir='', modulename=_create_modulename(cdef_source, verify_source, sys.version), libraries=libraries, ext_package="cryptography", extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, ) return ffi, ffi.verifier.load_library()
def build_ffi(cdef_source, verify_source, libraries=[], extra_compile_args=[], extra_link_args=[]): ffi = FFI() ffi.cdef(cdef_source) ffi.verifier = Verifier( ffi, verify_source, tmpdir='', modulename=_create_modulename(cdef_source, verify_source, sys.version), libraries=libraries, ext_package="cryptography", extra_compile_args=extra_compile_args, extra_link_args=extra_link_args, ) ffi.verifier.compile_module = _compile_module ffi.verifier._compile_module = _compile_module return ffi
os.chdir(_FILE_PATH) os.chdir(os.pardir) #use the absolute path to load the file here so we don't have to worry about working directory issues _CDEF = open("{}/python4D.h".format(_FILE_PATH)).read() ffi.cdef(_CDEF) _SOURCE = """ #include "fourd.h" """ source_files = glob.glob('lib4d_sql/*.c') ffi.verifier = Verifier(ffi, _SOURCE, modulename=_create_modulename(_CDEF, _SOURCE, sys.version), sources=source_files, include_dirs=['lib4d_sql', 'python4D/lib4d_sql']) #ffi.verifier.compile_module = _compile_module #ffi.verifier._compile_module = _compile_module lib4d_sql = LazyLoadLib(ffi) os.chdir(_CWD) ######################################################################## ######################################################################## ## Error Classes ######################################################################## class Warning(Exception):
return '_xprintidle_cffi_{0}{1}'.format(k1, k2) source_file = os.path.join(os.path.abspath( os.path.dirname(__file__)), 'src', "xprintidle.c") f = open(source_file) SOURCE = f.read() CDEF = """ unsigned long idle_time(); """ ffi = FFI() ffi.cdef(CDEF) ffi.verifier = Verifier( ffi, SOURCE, libraries=['X11', 'Xss'], modulename=_create_modulename(CDEF, SOURCE, sys.version), ) class LazyLibrary(object): def __init__(self, ffi): self._ffi = ffi self._lib = None self._lock = threading.Lock() def __getattr__(self, name): if self._lib is None: with self._lock: if self._lib is None: self._lib = self._ffi.verifier.load_library()
def __getattr__(self, name): self.load() return getattr(self._lib, name) def load(self): if self._lib is None: with self._lock: if self._lib is None: self._lib = self._ffi.verifier.load_library() CDEF = pkgutil.get_data('posix_spawn', 'c/cdef.h').decode('ascii') SOURCE = pkgutil.get_data('posix_spawn', 'c/source.c').decode('ascii') ffi = FFI() ffi.cdef(CDEF) ffi.verifier = Verifier( ffi, SOURCE, modulename=_create_modulename(CDEF, SOURCE, sys.version), ) # Patch the Verifier() instance to prevent CFFI from compiling the module ffi.verifier.compile_module = _compile_module ffi.verifier._compile_module = _compile_module lib = LazyLibrary(ffi) __all__ = ('lib', 'ffi')
""" SOURCE = """ #include "ow-crypt.h" """ _ffi = FFI() _ffi.cdef(CDEF) _ffi.verifier = Verifier( _ffi, SOURCE, sources=[ str(os.path.join(_bundled_dir, "crypt_blowfish.c")), str(os.path.join(_bundled_dir, "crypt_gensalt.c")), str(os.path.join(_bundled_dir, "wrapper.c")), # How can we get distutils to work with a .S file? # Set bcrypt/crypt_blowfish-1.3/crypt_blowfish.c#57 back to 1 if we # get ASM loaded. # str(os.path.join(_bundled_dir, "x86.S")), ], include_dirs=[str(_bundled_dir)], modulename=_create_modulename(CDEF, SOURCE, sys.version), ) # Patch the Verifier() instance to prevent CFFI from compiling the module _ffi.verifier.compile_module = _compile_module _ffi.verifier._compile_module = _compile_module _bcrypt_lib = LazyLibrary(_ffi)
# attempting to load the library, which would trigger a compile normally if it # can't be loaded, which we want to delay so it doesn't happen on import. This # will enable us to import this module, and use it in our setup.py to get the # Extension object to allow distutils to build it normally. ffi.verifier = Verifier( ffi, SOURCE, # This needs to match the value in setup.py ext_package="http11", # Fix the fact that CFFI doesn't sanely work when you don't have the exact # version installed that a library was built against. modulename=create_modulename(CDEF, SOURCE, sys.version), # We want to compile the http_parser.c instead of trying to link against it # or anything like that. sources=[ os.path.join(SRC_DIR, "http11.c"), ], # We need to include the bundled dir so that we can include the header # files located in it. include_dirs=[ SRC_DIR, ], ) class Library: def __init__(self, ffi):
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff) k2 = k2.lstrip('0').rstrip('L') return '_gphoto2_cffi_{0}{1}'.format(k1, k2) def _compile_module(*args, **kwargs): raise RuntimeError( "Attempted implicit compile of a cffi module. All cffi modules should " "be pre-compiled at installation time.") ffi = FFI() ffi.cdef(CDEF) ffi.verifier = Verifier(ffi, SOURCE, modulename=_create_modulename(CDEF, SOURCE, sys.version), libraries=['gphoto2']) # Patch the Verifier() instance to prevent CFFI from compiling the module ffi.verifier.compile_module = _compile_module ffi.verifier._compile_module = _compile_module class _globals(object): def __init__(self, lib): self._lib = lib @property def FILE_TYPES(self): """ Mapping from libgphoto2 file type constants to human-readable
//int ikcp_rcvbuf_count(const ikcpcb *kcp); //int ikcp_sndbuf_count(const ikcpcb *kcp); void ikcp_log(ikcpcb *kcp, int mask, const char *fmt, ...); // setup allocator void ikcp_allocator(void* (*new_malloc)(size_t), void (*new_free)(void*)); int ikcp_get_conv(const char *data, long size, IUINT32* conv_out); """ SOURCE = """ #include <ikcp.c> """ ffi.cdef(CDEF) ffi.verifier = Verifier(ffi,SOURCE , include_dirs=[include_dir], modulename=_create_modulename(CDEF, SOURCE, sys.version)) ffi.verifier.compile_module = _compile_module ffi.verifier._compile_module = _compile_module ikcp_impl = LazyLibrary(ffi) @ffi.callback("int(const char *, int , ikcpcb*, void *)") def ikcp_output(cdata, size, kcp, user_handle): buffer = ffi.buffer(cdata = cdata, size = size) kcp = ffi.from_handle(user_handle) return kcp.output(buffer) DEFAULT_MODE = 0 NORMAL_MODE = 1 FAST_MODE = 2
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff) k2 = k2.lstrip('0').rstrip('L') return '_{2}_cffi_{0}{1}'.format(k1, k2, prefix) class LazyLibrary(object): def __init__(self, ffi): self._ffi = ffi self._lib = None self._lock = threading.Lock() @property def lib(self): if self._lib is None: self._lib = self._ffi.verifier.load_library() return self._lib def __getattr__(self, name): return getattr(self.lib, name) ffi = FFI() ffi.cdef(_CDEF) ffi.verifier = Verifier( ffi, _SOURCE, libraries=["smi"], modulename=create_modulename("Mib", _CDEF, _SOURCE)) smi = LazyLibrary(ffi) __all__ = [ffi]
if( (!croaring_get_elt( x, stop - sign , &first_elt)) || (!croaring_get_elt( x, start, &last_elt)) ) return roaring_bitmap_create(); } _croaring = roaring_bitmap_from_range(first_elt, last_elt + 1, abs(step)); roaring_bitmap_and_inplace(_croaring, x); _croaring->copy_on_write = x->copy_on_write; return _croaring; }else{ return NULL; } } """ ffi.cdef(CDEF) ffi.verifier = Verifier(ffi, SOURCE, include_dirs=[include_dir], modulename=_create_modulename(CDEF, SOURCE, sys.version), extra_compile_args=['-std=c99', '-O3', '-msse4.2']) lib = ffi.verifier.load_library() class BitSet(object): def __init__(self, values=None, copy_on_write=False, croaring=None): if croaring: assert values is None and not copy_on_write self._croaring = croaring return elif values is None: self._croaring = lib.roaring_bitmap_create() elif isinstance(values, self.__class__):
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff) k2 = k2.lstrip('0').rstrip('L') return '_{2}_cffi_{0}{1}'.format(k1, k2, prefix) class LazyLibrary(object): def __init__(self, ffi): self._ffi = ffi self._lib = None self._lock = threading.Lock() @property def lib(self): if self._lib is None: self._lib = self._ffi.verifier.load_library() return self._lib def __getattr__(self, name): return getattr(self.lib, name) ffi = FFI() ffi.cdef(_CDEF) ffi.verifier = Verifier(ffi, _SOURCE, libraries=["smi"], modulename=create_modulename("Mib", _CDEF, _SOURCE)) smi = LazyLibrary(ffi) __all__ = [ffi]
ffi.cdef(CDEF) # Construct a Verifier manually, this will prevent the ffi instance from # attempting to load the library, which would trigger a compile normally if it # can't be loaded, which we want to delay so it doesn't happen on import. This # will enable us to import this module, and use it in our setup.py to get the # Extension object to allow distutils to build it normally. ffi.verifier = Verifier( ffi, SOURCE, # This needs to match the value in setup.py ext_package="http11", # We want to compile the http_parser.c instead of trying to link against it # or anything like that. sources=[ os.path.join(SRC_DIR, "http11.c"), ], # We need to include the bundled dir so that we can include the header # files located in it. include_dirs=[ SRC_DIR, ], ) lib = ffi.verifier.load_library()
os.chdir(_FILE_PATH) os.chdir(os.pardir) #use the absolute path to load the file here so we don't have to worry about working directory issues _CDEF = open("{}/py_fourd.h".format(_FILE_PATH)).read() ffi.cdef(_CDEF) _SOURCE = """ #include "fourd.h" """ source_files = glob.glob('lib4d_sql/*.c') ffi.verifier = Verifier(ffi, _SOURCE, modulename=_create_modulename(_CDEF, _SOURCE, sys.version), sources=source_files, include_dirs=['lib4d_sql', 'py4d/lib4d_sql']) #ffi.verifier.compile_module = _compile_module #ffi.verifier._compile_module = _compile_module lib4d_sql = LazyLoadLib(ffi) os.chdir(_CWD) ######################################################################## ######################################################################## ## Error Classes ######################################################################## class Warning(Exception):
//int ikcp_rcvbuf_count(const ikcpcb *kcp); //int ikcp_sndbuf_count(const ikcpcb *kcp); void ikcp_log(ikcpcb *kcp, int mask, const char *fmt, ...); // setup allocator void ikcp_allocator(void* (*new_malloc)(size_t), void (*new_free)(void*)); // read conv IUINT32 ikcp_getconv(const void *ptr); """ SOURCE = """ #include <ikcp.c> """ ffi.cdef(CDEF) ffi.verifier = Verifier(ffi,SOURCE , include_dirs=[include_dir], modulename=_create_modulename(CDEF, SOURCE, sys.version)) ffi.verifier.compile_module = _compile_module ffi.verifier._compile_module = _compile_module ikcp_impl = LazyLibrary(ffi) @ffi.callback("int(const char *, int , ikcpcb*, void *)") def ikcp_output(cdata, size, kcp, user_handle): buffer = ffi.buffer(cdata = cdata, size = size) kcp = ffi.from_handle(user_handle) return kcp.output(buffer) DEFAULT_MODE = 0 NORMAL_MODE = 1 FAST_MODE = 2
# Add all of our header files, but sort first for consistency of the # hash that CFFI generates and uses in the .so filename (the order of # glob() results cannot be relied on) for header in sorted(HEADERS): with open(header, "r") as hfile: ffi.cdef(hfile.read()) # TODO: Can we use the ABI of libsodium for this instead? ffi.verifier = Verifier( ffi, "#include <sodium.h>", # We need to link to the sodium library libraries=["sodium"], # Our ext_package is nacl so look for it ext_package="nacl._lib", ) class Library(object): def __init__(self, ffi): self.ffi = ffi self._initialized = False # This prevents the compile_module() from being called, the module # should have been compiled by setup.py
# Build our FFI instance ffi = FFI() # Add all of our header files, but sort first for consistency of the # hash that CFFI generates and uses in the .so filename (the order of # glob() results cannot be relied on) for header in sorted(HEADERS): with open(header, "r") as hfile: ffi.cdef(hfile.read()) # TODO: Can we use the ABI of libsodium for this instead? ffi.verifier = Verifier( ffi, "#include <sodium.h>", # We need to link to the sodium library libraries=["sodium"], # Our ext_package is nacl so look for it ext_package="nacl._lib", ) class Library(object): def __init__(self, ffi): self.ffi = ffi self._lib = None # This prevents the compile_module() from being called, the module # should have been compiled by setup.py def _compile_module(*args, **kwargs): raise RuntimeError("Cannot compile module during runtime")
# attempting to load the library, which would trigger a compile normally if it # can't be loaded, which we want to delay so it doesn't happen on import. This # will enable us to import this module, and use it in our setup.py to get the # Extension object to allow distutils to build it normally. ffi.verifier = Verifier( ffi, SOURCE, # This needs to match the value in setup.py ext_package="http11", # Fix the fact that CFFI doesn't sanely work when you don't have the exact # version installed that a library was built against. modulename=create_modulename(CDEF, SOURCE, sys.version), # We want to compile the http_parser.c instead of trying to link against it # or anything like that. sources=[ os.path.join(SRC_DIR, "http11.c"), ], # We need to include the bundled dir so that we can include the header # files located in it. include_dirs=[ SRC_DIR, ], ) class Library: