| 1 |
import py |
| 2 |
|
| 3 |
from rpython.annotator import model as annmodel |
| 4 |
from rpython.annotator import specialize |
| 5 |
from rpython.rtyper.lltypesystem.lltype import typeOf |
| 6 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 7 |
from rpython.rtyper.llannotation import SomePtr, lltype_to_annotation |
| 8 |
|
| 9 |
|
| 10 |
class MyBase: |
| 11 |
def m(self, x): |
| 12 |
return self.z + x |
| 13 |
|
| 14 |
class MySubclass(MyBase): |
| 15 |
def m(self, x): |
| 16 |
return self.z - x |
| 17 |
|
| 18 |
class MyStrangerSubclass(MyBase): |
| 19 |
def m(self, x, y): |
| 20 |
return x*y |
| 21 |
|
| 22 |
class MyBaseWithInit: |
| 23 |
def __init__(self, a): |
| 24 |
self.a1 = a |
| 25 |
|
| 26 |
class MySubclassWithInit(MyBaseWithInit): |
| 27 |
def __init__(self, a, b): |
| 28 |
MyBaseWithInit.__init__(self, a) |
| 29 |
self.b1 = b |
| 30 |
|
| 31 |
class MySubclassWithoutInit(MyBaseWithInit): |
| 32 |
pass |
| 33 |
|
| 34 |
class MySubclassWithoutMethods(MyBase): |
| 35 |
pass |
| 36 |
|
| 37 |
|
| 38 |
class Freezing: |
| 39 |
def _freeze_(self): |
| 40 |
return True |
| 41 |
def mymethod(self, y): |
| 42 |
return self.x + y |
| 43 |
|
| 44 |
|
| 45 |
class TestRPBC(BaseRtypingTest): |
| 46 |
|
| 47 |
def test_easy_call(self): |
| 48 |
def f(x): |
| 49 |
return x+1 |
| 50 |
def g(y): |
| 51 |
return f(y+2) |
| 52 |
res = self.interpret(g, [5]) |
| 53 |
assert res == 8 |
| 54 |
|
| 55 |
def test_multiple_call(self): |
| 56 |
def f1(x): |
| 57 |
return x+1 |
| 58 |
def f2(x): |
| 59 |
return x+2 |
| 60 |
def g(y): |
| 61 |
if y < 0: |
| 62 |
f = f1 |
| 63 |
else: |
| 64 |
f = f2 |
| 65 |
return f(y+3) |
| 66 |
res = self.interpret(g, [-1]) |
| 67 |
assert res == 3 |
| 68 |
res = self.interpret(g, [1]) |
| 69 |
assert res == 6 |
| 70 |
|
| 71 |
def test_function_is_null(self): |
| 72 |
def f1(x): |
| 73 |
return x+1 |
| 74 |
def f2(x): |
| 75 |
return x+2 |
| 76 |
def g(x): |
| 77 |
if x < 0: |
| 78 |
return None |
| 79 |
elif x == 0: |
| 80 |
return f2 |
| 81 |
else: |
| 82 |
return f1 |
| 83 |
def fn(x): |
| 84 |
func = g(x) |
| 85 |
if func: |
| 86 |
return 42 |
| 87 |
else: |
| 88 |
return 43 |
| 89 |
assert self.interpret(fn, [1]) == 42 |
| 90 |
assert self.interpret(fn, [-1]) == 43 |
| 91 |
|
| 92 |
def test_method_call(self): |
| 93 |
def f(a, b): |
| 94 |
obj = MyBase() |
| 95 |
obj.z = a |
| 96 |
return obj.m(b) |
| 97 |
res = self.interpret(f, [4, 5]) |
| 98 |
assert res == 9 |
| 99 |
|
| 100 |
def test_virtual_method_call(self): |
| 101 |
def f(a, b): |
| 102 |
if a > 0: |
| 103 |
obj = MyBase() |
| 104 |
else: |
| 105 |
obj = MySubclass() |
| 106 |
obj.z = a |
| 107 |
return obj.m(b) |
| 108 |
res = self.interpret(f, [1, 2.3]) |
| 109 |
assert res == 3.3 |
| 110 |
res = self.interpret(f, [-1, 2.3]) |
| 111 |
assert res == -3.3 |
| 112 |
|
| 113 |
def test_stranger_subclass_1(self): |
| 114 |
def f1(): |
| 115 |
obj = MyStrangerSubclass() |
| 116 |
obj.z = 100 |
| 117 |
return obj.m(6, 7) |
| 118 |
res = self.interpret(f1, []) |
| 119 |
assert res == 42 |
| 120 |
|
| 121 |
def test_stranger_subclass_2(self): |
| 122 |
def f2(): |
| 123 |
obj = MyStrangerSubclass() |
| 124 |
obj.z = 100 |
| 125 |
return obj.m(6, 7) + MyBase.m(obj, 58) |
| 126 |
res = self.interpret(f2, []) |
| 127 |
assert res == 200 |
| 128 |
|
| 129 |
|
| 130 |
def test_class_init(self): |
| 131 |
def f(a): |
| 132 |
instance = MyBaseWithInit(a) |
| 133 |
return instance.a1 |
| 134 |
assert self.interpret(f, [5]) == 5 |
| 135 |
|
| 136 |
def test_class_init_2(self): |
| 137 |
def f(a, b): |
| 138 |
instance = MySubclassWithInit(a, b) |
| 139 |
return instance.a1 * instance.b1 |
| 140 |
assert self.interpret(f, [6, 7]) == 42 |
| 141 |
|
| 142 |
def test_class_calling_init(self): |
| 143 |
def f(): |
| 144 |
instance = MySubclassWithInit(1, 2) |
| 145 |
instance.__init__(3, 4) |
| 146 |
return instance.a1 * instance.b1 |
| 147 |
assert self.interpret(f, []) == 12 |
| 148 |
|
| 149 |
def test_class_init_w_kwds(self): |
| 150 |
def f(a): |
| 151 |
instance = MyBaseWithInit(a=a) |
| 152 |
return instance.a1 |
| 153 |
assert self.interpret(f, [5]) == 5 |
| 154 |
|
| 155 |
def test_class_init_2_w_kwds(self): |
| 156 |
def f(a, b): |
| 157 |
instance = MySubclassWithInit(a, b=b) |
| 158 |
return instance.a1 * instance.b1 |
| 159 |
assert self.interpret(f, [6, 7]) == 42 |
| 160 |
|
| 161 |
def test_class_init_inherited(self): |
| 162 |
def f(a): |
| 163 |
instance = MySubclassWithoutInit(a) |
| 164 |
return instance.a1 |
| 165 |
assert self.interpret(f, [42]) == 42 |
| 166 |
|
| 167 |
def test_class_method_inherited(self): |
| 168 |
|
| 169 |
|
| 170 |
class Task: |
| 171 |
def waitTask(self, a): |
| 172 |
return a+1 |
| 173 |
|
| 174 |
def fn(self, a): |
| 175 |
raise NotImplementedError |
| 176 |
|
| 177 |
def runTask(self, a): |
| 178 |
return self.fn(a) |
| 179 |
|
| 180 |
class HandlerTask(Task): |
| 181 |
def fn(self, a): |
| 182 |
return self.waitTask(a)+2 |
| 183 |
|
| 184 |
class DeviceTask(Task): |
| 185 |
def fn(self, a): |
| 186 |
return self.waitTask(a)+3 |
| 187 |
|
| 188 |
def f(a, b): |
| 189 |
if b: |
| 190 |
inst = HandlerTask() |
| 191 |
else: |
| 192 |
inst = DeviceTask() |
| 193 |
|
| 194 |
return inst.runTask(a) |
| 195 |
|
| 196 |
assert self.interpret(f, [42, True]) == 45 |
| 197 |
assert self.interpret(f, [42, False]) == 46 |
| 198 |
|
| 199 |
def test_freezing(self): |
| 200 |
fr1 = Freezing() |
| 201 |
fr2 = Freezing() |
| 202 |
fr1.x = 5 |
| 203 |
fr2.x = 6 |
| 204 |
def g(fr): |
| 205 |
return fr.x |
| 206 |
def f(n): |
| 207 |
if n > 0: |
| 208 |
fr = fr1 |
| 209 |
elif n < 0: |
| 210 |
fr = fr2 |
| 211 |
else: |
| 212 |
fr = None |
| 213 |
return g(fr) |
| 214 |
res = self.interpret(f, [1]) |
| 215 |
assert res == 5 |
| 216 |
res = self.interpret(f, [-1]) |
| 217 |
assert res == 6 |
| 218 |
|
| 219 |
def test_call_frozen_pbc_simple(self): |
| 220 |
fr1 = Freezing() |
| 221 |
fr1.x = 5 |
| 222 |
def f(n): |
| 223 |
return fr1.mymethod(n) |
| 224 |
res = self.interpret(f, [6]) |
| 225 |
assert res == 11 |
| 226 |
|
| 227 |
def test_call_frozen_pbc_simple_w_kwds(self): |
| 228 |
fr1 = Freezing() |
| 229 |
fr1.x = 5 |
| 230 |
def f(n): |
| 231 |
return fr1.mymethod(y=n) |
| 232 |
res = self.interpret(f, [6]) |
| 233 |
assert res == 11 |
| 234 |
|
| 235 |
def test_call_frozen_pbc_multiple(self): |
| 236 |
fr1 = Freezing() |
| 237 |
fr2 = Freezing() |
| 238 |
fr1.x = 5 |
| 239 |
fr2.x = 6 |
| 240 |
def f(n): |
| 241 |
if n > 0: |
| 242 |
fr = fr1 |
| 243 |
else: |
| 244 |
fr = fr2 |
| 245 |
return fr.mymethod(n) |
| 246 |
res = self.interpret(f, [1]) |
| 247 |
assert res == 6 |
| 248 |
res = self.interpret(f, [-1]) |
| 249 |
assert res == 5 |
| 250 |
|
| 251 |
def test_call_frozen_pbc_multiple_w_kwds(self): |
| 252 |
fr1 = Freezing() |
| 253 |
fr2 = Freezing() |
| 254 |
fr1.x = 5 |
| 255 |
fr2.x = 6 |
| 256 |
def f(n): |
| 257 |
if n > 0: |
| 258 |
fr = fr1 |
| 259 |
else: |
| 260 |
fr = fr2 |
| 261 |
return fr.mymethod(y=n) |
| 262 |
res = self.interpret(f, [1]) |
| 263 |
assert res == 6 |
| 264 |
res = self.interpret(f, [-1]) |
| 265 |
assert res == 5 |
| 266 |
|
| 267 |
def test_is_among_frozen(self): |
| 268 |
fr1 = Freezing() |
| 269 |
fr2 = Freezing() |
| 270 |
def givefr1(): |
| 271 |
return fr1 |
| 272 |
def givefr2(): |
| 273 |
return fr2 |
| 274 |
def f(i): |
| 275 |
if i == 1: |
| 276 |
fr = givefr1() |
| 277 |
else: |
| 278 |
fr = givefr2() |
| 279 |
return fr is fr1 |
| 280 |
res = self.interpret(f, [0]) |
| 281 |
assert res is False |
| 282 |
res = self.interpret(f, [1]) |
| 283 |
assert res is True |
| 284 |
|
| 285 |
def test_unbound_method(self): |
| 286 |
def f(): |
| 287 |
inst = MySubclass() |
| 288 |
inst.z = 40 |
| 289 |
return MyBase.m(inst, 2) |
| 290 |
res = self.interpret(f, []) |
| 291 |
assert res == 42 |
| 292 |
|
| 293 |
def test_call_defaults(self): |
| 294 |
def g(a, b=2, c=3): |
| 295 |
return a+b+c |
| 296 |
def f1(): |
| 297 |
return g(1) |
| 298 |
def f2(): |
| 299 |
return g(1, 10) |
| 300 |
def f3(): |
| 301 |
return g(1, 10, 100) |
| 302 |
res = self.interpret(f1, []) |
| 303 |
assert res == 1+2+3 |
| 304 |
res = self.interpret(f2, []) |
| 305 |
assert res == 1+10+3 |
| 306 |
res = self.interpret(f3, []) |
| 307 |
assert res == 1+10+100 |
| 308 |
|
| 309 |
def test_call_memoized_function(self): |
| 310 |
fr1 = Freezing() |
| 311 |
fr2 = Freezing() |
| 312 |
def getorbuild(key): |
| 313 |
a = 1 |
| 314 |
if key is fr1: |
| 315 |
result = eval("a+2") |
| 316 |
else: |
| 317 |
result = eval("a+6") |
| 318 |
return result |
| 319 |
getorbuild._annspecialcase_ = "specialize:memo" |
| 320 |
|
| 321 |
def f1(i): |
| 322 |
if i > 0: |
| 323 |
fr = fr1 |
| 324 |
else: |
| 325 |
fr = fr2 |
| 326 |
return getorbuild(fr) |
| 327 |
|
| 328 |
res = self.interpret(f1, [0]) |
| 329 |
assert res == 7 |
| 330 |
res = self.interpret(f1, [1]) |
| 331 |
assert res == 3 |
| 332 |
|
| 333 |
def test_call_memoized_function_with_bools(self): |
| 334 |
fr1 = Freezing() |
| 335 |
fr2 = Freezing() |
| 336 |
def getorbuild(key, flag1, flag2): |
| 337 |
a = 1 |
| 338 |
if key is fr1: |
| 339 |
result = eval("a+2") |
| 340 |
else: |
| 341 |
result = eval("a+6") |
| 342 |
if flag1: |
| 343 |
result += 100 |
| 344 |
if flag2: |
| 345 |
result += 1000 |
| 346 |
return result |
| 347 |
getorbuild._annspecialcase_ = "specialize:memo" |
| 348 |
|
| 349 |
def f1(i): |
| 350 |
if i > 0: |
| 351 |
fr = fr1 |
| 352 |
else: |
| 353 |
fr = fr2 |
| 354 |
return getorbuild(fr, i % 2 == 0, i % 3 == 0) |
| 355 |
|
| 356 |
for n in [0, 1, 2, -3, 6]: |
| 357 |
res = self.interpret(f1, [n]) |
| 358 |
assert res == f1(n) |
| 359 |
|
| 360 |
def test_call_memoized_cache(self): |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
from rpython.rlib.cache import Cache |
| 367 |
fr1 = Freezing() |
| 368 |
fr2 = Freezing() |
| 369 |
|
| 370 |
class Cache1(Cache): |
| 371 |
def _build(self, key): |
| 372 |
"NOT_RPYTHON" |
| 373 |
if key is fr1: |
| 374 |
return fr2 |
| 375 |
else: |
| 376 |
return fr1 |
| 377 |
|
| 378 |
class Cache2(Cache): |
| 379 |
def _build(self, key): |
| 380 |
"NOT_RPYTHON" |
| 381 |
a = 1 |
| 382 |
if key is fr1: |
| 383 |
result = eval("a+2") |
| 384 |
else: |
| 385 |
result = eval("a+6") |
| 386 |
return result |
| 387 |
|
| 388 |
cache1 = Cache1() |
| 389 |
cache2 = Cache2() |
| 390 |
|
| 391 |
def f1(i): |
| 392 |
if i > 0: |
| 393 |
fr = fr1 |
| 394 |
else: |
| 395 |
fr = fr2 |
| 396 |
newfr = cache1.getorbuild(fr) |
| 397 |
return cache2.getorbuild(newfr) |
| 398 |
|
| 399 |
res = self.interpret(f1, [0]) |
| 400 |
assert res == 3 |
| 401 |
res = self.interpret(f1, [1]) |
| 402 |
assert res == 7 |
| 403 |
|
| 404 |
def test_call_memo_with_single_value(self): |
| 405 |
class A: pass |
| 406 |
def memofn(cls): |
| 407 |
return len(cls.__name__) |
| 408 |
memofn._annspecialcase_ = "specialize:memo" |
| 409 |
|
| 410 |
def f1(): |
| 411 |
A() |
| 412 |
return memofn(A) |
| 413 |
res = self.interpret(f1, []) |
| 414 |
assert res == 1 |
| 415 |
|
| 416 |
def test_call_memo_with_class(self): |
| 417 |
class A: pass |
| 418 |
class FooBar(A): pass |
| 419 |
def memofn(cls): |
| 420 |
return len(cls.__name__) |
| 421 |
memofn._annspecialcase_ = "specialize:memo" |
| 422 |
|
| 423 |
def f1(i): |
| 424 |
if i == 1: |
| 425 |
cls = A |
| 426 |
else: |
| 427 |
cls = FooBar |
| 428 |
FooBar() |
| 429 |
return memofn(cls) |
| 430 |
res = self.interpret(f1, [1]) |
| 431 |
assert res == 1 |
| 432 |
res = self.interpret(f1, [2]) |
| 433 |
assert res == 6 |
| 434 |
|
| 435 |
def test_call_memo_with_string(self): |
| 436 |
def memofn(s): |
| 437 |
return eval(s) |
| 438 |
memofn._annspecialcase_ = "specialize:memo" |
| 439 |
|
| 440 |
def f1(i): |
| 441 |
if i == 1: |
| 442 |
return memofn("6*7") |
| 443 |
else: |
| 444 |
return memofn("1+2+3+4") |
| 445 |
res = self.interpret(f1, [1]) |
| 446 |
assert res == 42 |
| 447 |
res = self.interpret(f1, [2]) |
| 448 |
assert res == 10 |
| 449 |
|
| 450 |
def test_rpbc_bound_method_static_call(self): |
| 451 |
class R: |
| 452 |
def meth(self): |
| 453 |
return 0 |
| 454 |
r = R() |
| 455 |
m = r.meth |
| 456 |
def fn(): |
| 457 |
return m() |
| 458 |
res = self.interpret(fn, []) |
| 459 |
assert res == 0 |
| 460 |
|
| 461 |
def test_rpbc_bound_method_static_call_w_kwds(self): |
| 462 |
class R: |
| 463 |
def meth(self, x): |
| 464 |
return x |
| 465 |
r = R() |
| 466 |
m = r.meth |
| 467 |
def fn(): |
| 468 |
return m(x=3) |
| 469 |
res = self.interpret(fn, []) |
| 470 |
assert res == 3 |
| 471 |
|
| 472 |
|
| 473 |
def test_constant_return_disagreement(self): |
| 474 |
class R: |
| 475 |
def meth(self): |
| 476 |
return 0 |
| 477 |
r = R() |
| 478 |
def fn(): |
| 479 |
return r.meth() |
| 480 |
res = self.interpret(fn, []) |
| 481 |
assert res == 0 |
| 482 |
|
| 483 |
def test_None_is_false(self): |
| 484 |
def fn(i): |
| 485 |
if i == 0: |
| 486 |
v = None |
| 487 |
else: |
| 488 |
v = fn |
| 489 |
return bool(v) |
| 490 |
res = self.interpret(fn, [1]) |
| 491 |
assert res is True |
| 492 |
res = self.interpret(fn, [0]) |
| 493 |
assert res is False |
| 494 |
|
| 495 |
def test_classpbc_getattr(self): |
| 496 |
class A: |
| 497 |
myvalue = 123 |
| 498 |
class B(A): |
| 499 |
myvalue = 456 |
| 500 |
def f(i): |
| 501 |
if i == 0: |
| 502 |
v = A |
| 503 |
else: |
| 504 |
v = B |
| 505 |
return v.myvalue |
| 506 |
res = self.interpret(f, [0]) |
| 507 |
assert res == 123 |
| 508 |
res = self.interpret(f, [1]) |
| 509 |
assert res == 456 |
| 510 |
|
| 511 |
def test_function_or_None(self): |
| 512 |
def g1(): |
| 513 |
return 42 |
| 514 |
def f(i): |
| 515 |
g = None |
| 516 |
if i > 5: |
| 517 |
g = g1 |
| 518 |
if i > 6: |
| 519 |
return g() |
| 520 |
else: |
| 521 |
return 12 |
| 522 |
|
| 523 |
res = self.interpret(f, [0]) |
| 524 |
assert res == 12 |
| 525 |
res = self.interpret(f, [6]) |
| 526 |
assert res == 12 |
| 527 |
res = self.interpret(f, [7]) |
| 528 |
assert res == 42 |
| 529 |
|
| 530 |
def test_simple_function_pointer(self): |
| 531 |
def f1(x): |
| 532 |
return x + 1 |
| 533 |
def f2(x): |
| 534 |
return x + 2 |
| 535 |
|
| 536 |
l = [f1, f2] |
| 537 |
|
| 538 |
def pointersimple(i): |
| 539 |
return l[i](i) |
| 540 |
|
| 541 |
res = self.interpret(pointersimple, [1]) |
| 542 |
assert res == 3 |
| 543 |
|
| 544 |
def test_classdef_getattr(self): |
| 545 |
class A: |
| 546 |
myvalue = 123 |
| 547 |
class B(A): |
| 548 |
myvalue = 456 |
| 549 |
def f(i): |
| 550 |
B() |
| 551 |
if i == 0: |
| 552 |
v = A |
| 553 |
else: |
| 554 |
v = B |
| 555 |
return v.myvalue |
| 556 |
res = self.interpret(f, [0]) |
| 557 |
assert res == 123 |
| 558 |
res = self.interpret(f, [1]) |
| 559 |
assert res == 456 |
| 560 |
|
| 561 |
def test_call_classes(self): |
| 562 |
class A: pass |
| 563 |
class B(A): pass |
| 564 |
def f(i): |
| 565 |
if i == 1: |
| 566 |
cls = B |
| 567 |
else: |
| 568 |
cls = A |
| 569 |
return cls() |
| 570 |
res = self.interpret(f, [0]) |
| 571 |
assert self.class_name(res) == 'A' |
| 572 |
res = self.interpret(f, [1]) |
| 573 |
assert self.class_name(res) == 'B' |
| 574 |
|
| 575 |
def test_call_classes_or_None(self): |
| 576 |
class A: pass |
| 577 |
class B(A): pass |
| 578 |
def f(i): |
| 579 |
if i == -1: |
| 580 |
cls = None |
| 581 |
elif i == 1: |
| 582 |
cls = B |
| 583 |
else: |
| 584 |
cls = A |
| 585 |
return cls() |
| 586 |
res = self.interpret(f, [0]) |
| 587 |
assert self.class_name(res) == 'A' |
| 588 |
res = self.interpret(f, [1]) |
| 589 |
assert self.class_name(res) == 'B' |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
def test_call_classes_with_init2(self): |
| 601 |
class A: |
| 602 |
def __init__(self, z): |
| 603 |
self.z = z |
| 604 |
class B(A): |
| 605 |
def __init__(self, z, x=42): |
| 606 |
A.__init__(self, z) |
| 607 |
self.extra = x |
| 608 |
def f(i, z): |
| 609 |
if i == 1: |
| 610 |
cls = B |
| 611 |
else: |
| 612 |
cls = A |
| 613 |
return cls(z) |
| 614 |
res = self.interpret(f, [0, 5]) |
| 615 |
assert self.class_name(res) == 'A' |
| 616 |
assert self.read_attr(res, "z") == 5 |
| 617 |
res = self.interpret(f, [1, -7645]) |
| 618 |
assert self.class_name(res) == 'B' |
| 619 |
assert self.read_attr(res, "z") == -7645 |
| 620 |
assert self.read_attr(res, "extra") == 42 |
| 621 |
|
| 622 |
def test_conv_from_None(self): |
| 623 |
class A(object): pass |
| 624 |
def none(): |
| 625 |
return None |
| 626 |
|
| 627 |
def f(i): |
| 628 |
if i == 1: |
| 629 |
return none() |
| 630 |
else: |
| 631 |
return "ab" |
| 632 |
res = self.interpret(f, [1]) |
| 633 |
assert not res |
| 634 |
res = self.interpret(f, [0]) |
| 635 |
assert self.ll_to_string(res) == "ab" |
| 636 |
|
| 637 |
def g(i): |
| 638 |
if i == 1: |
| 639 |
return none() |
| 640 |
else: |
| 641 |
return A() |
| 642 |
res = self.interpret(g, [1]) |
| 643 |
assert not res |
| 644 |
res = self.interpret(g, [0]) |
| 645 |
assert self.class_name(res) == 'A' |
| 646 |
|
| 647 |
|
| 648 |
def test_conv_from_classpbcset_to_larger(self): |
| 649 |
class A(object): pass |
| 650 |
class B(A): pass |
| 651 |
class C(A): pass |
| 652 |
|
| 653 |
def a(): |
| 654 |
return A |
| 655 |
def b(): |
| 656 |
return B |
| 657 |
|
| 658 |
|
| 659 |
def g(i): |
| 660 |
if i == 1: |
| 661 |
cls = a() |
| 662 |
else: |
| 663 |
cls = b() |
| 664 |
return cls() |
| 665 |
|
| 666 |
res = self.interpret(g, [0]) |
| 667 |
assert self.class_name(res) == 'B' |
| 668 |
res = self.interpret(g, [1]) |
| 669 |
assert self.class_name(res) == 'A' |
| 670 |
|
| 671 |
def bc(j): |
| 672 |
if j == 1: |
| 673 |
return B |
| 674 |
else: |
| 675 |
return C |
| 676 |
|
| 677 |
def g(i, j): |
| 678 |
if i == 1: |
| 679 |
cls = a() |
| 680 |
else: |
| 681 |
cls = bc(j) |
| 682 |
return cls() |
| 683 |
|
| 684 |
res = self.interpret(g, [0, 0]) |
| 685 |
assert self.class_name(res) == 'C' |
| 686 |
res = self.interpret(g, [0, 1]) |
| 687 |
assert self.class_name(res) == 'B' |
| 688 |
res = self.interpret(g, [1, 0]) |
| 689 |
assert self.class_name(res) == 'A' |
| 690 |
|
| 691 |
def test_call_starargs(self): |
| 692 |
def g(x=-100, *arg): |
| 693 |
return x + len(arg) |
| 694 |
def f(i): |
| 695 |
if i == -1: |
| 696 |
return g() |
| 697 |
elif i == 0: |
| 698 |
return g(4) |
| 699 |
elif i == 1: |
| 700 |
return g(5, 15) |
| 701 |
elif i == 2: |
| 702 |
return g(7, 17, 27) |
| 703 |
else: |
| 704 |
return g(10, 198, 1129, 13984) |
| 705 |
res = self.interpret(f, [-1]) |
| 706 |
assert res == -100 |
| 707 |
res = self.interpret(f, [0]) |
| 708 |
assert res == 4 |
| 709 |
res = self.interpret(f, [1]) |
| 710 |
assert res == 6 |
| 711 |
res = self.interpret(f, [2]) |
| 712 |
assert res == 9 |
| 713 |
res = self.interpret(f, [3]) |
| 714 |
assert res == 13 |
| 715 |
|
| 716 |
def test_call_keywords(self): |
| 717 |
def g(a=1, b=2, c=3): |
| 718 |
return 100*a+10*b+c |
| 719 |
|
| 720 |
def f(i): |
| 721 |
if i == 0: |
| 722 |
return g(a=7) |
| 723 |
elif i == 1: |
| 724 |
return g(b=11) |
| 725 |
elif i == 2: |
| 726 |
return g(c=13) |
| 727 |
elif i == 3: |
| 728 |
return g(a=7, b=11) |
| 729 |
elif i == 4: |
| 730 |
return g(b=7, a=11) |
| 731 |
elif i == 5: |
| 732 |
return g(a=7, c=13) |
| 733 |
elif i == 6: |
| 734 |
return g(c=7, a=13) |
| 735 |
elif i == 7: |
| 736 |
return g(a=7,b=11,c=13) |
| 737 |
elif i == 8: |
| 738 |
return g(a=7,c=11,b=13) |
| 739 |
elif i == 9: |
| 740 |
return g(b=7,a=11,c=13) |
| 741 |
else: |
| 742 |
return g(b=7,c=11,a=13) |
| 743 |
|
| 744 |
for i in range(11): |
| 745 |
res = self.interpret(f, [i]) |
| 746 |
assert res == f(i) |
| 747 |
|
| 748 |
def test_call_star_and_keywords(self): |
| 749 |
def g(a=1, b=2, c=3): |
| 750 |
return 100*a+10*b+c |
| 751 |
|
| 752 |
def f(i, x): |
| 753 |
if x == 1: |
| 754 |
j = 11 |
| 755 |
else: |
| 756 |
j = 22 |
| 757 |
if i == 0: |
| 758 |
return g(7) |
| 759 |
elif i == 1: |
| 760 |
return g(7,*(j,)) |
| 761 |
elif i == 2: |
| 762 |
return g(7,*(11,j)) |
| 763 |
elif i == 3: |
| 764 |
return g(a=7) |
| 765 |
elif i == 4: |
| 766 |
return g(b=7, *(j,)) |
| 767 |
elif i == 5: |
| 768 |
return g(b=7, c=13, *(j,)) |
| 769 |
elif i == 6: |
| 770 |
return g(c=7, b=13, *(j,)) |
| 771 |
elif i == 7: |
| 772 |
return g(c=7,*(j,)) |
| 773 |
elif i == 8: |
| 774 |
return g(c=7,*(11,j)) |
| 775 |
else: |
| 776 |
return 0 |
| 777 |
|
| 778 |
for i in range(9): |
| 779 |
for x in range(1): |
| 780 |
res = self.interpret(f, [i, x]) |
| 781 |
assert res == f(i, x) |
| 782 |
|
| 783 |
def test_call_star_and_keywords_starargs(self): |
| 784 |
def g(a=1, b=2, c=3, *rest): |
| 785 |
return 1000*len(rest)+100*a+10*b+c |
| 786 |
|
| 787 |
def f(i, x): |
| 788 |
if x == 1: |
| 789 |
j = 13 |
| 790 |
else: |
| 791 |
j = 31 |
| 792 |
if i == 0: |
| 793 |
return g() |
| 794 |
elif i == 1: |
| 795 |
return g(*(j,)) |
| 796 |
elif i == 2: |
| 797 |
return g(*(13, j)) |
| 798 |
elif i == 3: |
| 799 |
return g(*(13, j, 19)) |
| 800 |
elif i == 4: |
| 801 |
return g(*(13, j, 19, 21)) |
| 802 |
elif i == 5: |
| 803 |
return g(7) |
| 804 |
elif i == 6: |
| 805 |
return g(7, *(j,)) |
| 806 |
elif i == 7: |
| 807 |
return g(7, *(13, j)) |
| 808 |
elif i == 8: |
| 809 |
return g(7, *(13, 17, j)) |
| 810 |
elif i == 9: |
| 811 |
return g(7, *(13, 17, j, 21)) |
| 812 |
elif i == 10: |
| 813 |
return g(7, 9) |
| 814 |
elif i == 11: |
| 815 |
return g(7, 9, *(j,)) |
| 816 |
elif i == 12: |
| 817 |
return g(7, 9, *(j, 17)) |
| 818 |
elif i == 13: |
| 819 |
return g(7, 9, *(13, j, 19)) |
| 820 |
elif i == 14: |
| 821 |
return g(7, 9, 11) |
| 822 |
elif i == 15: |
| 823 |
return g(7, 9, 11, *(j,)) |
| 824 |
elif i == 16: |
| 825 |
return g(7, 9, 11, *(13, j)) |
| 826 |
elif i == 17: |
| 827 |
return g(7, 9, 11, *(13, 17, j)) |
| 828 |
elif i == 18: |
| 829 |
return g(7, 9, 11, 2) |
| 830 |
elif i == 19: |
| 831 |
return g(7, 9, 11, 2, *(j,)) |
| 832 |
elif i == 20: |
| 833 |
return g(7, 9, 11, 2, *(13, j)) |
| 834 |
else: |
| 835 |
return 0 |
| 836 |
|
| 837 |
for i in range(21): |
| 838 |
for x in range(1): |
| 839 |
res = self.interpret(f, [i, x]) |
| 840 |
assert res == f(i, x) |
| 841 |
|
| 842 |
def test_conv_from_funcpbcset_to_larger(self): |
| 843 |
def f1(): |
| 844 |
return 7 |
| 845 |
def f2(): |
| 846 |
return 11 |
| 847 |
def f3(): |
| 848 |
return 13 |
| 849 |
|
| 850 |
def a(): |
| 851 |
return f1 |
| 852 |
def b(): |
| 853 |
return f2 |
| 854 |
|
| 855 |
|
| 856 |
def g(i): |
| 857 |
if i == 1: |
| 858 |
f = a() |
| 859 |
else: |
| 860 |
f = b() |
| 861 |
return f() |
| 862 |
|
| 863 |
res = self.interpret(g, [0]) |
| 864 |
assert res == 11 |
| 865 |
res = self.interpret(g, [1]) |
| 866 |
assert res == 7 |
| 867 |
|
| 868 |
def bc(j): |
| 869 |
if j == 1: |
| 870 |
return f2 |
| 871 |
else: |
| 872 |
return f3 |
| 873 |
|
| 874 |
def g(i, j): |
| 875 |
if i == 1: |
| 876 |
cls = a() |
| 877 |
else: |
| 878 |
cls = bc(j) |
| 879 |
return cls() |
| 880 |
|
| 881 |
res = self.interpret(g, [0, 0]) |
| 882 |
assert res == 13 |
| 883 |
res = self.interpret(g, [0, 1]) |
| 884 |
assert res == 11 |
| 885 |
res = self.interpret(g, [1, 0]) |
| 886 |
assert res == 7 |
| 887 |
|
| 888 |
def test_call_special_starargs_method(self): |
| 889 |
class Star: |
| 890 |
def __init__(self, d): |
| 891 |
self.d = d |
| 892 |
def meth(self, *args): |
| 893 |
return self.d + len(args) |
| 894 |
|
| 895 |
def f(i, j): |
| 896 |
s = Star(i) |
| 897 |
return s.meth(i, j) |
| 898 |
|
| 899 |
res = self.interpret(f, [3, 0]) |
| 900 |
assert res == 5 |
| 901 |
|
| 902 |
def test_call_star_method(self): |
| 903 |
class N: |
| 904 |
def __init__(self, d): |
| 905 |
self.d = d |
| 906 |
def meth(self, a, b): |
| 907 |
return self.d + a + b |
| 908 |
|
| 909 |
def f(i, j): |
| 910 |
n = N(i) |
| 911 |
return n.meth(*(i, j)) |
| 912 |
|
| 913 |
res = self.interpret(f, [3, 7]) |
| 914 |
assert res == 13 |
| 915 |
|
| 916 |
def test_call_star_special_starargs_method(self): |
| 917 |
class N: |
| 918 |
def __init__(self, d): |
| 919 |
self.d = d |
| 920 |
def meth(self, *args): |
| 921 |
return self.d + len(args) |
| 922 |
|
| 923 |
def f(i, j): |
| 924 |
n = N(i) |
| 925 |
return n.meth(*(i, j)) |
| 926 |
|
| 927 |
res = self.interpret(f, [3, 0]) |
| 928 |
assert res == 5 |
| 929 |
|
| 930 |
def test_various_patterns_but_one_signature_method(self): |
| 931 |
class A: |
| 932 |
def meth(self, a, b=0): |
| 933 |
raise NotImplementedError |
| 934 |
class B(A): |
| 935 |
def meth(self, a, b=0): |
| 936 |
return a+b |
| 937 |
|
| 938 |
class C(A): |
| 939 |
def meth(self, a, b=0): |
| 940 |
return a*b |
| 941 |
def f(i): |
| 942 |
if i == 0: |
| 943 |
x = B() |
| 944 |
else: |
| 945 |
x = C() |
| 946 |
r1 = x.meth(1) |
| 947 |
r2 = x.meth(3, 2) |
| 948 |
r3 = x.meth(7, b=11) |
| 949 |
return r1+r2+r3 |
| 950 |
res = self.interpret(f, [0]) |
| 951 |
assert res == 1+3+2+7+11 |
| 952 |
res = self.interpret(f, [1]) |
| 953 |
assert res == 3*2+11*7 |
| 954 |
|
| 955 |
|
| 956 |
def test_multiple_ll_one_hl_op(self): |
| 957 |
class E(Exception): |
| 958 |
pass |
| 959 |
class A(object): |
| 960 |
pass |
| 961 |
class B(A): |
| 962 |
pass |
| 963 |
class C(object): |
| 964 |
def method(self, x): |
| 965 |
if x: |
| 966 |
raise E() |
| 967 |
else: |
| 968 |
return A() |
| 969 |
class D(C): |
| 970 |
def method(self, x): |
| 971 |
if x: |
| 972 |
raise E() |
| 973 |
else: |
| 974 |
return B() |
| 975 |
def call(x): |
| 976 |
c = D() |
| 977 |
c.method(x) |
| 978 |
try: |
| 979 |
c.method(x + 1) |
| 980 |
except E: |
| 981 |
pass |
| 982 |
c = C() |
| 983 |
c.method(x) |
| 984 |
try: |
| 985 |
return c.method(x + 1) |
| 986 |
except E: |
| 987 |
return None |
| 988 |
res = self.interpret(call, [0]) |
| 989 |
|
| 990 |
def test_multiple_pbc_with_void_attr(self): |
| 991 |
class A: |
| 992 |
def _freeze_(self): |
| 993 |
return True |
| 994 |
a1 = A() |
| 995 |
a2 = A() |
| 996 |
unique = A() |
| 997 |
unique.result = 42 |
| 998 |
a1.value = unique |
| 999 |
a2.value = unique |
| 1000 |
def g(a): |
| 1001 |
return a.value.result |
| 1002 |
def f(i): |
| 1003 |
if i == 1: |
| 1004 |
a = a1 |
| 1005 |
else: |
| 1006 |
a = a2 |
| 1007 |
return g(a) |
| 1008 |
res = self.interpret(f, [0]) |
| 1009 |
assert res == 42 |
| 1010 |
res = self.interpret(f, [1]) |
| 1011 |
assert res == 42 |
| 1012 |
|
| 1013 |
def test_function_or_none(self): |
| 1014 |
def h(y): |
| 1015 |
return y+84 |
| 1016 |
def g(y): |
| 1017 |
return y+42 |
| 1018 |
def f(x, y): |
| 1019 |
if x == 1: |
| 1020 |
func = g |
| 1021 |
elif x == 2: |
| 1022 |
func = h |
| 1023 |
else: |
| 1024 |
func = None |
| 1025 |
if func: |
| 1026 |
return func(y) |
| 1027 |
return -1 |
| 1028 |
res = self.interpret(f, [1, 100]) |
| 1029 |
assert res == 142 |
| 1030 |
res = self.interpret(f, [2, 100]) |
| 1031 |
assert res == 184 |
| 1032 |
res = self.interpret(f, [3, 100]) |
| 1033 |
assert res == -1 |
| 1034 |
|
| 1035 |
def test_pbc_getattr_conversion(self): |
| 1036 |
fr1 = Freezing() |
| 1037 |
fr2 = Freezing() |
| 1038 |
fr3 = Freezing() |
| 1039 |
fr1.value = 10 |
| 1040 |
fr2.value = 5 |
| 1041 |
fr3.value = 2.5 |
| 1042 |
def pick12(i): |
| 1043 |
if i > 0: |
| 1044 |
return fr1 |
| 1045 |
else: |
| 1046 |
return fr2 |
| 1047 |
def pick23(i): |
| 1048 |
if i > 5: |
| 1049 |
return fr2 |
| 1050 |
else: |
| 1051 |
return fr3 |
| 1052 |
def f(i): |
| 1053 |
x = pick12(i) |
| 1054 |
y = pick23(i) |
| 1055 |
return x.value, y.value |
| 1056 |
for i in [0, 5, 10]: |
| 1057 |
res = self.interpret(f, [i]) |
| 1058 |
item0, item1 = self.ll_unpack_tuple(res, 2) |
| 1059 |
assert type(item0) is int |
| 1060 |
assert type(item1) in (float, int) |
| 1061 |
assert item0 == f(i)[0] |
| 1062 |
assert item1 == f(i)[1] |
| 1063 |
|
| 1064 |
def test_pbc_getattr_conversion_with_classes(self): |
| 1065 |
class base: pass |
| 1066 |
class fr1(base): pass |
| 1067 |
class fr2(base): pass |
| 1068 |
class fr3(base): pass |
| 1069 |
fr1.value = 10 |
| 1070 |
fr2.value = 5 |
| 1071 |
fr3.value = 2.5 |
| 1072 |
def pick12(i): |
| 1073 |
if i > 0: |
| 1074 |
return fr1 |
| 1075 |
else: |
| 1076 |
return fr2 |
| 1077 |
def pick23(i): |
| 1078 |
if i > 5: |
| 1079 |
return fr2 |
| 1080 |
else: |
| 1081 |
return fr3 |
| 1082 |
def f(i): |
| 1083 |
x = pick12(i) |
| 1084 |
y = pick23(i) |
| 1085 |
return x.value, y.value |
| 1086 |
for i in [0, 5, 10]: |
| 1087 |
res = self.interpret(f, [i]) |
| 1088 |
item0, item1 = self.ll_unpack_tuple(res, 2) |
| 1089 |
assert type(item0) is int |
| 1090 |
assert type(item1) in (float, int) |
| 1091 |
assert item0 == f(i)[0] |
| 1092 |
assert item1 == f(i)[1] |
| 1093 |
|
| 1094 |
def test_pbc_imprecise_attrfamily(self): |
| 1095 |
fr1 = Freezing(); fr1.x = 5; fr1.y = [8] |
| 1096 |
fr2 = Freezing(); fr2.x = 6; fr2.y = ["string"] |
| 1097 |
def head(fr): |
| 1098 |
return fr.y[0] |
| 1099 |
def f(n): |
| 1100 |
if n == 1: |
| 1101 |
fr = fr1 |
| 1102 |
else: |
| 1103 |
fr = fr2 |
| 1104 |
return head(fr1) + fr.x |
| 1105 |
res = self.interpret(f, [2]) |
| 1106 |
assert res == 8 + 6 |
| 1107 |
|
| 1108 |
def test_multiple_specialized_functions(self): |
| 1109 |
def myadder(x, y): |
| 1110 |
return x+y |
| 1111 |
def myfirst(x, y): |
| 1112 |
return x |
| 1113 |
def mysecond(x, y): |
| 1114 |
return y |
| 1115 |
myadder._annspecialcase_ = 'specialize:argtype(0)' |
| 1116 |
myfirst._annspecialcase_ = 'specialize:argtype(0)' |
| 1117 |
mysecond._annspecialcase_ = 'specialize:argtype(0)' |
| 1118 |
def f(i): |
| 1119 |
if i == 0: |
| 1120 |
g = myfirst |
| 1121 |
elif i == 1: |
| 1122 |
g = mysecond |
| 1123 |
else: |
| 1124 |
g = myadder |
| 1125 |
s = g("hel", "lo") |
| 1126 |
n = g(40, 2) |
| 1127 |
return len(s) * n |
| 1128 |
for i in range(3): |
| 1129 |
res = self.interpret(f, [i]) |
| 1130 |
assert res == f(i) |
| 1131 |
|
| 1132 |
def test_specialized_method_of_frozen(self): |
| 1133 |
class space: |
| 1134 |
def _freeze_(self): |
| 1135 |
return True |
| 1136 |
def __init__(self, tag): |
| 1137 |
self.tag = tag |
| 1138 |
def wrap(self, x): |
| 1139 |
if isinstance(x, int): |
| 1140 |
return self.tag + '< %d >' % x |
| 1141 |
else: |
| 1142 |
return self.tag + x |
| 1143 |
wrap._annspecialcase_ = 'specialize:argtype(1)' |
| 1144 |
space1 = space("tag1:") |
| 1145 |
space2 = space("tag2:") |
| 1146 |
def f(i): |
| 1147 |
if i == 1: |
| 1148 |
sp = space1 |
| 1149 |
else: |
| 1150 |
sp = space2 |
| 1151 |
w1 = sp.wrap('hello') |
| 1152 |
w2 = sp.wrap(42) |
| 1153 |
return w1 + w2 |
| 1154 |
res = self.interpret(f, [1]) |
| 1155 |
assert self.ll_to_string(res) == 'tag1:hellotag1:< 42 >' |
| 1156 |
res = self.interpret(f, [0]) |
| 1157 |
assert self.ll_to_string(res) == 'tag2:hellotag2:< 42 >' |
| 1158 |
|
| 1159 |
def test_specialized_method(self): |
| 1160 |
class A: |
| 1161 |
def __init__(self, tag): |
| 1162 |
self.tag = tag |
| 1163 |
def wrap(self, x): |
| 1164 |
if isinstance(x, int): |
| 1165 |
return self.tag + '< %d >' % x |
| 1166 |
else: |
| 1167 |
return self.tag + x |
| 1168 |
wrap._annspecialcase_ = 'specialize:argtype(1)' |
| 1169 |
a1 = A("tag1:") |
| 1170 |
a2 = A("tag2:") |
| 1171 |
def f(i): |
| 1172 |
if i == 1: |
| 1173 |
sp = a1 |
| 1174 |
else: |
| 1175 |
sp = a2 |
| 1176 |
w1 = sp.wrap('hello') |
| 1177 |
w2 = sp.wrap(42) |
| 1178 |
return w1 + w2 |
| 1179 |
res = self.interpret(f, [1]) |
| 1180 |
assert self.ll_to_string(res) == 'tag1:hellotag1:< 42 >' |
| 1181 |
res = self.interpret(f, [0]) |
| 1182 |
assert self.ll_to_string(res) == 'tag2:hellotag2:< 42 >' |
| 1183 |
|
| 1184 |
def test_precise_method_call_1(self): |
| 1185 |
class A(object): |
| 1186 |
def meth(self, x=5): |
| 1187 |
return x+1 |
| 1188 |
class B(A): |
| 1189 |
def meth(self, x=5): |
| 1190 |
return x+2 |
| 1191 |
class C(A): |
| 1192 |
pass |
| 1193 |
def f(i, n): |
| 1194 |
|
| 1195 |
if i > 0: |
| 1196 |
x = A() |
| 1197 |
else: |
| 1198 |
x = B() |
| 1199 |
result1 = x.meth(n) |
| 1200 |
|
| 1201 |
result2 = C().meth() |
| 1202 |
return result1 * result2 |
| 1203 |
for i in [0, 1]: |
| 1204 |
res = self.interpret(f, [i, 1234]) |
| 1205 |
assert res == f(i, 1234) |
| 1206 |
|
| 1207 |
def test_precise_method_call_2(self): |
| 1208 |
class A(object): |
| 1209 |
def meth(self, x=5): |
| 1210 |
return x+1 |
| 1211 |
class B(A): |
| 1212 |
def meth(self, x=5): |
| 1213 |
return x+2 |
| 1214 |
class C(A): |
| 1215 |
def meth(self, x=5): |
| 1216 |
return x+3 |
| 1217 |
def f(i, n): |
| 1218 |
|
| 1219 |
if i > 0: |
| 1220 |
x = A() |
| 1221 |
else: |
| 1222 |
x = B() |
| 1223 |
result1 = x.meth(n) |
| 1224 |
|
| 1225 |
if i > 0: |
| 1226 |
x = C() |
| 1227 |
else: |
| 1228 |
x = A() |
| 1229 |
result2 = x.meth() |
| 1230 |
return result1 * result2 |
| 1231 |
for i in [0, 1]: |
| 1232 |
res = self.interpret(f, [i, 1234]) |
| 1233 |
assert res == f(i, 1234) |
| 1234 |
|
| 1235 |
def test_disjoint_pbcs(self): |
| 1236 |
class Frozen(object): |
| 1237 |
def __init__(self, v): |
| 1238 |
self.v = v |
| 1239 |
def _freeze_(self): |
| 1240 |
return True |
| 1241 |
|
| 1242 |
fr1 = Frozen(2) |
| 1243 |
fr2 = Frozen(3) |
| 1244 |
|
| 1245 |
def g1(x): |
| 1246 |
return x.v |
| 1247 |
def g2(y): |
| 1248 |
return y.v |
| 1249 |
def h(x): |
| 1250 |
return x is not None |
| 1251 |
def h2(x): |
| 1252 |
return x is fr1 |
| 1253 |
|
| 1254 |
def f(): |
| 1255 |
a = g1(fr1) |
| 1256 |
b = g2(fr2) |
| 1257 |
h(None) |
| 1258 |
return (h(fr1) + 10*h(fr2) + 100*a + 1000*b + |
| 1259 |
10000*h2(fr1) + 100000*h2(fr2)) |
| 1260 |
|
| 1261 |
res = self.interpret(f, []) |
| 1262 |
assert res == 13211 |
| 1263 |
|
| 1264 |
def test_disjoint_pbcs_2(self): |
| 1265 |
class Frozen(object): |
| 1266 |
def __init__(self, v): |
| 1267 |
self.v = v |
| 1268 |
def _freeze_(self): |
| 1269 |
return True |
| 1270 |
fr1 = Frozen(1) |
| 1271 |
fr2 = Frozen(2) |
| 1272 |
fr3 = Frozen(3) |
| 1273 |
def getv(x): |
| 1274 |
return x.v |
| 1275 |
def h(x): |
| 1276 |
return (x is not None) + 2*(x is fr2) + 3*(x is fr3) |
| 1277 |
def f(n): |
| 1278 |
if n == 1: |
| 1279 |
fr = fr1 |
| 1280 |
else: |
| 1281 |
fr = fr2 |
| 1282 |
total = getv(fr) |
| 1283 |
if n == 3: |
| 1284 |
fr = fr3 |
| 1285 |
h(None) |
| 1286 |
return total + 10*h(fr) |
| 1287 |
|
| 1288 |
res = self.interpret(f, [3]) |
| 1289 |
assert res == 42 |
| 1290 |
|
| 1291 |
def test_convert_multiple_to_single(self): |
| 1292 |
class A: |
| 1293 |
def meth(self, fr): |
| 1294 |
return 65 |
| 1295 |
class B(A): |
| 1296 |
def meth(self, fr): |
| 1297 |
return 66 |
| 1298 |
fr1 = Freezing() |
| 1299 |
fr2 = Freezing() |
| 1300 |
def f(): |
| 1301 |
return A().meth(fr1) * B().meth(fr2) |
| 1302 |
|
| 1303 |
res = self.interpret(f, []) |
| 1304 |
assert res == 65*66 |
| 1305 |
|
| 1306 |
def test_convert_multiple_to_single_method_of_frozen_pbc(self): |
| 1307 |
class A: |
| 1308 |
def meth(self, frmeth): |
| 1309 |
return frmeth(100) |
| 1310 |
class B(A): |
| 1311 |
def meth(self, frmeth): |
| 1312 |
return frmeth(1000) |
| 1313 |
fr1 = Freezing(); fr1.x = 65 |
| 1314 |
fr2 = Freezing(); fr2.x = 66 |
| 1315 |
def f(): |
| 1316 |
return A().meth(fr1.mymethod) * B().meth(fr2.mymethod) |
| 1317 |
|
| 1318 |
res = self.interpret(f, []) |
| 1319 |
assert res == 165 * 1066 |
| 1320 |
|
| 1321 |
def test_convert_none_to_frozen_pbc(self): |
| 1322 |
fr1 = Freezing(); fr1.x = 65 |
| 1323 |
fr2 = Freezing(); fr2.y = 65 |
| 1324 |
def g(fr): |
| 1325 |
return fr.x |
| 1326 |
def identity(z): |
| 1327 |
return z |
| 1328 |
def f(n): |
| 1329 |
if n == 0: |
| 1330 |
fr = identity(None) |
| 1331 |
else: |
| 1332 |
fr = fr1 |
| 1333 |
return g(fr) |
| 1334 |
res = self.interpret(f, [1]) |
| 1335 |
assert res == 65 |
| 1336 |
|
| 1337 |
def test_multiple_attribute_access_patterns(self): |
| 1338 |
class Base(object): |
| 1339 |
pass |
| 1340 |
class A(Base): |
| 1341 |
value = 1000 |
| 1342 |
def meth(self): return self.n + 1 |
| 1343 |
class B(A): |
| 1344 |
def meth(self): return self.n + 2 |
| 1345 |
class C(Base): |
| 1346 |
value = 2000 |
| 1347 |
def meth(self): ShouldNotBeSeen |
| 1348 |
def AorB(n): |
| 1349 |
if n == 5: return A |
| 1350 |
else: return B |
| 1351 |
def BorC(n): |
| 1352 |
if n == 3: return B |
| 1353 |
else: return C |
| 1354 |
def f(n): |
| 1355 |
value = BorC(n).value |
| 1356 |
x = B() |
| 1357 |
x.n = 100 |
| 1358 |
return value + AorB(n).meth(x) |
| 1359 |
|
| 1360 |
for i in [1, 3, 5]: |
| 1361 |
res = self.interpret(f, [i]) |
| 1362 |
assert res == f(i) |
| 1363 |
|
| 1364 |
def test_function_as_frozen_pbc(self): |
| 1365 |
def f1(): pass |
| 1366 |
def f2(): pass |
| 1367 |
def choose(n): |
| 1368 |
if n == 1: |
| 1369 |
return f1 |
| 1370 |
else: |
| 1371 |
return f2 |
| 1372 |
def f(n): |
| 1373 |
return choose(n) is f1 |
| 1374 |
res = self.interpret(f, [1]) |
| 1375 |
assert res == True |
| 1376 |
res = self.interpret(f, [2]) |
| 1377 |
assert res == False |
| 1378 |
|
| 1379 |
def test_call_from_list(self): |
| 1380 |
def f0(n): return n+200 |
| 1381 |
def f1(n): return n+192 |
| 1382 |
def f2(n): return n+46 |
| 1383 |
def f3(n): return n+2987 |
| 1384 |
def f4(n): return n+217 |
| 1385 |
lst = [f0, f1, f2, f3, f4] |
| 1386 |
def f(i, n): |
| 1387 |
return lst[i](n) |
| 1388 |
for i in range(5): |
| 1389 |
res = self.interpret(f, [i, 1000]) |
| 1390 |
assert res == f(i, 1000) |
| 1391 |
|
| 1392 |
def test_None_is_None(self): |
| 1393 |
def g(): |
| 1394 |
return None |
| 1395 |
def f(): |
| 1396 |
return g() is None |
| 1397 |
res = self.interpret(f, []) |
| 1398 |
assert res == True |
| 1399 |
|
| 1400 |
def test_except_class_call(self): |
| 1401 |
class A: |
| 1402 |
pass |
| 1403 |
def f(): |
| 1404 |
try: |
| 1405 |
A() |
| 1406 |
IndexError() |
| 1407 |
return 12 |
| 1408 |
except ValueError: |
| 1409 |
return 23 |
| 1410 |
res = self.interpret(f, []) |
| 1411 |
assert res == 12 |
| 1412 |
|
| 1413 |
def test_exception_with_non_empty_baseclass(self): |
| 1414 |
class BE(Exception): |
| 1415 |
pass |
| 1416 |
class E1(BE): |
| 1417 |
pass |
| 1418 |
class E2(BE): |
| 1419 |
pass |
| 1420 |
def f(x): |
| 1421 |
if x: |
| 1422 |
e = E1() |
| 1423 |
else: |
| 1424 |
e = E2() |
| 1425 |
witness = E1() |
| 1426 |
witness.x = 42 |
| 1427 |
e.x = 3 |
| 1428 |
return witness.x |
| 1429 |
|
| 1430 |
res = self.interpret(f, [0]) |
| 1431 |
assert res == 42 |
| 1432 |
res = self.interpret(f, [1]) |
| 1433 |
assert res == 42 |
| 1434 |
|
| 1435 |
def test_funcornone_to_func(self): |
| 1436 |
def g(y): |
| 1437 |
return y*2 |
| 1438 |
def f(x): |
| 1439 |
if x > 0: |
| 1440 |
g1 = g |
| 1441 |
else: |
| 1442 |
g1 = None |
| 1443 |
x += 1 |
| 1444 |
if g1: |
| 1445 |
return g1(x) |
| 1446 |
else: |
| 1447 |
return -1 |
| 1448 |
|
| 1449 |
res = self.interpret(f, [20]) |
| 1450 |
assert res == 42 |
| 1451 |
|
| 1452 |
def test_specialize_functionarg(self): |
| 1453 |
def f(x, y): |
| 1454 |
return x + y |
| 1455 |
def g(x, y, z): |
| 1456 |
return x + y + z |
| 1457 |
def functionarg(func, *extraargs): |
| 1458 |
return func(42, *extraargs) |
| 1459 |
functionarg._annspecialcase_ = "specialize:arg(0)" |
| 1460 |
def call_functionarg(): |
| 1461 |
return functionarg(f, 1) + functionarg(g, 1, 2) |
| 1462 |
assert call_functionarg() == 2 * 42 + 4 |
| 1463 |
res = self.interpret(call_functionarg, []) |
| 1464 |
assert res == 2 * 42 + 4 |
| 1465 |
|
| 1466 |
def test_convert_multiple_classes_to_single(self): |
| 1467 |
class A: |
| 1468 |
result = 321 |
| 1469 |
def meth(self, n): |
| 1470 |
if n: |
| 1471 |
return A |
| 1472 |
else: |
| 1473 |
return B |
| 1474 |
class B(A): |
| 1475 |
result = 123 |
| 1476 |
def meth(self, n): |
| 1477 |
return B |
| 1478 |
def f(n): |
| 1479 |
A().meth(n) |
| 1480 |
cls = B().meth(n) |
| 1481 |
return cls().result |
| 1482 |
res = self.interpret(f, [5]) |
| 1483 |
assert res == 123 |
| 1484 |
|
| 1485 |
def test_is_among_functions(self): |
| 1486 |
def g1(): pass |
| 1487 |
def g2(): pass |
| 1488 |
def g3(): pass |
| 1489 |
def f(n): |
| 1490 |
if n > 5: |
| 1491 |
g = g2 |
| 1492 |
else: |
| 1493 |
g = g1 |
| 1494 |
g() |
| 1495 |
g3() |
| 1496 |
return g is g3 |
| 1497 |
res = self.interpret(f, [2]) |
| 1498 |
assert res == False |
| 1499 |
|
| 1500 |
def test_is_among_functions_2(self): |
| 1501 |
def g1(): pass |
| 1502 |
def g2(): pass |
| 1503 |
def f(n): |
| 1504 |
if n > 5: |
| 1505 |
g = g2 |
| 1506 |
else: |
| 1507 |
g = g1 |
| 1508 |
g() |
| 1509 |
return g is g2 |
| 1510 |
res = self.interpret(f, [2]) |
| 1511 |
assert res == False |
| 1512 |
res = self.interpret(f, [8]) |
| 1513 |
assert res == True |
| 1514 |
|
| 1515 |
def test_is_among_functions_3(self): |
| 1516 |
def g0(): pass |
| 1517 |
def g1(): pass |
| 1518 |
def g2(): pass |
| 1519 |
def g3(): pass |
| 1520 |
def g4(): pass |
| 1521 |
def g5(): pass |
| 1522 |
def g6(): pass |
| 1523 |
def g7(): pass |
| 1524 |
glist = [g0, g1, g2, g3, g4, g5, g6, g7] |
| 1525 |
def f(n): |
| 1526 |
if n > 5: |
| 1527 |
g = g2 |
| 1528 |
else: |
| 1529 |
g = g1 |
| 1530 |
h = glist[n] |
| 1531 |
g() |
| 1532 |
h() |
| 1533 |
return g is h |
| 1534 |
res = self.interpret(f, [2]) |
| 1535 |
assert res == False |
| 1536 |
res = self.interpret(f, [1]) |
| 1537 |
assert res == True |
| 1538 |
res = self.interpret(f, [6]) |
| 1539 |
assert res == False |
| 1540 |
|
| 1541 |
def test_shrink_pbc_set(self): |
| 1542 |
def g1(): |
| 1543 |
return 10 |
| 1544 |
def g2(): |
| 1545 |
return 20 |
| 1546 |
def g3(): |
| 1547 |
return 30 |
| 1548 |
def h1(g): |
| 1549 |
return 1 + g() |
| 1550 |
def h2(g): |
| 1551 |
return 2 + g() |
| 1552 |
def f(n): |
| 1553 |
if n > 5: g = g1 |
| 1554 |
else: g = g2 |
| 1555 |
if n % 2: h = h1 |
| 1556 |
else: h = h2 |
| 1557 |
res = h(g) |
| 1558 |
if n > 7: g = g3 |
| 1559 |
h2(g) |
| 1560 |
return res |
| 1561 |
res = self.interpret(f, [7]) |
| 1562 |
assert res == 11 |
| 1563 |
|
| 1564 |
def test_single_pbc_getattr(self): |
| 1565 |
class C: |
| 1566 |
def __init__(self, v1, v2): |
| 1567 |
self.v1 = v1 |
| 1568 |
self.v2 = v2 |
| 1569 |
def _freeze_(self): |
| 1570 |
return True |
| 1571 |
c1 = C(11, lambda: "hello") |
| 1572 |
c2 = C(22, lambda: 623) |
| 1573 |
def f1(l, c): |
| 1574 |
l.append(c.v1) |
| 1575 |
def f2(c): |
| 1576 |
return c.v2 |
| 1577 |
def f3(c): |
| 1578 |
return c.v2 |
| 1579 |
def g(): |
| 1580 |
l = [] |
| 1581 |
f1(l, c1) |
| 1582 |
f1(l, c2) |
| 1583 |
return f2(c1)(), f3(c2)() |
| 1584 |
|
| 1585 |
res = self.interpret(g, []) |
| 1586 |
item0, item1 = self.ll_unpack_tuple(res, 2) |
| 1587 |
assert self.ll_to_string(item0) == "hello" |
| 1588 |
assert item1 == 623 |
| 1589 |
|
| 1590 |
def test_always_raising_methods(self): |
| 1591 |
class Base: |
| 1592 |
def m(self): |
| 1593 |
raise KeyError |
| 1594 |
class A(Base): |
| 1595 |
def m(self): |
| 1596 |
return 42 |
| 1597 |
class B(Base): |
| 1598 |
pass |
| 1599 |
def f(n): |
| 1600 |
if n > 3: |
| 1601 |
o = A() |
| 1602 |
else: |
| 1603 |
o = B() |
| 1604 |
try: |
| 1605 |
o.m() |
| 1606 |
except KeyError: |
| 1607 |
assert 0 |
| 1608 |
return B().m() |
| 1609 |
|
| 1610 |
self.interpret_raises(KeyError, f, [7]) |
| 1611 |
|
| 1612 |
def test_possible_missing_attribute_access(self): |
| 1613 |
py.test.skip("Should explode or give some warning") |
| 1614 |
class Base(object): |
| 1615 |
pass |
| 1616 |
|
| 1617 |
class A(Base): |
| 1618 |
a = 1 |
| 1619 |
b = 2 |
| 1620 |
|
| 1621 |
class B(Base): |
| 1622 |
a = 2 |
| 1623 |
b = 2 |
| 1624 |
|
| 1625 |
class C(Base): |
| 1626 |
b = 8 |
| 1627 |
|
| 1628 |
def f(n): |
| 1629 |
if n > 3: |
| 1630 |
x = A |
| 1631 |
elif n > 1: |
| 1632 |
x = B |
| 1633 |
else: |
| 1634 |
x = C |
| 1635 |
if n > 0: |
| 1636 |
return x.a |
| 1637 |
return 9 |
| 1638 |
|
| 1639 |
self.interpret(f, [int]) |
| 1640 |
|
| 1641 |
|
| 1642 |
def test_funcpointer_default_value(self): |
| 1643 |
def foo(x): return x+1 |
| 1644 |
class Foo: |
| 1645 |
func = None |
| 1646 |
def __init__(self, n): |
| 1647 |
if n == 1: |
| 1648 |
self.func = foo |
| 1649 |
|
| 1650 |
def fn(n): |
| 1651 |
a = Foo(n) |
| 1652 |
if a.func: |
| 1653 |
return a.func(n) |
| 1654 |
return -1 |
| 1655 |
|
| 1656 |
res = self.interpret(fn, [0]) |
| 1657 |
assert res == -1 |
| 1658 |
|
| 1659 |
def test_is_none(self): |
| 1660 |
from rpython.rlib.nonconst import NonConstant |
| 1661 |
def g(x): |
| 1662 |
return NonConstant(g) is None |
| 1663 |
res = self.interpret(g, [1]) |
| 1664 |
assert not res |
| 1665 |
|
| 1666 |
def test_pbc_of_classes_not_all_used(self): |
| 1667 |
class Base(object): pass |
| 1668 |
class A(Base): pass |
| 1669 |
class B(Base): pass |
| 1670 |
def poke(lst): |
| 1671 |
pass |
| 1672 |
def g(): |
| 1673 |
A() |
| 1674 |
poke([A, B]) |
| 1675 |
self.interpret(g, []) |
| 1676 |
|
| 1677 |
def test_pbc_of_classes_isinstance_only(self): |
| 1678 |
class Base(object): pass |
| 1679 |
class ASub(Base): pass |
| 1680 |
def g(): |
| 1681 |
x = Base() |
| 1682 |
return isinstance(x, ASub) |
| 1683 |
res = self.interpret(g, []) |
| 1684 |
assert res == False |
| 1685 |
|
| 1686 |
def test_class___name__(self): |
| 1687 |
class Base(object): pass |
| 1688 |
class ASub(Base): pass |
| 1689 |
def g(n): |
| 1690 |
if n == 1: |
| 1691 |
x = Base() |
| 1692 |
else: |
| 1693 |
x = ASub() |
| 1694 |
return x.__class__.__name__ |
| 1695 |
res = self.interpret(g, [1]) |
| 1696 |
assert self.ll_to_string(res) == "Base" |
| 1697 |
res = self.interpret(g, [2]) |
| 1698 |
assert self.ll_to_string(res) == "ASub" |
| 1699 |
|
| 1700 |
def test_str_class(self): |
| 1701 |
class Base(object): pass |
| 1702 |
class ASub(Base): pass |
| 1703 |
def g(n): |
| 1704 |
if n == 1: |
| 1705 |
x = Base() |
| 1706 |
else: |
| 1707 |
x = ASub() |
| 1708 |
return str(x.__class__) |
| 1709 |
res = self.interpret(g, [1]) |
| 1710 |
assert self.ll_to_string(res) == "Base" |
| 1711 |
res = self.interpret(g, [2]) |
| 1712 |
assert self.ll_to_string(res) == "ASub" |
| 1713 |
|
| 1714 |
def test_bug_callfamily(self): |
| 1715 |
def cb1(): |
| 1716 |
xxx |
| 1717 |
def cb2(): |
| 1718 |
pass |
| 1719 |
def g(cb, result): |
| 1720 |
assert (cb is None) == (result == 0) |
| 1721 |
def h(cb): |
| 1722 |
cb() |
| 1723 |
def f(): |
| 1724 |
g(None, 0) |
| 1725 |
g(cb1, 1) |
| 1726 |
g(cb2, 2) |
| 1727 |
h(cb2) |
| 1728 |
return 42 |
| 1729 |
res = self.interpret(f, []) |
| 1730 |
assert res == 42 |
| 1731 |
|
| 1732 |
def test_equality_of_frozen_pbcs_inside_data_structures(self): |
| 1733 |
class A: |
| 1734 |
def _freeze_(self): |
| 1735 |
return True |
| 1736 |
a1 = A() |
| 1737 |
a2 = A() |
| 1738 |
def f(): |
| 1739 |
return [a1] == [a1] |
| 1740 |
def g(i): |
| 1741 |
x1 = [a1, a2][i] |
| 1742 |
x2 = [a1, a2][i] |
| 1743 |
return (x1,) == (x2,) |
| 1744 |
res = self.interpret(f, []) |
| 1745 |
assert res == True |
| 1746 |
res = self.interpret(g, [1]) |
| 1747 |
assert res == True |
| 1748 |
|
| 1749 |
def test_convert_from_anything_to_impossible(self): |
| 1750 |
def f1(): |
| 1751 |
return 42 |
| 1752 |
def f2(): |
| 1753 |
raise ValueError |
| 1754 |
def f3(): |
| 1755 |
raise ValueError |
| 1756 |
def f(i): |
| 1757 |
if i > 5: |
| 1758 |
f = f2 |
| 1759 |
else: |
| 1760 |
f = f3 |
| 1761 |
try: |
| 1762 |
f() |
| 1763 |
except ValueError: |
| 1764 |
pass |
| 1765 |
if i > 1: |
| 1766 |
f = f2 |
| 1767 |
else: |
| 1768 |
f = f1 |
| 1769 |
return f() |
| 1770 |
self.interpret(f, [-5]) |
| 1771 |
|
| 1772 |
def test_single_function_to_noncallable_pbcs(self): |
| 1773 |
from rpython.annotator import annrpython |
| 1774 |
a = annrpython.RPythonAnnotator() |
| 1775 |
|
| 1776 |
def h1(i): |
| 1777 |
return i + 5 |
| 1778 |
def h3(i): |
| 1779 |
"NOT_RPYTHON" |
| 1780 |
return i + 7 |
| 1781 |
|
| 1782 |
def other_func(i): |
| 1783 |
h1(i) |
| 1784 |
return h1 |
| 1785 |
|
| 1786 |
def g(i): |
| 1787 |
fn = other_func(i) |
| 1788 |
if i > 5: |
| 1789 |
return fn |
| 1790 |
return h3 |
| 1791 |
self.interpret(g, [-5]) |
| 1792 |
|
| 1793 |
def test_multiple_functions_to_noncallable_pbcs(self): |
| 1794 |
py.test.skip("unsupported") |
| 1795 |
|
| 1796 |
from rpython.annotator import annrpython |
| 1797 |
a = annrpython.RPythonAnnotator() |
| 1798 |
|
| 1799 |
def h1(i): |
| 1800 |
return i + 5 |
| 1801 |
def h2(i): |
| 1802 |
return i + 5 |
| 1803 |
def h3(i): |
| 1804 |
"NOT_RPYTHON" |
| 1805 |
return i + 7 |
| 1806 |
|
| 1807 |
def g(i): |
| 1808 |
if i & 1: |
| 1809 |
fn = h1 |
| 1810 |
else: |
| 1811 |
fn = h2 |
| 1812 |
fn(i) |
| 1813 |
if i > 5: |
| 1814 |
return fn |
| 1815 |
return h3 |
| 1816 |
self.interpret(g, [-5]) |
| 1817 |
|
| 1818 |
def test_single_function_from_noncallable_pbcs(self): |
| 1819 |
from rpython.annotator import annrpython |
| 1820 |
a = annrpython.RPythonAnnotator() |
| 1821 |
|
| 1822 |
def h1(i): |
| 1823 |
return i + 5 |
| 1824 |
def h3(i): |
| 1825 |
"NOT_RPYTHON" |
| 1826 |
return i + 7 |
| 1827 |
|
| 1828 |
def other_func(i): |
| 1829 |
h1(i) |
| 1830 |
return h1 |
| 1831 |
|
| 1832 |
def g(i): |
| 1833 |
if i & 1: |
| 1834 |
fn = h1 |
| 1835 |
else: |
| 1836 |
fn = h3 |
| 1837 |
h1(i) |
| 1838 |
if fn is h1: |
| 1839 |
fn(i) |
| 1840 |
self.interpret(g, [-5]) |
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
def test_hlinvoke_simple(): |
| 1845 |
def f(a,b): |
| 1846 |
return a + b |
| 1847 |
from rpython.translator import translator |
| 1848 |
from rpython.annotator import annrpython |
| 1849 |
a = annrpython.RPythonAnnotator() |
| 1850 |
|
| 1851 |
s_f = a.bookkeeper.immutablevalue(f) |
| 1852 |
a.bookkeeper.emulate_pbc_call('f', s_f, [annmodel.SomeInteger(), annmodel.SomeInteger()]) |
| 1853 |
a.complete() |
| 1854 |
|
| 1855 |
from rpython.rtyper import rtyper |
| 1856 |
rt = rtyper.RPythonTyper(a) |
| 1857 |
rt.specialize() |
| 1858 |
|
| 1859 |
def ll_h(R, f, x): |
| 1860 |
from rpython.rlib.objectmodel import hlinvoke |
| 1861 |
return hlinvoke(R, f, x, 2) |
| 1862 |
|
| 1863 |
from rpython.rtyper import annlowlevel |
| 1864 |
|
| 1865 |
r_f = rt.getrepr(s_f) |
| 1866 |
|
| 1867 |
s_R = a.bookkeeper.immutablevalue(r_f) |
| 1868 |
s_ll_f = lltype_to_annotation(r_f.lowleveltype) |
| 1869 |
ll_h_graph = annlowlevel.annotate_lowlevel_helper(a, ll_h, [s_R, s_ll_f, annmodel.SomeInteger()]) |
| 1870 |
assert a.binding(ll_h_graph.getreturnvar()).knowntype == int |
| 1871 |
rt.specialize_more_blocks() |
| 1872 |
|
| 1873 |
from rpython.rtyper.llinterp import LLInterpreter |
| 1874 |
interp = LLInterpreter(rt) |
| 1875 |
|
| 1876 |
|
| 1877 |
res = interp.eval_graph(ll_h_graph, [None, None, 3]) |
| 1878 |
assert res == 5 |
| 1879 |
|
| 1880 |
def test_hlinvoke_simple2(): |
| 1881 |
def f1(a,b): |
| 1882 |
return a + b |
| 1883 |
def f2(a,b): |
| 1884 |
return a - b |
| 1885 |
from rpython.annotator import annrpython |
| 1886 |
a = annrpython.RPythonAnnotator() |
| 1887 |
|
| 1888 |
def g(i): |
| 1889 |
if i: |
| 1890 |
f = f1 |
| 1891 |
else: |
| 1892 |
f = f2 |
| 1893 |
f(5,4) |
| 1894 |
f(3,2) |
| 1895 |
|
| 1896 |
a.build_types(g, [int]) |
| 1897 |
|
| 1898 |
from rpython.rtyper import rtyper |
| 1899 |
rt = rtyper.RPythonTyper(a) |
| 1900 |
rt.specialize() |
| 1901 |
|
| 1902 |
def ll_h(R, f, x): |
| 1903 |
from rpython.rlib.objectmodel import hlinvoke |
| 1904 |
return hlinvoke(R, f, x, 2) |
| 1905 |
|
| 1906 |
from rpython.rtyper import annlowlevel |
| 1907 |
|
| 1908 |
f1desc = a.bookkeeper.getdesc(f1) |
| 1909 |
f2desc = a.bookkeeper.getdesc(f2) |
| 1910 |
|
| 1911 |
s_f = annmodel.SomePBC([f1desc, f2desc]) |
| 1912 |
r_f = rt.getrepr(s_f) |
| 1913 |
|
| 1914 |
s_R = a.bookkeeper.immutablevalue(r_f) |
| 1915 |
s_ll_f = lltype_to_annotation(r_f.lowleveltype) |
| 1916 |
ll_h_graph= annlowlevel.annotate_lowlevel_helper(a, ll_h, [s_R, s_ll_f, annmodel.SomeInteger()]) |
| 1917 |
assert a.binding(ll_h_graph.getreturnvar()).knowntype == int |
| 1918 |
rt.specialize_more_blocks() |
| 1919 |
|
| 1920 |
from rpython.rtyper.llinterp import LLInterpreter |
| 1921 |
interp = LLInterpreter(rt) |
| 1922 |
|
| 1923 |
|
| 1924 |
res = interp.eval_graph(ll_h_graph, [None, r_f.convert_desc(f1desc), 3]) |
| 1925 |
assert res == 5 |
| 1926 |
res = interp.eval_graph(ll_h_graph, [None, r_f.convert_desc(f2desc), 3]) |
| 1927 |
assert res == 1 |
| 1928 |
|
| 1929 |
def test_hlinvoke_hltype(): |
| 1930 |
class A(object): |
| 1931 |
def __init__(self, v): |
| 1932 |
self.v = v |
| 1933 |
def f(a): |
| 1934 |
return A(a) |
| 1935 |
|
| 1936 |
from rpython.annotator import annrpython |
| 1937 |
a = annrpython.RPythonAnnotator() |
| 1938 |
|
| 1939 |
def g(): |
| 1940 |
a = A(None) |
| 1941 |
f(a) |
| 1942 |
|
| 1943 |
a.build_types(g, []) |
| 1944 |
|
| 1945 |
from rpython.rtyper import rtyper |
| 1946 |
from rpython.rtyper import rclass |
| 1947 |
rt = rtyper.RPythonTyper(a) |
| 1948 |
rt.specialize() |
| 1949 |
|
| 1950 |
def ll_h(R, f, a): |
| 1951 |
from rpython.rlib.objectmodel import hlinvoke |
| 1952 |
return hlinvoke(R, f, a) |
| 1953 |
|
| 1954 |
from rpython.rtyper import annlowlevel |
| 1955 |
|
| 1956 |
s_f = a.bookkeeper.immutablevalue(f) |
| 1957 |
r_f = rt.getrepr(s_f) |
| 1958 |
|
| 1959 |
s_R = a.bookkeeper.immutablevalue(r_f) |
| 1960 |
s_ll_f = lltype_to_annotation(r_f.lowleveltype) |
| 1961 |
A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A). |
| 1962 |
getuniqueclassdef()) |
| 1963 |
ll_h_graph = annlowlevel.annotate_lowlevel_helper( |
| 1964 |
a, ll_h, [s_R, s_ll_f, SomePtr(A_repr.lowleveltype)]) |
| 1965 |
s = a.binding(ll_h_graph.getreturnvar()) |
| 1966 |
assert s.ll_ptrtype == A_repr.lowleveltype |
| 1967 |
rt.specialize_more_blocks() |
| 1968 |
|
| 1969 |
from rpython.rtyper.llinterp import LLInterpreter |
| 1970 |
interp = LLInterpreter(rt) |
| 1971 |
|
| 1972 |
|
| 1973 |
c_a = A_repr.convert_const(A(None)) |
| 1974 |
res = interp.eval_graph(ll_h_graph, [None, None, c_a]) |
| 1975 |
assert typeOf(res) == A_repr.lowleveltype |
| 1976 |
|
| 1977 |
def test_hlinvoke_method_hltype(): |
| 1978 |
class A(object): |
| 1979 |
def __init__(self, v): |
| 1980 |
self.v = v |
| 1981 |
class Impl(object): |
| 1982 |
def f(self, a): |
| 1983 |
return A(a) |
| 1984 |
|
| 1985 |
from rpython.annotator import annrpython |
| 1986 |
a = annrpython.RPythonAnnotator() |
| 1987 |
|
| 1988 |
def g(): |
| 1989 |
a = A(None) |
| 1990 |
i = Impl() |
| 1991 |
i.f(a) |
| 1992 |
|
| 1993 |
a.build_types(g, []) |
| 1994 |
|
| 1995 |
from rpython.rtyper import rtyper |
| 1996 |
from rpython.rtyper import rclass |
| 1997 |
rt = rtyper.RPythonTyper(a) |
| 1998 |
rt.specialize() |
| 1999 |
|
| 2000 |
def ll_h(R, f, a): |
| 2001 |
from rpython.rlib.objectmodel import hlinvoke |
| 2002 |
return hlinvoke(R, f, a) |
| 2003 |
|
| 2004 |
from rpython.rtyper import annlowlevel |
| 2005 |
|
| 2006 |
Impl_def = a.bookkeeper.getdesc(Impl).getuniqueclassdef() |
| 2007 |
Impl_f_desc = a.bookkeeper.getmethoddesc( |
| 2008 |
a.bookkeeper.getdesc(Impl.f.im_func), |
| 2009 |
Impl_def, |
| 2010 |
Impl_def, |
| 2011 |
'f') |
| 2012 |
s_f = annmodel.SomePBC([Impl_f_desc]) |
| 2013 |
r_f = rt.getrepr(s_f) |
| 2014 |
|
| 2015 |
s_R = a.bookkeeper.immutablevalue(r_f) |
| 2016 |
s_ll_f = lltype_to_annotation(r_f.lowleveltype) |
| 2017 |
A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A). |
| 2018 |
getuniqueclassdef()) |
| 2019 |
ll_h_graph = annlowlevel.annotate_lowlevel_helper( |
| 2020 |
a, ll_h, [s_R, s_ll_f, SomePtr(A_repr.lowleveltype)]) |
| 2021 |
s = a.binding(ll_h_graph.getreturnvar()) |
| 2022 |
assert s.ll_ptrtype == A_repr.lowleveltype |
| 2023 |
rt.specialize_more_blocks() |
| 2024 |
|
| 2025 |
from rpython.rtyper.llinterp import LLInterpreter |
| 2026 |
interp = LLInterpreter(rt) |
| 2027 |
|
| 2028 |
|
| 2029 |
c_f = rclass.getinstancerepr(rt, Impl_def).convert_const(Impl()) |
| 2030 |
c_a = A_repr.convert_const(A(None)) |
| 2031 |
res = interp.eval_graph(ll_h_graph, [None, c_f, c_a]) |
| 2032 |
assert typeOf(res) == A_repr.lowleveltype |
| 2033 |
|
| 2034 |
def test_hlinvoke_pbc_method_hltype(): |
| 2035 |
class A(object): |
| 2036 |
def __init__(self, v): |
| 2037 |
self.v = v |
| 2038 |
class Impl(object): |
| 2039 |
def _freeze_(self): |
| 2040 |
return True |
| 2041 |
|
| 2042 |
def f(self, a): |
| 2043 |
return A(a) |
| 2044 |
|
| 2045 |
from rpython.annotator import annrpython |
| 2046 |
a = annrpython.RPythonAnnotator() |
| 2047 |
|
| 2048 |
i = Impl() |
| 2049 |
|
| 2050 |
def g(): |
| 2051 |
a = A(None) |
| 2052 |
i.f(a) |
| 2053 |
|
| 2054 |
a.build_types(g, []) |
| 2055 |
|
| 2056 |
from rpython.rtyper import rtyper |
| 2057 |
from rpython.rtyper import rclass |
| 2058 |
rt = rtyper.RPythonTyper(a) |
| 2059 |
rt.specialize() |
| 2060 |
|
| 2061 |
def ll_h(R, f, a): |
| 2062 |
from rpython.rlib.objectmodel import hlinvoke |
| 2063 |
return hlinvoke(R, f, a) |
| 2064 |
|
| 2065 |
from rpython.rtyper import annlowlevel |
| 2066 |
|
| 2067 |
s_f = a.bookkeeper.immutablevalue(i.f) |
| 2068 |
r_f = rt.getrepr(s_f) |
| 2069 |
|
| 2070 |
s_R = a.bookkeeper.immutablevalue(r_f) |
| 2071 |
s_ll_f = lltype_to_annotation(r_f.lowleveltype) |
| 2072 |
|
| 2073 |
A_repr = rclass.getinstancerepr(rt, a.bookkeeper.getdesc(A). |
| 2074 |
getuniqueclassdef()) |
| 2075 |
ll_h_graph = annlowlevel.annotate_lowlevel_helper( |
| 2076 |
a, ll_h, [s_R, s_ll_f, SomePtr(A_repr.lowleveltype)]) |
| 2077 |
s = a.binding(ll_h_graph.getreturnvar()) |
| 2078 |
assert s.ll_ptrtype == A_repr.lowleveltype |
| 2079 |
rt.specialize_more_blocks() |
| 2080 |
|
| 2081 |
from rpython.rtyper.llinterp import LLInterpreter |
| 2082 |
interp = LLInterpreter(rt) |
| 2083 |
|
| 2084 |
c_f = r_f.convert_const(i.f) |
| 2085 |
c_a = A_repr.convert_const(A(None)) |
| 2086 |
res = interp.eval_graph(ll_h_graph, [None, c_f, c_a]) |
| 2087 |
assert typeOf(res) == A_repr.lowleveltype |
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
class TestSmallFuncSets(TestRPBC): |
| 2092 |
def setup_class(cls): |
| 2093 |
from rpython.config.translationoption import get_combined_translation_config |
| 2094 |
cls.config = get_combined_translation_config(translating=True) |
| 2095 |
cls.config.translation.withsmallfuncsets = 3 |
| 2096 |
|
| 2097 |
def interpret(self, fn, args, **kwds): |
| 2098 |
kwds['config'] = self.config |
| 2099 |
return TestRPBC.interpret(fn, args, **kwds) |
| 2100 |
|
| 2101 |
def test_class_missing_base_method_should_crash(self): |
| 2102 |
class Base(object): |
| 2103 |
pass |
| 2104 |
class A(Base): |
| 2105 |
def m(self): |
| 2106 |
return 42 |
| 2107 |
class B(Base): |
| 2108 |
def m(self): |
| 2109 |
return 63 |
| 2110 |
def g(n): |
| 2111 |
if n == 1: |
| 2112 |
return A() |
| 2113 |
elif n == 2: |
| 2114 |
return B() |
| 2115 |
else: |
| 2116 |
return Base() |
| 2117 |
def f(n): |
| 2118 |
return g(n).m() |
| 2119 |
|
| 2120 |
assert self.interpret(f, [1]) == 42 |
| 2121 |
assert self.interpret(f, [2]) == 63 |
| 2122 |
e = py.test.raises(ValueError, self.interpret, f, [3]) |
| 2123 |
assert str(e.value).startswith(r"exit case '\xff' not found") |
| 2124 |
|
| 2125 |
@py.test.mark.parametrize('limit', [3, 5]) |
| 2126 |
def test_conversion_table(self, limit): |
| 2127 |
|
| 2128 |
|
| 2129 |
def f1(): |
| 2130 |
return 111 |
| 2131 |
def f2(): |
| 2132 |
return 222 |
| 2133 |
def f3(): |
| 2134 |
return 333 |
| 2135 |
def g(n): |
| 2136 |
if n & 1: |
| 2137 |
return f1 |
| 2138 |
else: |
| 2139 |
return f2 |
| 2140 |
def f(n): |
| 2141 |
x = g(n) |
| 2142 |
if n > 10: |
| 2143 |
x = f3 |
| 2144 |
return x() |
| 2145 |
|
| 2146 |
from rpython.config.translationoption import get_combined_translation_config |
| 2147 |
self.config = get_combined_translation_config(translating=True) |
| 2148 |
self.config.translation.withsmallfuncsets = limit |
| 2149 |
assert self.interpret(f, [3]) == 111 |
| 2150 |
assert self.interpret(f, [4]) == 222 |
| 2151 |
assert self.interpret(f, [13]) == 333 |
| 2152 |
assert self.interpret(f, [14]) == 333 |
| 2153 |
|
| 2154 |
|
| 2155 |
def test_smallfuncsets_basic(): |
| 2156 |
from rpython.translator.translator import TranslationContext, graphof |
| 2157 |
from rpython.config.translationoption import get_combined_translation_config |
| 2158 |
from rpython.rtyper.llinterp import LLInterpreter |
| 2159 |
config = get_combined_translation_config(translating=True) |
| 2160 |
config.translation.withsmallfuncsets = 10 |
| 2161 |
|
| 2162 |
def g(x): |
| 2163 |
return x + 1 |
| 2164 |
def h(x): |
| 2165 |
return x - 1 |
| 2166 |
def f(x, y): |
| 2167 |
if y > 0: |
| 2168 |
func = g |
| 2169 |
else: |
| 2170 |
func = h |
| 2171 |
return func(x) |
| 2172 |
t = TranslationContext(config=config) |
| 2173 |
a = t.buildannotator() |
| 2174 |
a.build_types(f, [int, int]) |
| 2175 |
rtyper = t.buildrtyper() |
| 2176 |
rtyper.specialize() |
| 2177 |
graph = graphof(t, f) |
| 2178 |
calldispatcherop = graph.startblock.exits[0].target.operations[0] |
| 2179 |
assert calldispatcherop.args[0].value._obj0.graph.name == "dispatcher_g_etc" |
| 2180 |
interp = LLInterpreter(rtyper) |
| 2181 |
res = interp.eval_graph(graph, [0, 0]) |
| 2182 |
assert res == -1 |
| 2183 |
res = interp.eval_graph(graph, [0, 1]) |
| 2184 |
assert res == 1 |
| 2185 |
|
| 2186 |
def test_smallfuncsets_method(): |
| 2187 |
from rpython.translator.translator import TranslationContext, graphof |
| 2188 |
from rpython.config.translationoption import get_combined_translation_config |
| 2189 |
from rpython.rtyper.llinterp import LLInterpreter |
| 2190 |
config = get_combined_translation_config(translating=True) |
| 2191 |
config.translation.withsmallfuncsets = 10 |
| 2192 |
|
| 2193 |
class A(object): |
| 2194 |
def meth(self, x): |
| 2195 |
return x - 1 |
| 2196 |
class B(A): |
| 2197 |
def meth(self, x): |
| 2198 |
return x + 1 |
| 2199 |
def f(x, y): |
| 2200 |
if y > 0: |
| 2201 |
a = A() |
| 2202 |
else: |
| 2203 |
a = B() |
| 2204 |
return a.meth(x) |
| 2205 |
t = TranslationContext(config=config) |
| 2206 |
a = t.buildannotator() |
| 2207 |
a.build_types(f, [int, int]) |
| 2208 |
rtyper = t.buildrtyper() |
| 2209 |
rtyper.specialize() |
| 2210 |
graph = graphof(t, f) |
| 2211 |
calldispatcherop = [op for (_, op) in graph.iterblockops() if op.opname == "direct_call"][0] |
| 2212 |
assert calldispatcherop.args[0].value._obj0.graph.name == "dispatcher_meth" |
| 2213 |
interp = LLInterpreter(rtyper) |
| 2214 |
res = interp.eval_graph(graph, [0, 0]) |
| 2215 |
assert res == f(0, 0) |
| 2216 |
res = interp.eval_graph(graph, [0, 1]) |
| 2217 |
assert res == f(0, 1) |