Esempio n. 1
0
def test_ll_calling_ll2():
    import test_llann
    tst = test_llann.TestLowLevelAnnotateTestCase()
    a, vTs = tst.test_ll_calling_ll2()
    rt = RPythonTyper(a)
    rt.specialize()
    assert [vT.concretetype for vT in vTs] == [Void] * 3
Esempio n. 2
0
def test_ll_calling_ll2():
    import test_llann
    tst = test_llann.TestLowLevelAnnotateTestCase()
    a, vTs = tst.test_ll_calling_ll2()
    rt = RPythonTyper(a)
    rt.specialize()
    assert [vT.concretetype for vT in vTs] == [Void] * 3
Esempio n. 3
0
def test_typeOf_const(x):
    a = RPythonAnnotator()
    bk = a.bookkeeper
    rtyper = RPythonTyper(a)
    s_x = bk.immutablevalue(x)
    r_x = rtyper.getrepr(s_x)
    assert typeOf(r_x.convert_const(x)) == r_x.lowleveltype
Esempio n. 4
0
def test_typeOf_const(x):
    a = RPythonAnnotator()
    bk = a.bookkeeper
    rtyper = RPythonTyper(a)
    s_x = bk.immutablevalue(x)
    r_x = rtyper.getrepr(s_x)
    assert typeOf(r_x.convert_const(x)) == r_x.lowleveltype
Esempio n. 5
0
def test_reprkeys_dont_clash():
    stup1 = annmodel.SomeTuple((annmodel.SomeFloat(),
                                annmodel.SomeInteger()))
    stup2 = annmodel.SomeTuple((annmodel.SomeString(),
                                annmodel.SomeInteger()))
    rtyper = RPythonTyper(annrpython.RPythonAnnotator(None))
    key1 = rtyper.makekey(stup1)
    key2 = rtyper.makekey(stup2)
    assert key1 != key2
Esempio n. 6
0
def ll_rtype(llfn, argtypes=[]):
    a = RPythonAnnotator()
    graph = annotate_lowlevel_helper(a, llfn, argtypes)
    s = a.binding(graph.getreturnvar())
    t = a.translator
    typer = RPythonTyper(a)
    typer.specialize()
    #t.view()
    t.checkgraphs()
    return s, t
Esempio n. 7
0
def ll_rtype(llfn, argtypes=[]):
    a = RPythonAnnotator()
    graph = annotate_lowlevel_helper(a, llfn, argtypes)
    s = a.binding(graph.getreturnvar())
    t = a.translator
    typer = RPythonTyper(a)
    typer.specialize()
    #t.view()
    t.checkgraphs()
    return s, t
Esempio n. 8
0
def test_isinstance():
    class A(object):
        _alloc_flavor_ = "raw"

    class B(A):
        pass

    class C(B):
        pass

    def f(i):
        if i == 0:
            o = None
        elif i == 1:
            o = A()
        elif i == 2:
            o = B()
        else:
            o = C()
        res = 100 * isinstance(o, A) + 10 * isinstance(o, B) + 1 * isinstance(
            o, C)
        if i == 0:
            pass
        elif i == 1:
            assert isinstance(o, A)
            free_non_gc_object(o)
        elif i == 2:
            assert isinstance(o, B)
            free_non_gc_object(o)
        else:
            assert isinstance(o, C)
            free_non_gc_object(o)
        return res

    assert f(1) == 100
    assert f(2) == 110
    assert f(3) == 111
    assert f(0) == 0

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [int])
    assert s.knowntype == int
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    res = interpret(f, [1])
    assert res == 100
    res = interpret(f, [2])
    assert res == 110
    res = interpret(f, [3])
    assert res == 111
    res = interpret(f, [0])
    assert res == 0
Esempio n. 9
0
def test_alloc_flavor():
    class A:
        _alloc_flavor_ = "raw"
    def f():
        return A()
    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [])
    Adef = a.bookkeeper.getuniqueclassdef(A)
    assert s.knowntype == Adef
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    assert (Adef, 'raw') in rtyper.instance_reprs
    assert (Adef, 'gc') not in rtyper.instance_reprs    
