def from_ctypes_type(cty, ctypes_value=None): """ Convert a ctypes type to a flypy type. Supported are structs, unit types (int/float) """ if hashable(cty) and cty in ctypes_map: return ctypes_map[cty] elif cty is ctypes.c_void_p or cty is ctypes.py_object: return coretypes.Pointer[coretypes.void] elif is_ctypes_array_type(cty): return arrayobject.Array[from_ctypes_type(cty._type_), cty._length_] elif is_ctypes_pointer_type(cty): return coretypes.Pointer[from_ctypes_type(cty._type_)] elif is_ctypes_struct_type(cty): fields = [(name, from_ctypes_type(field_type)) for name, field_type in cty._fields_] fields = fields or [('dummy', coretypes.int8)] return coretypes.struct_(fields) elif is_ctypes_function_type(cty): # from_ctypes_type(cty._restype_) # <- this value is arbitrary, # it's always a c_int restype = from_ctypes_type(ctypes_value.restype) argtypes = tuple(from_ctypes_type(argty) for argty in ctypes_value.argtypes) return coretypes.ForeignFunction[argtypes + (restype,)] else: raise NotImplementedError(cty)
def from_ctypes_type(cty, ctypes_value=None): """ Convert a ctypes type to a flypy type. Supported are structs, unit types (int/float) """ if hashable(cty) and cty in ctypes_map: return ctypes_map[cty] elif cty is ctypes.c_void_p or cty is ctypes.py_object: return coretypes.Pointer[coretypes.void] elif is_ctypes_array_type(cty): return arrayobject.Array[from_ctypes_type(cty._type_), cty._length_] elif is_ctypes_pointer_type(cty): return coretypes.Pointer[from_ctypes_type(cty._type_)] elif is_ctypes_struct_type(cty): fields = [(name, from_ctypes_type(field_type)) for name, field_type in cty._fields_] fields = fields or [('dummy', coretypes.int8)] return coretypes.struct_(fields) elif is_ctypes_function_type(cty): # from_ctypes_type(cty._restype_) # <- this value is arbitrary, # it's always a c_int restype = from_ctypes_type(ctypes_value.restype) argtypes = tuple( from_ctypes_type(argty) for argty in ctypes_value.argtypes) return coretypes.ForeignFunction[argtypes + (restype, )] else: raise NotImplementedError(cty)
def __getattr__(self, attr): result = getattr(self.val, attr) cty = type(result) if is_ctypes_pointer_type(cty) or is_ctypes_struct_type(cty): result = CTypesStruct(result) return result
def __init__(self, ctypes_val): if is_ctypes_pointer_type(type(ctypes_val)): ctypes_val = ctypes_val[0] self.val = ctypes_val