def getInstance(hWnd): from cffi import FFI as _FFI _ffi = _FFI() _ffi.cdef('long __stdcall GetWindowLongA(void* hWnd, int nIndex);') _lib = _ffi.dlopen('User32.dll') return _lib.GetWindowLongA(_ffi.cast('void*', hWnd), -6) # GWL_HINSTANCE
def _has_load_extension(): """Only available since 3.3.6""" unverified_ffi = _FFI() unverified_ffi.cdef(""" typedef ... sqlite3; int sqlite3_enable_load_extension(sqlite3 *db, int onoff); """) libname = 'sqlite3' if sys.platform == 'win32': import os _libname = os.path.join(os.path.dirname(sys.executable), libname) if os.path.exists(_libname + '.dll'): libname = _libname unverified_lib = unverified_ffi.dlopen(libname) return hasattr(unverified_lib, 'sqlite3_enable_load_extension')
def _has_backup(): """Only available since 3.6.11""" unverified_ffi = _FFI() unverified_ffi.cdef(""" typedef ... sqlite3; typedef ... sqlite3_backup; sqlite3_backup* sqlite3_backup_init(sqlite3 *, const char* , sqlite3 *, const char*); """) libname = 'sqlite3' if sys.platform == 'win32': import os _libname = os.path.join(os.path.dirname(sys.executable), libname) if os.path.exists(_libname + '.dll'): libname = _libname unverified_lib = unverified_ffi.dlopen(libname) return hasattr(unverified_lib, 'sqlite3_backup_init')
def _ignore_stderr(): """Try to forward PortAudio messages from stderr to /dev/null.""" ffi = _FFI() ffi.cdef(""" /* from stdio.h */ FILE* fopen(const char* path, const char* mode); int fclose(FILE* fp); FILE* stderr; /* GNU C library */ FILE* __stderrp; /* Mac OS X */ """) stdio = ffi.dlopen(None) devnull = stdio.fopen(os.devnull.encode(), b'w') try: stdio.stderr = devnull except KeyError: try: stdio.__stderrp = devnull except KeyError: stdio.fclose(devnull)
def __init__(self, plugin_file=""): this_module_dir = _os.path.dirname(_os.path.realpath(__file__)) # find the C library by climbing the directory tree while plugin_file == "": plugin_pattern = _os.path.join(this_module_dir, "*ags_blosc_wrapper.*") candidate_plugins = _glob.glob(plugin_pattern) # if found then break if candidate_plugins: plugin_file = candidate_plugins[0] break # not found and already at root. We're not going to find it if this_module_dir == "/": raise ValueError("Cannot find plugin ags_blosc_wrapper") # go to parent directory and try again this_module_dir = _os.path.split(this_module_dir)[0] # specify the C signatures of the foreign functions self._ffi = _FFI() self._ffi.cdef("typedef void* ags_BloscWrapper;") self._ffi.cdef("ags_BloscWrapper ags_BloscWrapper_new();") self._ffi.cdef("void ags_BloscWrapper_delete(ags_BloscWrapper);") self._ffi.cdef( "size_t ags_BloscWrapper_reserveNeededToCompress(ags_BloscWrapper, size_t);" ) self._ffi.cdef( "size_t ags_BloscWrapper_reserveNeededToDecompress(ags_BloscWrapper, void*);" ) self._ffi.cdef( "size_t ags_BloscWrapper_compress(ags_BloscWrapper, void*, size_t, void*, size_t);" ) self._ffi.cdef( "size_t ags_BloscWrapper_decompress(ags_BloscWrapper, void*, void*, size_t);" ) self._cmodule = self._ffi.dlopen(plugin_file) # allocate a new raw instance self.blosc_wrapper = self._cmodule.ags_BloscWrapper_new()
def _ignore_stderr(): """ Try to forward PortAudio messages from stderr to /dev/null. """ ffi = _FFI() ffi.cdef(""" /* from stdio.h */ FILE* fopen(const char* path, const char* mode); int fclose(FILE* fp); FILE* stderr; /* GNU C library */ FILE* __stderrp; /* Mac OS X */ """) stdio = ffi.dlopen(None) devnull = stdio.fopen(os.devnull.encode(), b'w') try: stdio.stderr = devnull except KeyError: try: stdio.__stderrp = devnull except KeyError: stdio.fclose(devnull)
#!/usr/bin/env python import os import ctypes.util from cffi import FFI as _FFI _ffi = _FFI() with open(os.path.join(os.path.dirname(__file__), 'sox.h')) as soxh: _ffi.set_source('_sox', '''\ #include <sox.h> ''', libraries=["sox"]) _ffi.cdef(soxh.read()) if __name__ == '__main__': _ffi.compile(verbose=True)
import os as _os import re from cffi import FFI as _FFI #################################################################################################### from ctypes.util import find_library as _find_library # from util import _find_library from .Constantes import * #################################################################################################### _ffi = _FFI() _glfw = None # Use a function in order to don't spoil the module def _init(): glfw_library = None # First if there is an environment variable pointing to the library if 'GLFW_LIBRARY' in _os.environ: library_path = _os.path.realpath(_os.environ['GLFW_LIBRARY']) if _os.path.exists(library_path): glfw_library = library_path # Else, try to find it if glfw_library is None: ordered_library_names = ['glfw', 'glfw3']