def resolve_indices(self, ty, args, kws): assert not kws if len(args) != 1: raise errors.NumbaTypeError( "indices() takes exactly one argument (%d given)" % len(args)) typ, = args if not isinstance(typ, types.Integer): raise errors.NumbaTypeError( "'%s' object cannot be interpreted as an integer" % typ) return signature(types.UniTuple(types.intp, 3), types.intp)
def generic(self, args, kws): assert not kws [arg] = args if arg not in types.number_domain: raise errors.NumbaTypeError("float() only support for numbers") if arg in types.complex_domain: raise errors.NumbaTypeError("float() does not support complex") if arg in types.integer_domain: return signature(types.float64, arg) elif arg in types.real_domain: return signature(arg, arg)
def numpy_dtype(desc): """Provide an implementation so that numpy.dtype function can be lowered. """ if isinstance(desc, (types.Literal, types.functions.NumberClass)): def imp(desc): return _make_dtype_object(desc) return imp else: raise errors.NumbaTypeError('unknown dtype descriptor: {}'.format(desc))
def leading_zeros(typeingctx, src): """Counts leading zeros in the binary representation of an integer.""" if not isinstance(src, types.Integer): msg = ("leading_zeros is only defined for integers, but value passed " f"was '{src}'.") raise errors.NumbaTypeError(msg) def codegen(context, builder, signature, args): [src] = args return builder.ctlz(src, ir.Constant(ir.IntType(1), 0)) return src(src), codegen
def generic(self, args, kws): assert not kws it = args[0] if len(args) > 1 and not isinstance(args[1], types.Integer): raise errors.NumbaTypeError("Only integers supported as start " "value in enumerate") elif len(args) > 2: #let python raise its own error enumerate(*args) if isinstance(it, types.IterableType): enumerate_type = types.EnumerateType(it) return signature(enumerate_type, *args)
def legalize_return_type(return_type, interp, targetctx): """ Only accept array return type iff it is passed into the function. Reject function object return types if in nopython mode. """ if (not targetctx.enable_nrt and isinstance(return_type, types.Array)): # Walk IR to discover all arguments and all return statements retstmts = [] caststmts = {} argvars = set() for bid, blk in interp.blocks.items(): for inst in blk.body: if isinstance(inst, ir.Return): retstmts.append(inst.value.name) elif isinstance(inst, ir.Assign): if (isinstance(inst.value, ir.Expr) and inst.value.op == 'cast'): caststmts[inst.target.name] = inst.value elif isinstance(inst.value, ir.Arg): argvars.add(inst.target.name) assert retstmts, "No return statements?" for var in retstmts: cast = caststmts.get(var) if cast is None or cast.value.name not in argvars: if self._raise_errors: msg = ( "Only accept returning of array passed into " "the function as argument") raise errors.NumbaTypeError(msg) elif (isinstance(return_type, types.Function) or isinstance(return_type, types.Phantom)): if self._raise_errors: msg = "Can't return function object ({}) in nopython mode" raise errors.NumbaTypeError(msg.format(return_type))
def generic(self, args, kws): assert not kws if len(args) == 1: [arg] = args if arg not in types.number_domain: raise errors.NumbaTypeError( "complex() only support for numbers") if arg == types.float32: return signature(types.complex64, arg) else: return signature(types.complex128, arg) elif len(args) == 2: [real, imag] = args if (real not in types.number_domain or imag not in types.number_domain): raise errors.NumbaTypeError( "complex() only support for numbers") if real == imag == types.float32: return signature(types.complex64, real, imag) else: return signature(types.complex128, real, imag)
def generic(self, args, kws): # Redirect resolution to __init__ instance_type = self.key.instance_type ctor = instance_type.jit_methods['__init__'] boundargs = (instance_type.get_reference_type(), ) + args disp_type = types.Dispatcher(ctor) sig = disp_type.get_call_type(self.context, boundargs, kws) if not isinstance(sig.return_type, types.NoneType): raise errors.NumbaTypeError( f"__init__() should return None, not '{sig.return_type}'") # Actual constructor returns an instance value (not None) out = templates.signature(instance_type, *sig.args[1:]) return out
def _best_signature(self, candidates): """ Returns the best signature out of the candidates """ ordered, genericity = self._sort_signatures(candidates) # check for ambiguous signatures if len(ordered) > 1: firstscore = genericity[ordered[0]] same = list(takewhile(lambda x: genericity[x] == firstscore, ordered)) if len(same) > 1: msg = ["{n} ambiguous signatures".format(n=len(same))] for sig in same: msg += ["{0} => {1}".format(sig, candidates[sig])] raise errors.NumbaTypeError('\n'.join(msg)) return ordered[0]