def typeof_pyval(cls, val): """ This is called from numba._dispatcher as a fallback if the native code cannot decide the type. """ if isinstance(val, numpy.ndarray): # TODO complete dtype mapping dtype = numpy_support.from_dtype(val.dtype) ndim = val.ndim if ndim == 0: # is array scalar return numpy_support.from_dtype(val.dtype) layout = numpy_support.map_layout(val) aryty = types.Array(dtype, ndim, layout) return aryty # The following are handled in the C version for exact type match # So test these later elif isinstance(val, INT_TYPES): return types.int64 elif isinstance(val, float): return types.float64 elif isinstance(val, complex): return types.complex128 elif numpy_support.is_arrayscalar(val): # Array scalar return numpy_support.from_dtype(numpy.dtype(type(val))) # Other object else: return types.pyobject
def typeof_pyval(val): """ This is called from numba._dispatcher as a fallback if the native code cannot decide the type. """ if isinstance(val, numpy.ndarray): # TODO complete dtype mapping dtype = numpy_support.from_dtype(val.dtype) ndim = val.ndim if ndim == 0: # is array scalar return numpy_support.from_dtype(val.dtype) layout = numpy_support.map_layout(val) aryty = types.Array(dtype, ndim, layout) return aryty # The following are handled in the C version for exact type match # So test these later elif isinstance(val, INT_TYPES): return types.int32 elif isinstance(val, float): return types.float64 elif isinstance(val, complex): return types.complex128 elif numpy_support.is_arrayscalar(val): # Array scalar return numpy_support.from_dtype(numpy.dtype(type(val))) # Other object else: return types.pyobject
def resolve_value_type(self, val): """ Return the numba type of a Python value Return None if fail to type. """ if val is True or val is False: return types.boolean elif isinstance(val, utils.INT_TYPES + (float,)): return self.get_number_type(val) elif val is None: return types.none elif isinstance(val, str): return types.string elif isinstance(val, complex): return types.complex128 elif isinstance(val, tuple): tys = [self.resolve_value_type(v) for v in val] distinct_types = set(tys) if len(distinct_types) == 1: return types.UniTuple(tys[0], len(tys)) else: return types.Tuple(tys) elif numpy_support.is_arrayscalar(val): return numpy_support.map_arrayscalar_type(val) elif numpy_support.is_array(val): ary = val dtype = numpy_support.from_dtype(ary.dtype) # Force C contiguous return types.Array(dtype, ary.ndim, 'C') elif ctypes_utils.is_ctypes_funcptr(val): cfnptr = val return ctypes_utils.make_function_type(cfnptr) elif cffi_utils.SUPPORTED and cffi_utils.is_cffi_func(val): return cffi_utils.make_function_type(val) elif (cffi_utils.SUPPORTED and isinstance(val, cffi_utils.ExternCFunction)): return val elif type(val) is type and issubclass(val, BaseException): return types.exception_type else: try: # Try to look up target specific typing information return self.get_global_type(val) except KeyError: pass return None
def typeof_global(self, inst, target, gvar): if (gvar.name in ('range', 'xrange') and gvar.value in RANGE_ITER_OBJECTS): gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) if gvar.name == 'slice' and gvar.value is slice: gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif gvar.name == 'len' and gvar.value is len: gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif gvar.name in ('True', 'False'): assert gvar.value in (True, False) self.typevars[target.name].lock(types.boolean) self.assumed_immutables.add(inst) elif isinstance(gvar.value, (int, float)): gvty = self.context.get_number_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif numpy_support.is_arrayscalar(gvar.value): gvty = numpy_support.map_arrayscalar_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif numpy_support.is_array(gvar.value): ary = gvar.value dtype = numpy_support.from_dtype(ary.dtype) # force C contiguous gvty = types.Array(dtype, ary.ndim, 'C') self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif ctypes_utils.is_ctypes_funcptr(gvar.value): cfnptr = gvar.value fnty = ctypes_utils.make_function_type(cfnptr) self.typevars[target.name].lock(fnty) self.assumed_immutables.add(inst) elif cffi_support.SUPPORTED and cffi_support.is_cffi_func(gvar.value): fnty = cffi_support.make_function_type(gvar.value) self.typevars[target.name].lock(fnty) self.assumed_immutables.add(inst) else: try: gvty = self.context.get_global_type(gvar.value) except KeyError: raise TypingError("Untyped global name '%s'" % gvar.name, loc=inst.loc) self.assumed_immutables.add(inst) self.typevars[target.name].lock(gvty)
def typeof_global(self, inst, target, gvar): try: # Is it a supported value type? typ = self.resolve_value_type(inst, gvar.value) except TypingError: # Fallback on other cases below pass else: self.typevars[target.name].lock(typ) self.assumed_immutables.add(inst) return if (gvar.name in ('range', 'xrange') and gvar.value in RANGE_ITER_OBJECTS): gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif gvar.name == 'slice' and gvar.value is slice: gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif gvar.name == 'len' and gvar.value is len: gvty = self.context.get_global_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif numpy_support.is_arrayscalar(gvar.value): gvty = numpy_support.map_arrayscalar_type(gvar.value) self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif numpy_support.is_array(gvar.value): ary = gvar.value dtype = numpy_support.from_dtype(ary.dtype) # force C contiguous gvty = types.Array(dtype, ary.ndim, 'C') self.typevars[target.name].lock(gvty) self.assumed_immutables.add(inst) elif ctypes_utils.is_ctypes_funcptr(gvar.value): cfnptr = gvar.value fnty = ctypes_utils.make_function_type(cfnptr) self.typevars[target.name].lock(fnty) self.assumed_immutables.add(inst) elif cffi_support.SUPPORTED and cffi_support.is_cffi_func(gvar.value): fnty = cffi_support.make_function_type(gvar.value) self.typevars[target.name].lock(fnty) self.assumed_immutables.add(inst) elif (cffi_support.SUPPORTED and isinstance(gvar.value, cffi_support.ExternCFunction)): fnty = gvar.value self.typevars[target.name].lock(fnty) self.assumed_immutables.add(inst) elif type(gvar.value) is type and issubclass(gvar.value, BaseException): self.typevars[target.name].lock(types.exception_type) self.assumed_immutables.add(inst) else: try: gvty = self.context.get_global_type(gvar.value) except KeyError: raise TypingError("Untyped global name '%s'" % gvar.name, loc=inst.loc) self.assumed_immutables.add(inst) self.typevars[target.name].lock(gvty)