| 1 | |
| 2 | """ Tests some error handling routines |
| 3 | """ |
| 4 | |
| 5 | from rpython.translator.translator import TranslationContext |
| 6 | from rpython.annotator.model import UnionError |
| 7 | |
| 8 | import py |
| 9 | |
| 10 | def compile_function(function, annotation=[]): |
| 11 | t = TranslationContext() |
| 12 | t.buildannotator().build_types(function, annotation) |
| 13 | |
| 14 | class AAA(object): |
| 15 | pass |
| 16 | |
| 17 | def test_someobject(): |
| 18 | def someobject_degeneration(n): |
| 19 | if n == 3: |
| 20 | a = "a" |
| 21 | else: |
| 22 | a = 9 |
| 23 | return a |
| 24 | |
| 25 | py.test.raises(UnionError, compile_function, someobject_degeneration, [int]) |
| 26 | |
| 27 | def test_someobject2(): |
| 28 | def someobject_deg(n): |
| 29 | if n == 3: |
| 30 | a = "a" |
| 31 | else: |
| 32 | return AAA() |
| 33 | return a |
| 34 | |
| 35 | py.test.raises(UnionError, compile_function, someobject_deg, [int]) |
| 36 | |
| 37 | def test_eval_someobject(): |
| 38 | exec("def f(n):\n if n == 2:\n return 'a'\n else:\n return 3") |
| 39 | |
| 40 | py.test.raises(UnionError, compile_function, f, [int]) |
| 41 | |
| 42 | def test_someobject_from_call(): |
| 43 | def one(x): |
| 44 | return str(x) |
| 45 | |
| 46 | def two(x): |
| 47 | return int(x) |
| 48 | |
| 49 | def fn(n): |
| 50 | if n: |
| 51 | to_call = one |
| 52 | else: |
| 53 | to_call = two |
| 54 | return to_call(n) |
| 55 | |
| 56 | try: |
| 57 | compile_function(fn, [int]) |
| 58 | except UnionError as e: |
| 59 | assert 'function one' in str(e) |
| 60 | assert 'function two' in str(e) |