1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
import py
from rpython.translator.translator import TranslationContext, graphof
from rpython.translator.simplify import join_blocks
from rpython.translator import exceptiontransform
from rpython.flowspace.model import summary
from rpython.rtyper.llinterp import LLException
from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.annlowlevel import llhelper
from rpython.rtyper.test.test_llinterp import get_interpreter
from rpython.rlib.objectmodel import llhelper_error_value, dont_inline
from rpython.translator.backendopt.all import backend_optimizations
from rpython.conftest import option
import sys
def check_debug_build():
# the 'not option.view' is because debug builds rarely
# have pygame, so if you want to see the graphs pass --view and
# don't be surprised when the test then passes when it shouldn't.
if not hasattr(sys, 'gettotalrefcount') and not option.view:
py.test.skip("test needs a debug build of Python")
_already_transformed = {}
def interpret(func, values):
interp, graph = get_interpreter(func, values)
t = interp.typer.annotator.translator
if t not in _already_transformed:
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.transform_completely()
_already_transformed[t] = True
return interp.eval_graph(graph, values)
class TestExceptionTransform:
def compile(self, fn, inputargs, **kwargs):
from rpython.translator.c.test.test_genc import compile
return compile(fn, inputargs, **kwargs)
def transform_func(self, fn, inputtypes, backendopt=False):
t = TranslationContext()
t.buildannotator().build_types(fn, inputtypes)
t.buildrtyper().specialize()
if option.view:
t.view()
if backendopt:
backend_optimizations(t)
g = graphof(t, fn)
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.create_exception_handling(g)
join_blocks(g)
if option.view:
t.view()
return t, g
def test_simple(self):
def one():
return 1
def foo():
one()
return one()
t, g = self.transform_func(foo, [])
assert len(list(g.iterblocks())) == 2 # graph does not change
result = interpret(foo, [])
assert result == 1
f = self.compile(foo, [])
assert f() == 1
def test_passthrough(self):
def one(x):
if x:
raise ValueError()
def foo():
one(0)
one(1)
t, g = self.transform_func(foo, [])
f = self.compile(foo, [])
f(expected_exception_name='ValueError')
def test_catches(self):
def one(x):
if x == 1:
raise ValueError()
elif x == 2:
raise TypeError()
return x - 5
def foo(x):
x = one(x)
try:
x = one(x)
except ValueError:
return 1 + x
except TypeError:
return 2 + x
except:
return 3 + x
return 4 + x
t, g = self.transform_func(foo, [int])
assert len(list(g.iterblocks())) == 10
f = self.compile(foo, [int])
result = interpret(foo, [6])
assert result == 2
result = f(6)
assert result == 2
result = interpret(foo, [7])
assert result == 4
result = f(7)
assert result == 4
result = interpret(foo, [8])
assert result == 2
result = f(8)
assert result == 2
def test_type_checking_catching_does_no_calls(self):
def one(x):
if x == 1:
raise ValueError()
elif x == 2:
raise TypeError()
return x - 5
def foo(x):
x = one(x)
try:
x = one(x)
except ValueError:
return 1 + x
except TypeError:
return 2 + x
except:
return 3 + x
return 4 + x
t, g = self.transform_func(foo, [int])
s = summary(g)
assert s['direct_call'] == 2 # two calls to one, 0 to ll_issubclass
assert s['int_between'] == 2 # subclass checks done directly
def test_bare_except(self):
def one(x):
if x == 1:
raise ValueError()
elif x == 2:
raise TypeError()
return x - 5
def foo(x):
x = one(x)
try:
x = one(x)
except:
return 1 + x
return 4 + x
t, g = self.transform_func(foo, [int])
assert len(list(g.iterblocks())) == 6
f = self.compile(foo, [int])
result = interpret(foo, [6])
assert result == 2
result = f(6)
assert result == 2
result = interpret(foo, [7])
assert result == 3
result = f(7)
assert result == 3
result = interpret(foo, [8])
assert result == 2
result = f(8)
assert result == 2
def test_raises(self):
def foo(x):
if x:
raise ValueError()
t, g = self.transform_func(foo, [int])
assert len(list(g.iterblocks())) == 3
f = self.compile(foo, [int])
f(0)
f(1, expected_exception_name='ValueError')
def test_no_multiple_transform(self):
def f(x):
return x + 1
t = TranslationContext()
t.buildannotator().build_types(f, [int])
t.buildrtyper().specialize()
g = graphof(t, f)
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.create_exception_handling(g)
etrafo2 = exceptiontransform.ExceptionTransformer(t)
py.test.raises(AssertionError, etrafo2.create_exception_handling, g)
def test_preserve_can_raise(self):
def f(x):
raise ValueError
t = TranslationContext()
t.buildannotator().build_types(f, [int])
t.buildrtyper().specialize()
g = graphof(t, f)
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.create_exception_handling(g)
assert etrafo.raise_analyzer.analyze_direct_call(g)
def test_reraise_is_not_raise(self):
def one(x):
if x == 1:
raise ValueError()
elif x == 2:
raise TypeError()
return x - 5
def foo(x):
try:
return one(x)
except ValueError:
return -42
t, g = self.transform_func(foo, [int])
for block in g.iterblocks():
for op in block.operations:
# the operation 'debug_record_traceback' should only show up
# in a normal raise, not in a reraise
assert op.opname != 'debug_record_traceback'
f = self.compile(foo, [int])
result = interpret(foo, [7])
assert result == 2
result = f(7)
assert result == 2
result = interpret(foo, [1])
assert result == -42
result = f(1)
assert result == -42
def test_needs_keepalive(self):
check_debug_build()
from rpython.rtyper.lltypesystem import lltype
X = lltype.GcStruct("X",
('y', lltype.Struct("Y", ('z', lltype.Signed))))
def can_raise(n):
if n:
raise Exception
else:
return 1
def foo(n):
x = lltype.malloc(X)
y = x.y
y.z = 42
r = can_raise(n)
return r + y.z
f = self.compile(foo, [int])
res = f(0)
assert res == 43
def test_inserting_zeroing_op(self):
from rpython.rtyper.lltypesystem import lltype
S = lltype.GcStruct("S", ('x', lltype.Signed))
def f(x):
s = lltype.malloc(S)
s.x = 0
return s.x
t = TranslationContext()
t.buildannotator().build_types(f, [int])
t.buildrtyper().specialize()
g = graphof(t, f)
etrafo = exceptiontransform.ExceptionTransformer(t)
etrafo.create_exception_handling(g)
ops = dict.fromkeys([o.opname for b, o in g.iterblockops()])
assert 'zero_gc_pointers_inside' in ops
def test_llexternal(self):
from rpython.rtyper.lltypesystem.rffi import llexternal
from rpython.rtyper.lltypesystem import lltype
z = llexternal('z', [lltype.Signed], lltype.Signed)
def f(x):
y = -1
if x > 0:
y = z(x)
return y + x
t,g = self.transform_func(f, [int], True)
# llexternals normally should not raise, the graph should have no exception
# checking
assert summary(g) == {'int_gt': 1, 'int_add': 1, 'direct_call': 1}
def test_get_exception_addr(self):
from rpython.rtyper.lltypesystem import lltype, llmemory
from rpython.rtyper.lltypesystem.lloperation import llop
def foo():
# a bit hard to test, really
a = llop.get_exception_addr(llmemory.Address)
assert lltype.typeOf(a) is llmemory.Address
a = llop.get_exc_value_addr(llmemory.Address)
assert lltype.typeOf(a) is llmemory.Address
return 42
f = self.compile(foo, [])
res = f()
assert res == 42
def test_default_error_value(self):
def foo(x):
if x == 42:
raise ValueError
return 123
result = interpret(foo, [1])
assert result == 123
with py.test.raises(LLException) as exc:
interpret(foo, [42])
assert exc.value.error_value == -1
assert 'ValueError' in str(exc.value)
def test_llhelper_error_value(self):
@llhelper_error_value(-456)
def _foo(x):
if x == 42:
raise ValueError
return x
FN = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
def bar(x):
foo = llhelper(FN, _foo)
return foo(x)
result = interpret(bar, [123])
assert result == 123
with py.test.raises(LLException) as exc:
interpret(bar, [42])
assert exc.value.error_value == -456
assert 'ValueError' in str(exc.value)
#
compiled_foo = self.compile(bar, [int], backendopt=False)
assert compiled_foo(123) == 123
compiled_foo(42, expected_exception_name='ValueError')
def test_enforce_llhelper_error_value_in_case_of_nested_exception(self):
@dont_inline
def my_divide(a, b):
if b == 0:
raise ZeroDivisionError
return a/b
@llhelper_error_value(-456)
def _foo(a, b):
res = my_divide(a, b)
return res
FN = lltype.Ptr(lltype.FuncType([lltype.Signed, lltype.Signed], lltype.Signed))
def bar(a, b):
foo = llhelper(FN, _foo)
return foo(a, b)
result = interpret(bar, [21, 3])
assert result == 7
with py.test.raises(LLException) as exc:
interpret(bar, [21, 0])
assert exc.value.error_value == -456
assert 'ZeroDivisionError' in str(exc.value)
#
compiled_foo = self.compile(bar, [int, int], backendopt=False)
assert compiled_foo(21, 3) == 7
compiled_foo(21, 0, expected_exception_name='ZeroDivisionError')
|