def test(s, caller=False): with heavydb.targets['cpu']: with Type.alias(**heavydb.typesystem_aliases): typ = Type.fromobject(s) if caller: typ = heavydb.caller_signature(typ) return heavydb.format_type(typ)
def generic(self, args, kws): t = Type.fromobject(self.obj._signature).tonumba() name = self.key # lowering def codegen(context, builder, sig, args): fndesc = funcdesc.ExternalFunctionDescriptor( name, sig.return_type, sig.args) func = context.declare_external_function( builder.module, fndesc) return builder.call(func, args) extending.lower_builtin(name, *t.args)(codegen) return t
def test_fromobject(target_info): import ctypes assert Type.fromobject('i8') == Type.fromstring('i8') assert Type.fromobject(int) == Type.fromstring('i64') assert Type.fromobject(ctypes.c_int16) == Type.fromstring('i16') if nb is not None: assert Type.fromobject(nb.int16) == Type.fromstring('i16') if np is not None: assert Type.fromobject(np.int32) == Type.fromstring('i32') assert Type.fromobject(np.complex64) == Type.fromstring('complex64') def foo(): pass assert Type.fromobject(foo) == Type.fromstring('void(void)')
def test_dummy(): with pytest.raises( RuntimeError, match=r'Target not specified.'): Type.fromobject('int') with TargetInfo.dummy(): t = Type.fromobject('int') assert str(t) == 'int' t = Type.fromobject('int(int)') assert t.is_function t = Type.fromobject('foo(bar)') assert t.is_function with pytest.raises( RuntimeError, match=r'Target not specified.'): Type.fromobject('foo(bar)')
def external(cls, *args): """ Parameters ---------- signature : object (str, ctypes function, python callable, numba function) Any object convertible to a Numba function via Type.fromobject(...).tonumba() """ ts = defaultdict(list) key = None for signature in args: with TargetInfo.dummy(): t = Type.fromobject(signature) if not t.is_function: raise ValueError("signature must represent a function type") if not t.name: raise ValueError( f"external function name not specified for signature {signature}" ) if key is None: key = t.name if not key: raise ValueError( f"external function name not specified for signature {signature}" ) for device in [ a for a in t.annotation() or [] if a in ["CPU", "GPU"] ] or [ "CPU", "GPU", ]: ts[device].append(signature) obj = cls(key, ts) obj.register() return obj
def fromobject(cls, signature, name: str = None): """ Parameters ---------- signature : object (str, ctypes function, python callable, numba function) Any object convertible to a Numba function via Type.fromobject(...).tonumba() name : str The name of the external function """ # Make inner function for the actual work target_info = TargetInfo.dummy() with target_info: t = Type.fromobject(signature) if not t.is_function: raise ValueError("signature must represent a function type") if name is None: name = t.name if not name: raise ValueError( f"external function name not specified for signature {signature}" ) return cls(name, t)
def Type_fromobject(s): return Type.fromobject(s, target_info)