| 1 |
import py |
| 2 |
import sys |
| 3 |
from rpython.translator.c.test import test_typed |
| 4 |
from rpython.translator.c.test import test_backendoptimized |
| 5 |
from rpython.rtyper.lltypesystem import lltype |
| 6 |
from rpython.rlib.rarithmetic import ovfcheck |
| 7 |
|
| 8 |
getcompiled = test_typed.TestTypedTestCase().getcompiled |
| 9 |
getcompiledopt = test_backendoptimized.TestTypedOptimizedTestCase().getcompiled |
| 10 |
|
| 11 |
|
| 12 |
class InTestException(Exception): |
| 13 |
pass |
| 14 |
|
| 15 |
class MyException(Exception): |
| 16 |
pass |
| 17 |
|
| 18 |
def test_simple1(): |
| 19 |
def raise_(i): |
| 20 |
if i == 0: |
| 21 |
raise InTestException() |
| 22 |
elif i == 1: |
| 23 |
raise MyException() |
| 24 |
else: |
| 25 |
return 3 |
| 26 |
def fn(i): |
| 27 |
try: |
| 28 |
a = raise_(i) + 11 |
| 29 |
b = raise_(i) + 12 |
| 30 |
c = raise_(i) + 13 |
| 31 |
return a+b+c |
| 32 |
except InTestException: |
| 33 |
return 7 |
| 34 |
except MyException: |
| 35 |
return 123 |
| 36 |
except: |
| 37 |
return 22 |
| 38 |
return 66 |
| 39 |
f = getcompiled(fn, [int]) |
| 40 |
assert f(0) == fn(0) |
| 41 |
assert f(1) == fn(1) |
| 42 |
assert f(2) == fn(2) |
| 43 |
|
| 44 |
def test_implicit_index_error_lists(): |
| 45 |
def fn(n): |
| 46 |
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 47 |
try: |
| 48 |
return lst[n] |
| 49 |
except: |
| 50 |
return 2 |
| 51 |
f = getcompiled(fn, [int]) |
| 52 |
assert f(-1) == fn(-1) |
| 53 |
assert f( 0) == fn( 0) |
| 54 |
assert f(10) == fn(10) |
| 55 |
|
| 56 |
def test_myexception(): |
| 57 |
def g(): |
| 58 |
raise MyException |
| 59 |
def f(): |
| 60 |
try: |
| 61 |
g() |
| 62 |
except MyException: |
| 63 |
return 5 |
| 64 |
else: |
| 65 |
return 2 |
| 66 |
f1 = getcompiled(f, []) |
| 67 |
assert f1() == 5 |
| 68 |
|
| 69 |
def test_raise_outside_testfn(): |
| 70 |
def testfn(n): |
| 71 |
if n < 0: |
| 72 |
raise ValueError("hello") |
| 73 |
elif n > 0: |
| 74 |
raise MyException("world") |
| 75 |
else: |
| 76 |
return 0 |
| 77 |
f1 = getcompiled(testfn, [int]) |
| 78 |
f1(-1, expected_exception_name='ValueError') |
| 79 |
f1(1, expected_exception_name='MyException') |
| 80 |
|
| 81 |
def test_memoryerror(): |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
A = lltype.Array(lltype.Signed) |
| 94 |
S = lltype.GcStruct('S', ('a', lltype.Signed), |
| 95 |
('b', lltype.Signed), |
| 96 |
('c', lltype.Signed), |
| 97 |
('tail', A)) |
| 98 |
def g(n, tag): |
| 99 |
s = lltype.malloc(S, n) |
| 100 |
tag.a = 42 |
| 101 |
return s |
| 102 |
def testfn(n): |
| 103 |
tag = lltype.malloc(S, 0) |
| 104 |
try: |
| 105 |
s = g(n, tag) |
| 106 |
result = s.tail[n//2] |
| 107 |
except MemoryError: |
| 108 |
result = 1000 |
| 109 |
return result + tag.a |
| 110 |
f1 = getcompiled(testfn, [int]) |
| 111 |
assert f1(10) == 42 |
| 112 |
assert f1(sys.maxint) == 1000 |
| 113 |
for i in range(20): |
| 114 |
assert f1(int((sys.maxint+1) // 2 - i)) == 1000 |
| 115 |
assert f1(sys.maxint // 2 - 16384) == 1000 |
| 116 |
assert f1(sys.maxint // 2 + 16384) == 1000 |
| 117 |
|
| 118 |
def test_assert(): |
| 119 |
def testfn(n): |
| 120 |
assert n >= 0 |
| 121 |
|
| 122 |
f1 = getcompiled(testfn, [int]) |
| 123 |
res = f1(0) |
| 124 |
assert res is None, repr(res) |
| 125 |
res = f1(42) |
| 126 |
assert res is None, repr(res) |
| 127 |
f1(-2, expected_exception_name='AssertionError') |
| 128 |
|
| 129 |
|
| 130 |
def test_reraise_exception(): |
| 131 |
class A(Exception): |
| 132 |
pass |
| 133 |
|
| 134 |
def raise_something(n): |
| 135 |
if n > 10: |
| 136 |
raise A |
| 137 |
else: |
| 138 |
raise Exception |
| 139 |
|
| 140 |
def foo(n): |
| 141 |
try: |
| 142 |
raise_something(n) |
| 143 |
except A: |
| 144 |
raise |
| 145 |
except Exception as e: |
| 146 |
return 100 |
| 147 |
return -1 |
| 148 |
|
| 149 |
def fn(n): |
| 150 |
try: |
| 151 |
return foo(n) |
| 152 |
except A: |
| 153 |
return 42 |
| 154 |
|
| 155 |
f1 = getcompiledopt(fn, [int]) |
| 156 |
res = f1(100) |
| 157 |
assert res == 42 |
| 158 |
res = f1(0) |
| 159 |
assert res == 100 |
| 160 |
|
| 161 |
def test_dict_keyerror_inside_try_finally(): |
| 162 |
class CtxMgr: |
| 163 |
def __enter__(self): |
| 164 |
return 42 |
| 165 |
def __exit__(self, *args): |
| 166 |
pass |
| 167 |
def fn(x): |
| 168 |
d = {5: x} |
| 169 |
with CtxMgr() as forty_two: |
| 170 |
try: |
| 171 |
return d[x] |
| 172 |
except KeyError: |
| 173 |
return forty_two |
| 174 |
f1 = getcompiledopt(fn, [int]) |
| 175 |
res = f1(100) |
| 176 |
assert res == 42 |
| 177 |
|
| 178 |
def test_getitem_custom_exception(): |
| 179 |
class MyError(Exception): |
| 180 |
pass |
| 181 |
class BadContainer(object): |
| 182 |
def __getitem__(self, n): |
| 183 |
raise MyError |
| 184 |
def f(): |
| 185 |
d = BadContainer() |
| 186 |
try: |
| 187 |
return d[0] |
| 188 |
except KeyError: |
| 189 |
return 1 |
| 190 |
def g(): |
| 191 |
try: |
| 192 |
return f() |
| 193 |
except MyError: |
| 194 |
return -1 |
| 195 |
|
| 196 |
assert g() == -1 |
| 197 |
compiled = getcompiled(g, []) |
| 198 |
assert compiled() == -1 |
| 199 |
|
| 200 |
def test_ovf_propagation(): |
| 201 |
def div(a, b): |
| 202 |
try: |
| 203 |
return ovfcheck(a//b) |
| 204 |
except ZeroDivisionError: |
| 205 |
raise |
| 206 |
def f(): |
| 207 |
div(4, 2) |
| 208 |
try: |
| 209 |
return div(-sys.maxint-1, -1) |
| 210 |
except OverflowError: |
| 211 |
return 0 |
| 212 |
assert f() == 0 |
| 213 |
compiled = getcompiled(f, []) |
| 214 |
assert compiled() == 0 |