| 1 |
import py |
| 2 |
from rpython.rtyper.rtuple import TUPLE_TYPE, TupleRepr |
| 3 |
from rpython.rtyper.lltypesystem.lltype import Signed, Bool |
| 4 |
from rpython.rtyper.rbool import bool_repr |
| 5 |
from rpython.rtyper.rint import signed_repr |
| 6 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 7 |
from rpython.rtyper.error import TyperError |
| 8 |
from rpython.rlib.objectmodel import compute_hash |
| 9 |
from rpython.translator.translator import TranslationContext |
| 10 |
|
| 11 |
|
| 12 |
def test_rtuple(): |
| 13 |
rtuple = TupleRepr(None, [signed_repr, bool_repr]) |
| 14 |
assert rtuple.lowleveltype == TUPLE_TYPE([Signed, Bool]) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class TestRtuple(BaseRtypingTest): |
| 19 |
|
| 20 |
def test_simple(self): |
| 21 |
def dummyfn(x): |
| 22 |
l = (10, x, 30) |
| 23 |
return l[2] |
| 24 |
res = self.interpret(dummyfn, [4]) |
| 25 |
assert res == 30 |
| 26 |
|
| 27 |
def test_len(self): |
| 28 |
def dummyfn(x): |
| 29 |
l = (5,x) |
| 30 |
return len(l) |
| 31 |
res = self.interpret(dummyfn, [4]) |
| 32 |
assert res == 2 |
| 33 |
|
| 34 |
def test_return_tuple(self): |
| 35 |
def dummyfn(x, y): |
| 36 |
return (x<y, x>y) |
| 37 |
res = self.interpret(dummyfn, [4,5]) |
| 38 |
assert res.item0 == True |
| 39 |
assert res.item1 == False |
| 40 |
|
| 41 |
def test_tuple_concatenation(self): |
| 42 |
def f(n): |
| 43 |
tup = (1,n) |
| 44 |
tup1 = (3,) |
| 45 |
res = tup + tup1 + () |
| 46 |
return res[0]*100 + res[1]*10 + res[2] |
| 47 |
res = self.interpret(f, [2]) |
| 48 |
assert res == 123 |
| 49 |
|
| 50 |
def test_tuple_concatenation_mix(self): |
| 51 |
def f(n): |
| 52 |
tup = (1,n) |
| 53 |
tup1 = ('3',) |
| 54 |
res = tup + tup1 |
| 55 |
return res[0]*100 + res[1]*10 + ord(res[2]) - ord('0') |
| 56 |
res = self.interpret(f, [2]) |
| 57 |
assert res == 123 |
| 58 |
|
| 59 |
def test_constant_tuple_contains(self): |
| 60 |
def f(i): |
| 61 |
t1 = (1, 2, 3, 4) |
| 62 |
return i in t1 |
| 63 |
res = self.interpret(f, [3]) |
| 64 |
assert res is True |
| 65 |
res = self.interpret(f, [0]) |
| 66 |
assert res is False |
| 67 |
|
| 68 |
def test_constant_tuple_contains2(self): |
| 69 |
def t1(): |
| 70 |
return (1,2,3,4) |
| 71 |
def f(i): |
| 72 |
return i in t1() |
| 73 |
res = self.interpret(f, [3]) |
| 74 |
assert res is True |
| 75 |
res = self.interpret(f, [0]) |
| 76 |
assert res is False |
| 77 |
|
| 78 |
def test_constant_tuple_contains3(self): |
| 79 |
def f(i): |
| 80 |
return i in () |
| 81 |
res = self.interpret(f, [3]) |
| 82 |
assert res is False |
| 83 |
|
| 84 |
def test_constant_tuple_contains4(self): |
| 85 |
def f(i): |
| 86 |
return i in (3,) |
| 87 |
res = self.interpret(f, [3]) |
| 88 |
assert res is True |
| 89 |
res = self.interpret(f, [4]) |
| 90 |
assert res is False |
| 91 |
|
| 92 |
def test_constant_unichar_tuple_contains(self): |
| 93 |
def f(i): |
| 94 |
return unichr(i) in (u'1', u'9') |
| 95 |
res = self.interpret(f, [49]) |
| 96 |
assert res is True |
| 97 |
res = self.interpret(f, [50]) |
| 98 |
assert res is False |
| 99 |
|
| 100 |
def test_constant_tuple_contains_bug(self): |
| 101 |
def f(i): |
| 102 |
return chr(i) in ('1', '2', '34') |
| 103 |
res = self.interpret(f, [ord('1')]) |
| 104 |
assert res is True |
| 105 |
res = self.interpret(f, [ord('3')]) |
| 106 |
assert res is False |
| 107 |
|
| 108 |
def test_conv(self): |
| 109 |
def t0(): |
| 110 |
return (3, 2, None) |
| 111 |
def t1(): |
| 112 |
return (7, 2, "xy") |
| 113 |
def f(i): |
| 114 |
if i == 1: |
| 115 |
return t1() |
| 116 |
else: |
| 117 |
return t0() |
| 118 |
|
| 119 |
res = self.interpret(f, [1]) |
| 120 |
assert res.item0 == 7 |
| 121 |
assert self.ll_to_string(res.item2) == "xy" |
| 122 |
|
| 123 |
res = self.interpret(f, [0]) |
| 124 |
assert res.item0 == 3 |
| 125 |
assert not res.item2 |
| 126 |
|
| 127 |
def test_constant_tuples_shared(self): |
| 128 |
def g(n): |
| 129 |
x = (n, 42) |
| 130 |
y = (5, 42) |
| 131 |
z = x + () |
| 132 |
return x, y, z |
| 133 |
def f(): |
| 134 |
return g(5) |
| 135 |
res = self.interpret(f, []) |
| 136 |
assert res.item0 == res.item1 == res.item2 |
| 137 |
|
| 138 |
def test_inst_tuple_getitem(self): |
| 139 |
class A: |
| 140 |
pass |
| 141 |
class B(A): |
| 142 |
pass |
| 143 |
|
| 144 |
def f(i): |
| 145 |
if i: |
| 146 |
x = (1, A()) |
| 147 |
else: |
| 148 |
x = (1, B()) |
| 149 |
return x[1] |
| 150 |
|
| 151 |
res = self.interpret(f, [0]) |
| 152 |
assert self.class_name(res) == "B" |
| 153 |
|
| 154 |
def test_inst_tuple_add_getitem(self): |
| 155 |
class A: |
| 156 |
pass |
| 157 |
class B(A): |
| 158 |
pass |
| 159 |
|
| 160 |
def f(i): |
| 161 |
x = (1, A()) |
| 162 |
y = (2, B()) |
| 163 |
if i: |
| 164 |
z = x + y |
| 165 |
else: |
| 166 |
z = y + x |
| 167 |
return z[1] |
| 168 |
|
| 169 |
res = self.interpret(f, [1]) |
| 170 |
assert self.class_name(res) == "A" |
| 171 |
|
| 172 |
res = self.interpret(f, [0]) |
| 173 |
assert self.class_name(res) == "B" |
| 174 |
|
| 175 |
def test_type_erase(self): |
| 176 |
class A(object): |
| 177 |
pass |
| 178 |
class B(object): |
| 179 |
pass |
| 180 |
|
| 181 |
def f(): |
| 182 |
return (A(), B()), (B(), A()) |
| 183 |
|
| 184 |
t = TranslationContext() |
| 185 |
s = t.buildannotator().build_types(f, []) |
| 186 |
rtyper = t.buildrtyper() |
| 187 |
rtyper.specialize() |
| 188 |
|
| 189 |
s_AB_tup = s.items[0] |
| 190 |
s_BA_tup = s.items[1] |
| 191 |
|
| 192 |
r_AB_tup = rtyper.getrepr(s_AB_tup) |
| 193 |
r_BA_tup = rtyper.getrepr(s_AB_tup) |
| 194 |
|
| 195 |
assert r_AB_tup.lowleveltype == r_BA_tup.lowleveltype |
| 196 |
|
| 197 |
def test_tuple_hash(self): |
| 198 |
def f(i, j): |
| 199 |
return compute_hash((i, j)) |
| 200 |
|
| 201 |
res1 = self.interpret(f, [12, 27]) |
| 202 |
res2 = self.interpret(f, [27, 12]) |
| 203 |
assert res1 != res2 |
| 204 |
|
| 205 |
def test_constant_tuple_hash_str(self): |
| 206 |
def f(i): |
| 207 |
if i: |
| 208 |
t = (None, "abc") |
| 209 |
else: |
| 210 |
t = ("abc", None) |
| 211 |
return compute_hash(t) |
| 212 |
|
| 213 |
res1 = self.interpret(f, [0]) |
| 214 |
res2 = self.interpret(f, [1]) |
| 215 |
assert res1 != res2 |
| 216 |
|
| 217 |
def test_tuple_to_list(self): |
| 218 |
def f(i, j): |
| 219 |
return list((i, j)) |
| 220 |
|
| 221 |
res = self.interpret(f, [2, 3]) |
| 222 |
assert self.ll_to_list(res) == [2, 3] |
| 223 |
|
| 224 |
def test_tuple_iterator_length1(self): |
| 225 |
def f(i): |
| 226 |
total = 0 |
| 227 |
for x in (i,): |
| 228 |
total += x |
| 229 |
return total |
| 230 |
res = self.interpret(f, [93813]) |
| 231 |
assert res == 93813 |
| 232 |
|
| 233 |
def test_tuple_iterator_reversed_unsupported(self): |
| 234 |
def f(i): |
| 235 |
total = 0 |
| 236 |
t = (i,) |
| 237 |
for x in reversed(t): |
| 238 |
total += x |
| 239 |
return total |
| 240 |
py.test.raises(TyperError, self.interpret, f, [93813]) |
| 241 |
|
| 242 |
def test_inst_tuple_iter(self): |
| 243 |
class A: |
| 244 |
pass |
| 245 |
class B(A): |
| 246 |
pass |
| 247 |
|
| 248 |
def f(i): |
| 249 |
if i: |
| 250 |
x = (A(),) |
| 251 |
else: |
| 252 |
x = (B(),) |
| 253 |
l = None |
| 254 |
for y in x: |
| 255 |
l = y |
| 256 |
return l |
| 257 |
|
| 258 |
res = self.interpret(f, [0]) |
| 259 |
assert self.class_name(res) == "B" |
| 260 |
|
| 261 |
def test_access_in_try(self): |
| 262 |
def f(sq): |
| 263 |
try: |
| 264 |
return sq[2] |
| 265 |
except ZeroDivisionError: |
| 266 |
return 42 |
| 267 |
return -1 |
| 268 |
def g(n): |
| 269 |
t = (1,2,n) |
| 270 |
return f(t) |
| 271 |
res = self.interpret(g, [3]) |
| 272 |
assert res == 3 |
| 273 |
|
| 274 |
def test_void_items(self): |
| 275 |
def f(): |
| 276 |
return 6 |
| 277 |
def getf(): |
| 278 |
return f |
| 279 |
def g(): |
| 280 |
f1 = getf() |
| 281 |
return (f1, 12) |
| 282 |
def example(): |
| 283 |
return g()[0]() |
| 284 |
res = self.interpret(example, []) |
| 285 |
assert res == 6 |
| 286 |
|
| 287 |
def test_empty_tuple(self): |
| 288 |
def f(): |
| 289 |
lst = [(), (), ()] |
| 290 |
res = [] |
| 291 |
for x in lst: |
| 292 |
res.append(list(x)) |
| 293 |
assert res[0] == res[1] == res[2] == [] |
| 294 |
self.interpret(f, []) |
| 295 |
|
| 296 |
def test_slice(self): |
| 297 |
def g(n): |
| 298 |
t = (1.5, "hello", n) |
| 299 |
return t[1:] + t[:-1] + t[12:] + t[0:2] |
| 300 |
def f(n): |
| 301 |
res = g(n) |
| 302 |
assert len(res) == 6 |
| 303 |
assert res[0] == "hello" |
| 304 |
assert res[1] == n |
| 305 |
assert res[2] == 1.5 |
| 306 |
assert res[3] == "hello" |
| 307 |
assert res[4] == 1.5 |
| 308 |
assert res[5] == "hello" |
| 309 |
self.interpret(f, [9]) |
| 310 |
|
| 311 |
def test_tuple_eq(self): |
| 312 |
def f(n): |
| 313 |
return (n, 6) == (3, n*2) |
| 314 |
res = self.interpret(f, [3]) |
| 315 |
assert res is True |
| 316 |
res = self.interpret(f, [2]) |
| 317 |
assert res is False |
| 318 |
|
| 319 |
def test_tuple_ne(self): |
| 320 |
def f(n): |
| 321 |
return (n, 6) != (3, n*2) |
| 322 |
res = self.interpret(f, [3]) |
| 323 |
assert res is False |
| 324 |
res = self.interpret(f, [2]) |
| 325 |
assert res is True |
| 326 |
|
| 327 |
def test_tuple_eq_list(self): |
| 328 |
def f(n): |
| 329 |
lst1 = [1, n] |
| 330 |
lst1.append(3) |
| 331 |
return (lst1, 52) == ([1, 53, 3], n-1) |
| 332 |
res = self.interpret(f, [53]) |
| 333 |
assert res is True |
| 334 |
|
| 335 |
def test_compare_list_char_str(self): |
| 336 |
def fn(i, j): |
| 337 |
t1 = ([str(i)],) |
| 338 |
t2 = ([chr(j)],) |
| 339 |
return t1 == t2 |
| 340 |
res = self.interpret(fn, [65, 65]) |
| 341 |
assert res is False |
| 342 |
res = self.interpret(fn, [1, 49]) |
| 343 |
assert res is True |
| 344 |
|
| 345 |
def test_tuple_hash_2(self): |
| 346 |
def f(n): |
| 347 |
return compute_hash((n, 6)) == compute_hash((3, n*2)) |
| 348 |
res = self.interpret(f, [3]) |
| 349 |
assert res is True |
| 350 |
|
| 351 |
def test_tuple_str(self): |
| 352 |
def f(n): |
| 353 |
assert str(()) == "()" |
| 354 |
assert str((n,)) == "(%d,)" % n |
| 355 |
assert str((n, 6)) == "(%d, 6)" % n |
| 356 |
assert str(((n,),)) == "((%d,),)" % n |
| 357 |
self.interpret(f, [3]) |