def tonumpy(arr, dtype): """Build a NumPy array from an NDArray""" itemsize = flypy.types.sizeof_type(dtype) steps = np.array(_getsteps(arr.dims)) shape = np.array(_getshape(arr.dims)) strides = steps * itemsize np_dtype = numpy_support.to_dtype(dtype) # Build NumPy array data = toobject(arr.data, Pointer[dtype]) ndarray = libcpy.create_array(address(data), shape, strides, np_dtype) return ndarray
def __call__(self, *args, **kwargs): from flypy.representation import byref, stack_allocate from flypy.conversion import (toctypes, fromctypes, toobject, fromobject, ctype) #from flypy.support.ctypes_support import CTypesStruct #from flypy.types import Function # Keep this alive for the duration of the call keepalive = list(args) + list(kwargs.values()) # Order arguments args = flatargs(self.py_func, args, kwargs) # Translate cfunc, restype = self.translate([typeof(x) for x in args.flat]) # Construct flypy values argtypes = [typeof(x) for x in args] arg_objs = list(starmap(fromobject, zip(args, argtypes))) # Map flypy values to a ctypes representation args = [] for arg, argtype in zip(arg_objs, argtypes): c_arg = toctypes(arg, argtype, keepalive) if byref(argtype) and stack_allocate(argtype): c_arg = ctypes.pointer(c_arg) args.append(c_arg) # We need this cast since the ctypes function constructed from LLVM # IR has different structs (which are structurally equivalent) c_restype = ctype(restype) if byref(restype): c_result = c_restype() # dummy result value args.append(ctypes.pointer(c_result)) c_restype = None # void c_signature = ctypes.PYFUNCTYPE(c_restype, *[type(arg) for arg in args]) cfunc = ctypes.cast(cfunc, c_signature) # Handle calling convention if byref(restype): cfunc(*args) else: c_result = cfunc(*args) # Map ctypes result back to a python value result = fromctypes(c_result, restype) result_obj = toobject(result, restype) return result_obj
def __call__(self, *args, **kwargs): from flypy.representation import byref, stack_allocate from flypy.conversion import ( toctypes, fromctypes, toobject, fromobject, ctype) #from flypy.support.ctypes_support import CTypesStruct #from flypy.types import Function # Keep this alive for the duration of the call keepalive = list(args) + list(kwargs.values()) # Order arguments args = flatargs(self.py_func, args, kwargs) # Translate cfunc, restype = self.translate([typeof(x) for x in args.flat]) # Construct flypy values argtypes = [typeof(x) for x in args] arg_objs = list(starmap(fromobject, zip(args, argtypes))) # Map flypy values to a ctypes representation args = [] for arg, argtype in zip(arg_objs, argtypes): c_arg = toctypes(arg, argtype, keepalive) if byref(argtype) and stack_allocate(argtype): c_arg = ctypes.pointer(c_arg) args.append(c_arg) # We need this cast since the ctypes function constructed from LLVM # IR has different structs (which are structurally equivalent) c_restype = ctype(restype) if byref(restype): c_result = c_restype() # dummy result value args.append(ctypes.pointer(c_result)) c_restype = None # void c_signature = ctypes.PYFUNCTYPE(c_restype, *[type(arg) for arg in args]) cfunc = ctypes.cast(cfunc, c_signature) # Handle calling convention if byref(restype): cfunc(*args) else: c_result = cfunc(*args) # Map ctypes result back to a python value result = fromctypes(c_result, restype) result_obj = toobject(result, restype) return result_obj
def topy(tup): return toobject(tup, typeof(tup))
def toobject(value, type): head, tail = type.parameters hd = toobject(value.hd, head) if isinstance(value.tl, EmptyTuple): return (hd, ) return (hd, ) + toobject(value.tl, tail)
def toobject(obj, type): [base_type] = type.parameters return [toobject(obj[i], base_type) for i in xrange(len(obj))]
def toobject(value, type): head, tail = type.parameters hd = toobject(value.hd, head) if isinstance(value.tl, EmptyTuple): return (hd,) return (hd,) + toobject(value.tl, tail)