Beispiel #1
0
    def test_callback(self):
        """
        Verify annotation when a callback function is in the arguments list.
        """
        def d(y):
            return eval("y()")

        class DTestFuncEntry(ExtFuncEntry):
            _about_ = d
            name = 'd'
            signature_args = [
                annmodel.SomeGenericCallable(args=[],
                                             result=annmodel.SomeFloat())
            ]
            signature_result = annmodel.SomeFloat()

        def callback():
            return 2.5

        def f():
            return d(callback)

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeFloat)
        assert a.translator._graphof(callback)
Beispiel #2
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)
Beispiel #3
0
def test_global():
    def access_global():
        return glob_b.a

    a = RPythonAnnotator()
    s = a.build_types(access_global, [])
    assert s.knowntype is int
Beispiel #4
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()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)

        res = interpret(f, [])
        assert res == 42
Beispiel #5
0
def test_oostring():
    def oof():
        return oostring

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeBuiltin)
Beispiel #6
0
def test_new_bltn():
    def new():
        return C()

    a = RPythonAnnotator()
    s = a.build_types(new, [])
    assert s.knowntype is C
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
0
def test_string():
    def oof():
        return new(String)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert s == annmodel.SomeOOInstance(String)
Beispiel #11
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
Beispiel #12
0
    def test_implicit_cast(self):
        z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT)

        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 = {
            'cast_int_to_uint': 1,
            'direct_call': 1,
            'cast_primitive': 2,
            'cast_int_to_float': 1
        }
        for k, v in expected.items():
            assert s[k] == v
Beispiel #13
0
def test_null_object():
    def fn():
        return NULL

    a = RPythonAnnotator()
    s = a.build_types(fn, [])
    assert type(s) is annmodel.SomeOOObject
Beispiel #14
0
def test_bltn_attrs():
    def access_attr():
        a = A()
        return a.b

    a = RPythonAnnotator()
    s = a.build_types(access_attr, [])
    assert s.knowntype is int
Beispiel #15
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'
Beispiel #16
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)
Beispiel #17
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
Beispiel #18
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'
Beispiel #19
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
Beispiel #20
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'
Beispiel #21
0
def test_bltn_method():
    def access_meth():
        a = B()
        return a.m(3)

    a = RPythonAnnotator()
    s = a.build_types(access_meth, [])
    assert s.knowntype is int
Beispiel #22
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
Beispiel #23
0
def test_call_dummy():
    def func():
        x = dummy()
        return x
    
    a = RPythonAnnotator()
    s = a.build_types(func, [])
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #24
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')
Beispiel #25
0
def test_register_external_signature():
    def f():
        return dd(3)

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #26
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')
Beispiel #27
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)
Beispiel #28
0
def test_annotation_b():
    def f():
        return b(1)

    policy = AnnotatorPolicy()
    policy.allow_someobjects = False
    a = RPythonAnnotator(policy=policy)
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInteger)
Beispiel #29
0
def test_flowin():
    def set_callback():
        a = CB()
        a.m = some_int
        return a.m()

    a = RPythonAnnotator()
    s = a.build_types(set_callback, [])
    assert s.knowntype is int
Beispiel #30
0
def test_bltn_set_attr():
    def add_attr():
        a = AA()
        a.b = 3
        return a.b

    a = RPythonAnnotator()
    s = a.build_types(add_attr, [])
    assert s.knowntype is int