Esempio n. 1
0
    def test_overloaded_meth_string(self):
        C = Instance("test", ROOT, {}, {
            'foo':
            overload(meth(Meth([Char], Signed)),
                     meth(Meth([String], Float)),
                     resolver=OverloadingResolver),
            'bar':
            overload(meth(Meth([Signed], Char)),
                     meth(Meth([Float], String)),
                     resolver=OverloadingResolver)
        })

        def fn1():
            return new(C).foo('a')

        def fn2():
            return new(C).foo('aa')

        def fn3(x):
            return new(C).bar(x)

        a = RPythonAnnotator()
        assert isinstance(a.build_types(fn1, []), annmodel.SomeInteger)
        assert isinstance(a.build_types(fn2, []), annmodel.SomeFloat)
        assert isinstance(a.build_types(fn3, [int]), annmodel.SomeChar)
        assert isinstance(a.build_types(fn3, [float]), annmodel.SomeString)
Esempio n. 2
0
 def test_charp2str_exact_result(self):
     from rpython.annotator.annrpython import RPythonAnnotator
     from rpython.rtyper.llannotation import SomePtr
     a = RPythonAnnotator()
     s = a.build_types(charpsize2str, [SomePtr(CCHARP), int])
     assert s.knowntype == str
     assert s.can_be_None is False
     assert s.no_nul is False
     #
     a = RPythonAnnotator()
     s = a.build_types(charp2str, [SomePtr(CCHARP)])
     assert s.knowntype == str
     assert s.can_be_None is False
     assert s.no_nul is True
Esempio n. 3
0
 def test_charp2str_exact_result(self):
     from rpython.annotator.annrpython import RPythonAnnotator
     from rpython.rtyper.llannotation import SomePtr
     a = RPythonAnnotator()
     s = a.build_types(charpsize2str, [SomePtr(CCHARP), int])
     assert s.knowntype == str
     assert s.can_be_None is False
     assert s.no_nul is False
     #
     a = RPythonAnnotator()
     s = a.build_types(charp2str, [SomePtr(CCHARP)])
     assert s.knowntype == str
     assert s.can_be_None is False
     assert s.no_nul is True
Esempio n. 4
0
def test_overloaded_meth():
    C = Instance("test", ROOT, {},
                 {'foo': overload(meth(Meth([Float], Void)),
                                  meth(Meth([Signed], Signed)),
                                  meth(Meth([], Float)))})
    def fn1():
        return new(C).foo(42.5)
    def fn2():
        return new(C).foo(42)
    def fn3():
        return new(C).foo()
    a = RPythonAnnotator()
    assert a.build_types(fn1, []) is annmodel.s_None
    assert isinstance(a.build_types(fn2, []), annmodel.SomeInteger)
    assert isinstance(a.build_types(fn3, []), annmodel.SomeFloat)
Esempio n. 5
0
def test_ooparse_int():
    def oof(n, b):
        return ooparse_int(oostring(n, b), b)

    a = RPythonAnnotator()
    s = a.build_types(oof, [int, int])
    assert isinstance(s, annmodel.SomeInteger)
Esempio n. 6
0
def test_ooparse_int():
    def oof(n, b):
        return ooparse_int(oostring(n, b), b)

    a = RPythonAnnotator()
    s = a.build_types(oof, [int, int])
    assert isinstance(s, annmodel.SomeInteger)
Esempio n. 7
0
 def test_unbox(self):
     def fn():
         x = box(42)
         return unbox(x, ootype.Signed)
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeInteger)
Esempio n. 8
0
def test_string():
    def oof():
        return new(String)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert s == annmodel.SomeOOInstance(String)
Esempio n. 9
0
def test_oostring_result_annotation():
    def oof():
        return ootype.oostring(42, -1)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeOOInstance) and s.ootype is ootype.String
Esempio n. 10
0
def test_oostring():
    def oof():
        return oostring

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeBuiltin)
Esempio n. 11
0
def test_oostring_annotation():
    def oof():
        return ootype.oostring

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeBuiltin)
Esempio n. 12
0
def test_string():
    def oof():
        return new(String)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert s == annmodel.SomeOOInstance(String)