Esempio n. 10
0
def test_isinstance():
    class A(object):
        _alloc_flavor_ = "raw"
    class B(A):
        pass
    class C(B):
        pass
    
    def f(i):
        if i == 0:
            o = None
        elif i == 1:
            o = A()
        elif i == 2:
            o = B()
        else:
            o = C()
        res = 100*isinstance(o, A) + 10*isinstance(o, B) + 1*isinstance(o, C)
        if i == 0:
            pass
        elif i == 1:
            assert isinstance(o, A)
            free_non_gc_object(o)
        elif i == 2:
            assert isinstance(o, B)
            free_non_gc_object(o)
        else:
            assert isinstance(o, C)
            free_non_gc_object(o)
        return res

    assert f(1) == 100
    assert f(2) == 110
    assert f(3) == 111
    assert f(0) == 0

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [int])
    assert s.knowntype == int
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    res = interpret(f, [1])
    assert res == 100
    res = interpret(f, [2])
    assert res == 110
    res = interpret(f, [3])
    assert res == 111
    res = interpret(f, [0])
    assert res == 0
Esempio n. 11
0
def test_alloc_flavor():
    class A:
        _alloc_flavor_ = "raw"

    def f():
        return A()

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [])
    Adef = a.bookkeeper.getuniqueclassdef(A)
    assert s.knowntype == Adef
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    assert (Adef, 'raw') in rtyper.instance_reprs
    assert (Adef, 'gc') not in rtyper.instance_reprs
Esempio n. 12
0
def test_is():
    class A:
        _alloc_flavor_ = "raw"
        pass

    class B(A):
        pass

    class C:
        _alloc_flavor_ = "raw"

    def f(i):
        a = A()
        b = B()
        c = C()
        d = None
        e = None
        if i == 0:
            d = a
        elif i == 1:
            d = b
        elif i == 2:
            e = c
        res = (0x0001 * (a is b) | 0x0002 * (a is c) | 0x0004 * (a is d)
               | 0x0008 * (a is e) | 0x0010 * (b is c) | 0x0020 * (b is d)
               | 0x0040 * (b is e) | 0x0080 * (c is d) | 0x0100 * (c is e)
               | 0x0200 * (d is e))
        free_non_gc_object(a)
        free_non_gc_object(b)
        free_non_gc_object(c)
        return res

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [int])
    assert s.knowntype == int
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    res = interpret(f, [0])
    assert res == 0x0004
    res = interpret(f, [1])
    assert res == 0x0020
    res = interpret(f, [2])
    assert res == 0x0100
    res = interpret(f, [3])
    assert res == 0x0200
Esempio n. 13
0
 def buildrtyper(self):
     if self.annotator is None:
         raise ValueError("no annotator")
     if self.rtyper is not None:
         raise ValueError("we already have an rtyper")
     from rpython.rtyper.rtyper import RPythonTyper
     self.rtyper = RPythonTyper(self.annotator)
     return self.rtyper
Esempio n. 14
0
def test_rtype_nongc_object():
    class TestClass(object):
        _alloc_flavor_ = "raw"
        def __init__(self, a):
            self.a = a
        def method1(self):
            return self.a
    def malloc_and_free(a):
        ci = TestClass(a)
        b = ci.method1()
        free_non_gc_object(ci)
        return b
    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(malloc_and_free, [annmodel.SomeAddress()])
    assert isinstance(s, annmodel.SomeAddress)
    rtyper = RPythonTyper(a)
    rtyper.specialize()
