Exemple #1
0
    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
Exemple #2
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and 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)

        else:
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        if numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return

            if ary.flags.c_contiguous:
                layout = 'C'
            elif ary.flags.f_contiguous:
                layout = 'F'
            else:
                layout = 'A'
            return types.Array(dtype, ary.ndim, layout)

        return
Exemple #3
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and 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)

        else:
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        if numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return

            if ary.flags.c_contiguous:
                layout = 'C'
            elif ary.flags.f_contiguous:
                layout = 'F'
            else:
                layout = 'A'
            return types.Array(dtype, ary.ndim, layout)

        return
Exemple #4
0
 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)
Exemple #5
0
 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)
Exemple #6
0
def _typeof_numpy_scalar(val, c):
    try:
        return numpy_support.map_arrayscalar_type(val)
    except NotImplementedError:
        pass
Exemple #7
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and 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 ctypes_utils.is_ctypes_funcptr(val):
            return ctypes_utils.make_function_type(val)

        elif cffi_utils.SUPPORTED and cffi_utils.is_cffi_func(val):
            return cffi_utils.make_function_type(val)

        elif numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return
            layout = numpy_support.map_layout(ary)
            readonly = not ary.flags.writeable
            return types.Array(dtype, ary.ndim, layout, readonly=readonly)

        elif sys.version_info >= (2, 7) and not isinstance(val, numpy.generic):
            try:
                m = memoryview(val)
            except TypeError:
                pass
            else:
                # Object has the buffer protocol
                try:
                    dtype = bufproto.decode_pep3118_format(m.format, m.itemsize)
                except ValueError:
                    pass
                else:
                    type_class = bufproto.get_type_class(type(val))
                    layout = bufproto.infer_layout(m)
                    return type_class(dtype, m.ndim, layout=layout,
                                      readonly=m.readonly)

        if isinstance(val, numpy.dtype):
            tp = numpy_support.from_dtype(val)
            return types.DType(tp)

        else:
            # Matching here is quite broad, so we have to do it after
            # the more specific matches above.
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        return
Exemple #8
0
def _typeof_numpy_scalar(val, c):
    try:
        return numpy_support.map_arrayscalar_type(val)
    except NotImplementedError:
        pass
Exemple #9
0
    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)
Exemple #10
0
    def resolve_data_type(self, val):
        """
        Return the numba type of a Python value representing data
        (e.g. a number or an array, but not more sophisticated types
         such as functions, etc.)

        This function can return None to if it cannot decide.
        """
        if val is True or val is False:
            return types.boolean

        # Under 2.x, we must guard against numpy scalars (np.intXY
        # subclasses Python int but get_number_type() wouldn't infer the
        # right bit width -- perhaps it should?).
        elif (not isinstance(val, numpy.number)
              and 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 ctypes_utils.is_ctypes_funcptr(val):
            return ctypes_utils.make_function_type(val)

        elif cffi_utils.SUPPORTED and cffi_utils.is_cffi_func(val):
            return cffi_utils.make_function_type(val)

        elif numpy_support.is_array(val):
            ary = val
            try:
                dtype = numpy_support.from_dtype(ary.dtype)
            except NotImplementedError:
                return
            layout = numpy_support.map_layout(ary)
            readonly = not ary.flags.writeable
            return types.Array(dtype, ary.ndim, layout, readonly=readonly)

        elif sys.version_info >= (2, 7) and not isinstance(val, numpy.generic):
            try:
                m = memoryview(val)
            except TypeError:
                pass
            else:
                # Object has the buffer protocol
                try:
                    dtype = bufproto.decode_pep3118_format(
                        m.format, m.itemsize)
                except ValueError:
                    pass
                else:
                    type_class = bufproto.get_type_class(type(val))
                    layout = bufproto.infer_layout(m)
                    return type_class(dtype,
                                      m.ndim,
                                      layout=layout,
                                      readonly=m.readonly)

        if isinstance(val, numpy.dtype):
            tp = numpy_support.from_dtype(val)
            return types.DType(tp)

        else:
            # Matching here is quite broad, so we have to do it after
            # the more specific matches above.
            try:
                return numpy_support.map_arrayscalar_type(val)
            except NotImplementedError:
                pass

        return