Esempio n. 13
0
    def test_basic(self):
        """
        A ExtFuncEntry provides an annotation for a function, no need to flow
        its graph.
        """
        def b(x):
            "NOT_RPYTHON"
            return eval("x+40")

        class BTestFuncEntry(ExtFuncEntry):
            _about_ = b
            name = 'b'
            signature_args = [SomeInteger()]
            signature_result = SomeInteger()

        def f():
            return b(2)

        policy = AnnotatorPolicy()
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, SomeInteger)

        res = interpret(f, [])
        assert res == 42
Esempio n. 14
0
def test_null_object():
    def fn():
        return NULL

    a = RPythonAnnotator()
    s = a.build_types(fn, [])
    assert type(s) is annmodel.SomeOOObject
Esempio n. 15
0
 def test_fullname(self):
     def fn():
         return CLR.System.Math
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, SomeCliClass)
     assert s.const is System.Math
Esempio n. 16
0
    def test_basic(self):
        """
        A ExtFuncEntry provides an annotation for a function, no need to flow
        its graph.
        """
        def b(x):
            "NOT_RPYTHON"
            return eval("x+40")

        class BTestFuncEntry(ExtFuncEntry):
            _about_ = b
            name = 'b'
            signature_args = [annmodel.SomeInteger()]
            signature_result = annmodel.SomeInteger()

        def f():
            return b(2)

        policy = AnnotatorPolicy()
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)

        res = interpret(f, [])
        assert res == 42
Esempio n. 17
0
 def test_list_of_str0(self):
     str0 = annmodel.SomeString(no_nul=True)
     def os_execve(l):
         pass
     register_external(os_execve, [[str0]], None)
     def f(l):
         return os_execve(l)
     policy = AnnotatorPolicy()
     a = RPythonAnnotator(policy=policy)
     a.build_types(f, [[str]])  # Does not raise
     assert a.translator.config.translation.check_str_without_nul == False
     # Now enable the str0 check, and try again with a similar function
     a.translator.config.translation.check_str_without_nul=True
     def g(l):
         return os_execve(l)
     py.test.raises(Exception, a.build_types, g, [[str]])
     a.build_types(g, [[str0]])  # Does not raise
Esempio n. 18
0
 def test_can_be_None(self):
     def fn():
         ttype = System.Type.GetType('foo')
         return ttype.get_Namespace()
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeString)
     assert s.can_be_None
Esempio n. 19
0
 def test_box(self):
     def fn():
         return box(42)
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeOOInstance)
     assert s.ootype._name == '[mscorlib]System.Object'
     assert not s.can_be_None
Esempio n. 20
0
 def test_new_instance(self):
     def fn():
         return ArrayList()
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeOOInstance)
     assert isinstance(s.ootype, NativeInstance)
     assert s.ootype._name == '[mscorlib]System.Collections.ArrayList'
Esempio n. 21
0
 def test_staticmeth_call(self):
     def fn1():
         return System.Math.Abs(42)
     def fn2():
         return System.Math.Abs(42.5)
     a = RPythonAnnotator()
     assert type(a.build_types(fn1, [])) is annmodel.SomeInteger
     assert type(a.build_types(fn2, [])) is annmodel.SomeFloat
Esempio n. 22
0
 def test_staticmeth(self):
     def fn():
         return System.Math.Abs
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, SomeCliStaticMethod)
     assert s.cli_class is System.Math
     assert s.meth_name == 'Abs'
Esempio n. 23
0
    def test_unbox(self):
        def fn():
            x = box(42)
            return unbox(x, ootype.Signed)

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeInteger)
Esempio n. 24
0
    def test_fullname(self):
        def fn():
            return CLR.System.Math

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, SomeCliClass)
        assert s.const is System.Math
Esempio n. 25
0
def test_annotate_1():
    def f():
        return virtual_ref(X())
    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, SomeVRef)
    assert isinstance(s.s_instance, annmodel.SomeInstance)
    assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)
Esempio n. 26
0
def test_call_dummy():
    def func():
        x = dummy()
        return x

    a = RPythonAnnotator()
    s = a.build_types(func, [])
    assert isinstance(s, annmodel.SomeInteger)
Esempio n. 27
0
 def test_array_getitem(self):
     def fn():
         x = ArrayList().ToArray()
         return x[0]
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeOOInstance)
     assert s.ootype._name == '[mscorlib]System.Object'
Esempio n. 28
0
def test_call_dummy():
    def func():
        x = dummy()
        return x

    a = RPythonAnnotator()
    s = a.build_types(func, [])
    assert isinstance(s, annmodel.SomeInteger)
