def test_windll(self): if os.name != 'nt': skip('Run only on windows') from pypy.rlib.libffi import WinDLL dll = WinDLL('Kernel32.dll') sleep = dll.getpointer('Sleep',[types.uint], types.void) chain = ArgChain() chain.arg(10) sleep.call(chain, lltype.Void, is_struct=False)
def test_windll(self): if os.name != 'nt': skip('Run only on windows') from pypy.rlib.libffi import WinDLL dll = WinDLL('Kernel32.dll') sleep = dll.getpointer('Sleep', [types.uint], types.void) chain = ArgChain() chain.arg(10) sleep.call(chain, lltype.Void, is_struct=False)
def f42(n): c_strchr = glob.c_strchr raw = rffi.str2charp("foobar" + chr((n & 63) + 32)) argchain = ArgChain() argchain = argchain.arg(rffi.cast(lltype.Signed, raw)) argchain = argchain.arg(rffi.cast(rffi.INT, ord('b'))) res = c_strchr.call(argchain, rffi.CCHARP) check(rffi.charp2str(res) == "bar" + chr((n & 63) + 32)) rffi.free_charp(raw)
def call(self, funcspec, args, RESULT, init_result=0, is_struct=False): """ Call the specified function after constructing and ArgChain with the arguments in ``args``. The function is specified with ``funcspec``, which is a tuple of the form (lib, name, argtypes, restype). This method is overridden by metainterp/test/test_fficall.py in order to do the call in a loop and JIT it. The optional arguments are used only by that overridden method. """ lib, name, argtypes, restype = funcspec func = lib.getpointer(name, argtypes, restype) chain = ArgChain() for arg in args: if isinstance(arg, r_singlefloat): chain.arg_singlefloat(float(arg)) elif IS_32_BIT and isinstance(arg, r_longlong): chain.arg_longlong(longlong2float(arg)) elif IS_32_BIT and isinstance(arg, r_ulonglong): arg = rffi.cast(rffi.LONGLONG, arg) chain.arg_longlong(longlong2float(arg)) elif isinstance(arg, tuple): methname, arg = arg meth = getattr(chain, methname) meth(arg) else: chain.arg(arg) return func.call(chain, RESULT, is_struct=is_struct)
def libffi_stuff(i, j): lib = CDLL(libm_name) func = lib.getpointer('fabs', [types.double], types.double) res = 0.0 x = float(j) while i > 0: jitdriver2.jit_merge_point(i=i, res=res, func=func, x=x) promote(func) argchain = ArgChain() argchain.arg(x) res = func.call(argchain, rffi.DOUBLE) i -= 1 return res
def f42(): length = len(glob.lst) c_qsort = glob.c_qsort raw = alloc1() fn = llhelper(CALLBACK, rffi._make_wrapper_for(CALLBACK, callback)) argchain = ArgChain() argchain = argchain.arg(rffi.cast(lltype.Signed, raw)) argchain = argchain.arg(rffi.cast(rffi.SIZE_T, 2)) argchain = argchain.arg(rffi.cast(rffi.SIZE_T, 8)) argchain = argchain.arg(rffi.cast(lltype.Signed, fn)) c_qsort.call(argchain, lltype.Void) free1(raw) check(len(glob.lst) > length) del glob.lst[:]
def test_argchain(self): chain = ArgChain() assert chain.numargs == 0 chain2 = chain.arg(42) assert chain2 is chain assert chain.numargs == 1 intarg = chain.first assert chain.last is intarg assert intarg.intval == 42 chain.arg(123.45) assert chain.numargs == 2 assert chain.first is intarg assert intarg.next is chain.last floatarg = intarg.next assert floatarg.floatval == 123.45
def g(func): # a different function, which is marked as "dont_look_inside" # in case it uses an unsupported argument argchain = ArgChain() # this loop is unrolled for method_name, argval in method_and_args: getattr(argchain, method_name)(argval) return func.call(argchain, RESULT, is_struct=is_struct)
def call(self, args_w): assert len(args_w) == len(self.argtypes_w) argchain = ArgChain() buffers = [] for i in xrange(len(args_w)): w_arg = args_w[i] assert isinstance(w_arg, self.argtypes_w[i]) if isinstance(w_arg, W_Integer): argchain.arg(w_arg.ival) elif isinstance(w_arg, W_Symbol): sval = w_arg.sval assert sval is not None charp = rffi.str2charp(sval) buffers.append(charp) argchain.arg(charp) elif isinstance(w_arg, W_String): sval = w_arg.content() assert sval is not None charp = rffi.str2charp(sval) buffers.append(charp) argchain.arg(charp) elif isinstance(w_arg, W_Boolean): argchain.arg(w_arg.to_bool()) else: assert 0, 'unsupported argument type' res = self.funcptr.call(argchain, rffi.LONG) # annotate type to LONG for charp in buffers: rffi.free_charp(charp) # if self.w_restype is W_Integer: w_retval = W_Integer(rffi.cast(rffi.LONG, res)) elif self.w_restype is W_Boolean: w_retval = w_boolean(rffi.cast(rffi.LONG, res)) elif self.w_restype is W_Symbol: # XXX who delete the returned pointer? w_retval = symbol(rffi.charp2str(rffi.cast(rffi.CCHARP, res))) elif self.w_restype is W_String: w_retval = W_String(rffi.charp2str(rffi.cast(rffi.CCHARP, res))) elif self.w_restype is W_Unspecified: w_retval = w_unspec else: assert 0, 'unsupported result type' return w_retval
def test_wrong_args(self): # so far the test passes but for the wrong reason :-), i.e. because # .arg() only supports integers and floats chain = ArgChain() x = lltype.malloc(lltype.GcStruct('xxx')) y = lltype.malloc(lltype.GcArray(rffi.SIGNED), 3) z = lltype.malloc(lltype.Array(rffi.SIGNED), 4, flavor='raw') py.test.raises(TypeError, "chain.arg(x)") py.test.raises(TypeError, "chain.arg(y)") py.test.raises(TypeError, "chain.arg(z)") lltype.free(z, flavor='raw')
def call(self, funcspec, args, RESULT, is_struct=False, jitif=[]): """ Call the specified function after constructing and ArgChain with the arguments in ``args``. The function is specified with ``funcspec``, which is a tuple of the form (lib, name, argtypes, restype). This method is overridden by metainterp/test/test_fficall.py in order to do the call in a loop and JIT it. The optional arguments are used only by that overridden method. """ lib, name, argtypes, restype = funcspec func = lib.getpointer(name, argtypes, restype) chain = ArgChain() for arg in args: if isinstance(arg, tuple): methname, arg = arg meth = getattr(chain, methname) meth(arg) else: chain.arg(arg) return func.call(chain, RESULT, is_struct=is_struct)