Esempio n. 1
0
def from_ctypes_value(value):
    """
    Convert a ctypes value to a numba type
    """
    if is_ctypes_type(value):
        # Value is a ctypes type, e.g. c_int
        return ts.meta(from_ctypes_type(value))

    elif is_ctypes_function(value):
        # TODO: move this to from_ctypes_type
        if value.argtypes is None:
            warnings.warn(
                "ctypes function %s has no argument types set" % (value,))
            return ts.object_

        restype = from_ctypes_type(value.restype)
        argtypes = [from_ctypes_type(at) for at in value.argtypes]
        signature = ts.function(return_type=restype, args=argtypes)
        return signature

    elif is_ctypes_type(type(value)) or hasattr(value, '_type_'):
        # Value is a ctypes value, e.g. c_int(10)
        result_type = from_ctypes_type(type(value))

        if result_type.is_pointer:
            # Handle ctypes pointers
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                addr_int = ctypes.cast(value, ctypes.c_void_p).value
                result_type = ts.known_pointer(result_type.base_type, addr_int)

        return result_type

    else:
        raise NotImplementedError(value)
Esempio n. 2
0
def from_ctypes_value(value):
    """
    Convert a ctypes value to a numba type
    """
    if is_ctypes_type(value):
        # Value is a ctypes type, e.g. c_int
        return ts.meta(from_ctypes_type(value))

    elif is_ctypes_function(value):
        # TODO: move this to from_ctypes_type
        if value.argtypes is None:
            warnings.warn(
                "ctypes function %s has no argument types set" % (value,))
            return ts.object_

        restype = from_ctypes_type(value.restype)
        argtypes = [from_ctypes_type(at) for at in value.argtypes]
        signature = ts.function(return_type=restype, args=argtypes)
        return signature

    elif is_ctypes_type(type(value)) or hasattr(value, '_type_'):
        # Value is a ctypes value, e.g. c_int(10)
        result_type = from_ctypes_type(type(value))

        if result_type.is_pointer:
            # Handle ctypes pointers
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                addr_int = ctypes.cast(value, ctypes.c_void_p).value
                result_type = ts.known_pointer(result_type.base_type, addr_int)

        return result_type

    else:
        raise NotImplementedError(value)
Esempio n. 3
0
def extmethod_to_function(ty):
    return numbatypes.function(ty.return_type, ty.args, ty.name)