| 1 | import py |
| 2 | |
| 3 | |
| 4 | from rpython.annotator.test.test_annrpython import graphof |
| 5 | from rpython.annotator.test.test_annrpython import TestAnnotateTestCase as parent |
| 6 | from rpython.annotator.model import AnnotatorError |
| 7 | |
| 8 | |
| 9 | class TestAnnotateAndSimplifyTestCase(parent): |
| 10 | """Same tests as test_annrpython.TestAnnotateTestCase, but automatically |
| 11 | running the simplify() method of the annotator after the annotation phase. |
| 12 | """ |
| 13 | |
| 14 | class RPythonAnnotator(parent.RPythonAnnotator): |
| 15 | def complete(self): |
| 16 | parent.RPythonAnnotator.complete(self) |
| 17 | if self.translator is not None: |
| 18 | self.simplify() |
| 19 | |
| 20 | def test_simple_pbc_call(self): |
| 21 | def f1(x,y=0): |
| 22 | pass |
| 23 | def f2(x): |
| 24 | pass |
| 25 | def f3(x): |
| 26 | pass |
| 27 | def g(f): |
| 28 | f(1) |
| 29 | def h(): |
| 30 | f1(1) |
| 31 | f1(1,2) |
| 32 | g(f2) |
| 33 | g(f3) |
| 34 | |
| 35 | a = self.RPythonAnnotator() |
| 36 | s = a.build_types(h, []) |
| 37 | |
| 38 | fdesc1 = a.bookkeeper.getdesc(f1) |
| 39 | fdesc2 = a.bookkeeper.getdesc(f2) |
| 40 | fdesc3 = a.bookkeeper.getdesc(f3) |
| 41 | |
| 42 | fam1 = fdesc1.getcallfamily() |
| 43 | fam2 = fdesc2.getcallfamily() |
| 44 | fam3 = fdesc3.getcallfamily() |
| 45 | |
| 46 | assert fam1 is not fam2 |
| 47 | assert fam1 is not fam3 |
| 48 | assert fam3 is fam2 |
| 49 | |
| 50 | gf1 = graphof(a, f1) |
| 51 | gf2 = graphof(a, f2) |
| 52 | gf3 = graphof(a, f3) |
| 53 | |
| 54 | assert fam1.calltables == {(2, (), False): [{fdesc1: gf1}], |
| 55 | (1, (), False): [{fdesc1: gf1}]} |
| 56 | assert fam2.calltables == {(1, (), False): [{fdesc2: gf2, fdesc3: gf3}]} |
| 57 | |
| 58 | def test_pbc_call_ins(self): |
| 59 | class A(object): |
| 60 | def m(self): |
| 61 | pass |
| 62 | class B(A): |
| 63 | def n(self): |
| 64 | pass |
| 65 | class C(A): |
| 66 | def __init__(self): |
| 67 | pass |
| 68 | def m(self): |
| 69 | pass |
| 70 | def f(x): |
| 71 | b = B() |
| 72 | c = C() |
| 73 | b.n() |
| 74 | if x: |
| 75 | a = b |
| 76 | else: |
| 77 | a = c |
| 78 | a.m() |
| 79 | |
| 80 | a = self.RPythonAnnotator() |
| 81 | s = a.build_types(f, [bool]) |
| 82 | |
| 83 | bookkeeper = a.bookkeeper |
| 84 | |
| 85 | def getmdesc(bmeth): |
| 86 | return bookkeeper.immutablevalue(bmeth).any_description() |
| 87 | |
| 88 | mdescA_m = getmdesc(A().m) |
| 89 | mdescC_m = getmdesc(C().m) |
| 90 | mdescB_n = getmdesc(B().n) |
| 91 | |
| 92 | assert mdescA_m.name == 'm' == mdescC_m.name |
| 93 | assert mdescB_n.name == 'n' |
| 94 | |
| 95 | famA_m = mdescA_m.getcallfamily() |
| 96 | famC_m = mdescC_m.getcallfamily() |
| 97 | famB_n = mdescB_n.getcallfamily() |
| 98 | |
| 99 | assert famA_m is famC_m |
| 100 | assert famB_n is not famA_m |
| 101 | |
| 102 | gfB_n = graphof(a, B.n.im_func) |
| 103 | gfA_m = graphof(a, A.m.im_func) |
| 104 | gfC_m = graphof(a, C.m.im_func) |
| 105 | |
| 106 | assert famB_n.calltables == {(1, (), False): [{mdescB_n.funcdesc: gfB_n}]} |
| 107 | assert famA_m.calltables == {(1, (), False): [ |
| 108 | {mdescA_m.funcdesc: gfA_m, mdescC_m.funcdesc: gfC_m }]} |
| 109 | |
| 110 | mdescCinit = getmdesc(C().__init__) |
| 111 | famCinit = mdescCinit.getcallfamily() |
| 112 | gfCinit = graphof(a, C.__init__.im_func) |
| 113 | |
| 114 | assert famCinit.calltables == {(1, (), False): [{mdescCinit.funcdesc: gfCinit}]} |
| 115 | |
| 116 | def test_call_classes_with_noarg_init(self): |
| 117 | class A: |
| 118 | foo = 21 |
| 119 | class B(A): |
| 120 | foo = 22 |
| 121 | class C(A): |
| 122 | def __init__(self): |
| 123 | self.foo = 42 |
| 124 | class D(A): |
| 125 | def __init__(self): |
| 126 | self.foo = 43 |
| 127 | def f(i): |
| 128 | if i == 1: |
| 129 | cls = B |
| 130 | elif i == 2: |
| 131 | cls = D |
| 132 | else: |
| 133 | cls = C |
| 134 | return cls().foo |
| 135 | a = self.RPythonAnnotator() |
| 136 | with py.test.raises(AnnotatorError): |
| 137 | a.build_types(f, [int]) |