Esempio n. 29
0
 def test_box_instance(self):
     class Foo:
         pass
     def fn():
         return box(Foo())
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeOOInstance)
     assert s.ootype._name == '[mscorlib]System.Object'
Esempio n. 30
0
def test_annotate_1():
    def f():
        return virtual_ref(X())

    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, SomeVRef)
    assert isinstance(s.s_instance, annmodel.SomeInstance)
    assert s.s_instance.classdef == a.bookkeeper.getuniqueclassdef(X)
Esempio n. 31
0
def test_nonconst_list():
    def nonconst_l():
        a = NonConstant([1, 2, 3])
        return a[0]

    a = RPythonAnnotator()
    s = a.build_types(nonconst_l, [])
    assert s.knowntype is int
    assert not hasattr(s, 'const')
Esempio n. 32
0
def test_nonconst():
    def nonconst_f():
        a = NonConstant(3)
        return a

    a = RPythonAnnotator()
    s = a.build_types(nonconst_f, [])
    assert s.knowntype is int
    assert not hasattr(s, 'const')
Esempio n. 33
0
def test_nonconst_list():
    def nonconst_l():
        a = NonConstant([1, 2, 3])
        return a[0]

    a = RPythonAnnotator()
    s = a.build_types(nonconst_l, [])
    assert s.knowntype is int
    assert not hasattr(s, 'const')
Esempio n. 34
0
    def test_can_be_None(self):
        def fn():
            ttype = System.Type.GetType('foo')
            return ttype.get_Namespace()

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeString)
        assert s.can_be_None
Esempio n. 35
0
    def test_array_getitem(self):
        def fn():
            x = ArrayList().ToArray()
            return x[0]

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeOOInstance)
        assert s.ootype._name == '[mscorlib]System.Object'
Esempio n. 36
0
    def test_new_instance(self):
        def fn():
            return ArrayList()

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeOOInstance)
        assert isinstance(s.ootype, NativeInstance)
        assert s.ootype._name == '[mscorlib]System.Collections.ArrayList'
Esempio n. 37
0
    def test_staticmeth(self):
        def fn():
            return System.Math.Abs

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, SomeCliStaticMethod)
        assert s.cli_class is System.Math
        assert s.meth_name == 'Abs'
Esempio n. 38
0
    def test_box(self):
        def fn():
            return box(42)

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeOOInstance)
        assert s.ootype._name == '[mscorlib]System.Object'
        assert not s.can_be_None
Esempio n. 39
0
def test_nonconst():
    def nonconst_f():
        a = NonConstant(3)
        return a

    a = RPythonAnnotator()
    s = a.build_types(nonconst_f, [])
    assert s.knowntype is int
    assert not hasattr(s, 'const')
Esempio n. 40
0
def test_overload_upcast():
    C = Instance("base", ROOT, {}, {
        'foo': overload(meth(Meth([], Void)),
                        meth(Meth([ROOT], Signed)))})
    def f():
        c = new(C)
        return c.foo(c)
    a = RPythonAnnotator()
    assert isinstance(a.build_types(f, []), annmodel.SomeInteger)
Esempio n. 41
0
def test_null_static_method():
    F = StaticMethod([Signed, Signed], Signed)

    def oof():
        return null(F)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])

    assert s == annmodel.SomeOOStaticMeth(F)
Esempio n. 42
0
def test_nullstring():
    def oof(b):
        if b:
            return 'foo'
        else:
            return None

    a = RPythonAnnotator()
    s = a.build_types(oof, [bool])
    assert annmodel.SomeString(can_be_None=True).contains(s)
Esempio n. 43
0
 def test_overloaded_meth_string(self):
     C = Instance("test", ROOT, {},
                  {'foo': overload(meth(Meth([Char], Signed)),
                                   meth(Meth([String], Float)),
                                   resolver=OverloadingResolver),
                   'bar': overload(meth(Meth([Signed], Char)),
                                   meth(Meth([Float], String)),
                                   resolver=OverloadingResolver)})
     def fn1():
         return new(C).foo('a')
     def fn2():
         return new(C).foo('aa')
     def fn3(x):
         return new(C).bar(x)
     a = RPythonAnnotator()
     assert isinstance(a.build_types(fn1, []), annmodel.SomeInteger)
     assert isinstance(a.build_types(fn2, []), annmodel.SomeFloat)
     assert isinstance(a.build_types(fn3, [int]), annmodel.SomeChar)
     assert isinstance(a.build_types(fn3, [float]), annmodel.SomeString)
