def test_oostring(): def oof(): return oostring a = RPythonAnnotator() s = a.build_types(oof, []) assert isinstance(s, annmodel.SomeBuiltin)
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_oostring_annotation(): def oof(): return ootype.oostring a = RPythonAnnotator() s = a.build_types(oof, []) assert isinstance(s, annmodel.SomeBuiltin)
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
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)
def test_string(): def oof(): return new(String) a = RPythonAnnotator() s = a.build_types(oof, []) assert s == annmodel.SomeOOInstance(String)
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_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)
def test_null_object(): def fn(): return NULL a = RPythonAnnotator() s = a.build_types(fn, []) assert type(s) is annmodel.SomeOOObject
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_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
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'
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
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_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
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
def test_call_dummy(): def func(): x = dummy() return x a = RPythonAnnotator() s = a.build_types(func, []) assert isinstance(s, annmodel.SomeInteger)
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)
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'
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')
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'
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')
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)
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)
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)
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
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
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
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')
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
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
def test_box_can_be_None(self): def fn(flag): if flag: return box(42) else: return box(None) a = RPythonAnnotator() s = a.build_types(fn, [bool]) assert isinstance(s, annmodel.SomeOOInstance) assert s.ootype._name == '[mscorlib]System.Object' assert s.can_be_None
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)
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)
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
def test_overload_reannotate(): C = Instance("test", ROOT, {}, {'foo': overload(meth(Meth([Signed], Signed)), meth(Meth([Float], Float)))}) def f(): c = new(C) mylist = [42] a = c.foo(mylist[0]) mylist.append(42.5) return a a = RPythonAnnotator() assert isinstance(a.build_types(f, []), annmodel.SomeFloat)
def test_simple_classof(): I = Instance("test", ROOT, {'a': Signed}) def oof(): i = new(I) return classof(i) a = RPythonAnnotator() s = a.build_types(oof, []) #a.translator.view() assert s == annmodel.SomeOOClass(I)