Beispiel #1
0
def test_raising_from_backend(nullary_mm):
    def raise_(foo):
        raise foo

    Foo = ua.BackendNotImplementedError("Foo")
    be = Backend()
    be.__ua_function__ = lambda f, a, kw: raise_(Foo)

    # BackendNotImplementedErrors are nested
    with ua.set_backend(be):
        with pytest.raises(ua.BackendNotImplementedError) as e:
            nullary_mm()

        assert (
            e.value.args[0] ==
            "No selected backends had an implementation for this function.")
        assert type(e.value.args[1]) == tuple
        assert e.value.args[1] == (be, Foo)

    Bar = ua.BackendNotImplementedError("Bar")
    be2 = Backend()
    be2.__ua_function__ = lambda f, a, kw: raise_(Bar)
    # Errors are in the order the backends were tried
    with ua.set_backend(be), ua.set_backend(be2):
        with pytest.raises(ua.BackendNotImplementedError) as e:
            nullary_mm()

        assert e.value.args[1] == (be2, Bar)
        assert e.value.args[2] == (be, Foo)

    be3 = Backend()
    be3.__ua_function__ = lambda f, a, kw: "Success"
    # Can succeed after a backend has raised BackendNotImplementedError
    with ua.set_backend(be3), ua.set_backend(be):
        assert nullary_mm() == "Success"
Beispiel #2
0
    def convert(x, coerce):
        if isinstance(x, xnd.array):
            return x

        try:
            return xnd.array.from_buffer(memoryview(x))
        except TypeError:
            pass

        if coerce:
            return xnd.array(x)

        if isinstance(x, (int, float, bool)):
            return x

        raise ua.BackendNotImplementedError("Unsupported output received.")
Beispiel #3
0
 def default2(*a, **kw):
     num_calls[0] = num_calls[0] + 1
     raise ua.BackendNotImplementedError()
Beispiel #4
0
    def __ua_function__(self, f, a, kw):
        if self.active:
            return self.ret

        raise ua.BackendNotImplementedError(self.__ua_domain__)