def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer( 'qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1, 2, 3, 4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result)
def test__ffi_call_releases_gil(self): from pypy.rlib.clibffi import get_libc_name def main(libc_name, n): import time import os from threading import Thread # if os.name == 'nt': from _ffi import WinDLL, types libc = WinDLL(libc_name) sleep = libc.getfunc('Sleep', [types.uint], types.uint) delays = [0]*n + [1000] else: from _ffi import CDLL, types libc = CDLL(libc_name) sleep = libc.getfunc('sleep', [types.uint], types.uint) delays = [0]*n + [1] # def loop_of_sleeps(i, delays): for delay in delays: sleep(delay) # ID: sleep # threads = [Thread(target=loop_of_sleeps, args=[i, delays]) for i in range(5)] start = time.time() for i, thread in enumerate(threads): thread.start() for thread in threads: thread.join() end = time.time() return end - start log = self.run(main, [get_libc_name(), 200], threshold=150, import_site=True) assert 1 <= log.result <= 1.5 # at most 0.5 seconds of overhead loops = log.loops_by_id('sleep') assert len(loops) == 1 # make sure that we actually JITted the loop
def test_dlopen(self): s = rffi.str2charp('xxxxxxxxxxxx') py.test.raises(DLOpenError, "dlopen(s)") rffi.free_charp(s) # s = rffi.str2charp(get_libc_name()) assert dlopen(s) rffi.free_charp(s)
def test_dlsym(self): s = rffi.str2charp(get_libc_name()) lib = dlopen(s) rffi.free_charp(s) handle = rffi.cast(lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), dlsym(lib, 'abs')) assert 1 == handle(1) assert 1 == handle(-1)
def test_dlsym(self): s = rffi.str2charp(get_libc_name()) lib = dlopen(s) rffi.free_charp(s) handle = rffi.cast( lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)), dlsym(lib, 'abs')) assert 1 == handle(1) assert 1 == handle(-1)
def memcpy(target, source, n): impl = clibffi.CDLL(clibffi.get_libc_name()).getpointer( "memcpy", [ clibffi.ffi_type_pointer, clibffi.ffi_type_pointer, clibffi.ffi_type_uint ], clibffi.ffi_type_void) impl.push_arg(target) impl.push_arg(source) impl.push_arg(n) impl.call(lltype.Void)
def setup_class(cls): space = gettestobjspace(usemodules=('_ffi', '_rawffi')) cls.space = space cls.w_iswin32 = space.wrap(sys.platform == 'win32') cls.w_libfoo_name = space.wrap(cls.prepare_c_example()) cls.w_libc_name = space.wrap(get_libc_name()) libm_name = get_libm_name(sys.platform) cls.w_libm_name = space.wrap(libm_name) libm = CDLL(libm_name) pow = libm.getpointer('pow', [], types.void) pow_addr = rffi.cast(rffi.LONG, pow.funcsym) cls.w_pow_addr = space.wrap(pow_addr)
def setup_class(cls): from pypy.rlib.clibffi import get_libc_name space = gettestobjspace(usemodules=('_rawffi', 'struct')) cls.space = space cls.w_lib_name = space.wrap(cls.prepare_c_example()) cls.w_libc_name = space.wrap(get_libc_name()) if sys.platform == 'win32': cls.w_iswin32 = space.wrap(True) cls.w_libm_name = space.wrap('msvcrt') else: cls.w_iswin32 = space.wrap(False) cls.w_libm_name = space.wrap('libm.so') if sys.platform == "darwin": cls.w_libm_name = space.wrap('libm.dylib') cls.w_platform = space.wrap(platform.name) cls.w_sizes_and_alignments = space.wrap(dict( [(k, (v.c_size, v.c_alignment)) for k,v in TYPEMAP.iteritems()]))
def setup_class(cls): from pypy.rlib.clibffi import get_libc_name space = gettestobjspace(usemodules=('_rawffi', 'struct')) cls.space = space cls.w_lib_name = space.wrap(cls.prepare_c_example()) cls.w_libc_name = space.wrap(get_libc_name()) if sys.platform == 'win32': cls.w_iswin32 = space.wrap(True) cls.w_libm_name = space.wrap('msvcrt') else: cls.w_iswin32 = space.wrap(False) cls.w_libm_name = space.wrap('libm.so') if sys.platform == "darwin": cls.w_libm_name = space.wrap('libm.dylib') cls.w_platform = space.wrap(platform.name) cls.w_sizes_and_alignments = space.wrap( dict([(k, (v.c_size, v.c_alignment)) for k, v in TYPEMAP.iteritems()]))
def f(): libc = CDLL(get_libc_name()) qsort = libc.getpointer('qsort', [ffi_type_pointer, ffi_size_t, ffi_size_t, ffi_type_pointer], ffi_type_void) ptr = CallbackFuncPtr([ffi_type_pointer, ffi_type_pointer], ffi_type_sint, callback) TP = rffi.CArray(rffi.LONG) to_sort = lltype.malloc(TP, 4, flavor='raw') to_sort[0] = 4 to_sort[1] = 3 to_sort[2] = 1 to_sort[3] = 2 qsort.push_arg(rffi.cast(rffi.VOIDP, to_sort)) qsort.push_arg(rffi.cast(rffi.SIZE_T, 4)) qsort.push_arg(rffi.cast(rffi.SIZE_T, rffi.sizeof(rffi.LONG))) qsort.push_arg(rffi.cast(rffi.VOIDP, ptr.ll_closure)) qsort.call(lltype.Void) result = [to_sort[i] for i in range(4)] == [1,2,3,4] lltype.free(to_sort, flavor='raw') keepalive_until_here(ptr) return int(result)
def __init__(self,library_path): self.lib = clibffi.CDLL(library_path) self._libc = clibffi.CDLL(clibffi.get_libc_name())
def get_libc(space): from pypy.rlib.clibffi import get_libc_name try: return space.wrap(W_CDLL(space, get_libc_name())) except OSError, e: raise wrap_oserror(space, e)
def get_libc(space): from pypy.rlib.clibffi import get_libc_name try: return space.wrap(W_CDLL(space, get_libc_name(), -1)) except OSError, e: raise wrap_oserror(space, e)
def setup_class(cls): cls.space = gettestobjspace(usemodules=['cpyext', 'thread', '_rawffi', 'array']) from pypy.rlib.clibffi import get_libc_name cls.w_libc = cls.space.wrap(get_libc_name())
def memcpy(target,source,n): impl = clibffi.CDLL(clibffi.get_libc_name()).getpointer("memcpy",[clibffi.ffi_type_pointer,clibffi.ffi_type_pointer,clibffi.ffi_type_uint],clibffi.ffi_type_void) impl.push_arg(target) impl.push_arg(source) impl.push_arg(n) impl.call(lltype.Void)
def setup_class(cls): cls.space = gettestobjspace( usemodules=['cpyext', 'thread', '_rawffi', 'array']) from pypy.rlib.clibffi import get_libc_name cls.w_libc = cls.space.wrap(get_libc_name())
def get_libc(space): try: return space.wrap(W_CDLL(space, get_libc_name(), -1)) except OSError, e: raise wrap_oserror(space, e)