Esempio n. 15
0
def test_ll_get_dict_item():
    """
    Tests the low-level implementation of get_dict_item.
    """
    from rpython.annotator.annrpython import RPythonAnnotator
    from rpython.annotator.model import SomeTuple, SomeInteger, SomeString

    from rpython.rtyper.rtyper import RPythonTyper
    from rpython.rtyper.rmodel import inputconst
    from rpython.rtyper.annlowlevel import llstr, hlstr

    from rpython.rtyper.lltypesystem import lltype, rffi
    from rpython.rtyper.lltypesystem import rordereddict, rstr

    dummykeyobj = None
    dummyvalueobj = None

    def _get_str_dict():
        # STR -> lltype.Signed
        DICT = rordereddict.get_ll_dict(lltype.Ptr(rstr.STR), lltype.Signed,
                            ll_fasthash_function=rstr.LLHelpers.ll_strhash,
                            ll_hash_function=rstr.LLHelpers.ll_strhash,
                            ll_eq_function=rstr.LLHelpers.ll_streq,
                            dummykeyobj=dummykeyobj,
                            dummyvalueobj=dummyvalueobj)
        return DICT
    s_tuple = SomeTuple([SomeString(), SomeInteger()])
    DICT = _get_str_dict()

    ll_d = rordereddict.ll_newdict(DICT)
    a = RPythonAnnotator()
    rtyper = RPythonTyper(a)
    a.translator.rtyper = rtyper
    r_tuple = rtyper.getrepr(s_tuple)
    cTUPLE = inputconst(lltype.Void, r_tuple.lowleveltype)
    s_tuple = rtyper.annotation(cTUPLE)
    rtyper.call_all_setups()

    for i in range(20):
        rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
    for i in range(20):
        element = ll_get_dict_item(s_tuple.const, ll_d, i)
        assert (str(i), i) == (hlstr(element.item0), element.item1)
Esempio n. 16
0
def test_alloc_flavor_subclassing():
    class A:
        _alloc_flavor_ = "raw"
    class B(A):
        def __init__(self, a):
            self.a = a
    def f():
        return B(0)
    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [])
    Adef = a.bookkeeper.getuniqueclassdef(A)
    Bdef = a.bookkeeper.getuniqueclassdef(B)
    assert s.knowntype == Bdef
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    assert (Adef, 'raw') in rtyper.instance_reprs
    assert (Adef, 'gc') not in rtyper.instance_reprs
    assert (Bdef, 'raw') in rtyper.instance_reprs
    assert (Bdef, 'gc') not in rtyper.instance_reprs        
Esempio n. 17
0
def test_is():
    class A:
        _alloc_flavor_ = "raw"
        pass
    class B(A): pass
    class C:
        _alloc_flavor_ = "raw"
    def f(i):
        a = A()
        b = B()
        c = C()
        d = None
        e = None
        if i == 0:
            d = a
        elif i == 1:
            d = b
        elif i == 2:
            e = c
        res =  (0x0001*(a is b) | 0x0002*(a is c) | 0x0004*(a is d) |
                0x0008*(a is e) | 0x0010*(b is c) | 0x0020*(b is d) |
                0x0040*(b is e) | 0x0080*(c is d) | 0x0100*(c is e) |
                0x0200*(d is e))
        free_non_gc_object(a)
        free_non_gc_object(b)
        free_non_gc_object(c)
        return res
    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [int])
    assert s.knowntype == int
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    res = interpret(f, [0])
    assert res == 0x0004
    res = interpret(f, [1])
    assert res == 0x0020
    res = interpret(f, [2])
    assert res == 0x0100
    res = interpret(f, [3])
    assert res == 0x0200
Esempio n. 18
0
    def test_implicit_cast(self):
        z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT,
                       sandboxsafe=True)   # to allow the wrapper to be inlined

        def f(x, y, xx, yy):
            return z(x, y, xx, yy)

        a = RPythonAnnotator()
        r = a.build_types(f, [int, int, int, int])
        rtyper = RPythonTyper(a)
        rtyper.specialize()
        a.translator.rtyper = rtyper
        backend_optimizations(a.translator)
        if option.view:
            a.translator.view()
        graph = graphof(a.translator, f)
        s = summary(graph)
        # there should be not too many operations here by now
        expected = {'force_cast': 3, 'cast_int_to_float': 1, 'direct_call': 1}
        for k, v in expected.items():
            assert s[k] == v