Esempio n. 44
0
def test_null_static_method():
    F = StaticMethod([Signed, Signed], Signed)

    def oof():
        return null(F)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])

    assert s == annmodel.SomeOOStaticMeth(F)
Esempio n. 45
0
def test_nullstring():
    def oof(b):
        if b:
            return 'foo'
        else:
            return None

    a = RPythonAnnotator()
    s = a.build_types(oof, [bool])
    assert annmodel.SomeString(can_be_None=True).contains(s)
Esempio n. 46
0
 def test_unbox_instance(self):
     class Foo:
         pass
     def fn():
         boxed = box(Foo())
         return unbox(boxed, Foo)
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeInstance)
     assert s.classdef.name.endswith('Foo')
Esempio n. 47
0
    def test_staticmeth_call(self):
        def fn1():
            return System.Math.Abs(42)

        def fn2():
            return System.Math.Abs(42.5)

        a = RPythonAnnotator()
        assert type(a.build_types(fn1, [])) is annmodel.SomeInteger
        assert type(a.build_types(fn2, [])) is annmodel.SomeFloat
Esempio n. 48
0
 def test_mix_None_and_instance(self):
     def fn(x):
         if x:
             return None
         else:
             return box(42)
     a = RPythonAnnotator()
     s = a.build_types(fn, [bool])
     assert isinstance(s, annmodel.SomeOOInstance)
     assert s.can_be_None == True
Esempio n. 49
0
 def test_unbox_can_be_None(self):
     class Foo:
         pass
     def fn():
         x = box(42)
         return unbox(x, Foo)
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, annmodel.SomeInstance)
     assert s.can_be_None
Esempio n. 50
0
    def test_mix_None_and_instance(self):
        def fn(x):
            if x:
                return None
            else:
                return box(42)

        a = RPythonAnnotator()
        s = a.build_types(fn, [bool])
        assert isinstance(s, annmodel.SomeOOInstance)
        assert s.can_be_None == True
Esempio n. 51
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. 52
0
def test_record():
    R = Record({'foo': Signed})
    r = new(R)

    def oof():
        return r

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeOOInstance)
    assert s.ootype == R
Esempio n. 53
0
def test_overload_upcast():
    C = Instance(
        "base", ROOT, {},
        {'foo': overload(meth(Meth([], Void)), meth(Meth([ROOT], Signed)))})

    def f():
        c = new(C)
        return c.foo(c)

    a = RPythonAnnotator()
    assert isinstance(a.build_types(f, []), annmodel.SomeInteger)
Esempio n. 54
0
def test_overloaded_meth():
    C = Instance("test", ROOT, {}, {
        'foo':
        overload(meth(Meth([Float], Void)), meth(Meth([Signed], Signed)),
                 meth(Meth([], Float)))
    })

    def fn1():
        return new(C).foo(42.5)

    def fn2():
        return new(C).foo(42)

    def fn3():
        return new(C).foo()

    a = RPythonAnnotator()
    assert a.build_types(fn1, []) is annmodel.s_None
    assert isinstance(a.build_types(fn2, []), annmodel.SomeInteger)
    assert isinstance(a.build_types(fn3, []), annmodel.SomeFloat)
Esempio n. 55
0
def test_record():
    R = Record({'foo': Signed})
    r = new(R)

    def oof():
        return r

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeOOInstance)
    assert s.ootype == R
Esempio n. 56
0
    def test_box_instance(self):
        class Foo:
            pass

        def fn():
            return box(Foo())

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeOOInstance)
        assert s.ootype._name == '[mscorlib]System.Object'
Esempio n. 57
0
def test_annotate_2():
    def f():
        x1 = X()
        vref = virtual_ref(x1)
        x2 = vref()
        virtual_ref_finish(vref, x1)
        return x2
    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(X)
Esempio n. 58
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. 59
0
    def test_unbox_instance(self):
        class Foo:
            pass

        def fn():
            boxed = box(Foo())
            return unbox(boxed, Foo)

        a = RPythonAnnotator()
        s = a.build_types(fn, [])
        assert isinstance(s, annmodel.SomeInstance)
        assert s.classdef.name.endswith('Foo')