| 1 |
import py |
| 2 |
|
| 3 |
from rpython.translator.translator import TranslationContext |
| 4 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 5 |
from rpython.rtyper.llinterp import LLException |
| 6 |
from rpython.rtyper.error import MissingRTypeOperation, TyperError |
| 7 |
|
| 8 |
|
| 9 |
class MyException(Exception): |
| 10 |
pass |
| 11 |
|
| 12 |
class MyStrangeException: |
| 13 |
pass |
| 14 |
|
| 15 |
def rtype(fn, argtypes=[]): |
| 16 |
t = TranslationContext() |
| 17 |
t.buildannotator().build_types(fn, argtypes) |
| 18 |
typer = t.buildrtyper() |
| 19 |
typer.specialize() |
| 20 |
|
| 21 |
t.checkgraphs() |
| 22 |
return t |
| 23 |
|
| 24 |
def test_simple(): |
| 25 |
def g(): |
| 26 |
raise MyException |
| 27 |
def dummyfn(): |
| 28 |
try: |
| 29 |
return g() |
| 30 |
except MyException: |
| 31 |
pass |
| 32 |
|
| 33 |
rtype(dummyfn) |
| 34 |
|
| 35 |
|
| 36 |
class TestException(BaseRtypingTest): |
| 37 |
def test_exception_with_arg(self): |
| 38 |
def g(n): |
| 39 |
raise IOError("test") |
| 40 |
def h(n): |
| 41 |
raise OSError(n, "?", None) |
| 42 |
def i(n): |
| 43 |
raise EnvironmentError(n, "?", "test") |
| 44 |
def j(n): |
| 45 |
raise IOError(0, "test") |
| 46 |
def k(n): |
| 47 |
raise OSError |
| 48 |
def f(n): |
| 49 |
try: |
| 50 |
g(n) |
| 51 |
except IOError as e: |
| 52 |
assert e.errno == 0 |
| 53 |
assert e.strerror == "test" |
| 54 |
assert e.filename is None |
| 55 |
else: |
| 56 |
assert False |
| 57 |
try: |
| 58 |
h(n) |
| 59 |
except OSError as e: |
| 60 |
assert e.errno == 42 |
| 61 |
assert e.strerror == "?" |
| 62 |
assert e.filename is None |
| 63 |
else: |
| 64 |
assert False |
| 65 |
try: |
| 66 |
i(n) |
| 67 |
except EnvironmentError as e: |
| 68 |
assert e.errno == 42 |
| 69 |
assert e.strerror == "?" |
| 70 |
assert e.filename == "test" |
| 71 |
else: |
| 72 |
assert False |
| 73 |
try: |
| 74 |
j(n) |
| 75 |
except (IOError, OSError) as e: |
| 76 |
assert e.errno == 0 |
| 77 |
assert e.strerror == "test" |
| 78 |
assert e.filename is None |
| 79 |
try: |
| 80 |
k(n) |
| 81 |
except EnvironmentError as e: |
| 82 |
assert e.errno == 0 |
| 83 |
assert e.strerror is None |
| 84 |
assert e.filename is None |
| 85 |
self.interpret(f, [42]) |
| 86 |
|
| 87 |
def test_catch_incompatible_class(self): |
| 88 |
class MyError(Exception): |
| 89 |
pass |
| 90 |
def h(x): |
| 91 |
pass |
| 92 |
def f(n): |
| 93 |
try: |
| 94 |
assert n < 10 |
| 95 |
except MyError as operr: |
| 96 |
h(operr) |
| 97 |
res = self.interpret(f, [7]) |
| 98 |
assert res is None |
| 99 |
|
| 100 |
def test_raise_and_catch_other(self): |
| 101 |
class BytecodeCorruption(Exception): |
| 102 |
pass |
| 103 |
class OperationError(Exception): |
| 104 |
def __init__(self, a): |
| 105 |
self.a = a |
| 106 |
def f(next_instr): |
| 107 |
if next_instr < 7: |
| 108 |
raise OperationError(next_instr) |
| 109 |
try: |
| 110 |
raise BytecodeCorruption() |
| 111 |
except OperationError as operr: |
| 112 |
next_instr -= operr.a |
| 113 |
py.test.raises(LLException, self.interpret, f, [10]) |
| 114 |
|
| 115 |
def test_raise_prebuilt_and_catch_other(self): |
| 116 |
class BytecodeCorruption(Exception): |
| 117 |
pass |
| 118 |
class OperationError(Exception): |
| 119 |
def __init__(self, a): |
| 120 |
self.a = a |
| 121 |
bcerr = BytecodeCorruption() |
| 122 |
def f(next_instr): |
| 123 |
if next_instr < 7: |
| 124 |
raise OperationError(next_instr) |
| 125 |
try: |
| 126 |
raise bcerr |
| 127 |
except OperationError as operr: |
| 128 |
next_instr -= operr.a |
| 129 |
py.test.raises(LLException, self.interpret, f, [10]) |
| 130 |
|
| 131 |
def test_catch_KeyboardInterrupt(self): |
| 132 |
def g(n): |
| 133 |
return n |
| 134 |
def f(n): |
| 135 |
try: |
| 136 |
return g(n) |
| 137 |
except KeyboardInterrupt: |
| 138 |
return -1 |
| 139 |
res = self.interpret(f, [11]) |
| 140 |
assert res == 11 |
| 141 |
|
| 142 |
def test_pop_inside_try(self): |
| 143 |
class MyException(Exception): |
| 144 |
pass |
| 145 |
def f(n): |
| 146 |
lst = [] |
| 147 |
lst.append(n) |
| 148 |
try: |
| 149 |
return lst.pop() |
| 150 |
except MyException: |
| 151 |
return -1 |
| 152 |
res = self.interpret(f, [42]) |
| 153 |
assert res == 42 |
| 154 |
|
| 155 |
def test_cannot_raise_ll_exception(self): |
| 156 |
from rpython.rtyper.annlowlevel import cast_instance_to_base_ptr |
| 157 |
def g(): |
| 158 |
e = OverflowError() |
| 159 |
lle = cast_instance_to_base_ptr(e) |
| 160 |
raise lle |
| 161 |
def f(): |
| 162 |
try: |
| 163 |
g() |
| 164 |
except OverflowError: |
| 165 |
return 42 |
| 166 |
py.test.raises(MissingRTypeOperation, self.interpret, f, []) |
| 167 |
|
| 168 |
def test_cannot_raise_something_annotated_as_none(self): |
| 169 |
def g(): |
| 170 |
return None |
| 171 |
def f(): |
| 172 |
raise g() |
| 173 |
py.test.raises(TyperError, rtype, f) |