Esempio n. 1
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. 2
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. 3
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. 4
0
def test_oostring_annotation():
    def oof():
        return ootype.oostring

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert isinstance(s, annmodel.SomeBuiltin)
Esempio n. 5
0
    def test_list_of_str0_unchecked(self):
        str0 = 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)
        assert a.translator.config.translation.check_str_without_nul == False
        a.build_types(f, [[str]])  # Does not raise
        # 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)

        with py.test.raises(AnnotatorError):
            # fails with TooLateForChange
            a.build_types(g, [[str]])
        a.build_types(g, [[str0]])  # Does not raise
Esempio n. 6
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. 7
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. 8
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. 9
0
def test_null_object():
    def fn():
        return NULL

    a = RPythonAnnotator()
    s = a.build_types(fn, [])
    assert type(s) is annmodel.SomeOOObject
Esempio n. 10
0
 def buildannotator(self, policy=None):
     if self.annotator is not None:
         raise ValueError("we already have an annotator")
     from rpython.annotator.annrpython import RPythonAnnotator
     self.annotator = RPythonAnnotator(
         self, policy=policy, keepgoing=self.config.translation.keepgoing)
     return self.annotator
Esempio n. 11
0
def test_string():
    def oof():
        return new(String)

    a = RPythonAnnotator()
    s = a.build_types(oof, [])
    assert s == annmodel.SomeOOInstance(String)
Esempio n. 12
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. 13
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. 14
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. 15
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. 16
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. 17
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. 18
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. 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_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. 21
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. 22
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. 23
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. 24
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. 25
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. 26
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. 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)
Esempio n. 28
0
    def test_annotation(self):
        from rpython.annotator.annrpython import RPythonAnnotator
        from rpython.annotator import model as annmodel

        def f():
            return importing.make_compiled_pathname('abc/foo.py')

        a = RPythonAnnotator()
        s = a.build_types(f, [])
        assert isinstance(s, annmodel.SomeString)
        assert s.no_nul
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_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