| 1 |
from rpython.annotator import model as annmodel, annrpython |
| 2 |
from rpython.flowspace.model import Constant |
| 3 |
from rpython.rtyper import rmodel |
| 4 |
from rpython.rtyper.lltypesystem.lltype import Signed, Void |
| 5 |
from rpython.rtyper.rtyper import RPythonTyper |
| 6 |
from rpython.rtyper.test.test_llinterp import interpret |
| 7 |
from rpython.translator.translator import TranslationContext, graphof |
| 8 |
|
| 9 |
|
| 10 |
def test_reprkeys_dont_clash(): |
| 11 |
stup1 = annmodel.SomeTuple((annmodel.SomeFloat(), |
| 12 |
annmodel.SomeInteger())) |
| 13 |
stup2 = annmodel.SomeTuple((annmodel.SomeString(), |
| 14 |
annmodel.SomeInteger())) |
| 15 |
key1 = stup1.rtyper_makekey() |
| 16 |
key2 = stup2.rtyper_makekey() |
| 17 |
assert key1 != key2 |
| 18 |
|
| 19 |
def test_simple(): |
| 20 |
def dummyfn(x): |
| 21 |
return x+1 |
| 22 |
|
| 23 |
res = interpret(dummyfn, [7]) |
| 24 |
assert res == 8 |
| 25 |
|
| 26 |
def test_function_call(): |
| 27 |
def g(x, y): |
| 28 |
return x-y |
| 29 |
def f(x): |
| 30 |
return g(1, x) |
| 31 |
|
| 32 |
res = interpret(f, [4]) |
| 33 |
assert res == -3 |
| 34 |
|
| 35 |
def test_retval(): |
| 36 |
def f(x): |
| 37 |
return x |
| 38 |
t = TranslationContext() |
| 39 |
t.buildannotator().build_types(f, [int]) |
| 40 |
t.buildrtyper().specialize() |
| 41 |
|
| 42 |
t.checkgraphs() |
| 43 |
graph = graphof(t, f) |
| 44 |
assert graph.getreturnvar().concretetype == Signed |
| 45 |
assert graph.startblock.exits[0].args[0].concretetype == Signed |
| 46 |
|
| 47 |
def test_retval_None(): |
| 48 |
def f(x): |
| 49 |
pass |
| 50 |
t = TranslationContext() |
| 51 |
t.buildannotator().build_types(f, [int]) |
| 52 |
t.buildrtyper().specialize() |
| 53 |
|
| 54 |
t.checkgraphs() |
| 55 |
graph = graphof(t, f) |
| 56 |
assert graph.getreturnvar().concretetype == Void |
| 57 |
assert graph.startblock.exits[0].args[0].concretetype == Void |
| 58 |
|
| 59 |
def test_ll_calling_ll(): |
| 60 |
import test_llann |
| 61 |
tst = test_llann.TestLowLevelAnnotateTestCase() |
| 62 |
a, vTs = tst.test_ll_calling_ll() |
| 63 |
rt = RPythonTyper(a) |
| 64 |
rt.specialize() |
| 65 |
assert [vT.concretetype for vT in vTs] == [Void] * 4 |
| 66 |
|
| 67 |
def test_ll_calling_ll2(): |
| 68 |
import test_llann |
| 69 |
tst = test_llann.TestLowLevelAnnotateTestCase() |
| 70 |
a, vTs = tst.test_ll_calling_ll2() |
| 71 |
rt = RPythonTyper(a) |
| 72 |
rt.specialize() |
| 73 |
assert [vT.concretetype for vT in vTs] == [Void] * 3 |
| 74 |
|
| 75 |
|
| 76 |
def test_getgcflavor(): |
| 77 |
class A: |
| 78 |
pass |
| 79 |
class B: |
| 80 |
_alloc_flavor_ = "gc" |
| 81 |
class R: |
| 82 |
_alloc_flavor_ = "raw" |
| 83 |
|
| 84 |
class DummyClsDescDef: |
| 85 |
def __init__(self, cls): |
| 86 |
self._cls = cls |
| 87 |
self.classdesc = self |
| 88 |
self.basedef = None |
| 89 |
|
| 90 |
def getmro(self): |
| 91 |
return [self] |
| 92 |
|
| 93 |
def get_param(self, name, default=None, inherit=True): |
| 94 |
return getattr(self._cls, name, default) |
| 95 |
|
| 96 |
assert rmodel.getgcflavor(DummyClsDescDef(A)) == 'gc' |
| 97 |
assert rmodel.getgcflavor(DummyClsDescDef(B)) == 'gc' |
| 98 |
assert rmodel.getgcflavor(DummyClsDescDef(R)) == 'raw' |