Exemple #1
0
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)')
Exemple #2
0
    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
Exemple #3
0
    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)