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
|
""" Tests some error handling routines
"""
from rpython.translator.translator import TranslationContext
from rpython.annotator.model import UnionError
import py
def compile_function(function, annotation=[]):
t = TranslationContext()
t.buildannotator().build_types(function, annotation)
class AAA(object):
pass
def test_someobject():
def someobject_degeneration(n):
if n == 3:
a = "a"
else:
a = 9
return a
py.test.raises(UnionError, compile_function, someobject_degeneration, [int])
def test_someobject2():
def someobject_deg(n):
if n == 3:
a = "a"
else:
return AAA()
return a
py.test.raises(UnionError, compile_function, someobject_deg, [int])
def test_eval_someobject():
exec("def f(n):\n if n == 2:\n return 'a'\n else:\n return 3")
py.test.raises(UnionError, compile_function, f, [int])
def test_someobject_from_call():
def one(x):
return str(x)
def two(x):
return int(x)
def fn(n):
if n:
to_call = one
else:
to_call = two
return to_call(n)
try:
compile_function(fn, [int])
except UnionError as e:
assert 'function one' in str(e)
assert 'function two' in str(e)
|