Exemple #1
0
 def test_raw_malloc(self):
     def f():
         return raw_malloc(100)
     a = RPythonAnnotator()
     s = a.build_types(f, [])
     assert isinstance(s, annmodel.SomeAddress)
     assert not s.is_null
Exemple #2
0
 def test_class(self):
     def fn():
         return Math
     a = RPythonAnnotator()
     s = a.build_types(fn, [])
     assert isinstance(s, SomeCliClass)
     assert s.const is Math
Exemple #3
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
Exemple #4
0
 def test_raw_free(self):
     def f(addr):
         raw_free(addr)
     a = RPythonAnnotator()
     s = a.build_types(f, [annmodel.SomeAddress()])
     rtyper = RPythonTyper(a)
     rtyper.specialize() #does not raise
Exemple #5
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)
Exemple #6
0
def test_oostring_annotation():
    def oof():
        return ootype.oostring

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeBuiltin)
Exemple #7
0
 def test_raw_malloc(self):
     def f():
         return raw_malloc(100)
     a = RPythonAnnotator()
     s = a.build_types(f, [])
     rtyper = RPythonTyper(a)
     rtyper.specialize() #does not raise
 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)
    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
Exemple #10
0
def test_annotate_1():
    def f():
        return eraseX(X())

    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, SomeErased)
Exemple #11
0
def test_annotate_erasing_pair():
    erase, unerase = new_erasing_pair("test1")
    erase2, unerase2 = new_erasing_pair("test2")

    class Foo:
        pass

    #
    def make(n):
        if n > 5:
            return erase([5, 6, n - 6])
        else:
            foo = Foo()
            foo.bar = n + 1
            return erase2(foo)

    def check(x, n):
        if n > 5:
            return unerase(x)[2]
        else:
            return unerase2(x).bar

    def f(n):
        x = make(n)
        return check(x, n)

    #
    a = RPythonAnnotator()
    s = a.build_types(f, [int])
    assert isinstance(s, annmodel.SomeInteger)
Exemple #12
0
def test_global():
    def access_global():
        return glob_b.a
    
    a = RPythonAnnotator()
    s = a.build_types(access_global, [])
    assert s.knowntype is int
Exemple #13
0
def test_isinstance():
    class A:
        _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()
        return 100*isinstance(o, A)+10*isinstance(o, B)+1*isinstance(o ,C)

    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
Exemple #14
0
def test_new_bltn():
    def new():
        return C()
    
    a = RPythonAnnotator()
    s = a.build_types(new, [])
    assert s.knowntype is C
Exemple #15
0
def test_annotate_lock():
    def fn():
        return thread.allocate_lock().acquire(False)
    a = RPythonAnnotator()
    s = a.build_types(fn, [])
    # result should be a boolean
    assert s.knowntype == bool
Exemple #16
0
def test_annotate_prebuilt_int():
    e1 = erase_int(42)
    def f(i):
        return unerase_int(e1)
    a = RPythonAnnotator()
    s = a.build_types(f, [int])
    assert isinstance(s, annmodel.SomeInteger)
Exemple #17
0
def test_annotate_reflowing():
    erase, unerase = new_erasing_pair("test1")
    class A: pass
    class B(A): pass
    class C(B): pass
    class D(C): pass

    def f():
        x = erase(None)
        while True:
            inst = unerase(x)
            if inst is None:
                inst = D()
                x = erase(inst)
            elif isinstance(inst, D):
                inst = C()
                x = erase(inst)
            elif isinstance(inst, C):
                inst = B()
                x = erase(inst)
            elif isinstance(inst, B):
                inst = A()
                x = erase(inst)
            else:
                return inst
    #
    a = RPythonAnnotator()
    s = a.build_types(f, [])
    assert isinstance(s, annmodel.SomeInstance)
    assert s.classdef == a.bookkeeper.getuniqueclassdef(A)
 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
    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)
Exemple #20
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
    def test_register_external_return_goes_back(self):
        """
        Check whether it works to pass the same list from one external
        fun to another
        [bookkeeper and list joining issues]
        """

        def function_with_list():
            pass

        register_external(function_with_list, [[int]], int)

        def function_returning_list():
            pass

        register_external(function_returning_list, [], [int])

        def f():
            return function_with_list(function_returning_list())

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeInteger)
    def test_register_external_tuple_args(self):
        """
        Verify the annotation of a registered external function which takes a
        tuple argument.
        """

        def function_with_tuple_arg():
            """
            Dummy function which is declared via register_external to take a
            tuple as an argument so that register_external's behavior for
            tuple-taking functions can be verified.
            """

        register_external(function_with_tuple_arg, [(int,)], int)

        def f():
            return function_with_tuple_arg((1,))

        policy = AnnotatorPolicy()
        policy.allow_someobjects = False
        a = RPythonAnnotator(policy=policy)
        s = a.build_types(f, [])

        # Not a very good assertion, but at least it means _something_ happened.
        assert isinstance(s, annmodel.SomeInteger)
Exemple #23
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)
Exemple #24
0
def test_string():
    def oof():
        return new(String)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert s == annmodel.SomeOOInstance(String)
 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
Exemple #26
0
 def test_memcopy(self):
     def f(addr1, addr2):
         raw_memcopy(addr1, addr2, 100)
     a = RPythonAnnotator()
     #does not raise:
     s = a.build_types(f, [annmodel.SomeAddress(), annmodel.SomeAddress()])
     rtyper = RPythonTyper(a)
     rtyper.specialize() #does not raise
Exemple #27
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
Exemple #28
0
 def test_null(self):
     def f():
         return NULL
     a = RPythonAnnotator()
     s = a.build_types(f, [])
     assert isinstance(s, annmodel.SomeAddress)
     assert s.is_null
     assert f() is NULL
 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'
Exemple #30
0
 def test_memory_access(self):
     def f(offset, value):
         addr = raw_malloc(offset * 2 + 1)
         addr.signed[offset] = value
         return addr.signed[offset]
     a = RPythonAnnotator()
     s = a.build_types(f, [annmodel.SomeInteger(), annmodel.SomeInteger()])
     assert isinstance(s, annmodel.SomeInteger)
Exemple #31
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'
 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'
Exemple #33
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'
Exemple #34
0
def test_call_dummy():
    def func():
        x = dummy()
        return x
    
    a = RPythonAnnotator()
    s = a.build_types(func, [])
    assert isinstance(s, annmodel.SomeInteger)
 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