示例#1
0
文件: typemapper.py 项目: dwf/numba
    def from_python(self, value):
        from numba.type_inference import module_type_inference

        if isinstance(value, np.ndarray):
            dtype = map_dtype(value.dtype)
            return minitypes.ArrayType(dtype, value.ndim) #,
                                       #is_c_contig=value.flags['C_CONTIGUOUS'],
                                       #is_f_contig=value.flags['F_CONTIGUOUS'])
        elif isinstance(value, np.dtype):
            return numba.typesystem.from_numpy_dtype(value)
        elif is_dtype_constructor(value):
            return numba.typesystem.from_numpy_dtype(np.dtype(value))
        elif isinstance(value, (tuple, list, dict)):
            return infer_container_type(self, value)
        elif isinstance(value, types.ModuleType):
            return ModuleType(value)
        # elif isinstance(value, (self.ctypes_func_type, self.ctypes_func_type2)):
        elif is_ctypes(value):
            result = from_ctypes_value(value)
            if result.is_function:
                pointer = ctypes.cast(value, ctypes.c_void_p).value
                return PointerFunctionType(value, pointer, result)
            else:
                return result
        elif cffi_support.is_cffi_func(value):
            signature = cffi_support.get_signature(value)
            pointer = cffi_support.get_pointer(value)
            return PointerFunctionType(value, pointer, signature)
        elif isinstance(value, minitypes.Type):
            return CastType(dst_type=value)
        elif hasattr(type(value), '__numba_ext_type'):
            return getattr(type(value), '__numba_ext_type')
        elif value is numba.NULL:
            return null_type
        elif numbawrapper.is_numba_wrapper(value):
            return JitType(value)
        elif isinstance(value, numbawrapper.NumbaSpecializingWrapper):
            return AutojitType(value)
        elif hasattr(value, 'from_address') and hasattr(value, 'in_dll'):
            # Try to detect ctypes pointers, or default to minivect
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                pass
                #type = convert_from_ctypes(value)
                #value = ctypes.cast(value, ctypes.c_void_p).value
                #return CTypesPointerType(type, valuee

        result_type = super(NumbaTypeMapper, self).from_python(value)

        if result_type == object_ and module_type_inference.is_registered(value):
            result = module_type_inference.module_attribute_type(value)
            if result is not None:
                result_type = result
            else:
                result_type = KnownValueType(value)

        return result_type
示例#2
0
    def from_python(self, value):
        if isinstance(value, np.ndarray):
            dtype = map_dtype(value.dtype)
            return minitypes.ArrayType(dtype, value.ndim,
                                       is_c_contig=value.flags['C_CONTIGUOUS'],
                                       is_f_contig=value.flags['F_CONTIGUOUS'])
        elif isinstance(value, tuple):
            return tuple_
        elif isinstance(value, types.ModuleType):
            return ModuleType(value)
        # elif isinstance(value, (self.ctypes_func_type, self.ctypes_func_type2)):
        elif hasattr(value, 'errcheck'):
            # ugh, ctypes
            if value.argtypes is None:
                return object_

            restype = convert_from_ctypes(value.restype)
            argtypes = map(convert_from_ctypes, value.argtypes)
            return CTypesFunctionType(value, restype, argtypes)
        elif isinstance(value, minitypes.Type):
            return CastType(dst_type=value)
        elif hasattr(type(value), '__numba_ext_type'):
            return type(value).__numba_ext_type
        else:
            return super(NumbaTypeMapper, self).from_python(value)
示例#3
0
def numba_type_from_sig(ufunc_signature):
    """
    Convert ufunc type signature string (e.g. 'dd->d') to a FunctionType
    """
    args, ret = ufunc_signature.split('->')
    to_numba = lambda c: minitypes.map_dtype(np.dtype(c))

    signature = to_numba(ret)(*map(to_numba, args))
    return signature
