| 1 |
from rpython.translator.translator import TranslationContext, graphof |
| 2 |
from rpython.translator.backendopt.canraise import RaiseAnalyzer |
| 3 |
from rpython.translator.backendopt.all import backend_optimizations |
| 4 |
from rpython.conftest import option |
| 5 |
|
| 6 |
class TestCanRaise(object): |
| 7 |
def translate(self, func, sig): |
| 8 |
t = TranslationContext() |
| 9 |
t.buildannotator().build_types(func, sig) |
| 10 |
t.buildrtyper().specialize() |
| 11 |
if option.view: |
| 12 |
t.view() |
| 13 |
return t, RaiseAnalyzer(t) |
| 14 |
|
| 15 |
|
| 16 |
def test_can_raise_simple(self): |
| 17 |
def g(x): |
| 18 |
return True |
| 19 |
|
| 20 |
def f(x): |
| 21 |
return g(x - 1) |
| 22 |
t, ra = self.translate(f, [int]) |
| 23 |
fgraph = graphof(t, f) |
| 24 |
result = ra.can_raise(fgraph.startblock.operations[0]) |
| 25 |
assert not result |
| 26 |
|
| 27 |
def test_can_raise_recursive(self): |
| 28 |
from rpython.translator.transform import insert_ll_stackcheck |
| 29 |
def g(x): |
| 30 |
return f(x) |
| 31 |
|
| 32 |
def f(x): |
| 33 |
if x: |
| 34 |
return g(x - 1) |
| 35 |
return 1 |
| 36 |
t, ra = self.translate(f, [int]) |
| 37 |
insert_ll_stackcheck(t) |
| 38 |
ggraph = graphof(t, g) |
| 39 |
result = ra.can_raise(ggraph.startblock.operations[-1]) |
| 40 |
assert result |
| 41 |
|
| 42 |
def test_bug_graphanalyze_recursive(self): |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def g(x): |
| 47 |
return f(x) |
| 48 |
|
| 49 |
def f(x): |
| 50 |
if x: |
| 51 |
if x % 2: |
| 52 |
return x |
| 53 |
raise ValueError |
| 54 |
return g(x - 1) |
| 55 |
t, ra = self.translate(f, [int]) |
| 56 |
ggraph = graphof(t, g) |
| 57 |
fgraph = graphof(t, f) |
| 58 |
result = ra.can_raise(ggraph.startblock.operations[-1]) |
| 59 |
assert result |
| 60 |
result = ra.can_raise(fgraph.startblock.exits[0].target.operations[-1]) |
| 61 |
assert result |
| 62 |
|
| 63 |
def test_recursive_cannot_raise(self): |
| 64 |
|
| 65 |
|
| 66 |
def g(x): |
| 67 |
return f(x) |
| 68 |
|
| 69 |
def f(x): |
| 70 |
if x: |
| 71 |
if x % 2: |
| 72 |
return x |
| 73 |
return 42 |
| 74 |
return g(x - 1) |
| 75 |
|
| 76 |
t, ra = self.translate(f, [int]) |
| 77 |
ggraph = graphof(t, g) |
| 78 |
fgraph = graphof(t, f) |
| 79 |
result = ra.can_raise(ggraph.startblock.operations[-1]) |
| 80 |
assert not result |
| 81 |
result = ra.can_raise(fgraph.startblock.exits[0].target.operations[-1]) |
| 82 |
assert not result |
| 83 |
|
| 84 |
def test_can_raise_exception(self): |
| 85 |
def g(): |
| 86 |
raise ValueError |
| 87 |
def f(): |
| 88 |
return g() |
| 89 |
t, ra = self.translate(f, []) |
| 90 |
fgraph = graphof(t, f) |
| 91 |
result = ra.can_raise(fgraph.startblock.operations[0]) |
| 92 |
assert result |
| 93 |
|
| 94 |
def test_indirect_call(self): |
| 95 |
def g1(): |
| 96 |
raise ValueError |
| 97 |
def g2(): |
| 98 |
return 2 |
| 99 |
def f(x): |
| 100 |
if x: |
| 101 |
g = g1 |
| 102 |
else: |
| 103 |
g = g2 |
| 104 |
return g() |
| 105 |
def h(x): |
| 106 |
return f(x) |
| 107 |
t, ra = self.translate(h, [int]) |
| 108 |
hgraph = graphof(t, h) |
| 109 |
result = ra.can_raise(hgraph.startblock.operations[0]) |
| 110 |
assert result |
| 111 |
|
| 112 |
def test_method(self): |
| 113 |
class A(object): |
| 114 |
def f(self): |
| 115 |
return 1 |
| 116 |
def m(self): |
| 117 |
raise ValueError |
| 118 |
class B(A): |
| 119 |
def f(self): |
| 120 |
return 2 |
| 121 |
def m(self): |
| 122 |
return 3 |
| 123 |
def f(a): |
| 124 |
return a.f() |
| 125 |
def m(a): |
| 126 |
return a.m() |
| 127 |
def h(flag): |
| 128 |
if flag: |
| 129 |
obj = A() |
| 130 |
else: |
| 131 |
obj = B() |
| 132 |
f(obj) |
| 133 |
m(obj) |
| 134 |
|
| 135 |
t, ra = self.translate(h, [int]) |
| 136 |
hgraph = graphof(t, h) |
| 137 |
|
| 138 |
block = hgraph.startblock.exits[0].target.exits[0].target |
| 139 |
op_call_f = block.operations[0] |
| 140 |
op_call_m = block.operations[1] |
| 141 |
|
| 142 |
|
| 143 |
def check_call(op, fname): |
| 144 |
assert op.opname == "direct_call" |
| 145 |
assert op.args[0].value._obj._name == fname |
| 146 |
check_call(op_call_f, "f") |
| 147 |
check_call(op_call_m, "m") |
| 148 |
|
| 149 |
assert not ra.can_raise(op_call_f) |
| 150 |
assert ra.can_raise(op_call_m) |
| 151 |
|
| 152 |
def test_method_recursive(self): |
| 153 |
class A: |
| 154 |
def m(self, x): |
| 155 |
if x > 0: |
| 156 |
return self.m(x-1) |
| 157 |
else: |
| 158 |
return 42 |
| 159 |
def m(a): |
| 160 |
return a.m(2) |
| 161 |
def h(): |
| 162 |
obj = A() |
| 163 |
m(obj) |
| 164 |
t, ra = self.translate(h, []) |
| 165 |
hgraph = graphof(t, h) |
| 166 |
|
| 167 |
block = hgraph.startblock |
| 168 |
op_call_m = block.operations[-1] |
| 169 |
assert op_call_m.opname == "direct_call" |
| 170 |
assert not ra.can_raise(op_call_m) |
| 171 |
|
| 172 |
def test_instantiate(self): |
| 173 |
|
| 174 |
|
| 175 |
from rpython.rlib.objectmodel import instantiate |
| 176 |
class A: |
| 177 |
pass |
| 178 |
class B(A): |
| 179 |
pass |
| 180 |
def g(x): |
| 181 |
if x: |
| 182 |
C = A |
| 183 |
else: |
| 184 |
C = B |
| 185 |
a = instantiate(C) |
| 186 |
def f(x): |
| 187 |
return g(x) |
| 188 |
t, ra = self.translate(f, [int]) |
| 189 |
fgraph = graphof(t, f) |
| 190 |
result = ra.can_raise(fgraph.startblock.operations[0]) |
| 191 |
assert result |
| 192 |
|
| 193 |
def test_llexternal(self): |
| 194 |
from rpython.rtyper.lltypesystem.rffi import llexternal |
| 195 |
from rpython.rtyper.lltypesystem import lltype |
| 196 |
z = llexternal('z', [lltype.Signed], lltype.Signed) |
| 197 |
def f(x): |
| 198 |
return z(x) |
| 199 |
t, ra = self.translate(f, [int]) |
| 200 |
fgraph = graphof(t, f) |
| 201 |
backend_optimizations(t) |
| 202 |
assert fgraph.startblock.operations[0].opname == 'direct_call' |
| 203 |
|
| 204 |
result = ra.can_raise(fgraph.startblock.operations[0]) |
| 205 |
assert not result |
| 206 |
|
| 207 |
z = llexternal('z', [lltype.Signed], lltype.Signed) |
| 208 |
def g(x): |
| 209 |
return z(x) |
| 210 |
t, ra = self.translate(g, [int]) |
| 211 |
ggraph = graphof(t, g) |
| 212 |
|
| 213 |
assert ggraph.startblock.operations[0].opname == 'direct_call' |
| 214 |
|
| 215 |
result = ra.can_raise(ggraph.startblock.operations[0]) |
| 216 |
assert result |
| 217 |
|
| 218 |
def test_ll_arraycopy(self): |
| 219 |
from rpython.rtyper.lltypesystem import rffi |
| 220 |
from rpython.rlib.rgc import ll_arraycopy |
| 221 |
def f(a, b, c, d, e): |
| 222 |
ll_arraycopy(a, b, c, d, e) |
| 223 |
t, ra = self.translate(f, [rffi.CCHARP, rffi.CCHARP, int, int, int]) |
| 224 |
fgraph = graphof(t, f) |
| 225 |
result = ra.can_raise(fgraph.startblock.operations[0]) |
| 226 |
assert not result |
| 227 |
|
| 228 |
def test_memoryerror(self): |
| 229 |
def f(x): |
| 230 |
return [x, 42] |
| 231 |
t, ra = self.translate(f, [int]) |
| 232 |
result = ra.analyze_direct_call(graphof(t, f)) |
| 233 |
assert result |
| 234 |
|
| 235 |
ra = RaiseAnalyzer(t) |
| 236 |
ra.do_ignore_memory_error() |
| 237 |
result = ra.analyze_direct_call(graphof(t, f)) |
| 238 |
assert not result |
| 239 |
|
| 240 |
def g(x): |
| 241 |
try: |
| 242 |
return f(x) |
| 243 |
except: |
| 244 |
raise |
| 245 |
t, ra = self.translate(g, [int]) |
| 246 |
ra.do_ignore_memory_error() |
| 247 |
result = ra.analyze_direct_call(graphof(t, g)) |
| 248 |
assert not result |
| 249 |
|
| 250 |
def h(x): |
| 251 |
return {5:6}[x] |
| 252 |
t, ra = self.translate(h, [int]) |
| 253 |
ra.do_ignore_memory_error() |
| 254 |
result = ra.analyze_direct_call(graphof(t, h)) |
| 255 |
assert result |