コード例 #1
0
ファイル: dummy.py プロジェクト: pombreda/flypy
    def ctype(cls, ty):
        restype = ctype(ty.restype)
        argtypes = [ctype(argtype) for argtype in ty.argtypes]

        if byref(ty.restype):
            argtypes.append(ctypes.POINTER(restype))
            restype = None  # void

        return ctypes.POINTER(ctypes.PYFUNCTYPE(restype, *argtypes))
コード例 #2
0
ファイル: dummy.py プロジェクト: filmackay/flypy
    def ctype(cls, ty):
        restype = ctype(ty.restype)
        argtypes = [ctype(argtype) for argtype in ty.argtypes]

        if byref(ty.restype):
            argtypes.append(ctypes.POINTER(restype))
            restype = None # void

        return ctypes.POINTER(ctypes.PYFUNCTYPE(restype, *argtypes))
コード例 #3
0
ファイル: layout.py プロジェクト: pombreda/flypy
def representation_type(ty):
    """
    Get the low-level representation type for a high-level (user-defined) type.

    Returns
    =======
    The pykit type for the object layout.
    """

    from flypy.lib import vectorobject
    from flypy.lib import arrayobject
    from flypy.runtime.obj import pointerobject

    # NOTE: special cases should be kept to an absolute minimum here. They
    #       should probably be introduced only if ctypes cannot represent the
    #       type
    if ty.impl == vectorobject.Vector:
        base, count = ty.parameters
        return ptypes.Vector(representation_type(base), count)
    elif ty.impl == pointerobject.Pointer:
        # type pointed to may not be supported by ctypes
        (base, ) = ty.parameters
        if base.impl == vectorobject.Vector:
            return ptypes.Pointer(representation_type(base))

    cty = conversion.ctype(ty)
    result_type = ctypes_support.from_ctypes_type(cty)

    # struct uses pointer
    if result_type.is_struct:
        result_type = ptypes.Pointer(result_type)

    return result_type
コード例 #4
0
ファイル: layout.py プロジェクト: filmackay/flypy
def representation_type(ty):
    """
    Get the low-level representation type for a high-level (user-defined) type.

    Returns
    =======
    The pykit type for the object layout.
    """

    from flypy.lib import vectorobject
    from flypy.lib import arrayobject
    from flypy.runtime.obj import pointerobject

    # NOTE: special cases should be kept to an absolute minimum here. They
    #       should probably be introduced only if ctypes cannot represent the
    #       type
    if ty.impl == vectorobject.Vector:
        base, count = ty.parameters
        return ptypes.Vector(representation_type(base), count)
    elif ty.impl == pointerobject.Pointer:
        # type pointed to may not be supported by ctypes
        (base,) = ty.parameters
        if base.impl == vectorobject.Vector:
            return ptypes.Pointer(representation_type(base))

    cty = conversion.ctype(ty)
    result_type = ctypes_support.from_ctypes_type(cty)

    # struct uses pointer
    if result_type.is_struct:
        result_type = ptypes.Pointer(result_type)

    return result_type
コード例 #5
0
ファイル: functionwrapper.py プロジェクト: pombreda/flypy
    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
コード例 #6
0
def make_ctypes_ptr(ptr, type):
    from flypy.support.cffi_support import is_cffi, ffi
    from flypy.support.ctypes_support import is_ctypes_pointer_type

    cty = ctype(type)

    if is_cffi(ptr):
        addr = ffi.cast('uintptr_t', ptr)
        ctypes_ptr = ctypes.c_void_p(int(addr))
        ptr = ctypes.cast(ctypes_ptr, cty)
    else:
        ptr = ctypes.cast(ptr, cty)

    return ptr
コード例 #7
0
ファイル: pointerobject.py プロジェクト: flypy/flypy
def make_ctypes_ptr(ptr, type):
    from flypy.support.cffi_support import is_cffi, ffi
    from flypy.support.ctypes_support import is_ctypes_pointer_type

    cty = ctype(type)

    if is_cffi(ptr):
        addr = ffi.cast('uintptr_t', ptr)
        ctypes_ptr = ctypes.c_void_p(int(addr))
        ptr = ctypes.cast(ctypes_ptr, cty)
    else:
        ptr = ctypes.cast(ptr, cty)

    return ptr
コード例 #8
0
ファイル: functionwrapper.py プロジェクト: pausz/flypy
    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
コード例 #9
0
 def ctype(cls, ty):
     [base] = ty.parameters
     return ctypes.POINTER(ctype(base))
コード例 #10
0
 def fromctypes(cls, val, ty):
     if isinstance(val, (int, long)):
         cty = ctype(ty)
         return cty(val)
     return val
コード例 #11
0
ファイル: dummy.py プロジェクト: filmackay/flypy
 def ctype(cls, ty):
     restype = ctype(ty.parameters[-1])
     argtypes = [ctype(argtype) for argtype in ty.parameters[:-1]]
     #return ctypes.CFUNCTYPE(restype, *argtypes)
     return ctypes.POINTER(ctypes.CFUNCTYPE(restype, *argtypes))
コード例 #12
0
ファイル: arrayobject.py プロジェクト: pombreda/flypy
 def fromctypes(cls, val, ty):
     if isinstance(val, ctypes.Array):
         cty = ctype(ty)
         return cty(*val)
     return val
コード例 #13
0
 def ctype(cls, ty):
     cty = conversion.ctype(ty.parameters[0])
     # Get the base type if a pointer
     if hasattr(cty, '_type_'):
         return cty._type_
     return cty
コード例 #14
0
ファイル: pointerobject.py プロジェクト: flypy/flypy
 def fromctypes(cls, val, ty):
     if isinstance(val, (int, long)):
         cty = ctype(ty)
         return cty(val)
     return val
コード例 #15
0
ファイル: arrayobject.py プロジェクト: pombreda/flypy
def make_ctypes_array(arr, type):
    cty = ctype(type)
    arr = ctypes.cast(arr, cty)
    return arr
コード例 #16
0
ファイル: vectorobject.py プロジェクト: filmackay/flypy
 def ctype(cls, ty):
     base, count = ty.parameters
     return ctype(base) * count
コード例 #17
0
ファイル: dummy.py プロジェクト: pombreda/flypy
 def ctype(cls, ty):
     restype = ctype(ty.parameters[-1])
     argtypes = [ctype(argtype) for argtype in ty.parameters[:-1]]
     #return ctypes.CFUNCTYPE(restype, *argtypes)
     return ctypes.POINTER(ctypes.CFUNCTYPE(restype, *argtypes))
コード例 #18
0
ファイル: arrayobject.py プロジェクト: pombreda/flypy
def newarray(basetype, size):
    arr = (ctype(basetype) * size)()
    return Array(arr)
コード例 #19
0
 def ctype(cls, ty):
     base, count = ty.parameters
     return ctype(base) * count
コード例 #20
0
ファイル: objects.py プロジェクト: filmackay/flypy
 def ctype(cls, ty):
     cty = conversion.ctype(ty.parameters[0])
     # Get the base type if a pointer
     if hasattr(cty, '_type_'):
         return cty._type_
     return cty
コード例 #21
0
ファイル: pointerobject.py プロジェクト: flypy/flypy
 def ctype(cls, ty):
     [base] = ty.parameters
     return ctypes.POINTER(ctype(base))