示例#4
0
文件: numpyufuncs.py 项目: dwf/numba
def numba_type_from_sig(ufunc_signature):
    """
    Convert ufunc type signature string (e.g. 'dd->d') to a FunctionType
    """
    args, ret = ufunc_signature.split('->')
    to_numba = lambda c: minitypes.map_dtype(np.dtype(c))

    signature = to_numba(ret)(*map(to_numba, args))
    return signature
示例#5
0
    def from_python(self, value):
        from numba.type_inference import module_type_inference

        if isinstance(value, np.ndarray):
            dtype = map_dtype(value.dtype)
            return minitypes.ArrayType(dtype, value.ndim) #,
                                       #is_c_contig=value.flags['C_CONTIGUOUS'],
                                       #is_f_contig=value.flags['F_CONTIGUOUS'])
        elif isinstance(value, np.dtype):
            return numba.typesystem.from_numpy_dtype(value)
        elif is_dtype_constructor(value):
            return numba.typesystem.from_numpy_dtype(np.dtype(value))
        elif isinstance(value, tuple):
            return tuple_
        elif isinstance(value, types.ModuleType):
            return ModuleType(value)
        # elif isinstance(value, (self.ctypes_func_type, self.ctypes_func_type2)):
        elif hasattr(value, 'errcheck'):
            # ugh, ctypes
            if value.argtypes is None:
                return object_

            restype = convert_from_ctypes(value.restype)
            argtypes = map(convert_from_ctypes, value.argtypes)
            return CTypesFunctionType(value, restype, argtypes)
        elif isinstance(value, minitypes.Type):
            return CastType(dst_type=value)
        elif hasattr(type(value), '__numba_ext_type'):
            return getattr(type(value), '__numba_ext_type')
        elif value is numba.NULL:
            return null_type
        elif isinstance(value, numbawrapper.NumbaCompiledWrapper):
            return JitType(value)
        elif isinstance(value, numbawrapper.NumbaSpecializingWrapper):
            return AutojitType(value)
        elif hasattr(value, 'from_address') and hasattr(value, 'in_dll'):
            # Try to detect ctypes pointers, or default to minivect
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                pass
                #type = convert_from_ctypes(value)
                #value = ctypes.cast(value, ctypes.c_void_p).value
                #return CTypesPointerType(type, valuee

        result_type = super(NumbaTypeMapper, self).from_python(value)

        if result_type == object_ and module_type_inference.is_registered(value):
            result = module_type_inference.module_attribute_type(value)
            if result is not None:
                result_type = result
            else:
                result_type = KnownValueType(value)

        return result_type
示例#6
0
    def from_python(self, value):
        if isinstance(value, np.ndarray):
            dtype = map_dtype(value.dtype)
            return minitypes.ArrayType(dtype, value.ndim,
                                       is_c_contig=value.flags['C_CONTIGUOUS'],
                                       is_f_contig=value.flags['F_CONTIGUOUS'])
        elif isinstance(value, tuple):
            return tuple_
        elif isinstance(value, types.ModuleType):
            return ModuleType(value)
        # elif isinstance(value, (self.ctypes_func_type, self.ctypes_func_type2)):
        elif hasattr(value, 'errcheck'):
            # ugh, ctypes
            if value.argtypes is None:
                return object_

            restype = convert_from_ctypes(value.restype)
            argtypes = map(convert_from_ctypes, value.argtypes)
            return CTypesFunctionType(value, restype, argtypes)
        elif isinstance(value, minitypes.Type):
            return CastType(dst_type=value)
        elif hasattr(type(value), '__numba_ext_type'):
            return type(value).__numba_ext_type
        elif value is numba.NULL:
            return null_type
        elif isinstance(value, numba.decorators.NumbaFunction):
            if value.signature is None:
                # autojit
                return AutojitType(value)
            else:
                return JitType(value)
        elif hasattr(value, 'from_address') and hasattr(value, 'in_dll'):
            # Try to detect ctypes pointers, or default to minivect
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                pass
                #type = convert_from_ctypes(value)
                #value = ctypes.cast(value, ctypes.c_void_p).value
                #return CTypesPointerType(type, value)

        return super(NumbaTypeMapper, self).from_python(value)