Esempio n. 19
0
    def test_implicit_cast(self):
        z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT,
                       sandboxsafe=True)   # to allow the wrapper to be inlined

        def f(x, y, xx, yy):
            return z(x, y, xx, yy)

        a = RPythonAnnotator()
        r = a.build_types(f, [int, int, int, int])
        rtyper = RPythonTyper(a)
        rtyper.specialize()
        a.translator.rtyper = rtyper
        backend_optimizations(a.translator)
        if option.view:
            a.translator.view()
        graph = graphof(a.translator, f)
        s = summary(graph)
        # there should be not too many operations here by now
        expected = {'force_cast': 3, 'cast_int_to_float': 1, 'direct_call': 1}
        for k, v in expected.items():
            assert s[k] == v
Esempio n. 20
0
def test_unsupported():
    class A:
        _alloc_flavor_ = "raw"

    def f():
        return str(A())

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [])
    assert s.knowntype == str
    rtyper = RPythonTyper(a)
    py.test.raises(TypeError, rtyper.specialize)  # results in an invalid cast
Esempio n. 21
0
def test_rtype_nongc_object():
    class TestClass(object):
        _alloc_flavor_ = "raw"

        def __init__(self, a):
            self.a = a

        def method1(self):
            return self.a

    def malloc_and_free(a):
        ci = TestClass(a)
        b = ci.method1()
        free_non_gc_object(ci)
        return b

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(malloc_and_free, [SomeAddress()])
    assert isinstance(s, SomeAddress)
    rtyper = RPythonTyper(a)
    rtyper.specialize()
Esempio n. 22
0
def test_alloc_flavor_subclassing():
    class A:
        _alloc_flavor_ = "raw"

    class B(A):
        def __init__(self, a):
            self.a = a

    def f():
        return B(0)

    a = RPythonAnnotator()
    #does not raise:
    s = a.build_types(f, [])
    Adef = a.bookkeeper.getuniqueclassdef(A)
    Bdef = a.bookkeeper.getuniqueclassdef(B)
    assert s.knowntype == Bdef
    rtyper = RPythonTyper(a)
    rtyper.specialize()
    assert (Adef, 'raw') in rtyper.instance_reprs
    assert (Adef, 'gc') not in rtyper.instance_reprs
    assert (Bdef, 'raw') in rtyper.instance_reprs
    assert (Bdef, 'gc') not in rtyper.instance_reprs
Esempio n. 23
0
def test_ll_get_dict_item():
    """
    Tests the low-level implementation of get_dict_item.
    """
    from rpython.annotator.annrpython import RPythonAnnotator
    from rpython.annotator.model import SomeTuple, SomeInteger, SomeString

    from rpython.rtyper.rtyper import RPythonTyper
    from rpython.rtyper.rmodel import inputconst
    from rpython.rtyper.annlowlevel import llstr, hlstr

    from rpython.rtyper.lltypesystem import lltype, rffi
    from rpython.rtyper.lltypesystem import rordereddict, rstr

    dummykeyobj = None
    dummyvalueobj = None

    def _get_str_dict():
        # STR -> lltype.Signed
        DICT = rordereddict.get_ll_dict(
            lltype.Ptr(rstr.STR),
            lltype.Signed,
            ll_fasthash_function=rstr.LLHelpers.ll_strhash,
            ll_hash_function=rstr.LLHelpers.ll_strhash,
            ll_eq_function=rstr.LLHelpers.ll_streq,
            dummykeyobj=dummykeyobj,
            dummyvalueobj=dummyvalueobj)
        return DICT

    s_tuple = SomeTuple([SomeString(), SomeInteger()])
    DICT = _get_str_dict()

    ll_d = rordereddict.ll_newdict(DICT)
    a = RPythonAnnotator()
    rtyper = RPythonTyper(a)
    a.translator.rtyper = rtyper
    r_tuple = rtyper.getrepr(s_tuple)
    cTUPLE = inputconst(lltype.Void, r_tuple.lowleveltype)
    s_tuple = rtyper.annotation(cTUPLE)
    rtyper.call_all_setups()

    for i in range(20):
        rordereddict.ll_dict_setitem(ll_d, llstr(str(i)), i)
    for i in range(20):
        element = ll_get_dict_item(s_tuple.const, ll_d, i)
        assert (str(i), i) == (hlstr(element.item0), element.item1)