示例#7
0
def to_minitype(ds):
    # To minitype through NumPy. Discards dimension information.
    return minitypes.map_dtype(to_numpy(extract_measure(ds)))
示例#8
0
 def resolve(self):
     return map_dtype(self.dtype)
示例#9
0
def from_numpy_dtype(np_dtype):
    """
    :param np_dtype: the NumPy dtype (e.g. np.dtype(np.double))
    :return: a dtype type representation
    """
    return dtype(minitypes.map_dtype(np_dtype))
示例#10
0
def minitype(dtype):
    return minitypes.map_dtype(dtype)
示例#11
0
 def resolve(self):
     return map_dtype(self.dtype)
示例#12
0
def to_minitype(ds):
    # To minitype through NumPy. Discards dimension information.
    return minitypes.map_dtype(to_numpy(extract_measure(ds)))
示例#13
0
def from_numpy_dtype(np_dtype):
    """
    :param np_dtype: the NumPy dtype (e.g. np.dtype(np.double))
    :return: a dtype type representation
    """
    return dtype(minitypes.map_dtype(np_dtype))
示例#14
0
    def from_python(self, value):
        from numba.type_inference import module_type_inference

        if isinstance(value, np.ndarray):
            dtype = map_dtype(value.dtype)
            return minitypes.ArrayType(dtype, value.ndim)  #,
            #is_c_contig=value.flags['C_CONTIGUOUS'],
            #is_f_contig=value.flags['F_CONTIGUOUS'])
        elif isinstance(value, np.dtype):
            return numba.typesystem.from_numpy_dtype(value)
        elif is_dtype_constructor(value):
            return numba.typesystem.from_numpy_dtype(np.dtype(value))
        elif isinstance(value, (tuple, list, dict)):
            return infer_container_type(self, value)
        elif isinstance(value, types.ModuleType):
            return ModuleType(value)
        # elif isinstance(value, (self.ctypes_func_type, self.ctypes_func_type2)):
        elif is_ctypes(value):
            result = from_ctypes_value(value)
            if result.is_function:
                pointer = ctypes.cast(value, ctypes.c_void_p).value
                return PointerFunctionType(value, pointer, result)
            else:
                return result
        elif cffi_support.is_cffi_func(value):
            signature = cffi_support.get_signature(value)
            pointer = cffi_support.get_pointer(value)
            return PointerFunctionType(value, pointer, signature)
        elif isinstance(value, minitypes.Type):
            return CastType(dst_type=value)
        elif hasattr(type(value), '__numba_ext_type'):
            return getattr(type(value), '__numba_ext_type')
        elif value is numba.NULL:
            return null_type
        elif numbawrapper.is_numba_wrapper(value):
            return JitType(value)
        elif isinstance(value, numbawrapper.NumbaSpecializingWrapper):
            return AutojitType(value)
        elif hasattr(value, 'from_address') and hasattr(value, 'in_dll'):
            # Try to detect ctypes pointers, or default to minivect
            try:
                ctypes.cast(value, ctypes.c_void_p)
            except ctypes.ArgumentError:
                pass
            else:
                pass
                #type = convert_from_ctypes(value)
                #value = ctypes.cast(value, ctypes.c_void_p).value
                #return CTypesPointerType(type, valuee

        result_type = super(NumbaTypeMapper, self).from_python(value)

        if result_type == object_ and module_type_inference.is_registered(
                value):
            result = module_type_inference.module_attribute_type(value)
            if result is not None:
                result_type = result
            else:
                result_type = KnownValueType(value)

        return result_type
示例#15
0
def minitype(dtype):
    return minitypes.map_dtype(dtype)