| 1 |
from __future__ import print_function, with_statement |
| 2 |
import types |
| 3 |
import py |
| 4 |
from contextlib import contextmanager |
| 5 |
|
| 6 |
from rpython.flowspace.model import ( |
| 7 |
Constant, mkentrymap, const) |
| 8 |
from rpython.translator.simplify import simplify_graph |
| 9 |
from rpython.flowspace.objspace import build_flow |
| 10 |
from rpython.flowspace.flowcontext import FlowingError, FlowContext |
| 11 |
from rpython.flowspace.bytecode import HostCode |
| 12 |
from rpython.conftest import option |
| 13 |
from rpython.tool.stdlib_opcode import host_bytecode_spec |
| 14 |
|
| 15 |
import os |
| 16 |
import operator |
| 17 |
is_operator = getattr(operator, 'is_', operator.eq) |
| 18 |
|
| 19 |
@contextmanager |
| 20 |
def patching_opcodes(**opcodes): |
| 21 |
meth_names = host_bytecode_spec.method_names |
| 22 |
old_name = {} |
| 23 |
for name, num in opcodes.items(): |
| 24 |
old_name[num] = meth_names[num] |
| 25 |
meth_names[num] = name |
| 26 |
old_tuple = HostCode.opnames |
| 27 |
HostCode.opnames = tuple(host_bytecode_spec.method_names) |
| 28 |
yield |
| 29 |
for name in opcodes: |
| 30 |
meth_names[num] = old_name[num] |
| 31 |
HostCode.opname = old_tuple |
| 32 |
|
| 33 |
|
| 34 |
class Base: |
| 35 |
def codetest(self, func, **kwds): |
| 36 |
import inspect |
| 37 |
try: |
| 38 |
func = func.im_func |
| 39 |
except AttributeError: |
| 40 |
pass |
| 41 |
|
| 42 |
graph = build_flow(func, **kwds) |
| 43 |
graph.source = inspect.getsource(func) |
| 44 |
self.show(graph) |
| 45 |
return graph |
| 46 |
|
| 47 |
def show(self, graph): |
| 48 |
if option.view: |
| 49 |
graph.show() |
| 50 |
|
| 51 |
def all_operations(self, graph): |
| 52 |
result = {} |
| 53 |
for node in graph.iterblocks(): |
| 54 |
for op in node.operations: |
| 55 |
result.setdefault(op.opname, 0) |
| 56 |
result[op.opname] += 1 |
| 57 |
return result |
| 58 |
|
| 59 |
def test_all_opcodes_defined(): |
| 60 |
opnames = set(host_bytecode_spec.method_names) |
| 61 |
methods = set([name for name in dir(FlowContext) if name.upper() == name]) |
| 62 |
handled_elsewhere = set(['EXTENDED_ARG', 'LOAD_REVDB_VAR']) |
| 63 |
missing = opnames - methods - handled_elsewhere |
| 64 |
assert not missing |
| 65 |
|
| 66 |
class TestFlowObjSpace(Base): |
| 67 |
|
| 68 |
def nothing(): |
| 69 |
pass |
| 70 |
|
| 71 |
def test_nothing(self): |
| 72 |
x = self.codetest(self.nothing) |
| 73 |
assert len(x.startblock.exits) == 1 |
| 74 |
link, = x.startblock.exits |
| 75 |
assert link.target == x.returnblock |
| 76 |
|
| 77 |
|
| 78 |
def simplefunc(x): |
| 79 |
return x+1 |
| 80 |
|
| 81 |
def test_simplefunc(self): |
| 82 |
graph = self.codetest(self.simplefunc) |
| 83 |
assert self.all_operations(graph) == {'add': 1} |
| 84 |
|
| 85 |
|
| 86 |
def simplebranch(i, j): |
| 87 |
if i < 0: |
| 88 |
return i |
| 89 |
return j |
| 90 |
|
| 91 |
def test_simplebranch(self): |
| 92 |
x = self.codetest(self.simplebranch) |
| 93 |
|
| 94 |
|
| 95 |
def ifthenelse(i, j): |
| 96 |
if i < 0: |
| 97 |
i = j |
| 98 |
return user_defined_function(i) + 1 |
| 99 |
|
| 100 |
def test_ifthenelse(self): |
| 101 |
x = self.codetest(self.ifthenelse) |
| 102 |
|
| 103 |
|
| 104 |
def loop(x): |
| 105 |
x = abs(x) |
| 106 |
while x: |
| 107 |
x = x - 1 |
| 108 |
|
| 109 |
def test_loop(self): |
| 110 |
graph = self.codetest(self.loop) |
| 111 |
assert self.all_operations(graph) == {'abs': 1, |
| 112 |
'bool': 1, |
| 113 |
'sub': 1} |
| 114 |
|
| 115 |
|
| 116 |
def print_(i): |
| 117 |
print(i, end=" ") |
| 118 |
print(i) |
| 119 |
|
| 120 |
def test_print(self): |
| 121 |
x = self.codetest(self.print_) |
| 122 |
|
| 123 |
def test_bad_print(self): |
| 124 |
def f(x): |
| 125 |
print("Hello", file=x) |
| 126 |
with py.test.raises(FlowingError): |
| 127 |
self.codetest(f) |
| 128 |
|
| 129 |
def test_bad_print2(self): |
| 130 |
def f(x): |
| 131 |
args = ["Hello", x] |
| 132 |
print(*args) |
| 133 |
with py.test.raises(FlowingError): |
| 134 |
self.codetest(f) |
| 135 |
|
| 136 |
|
| 137 |
def while_(i): |
| 138 |
while i > 0: |
| 139 |
i = i - 1 |
| 140 |
|
| 141 |
def test_while(self): |
| 142 |
x = self.codetest(self.while_) |
| 143 |
|
| 144 |
|
| 145 |
def union_easy(i): |
| 146 |
if i: |
| 147 |
pass |
| 148 |
else: |
| 149 |
i = 5 |
| 150 |
return i |
| 151 |
|
| 152 |
def test_union_easy(self): |
| 153 |
x = self.codetest(self.union_easy) |
| 154 |
|
| 155 |
|
| 156 |
def union_hard(i): |
| 157 |
if i: |
| 158 |
i = 5 |
| 159 |
return i |
| 160 |
|
| 161 |
def test_union_hard(self): |
| 162 |
x = self.codetest(self.union_hard) |
| 163 |
|
| 164 |
|
| 165 |
def while_union(i): |
| 166 |
total = 0 |
| 167 |
while i > 0: |
| 168 |
total += i |
| 169 |
i = i - 1 |
| 170 |
return total |
| 171 |
|
| 172 |
def test_while_union(self): |
| 173 |
x = self.codetest(self.while_union) |
| 174 |
|
| 175 |
|
| 176 |
def simple_for(lst): |
| 177 |
total = 0 |
| 178 |
for i in lst: |
| 179 |
total += i |
| 180 |
return total |
| 181 |
|
| 182 |
def test_simple_for(self): |
| 183 |
x = self.codetest(self.simple_for) |
| 184 |
|
| 185 |
|
| 186 |
def nested_whiles(i, j): |
| 187 |
s = '' |
| 188 |
z = 5 |
| 189 |
while z > 0: |
| 190 |
z = z - 1 |
| 191 |
u = i |
| 192 |
while u < j: |
| 193 |
u = u + 1 |
| 194 |
s = s + '.' |
| 195 |
s = s + '!' |
| 196 |
return s |
| 197 |
|
| 198 |
def test_nested_whiles(self): |
| 199 |
x = self.codetest(self.nested_whiles) |
| 200 |
|
| 201 |
|
| 202 |
def break_continue(x): |
| 203 |
result = [] |
| 204 |
i = 0 |
| 205 |
while 1: |
| 206 |
i = i + 1 |
| 207 |
try: |
| 208 |
if i&1: |
| 209 |
continue |
| 210 |
if i >= x: |
| 211 |
break |
| 212 |
finally: |
| 213 |
result.append(i) |
| 214 |
i = i + 1 |
| 215 |
return result |
| 216 |
|
| 217 |
def test_break_continue(self): |
| 218 |
x = self.codetest(self.break_continue) |
| 219 |
|
| 220 |
def test_break_from_handler(self): |
| 221 |
def f(x): |
| 222 |
while True: |
| 223 |
try: |
| 224 |
x() |
| 225 |
except TypeError: |
| 226 |
if x: |
| 227 |
raise |
| 228 |
break |
| 229 |
assert f(0) is None |
| 230 |
graph = self.codetest(f) |
| 231 |
simplify_graph(graph) |
| 232 |
entrymap = mkentrymap(graph) |
| 233 |
links = entrymap[graph.returnblock] |
| 234 |
assert len(links) == 1 |
| 235 |
|
| 236 |
|
| 237 |
def unpack_tuple(lst): |
| 238 |
a, b, c = lst |
| 239 |
|
| 240 |
def test_unpack_tuple(self): |
| 241 |
x = self.codetest(self.unpack_tuple) |
| 242 |
|
| 243 |
|
| 244 |
def reverse_3(lst): |
| 245 |
try: |
| 246 |
a, b, c = lst |
| 247 |
except: |
| 248 |
return 0, 0, 0 |
| 249 |
else: |
| 250 |
return c, b, a |
| 251 |
|
| 252 |
def test_reverse_3(self): |
| 253 |
x = self.codetest(self.reverse_3) |
| 254 |
|
| 255 |
|
| 256 |
def finallys(lst): |
| 257 |
x = 1 |
| 258 |
try: |
| 259 |
x = 2 |
| 260 |
try: |
| 261 |
x = 3 |
| 262 |
a, = lst |
| 263 |
x = 4 |
| 264 |
except KeyError: |
| 265 |
return 5 |
| 266 |
except ValueError: |
| 267 |
return 6 |
| 268 |
b, = lst |
| 269 |
x = 7 |
| 270 |
finally: |
| 271 |
x = 8 |
| 272 |
return x |
| 273 |
|
| 274 |
def test_finallys(self): |
| 275 |
x = self.codetest(self.finallys) |
| 276 |
|
| 277 |
def test_branching_in_finally(self): |
| 278 |
def f(x, y): |
| 279 |
try: |
| 280 |
return x |
| 281 |
finally: |
| 282 |
if x: |
| 283 |
x = 0 |
| 284 |
if y > 0: |
| 285 |
y -= 1 |
| 286 |
return y |
| 287 |
self.codetest(f) |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
def const_pow(): |
| 292 |
return 2 ** 5 |
| 293 |
|
| 294 |
def test_const_pow(self): |
| 295 |
x = self.codetest(self.const_pow) |
| 296 |
|
| 297 |
|
| 298 |
def implicitException(lst): |
| 299 |
try: |
| 300 |
x = lst[5] |
| 301 |
except Exception: |
| 302 |
return 'catch' |
| 303 |
return lst[3] |
| 304 |
|
| 305 |
def test_implicitException(self): |
| 306 |
x = self.codetest(self.implicitException) |
| 307 |
simplify_graph(x) |
| 308 |
self.show(x) |
| 309 |
for link in x.iterlinks(): |
| 310 |
assert link.target is not x.exceptblock |
| 311 |
|
| 312 |
def implicitAttributeError(x): |
| 313 |
try: |
| 314 |
x = getattr(x, "y") |
| 315 |
except AttributeError: |
| 316 |
return 'catch' |
| 317 |
return getattr(x, "z") |
| 318 |
|
| 319 |
def test_implicitAttributeError(self): |
| 320 |
x = self.codetest(self.implicitAttributeError) |
| 321 |
simplify_graph(x) |
| 322 |
self.show(x) |
| 323 |
for link in x.iterlinks(): |
| 324 |
assert link.target is not x.exceptblock |
| 325 |
|
| 326 |
|
| 327 |
def implicitException_int_and_id(x): |
| 328 |
try: |
| 329 |
return int(x) + id(x) |
| 330 |
except TypeError: |
| 331 |
return 0 |
| 332 |
|
| 333 |
def test_implicitException_int_and_id(self): |
| 334 |
x = self.codetest(self.implicitException_int_and_id) |
| 335 |
simplify_graph(x) |
| 336 |
self.show(x) |
| 337 |
assert len(x.startblock.exits) == 1 |
| 338 |
assert x.startblock.exits[0].target is x.returnblock |
| 339 |
|
| 340 |
|
| 341 |
def implicitException_os_stat(x): |
| 342 |
try: |
| 343 |
return os.stat(x) |
| 344 |
except OSError: |
| 345 |
return 0 |
| 346 |
|
| 347 |
def test_implicitException_os_stat(self): |
| 348 |
x = self.codetest(self.implicitException_os_stat) |
| 349 |
simplify_graph(x) |
| 350 |
self.show(x) |
| 351 |
assert len(x.startblock.exits) == 3 |
| 352 |
d = {} |
| 353 |
for link in x.startblock.exits: |
| 354 |
d[link.exitcase] = True |
| 355 |
assert d == {None: True, OSError: True, Exception: True} |
| 356 |
|
| 357 |
|
| 358 |
def reraiseAnythingDicCase(dic): |
| 359 |
try: |
| 360 |
dic[5] |
| 361 |
except: |
| 362 |
raise |
| 363 |
|
| 364 |
def test_reraiseAnythingDicCase(self): |
| 365 |
x = self.codetest(self.reraiseAnythingDicCase) |
| 366 |
simplify_graph(x) |
| 367 |
self.show(x) |
| 368 |
found = {} |
| 369 |
for link in x.iterlinks(): |
| 370 |
if link.target is x.exceptblock: |
| 371 |
if isinstance(link.args[0], Constant): |
| 372 |
found[link.args[0].value] = True |
| 373 |
else: |
| 374 |
found[link.exitcase] = None |
| 375 |
assert found == {IndexError: True, KeyError: True, Exception: None} |
| 376 |
|
| 377 |
def reraiseAnything(x): |
| 378 |
try: |
| 379 |
pow(x, 5) |
| 380 |
except: |
| 381 |
raise |
| 382 |
|
| 383 |
def test_reraiseAnything(self): |
| 384 |
x = self.codetest(self.reraiseAnything) |
| 385 |
simplify_graph(x) |
| 386 |
self.show(x) |
| 387 |
found = {} |
| 388 |
for link in x.iterlinks(): |
| 389 |
if link.target is x.exceptblock: |
| 390 |
assert isinstance(link.args[0], Constant) |
| 391 |
found[link.args[0].value] = True |
| 392 |
assert found == {ValueError: True, ZeroDivisionError: True, OverflowError: True} |
| 393 |
|
| 394 |
def loop_in_bare_except_bug(lst): |
| 395 |
try: |
| 396 |
for x in lst: |
| 397 |
pass |
| 398 |
except: |
| 399 |
lst.append(5) |
| 400 |
raise |
| 401 |
|
| 402 |
def test_loop_in_bare_except_bug(self): |
| 403 |
x = self.codetest(self.loop_in_bare_except_bug) |
| 404 |
simplify_graph(x) |
| 405 |
self.show(x) |
| 406 |
|
| 407 |
|
| 408 |
def freevar(self, x): |
| 409 |
def adder(y): |
| 410 |
return x+y |
| 411 |
return adder |
| 412 |
|
| 413 |
def test_freevar(self): |
| 414 |
x = self.codetest(self.freevar(3)) |
| 415 |
|
| 416 |
|
| 417 |
def raise1(msg): |
| 418 |
raise IndexError |
| 419 |
|
| 420 |
def test_raise1(self): |
| 421 |
x = self.codetest(self.raise1) |
| 422 |
simplify_graph(x) |
| 423 |
self.show(x) |
| 424 |
ops = x.startblock.operations |
| 425 |
assert len(ops) == 2 |
| 426 |
assert ops[0].opname == 'simple_call' |
| 427 |
assert ops[0].args == [Constant(IndexError)] |
| 428 |
assert ops[1].opname == 'type' |
| 429 |
assert ops[1].args == [ops[0].result] |
| 430 |
assert x.startblock.exits[0].args == [ops[1].result, ops[0].result] |
| 431 |
assert x.startblock.exits[0].target is x.exceptblock |
| 432 |
|
| 433 |
def test_simple_raise(self): |
| 434 |
def f(): |
| 435 |
raise ValueError('ouch') |
| 436 |
x = self.codetest(f) |
| 437 |
simplify_graph(x) |
| 438 |
self.show(x) |
| 439 |
ops = x.startblock.operations |
| 440 |
assert ops[0].opname == 'simple_call' |
| 441 |
assert ops[0].args == [Constant(ValueError), Constant('ouch')] |
| 442 |
|
| 443 |
def test_raise_prebuilt(self): |
| 444 |
error = ValueError('ouch') |
| 445 |
def g(x): return x |
| 446 |
def f(): |
| 447 |
raise g(error) |
| 448 |
x = self.codetest(f) |
| 449 |
simplify_graph(x) |
| 450 |
self.show(x) |
| 451 |
ops = x.startblock.operations |
| 452 |
assert ops[0].opname == 'simple_call' |
| 453 |
assert ops[0].args == [const(g), const(error)] |
| 454 |
|
| 455 |
|
| 456 |
def raise3(msg): |
| 457 |
raise IndexError(msg) |
| 458 |
|
| 459 |
def test_raise3(self): |
| 460 |
x = self.codetest(self.raise3) |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
def raise4(stuff): |
| 465 |
raise stuff |
| 466 |
|
| 467 |
def test_raise4(self): |
| 468 |
x = self.codetest(self.raise4) |
| 469 |
|
| 470 |
|
| 471 |
def raise_and_catch_1(exception_instance): |
| 472 |
try: |
| 473 |
raise exception_instance |
| 474 |
except IndexError: |
| 475 |
return -1 |
| 476 |
return 0 |
| 477 |
|
| 478 |
def test_raise_and_catch_1(self): |
| 479 |
x = self.codetest(self.raise_and_catch_1) |
| 480 |
|
| 481 |
|
| 482 |
def catch_simple_call(): |
| 483 |
try: |
| 484 |
user_defined_function() |
| 485 |
except IndexError: |
| 486 |
return -1 |
| 487 |
return 0 |
| 488 |
|
| 489 |
def test_catch_simple_call(self): |
| 490 |
x = self.codetest(self.catch_simple_call) |
| 491 |
|
| 492 |
|
| 493 |
def multiple_catch_simple_call(): |
| 494 |
try: |
| 495 |
user_defined_function() |
| 496 |
except (IndexError, OSError): |
| 497 |
return -1 |
| 498 |
return 0 |
| 499 |
|
| 500 |
def test_multiple_catch_simple_call(self): |
| 501 |
graph = self.codetest(self.multiple_catch_simple_call) |
| 502 |
simplify_graph(graph) |
| 503 |
assert self.all_operations(graph) == {'simple_call': 1} |
| 504 |
entrymap = mkentrymap(graph) |
| 505 |
links = entrymap[graph.returnblock] |
| 506 |
assert len(links) == 3 |
| 507 |
assert (dict.fromkeys([link.exitcase for link in links]) == |
| 508 |
dict.fromkeys([None, IndexError, OSError])) |
| 509 |
links = entrymap[graph.exceptblock] |
| 510 |
assert len(links) == 1 |
| 511 |
assert links[0].exitcase is Exception |
| 512 |
|
| 513 |
|
| 514 |
def dellocal(): |
| 515 |
x = 1 |
| 516 |
del x |
| 517 |
for i in range(10): |
| 518 |
pass |
| 519 |
|
| 520 |
def test_dellocal(self): |
| 521 |
x = self.codetest(self.dellocal) |
| 522 |
|
| 523 |
|
| 524 |
def globalconstdict(name): |
| 525 |
x = DATA['x'] |
| 526 |
z = DATA[name] |
| 527 |
return x, z |
| 528 |
|
| 529 |
def test_globalconstdict(self): |
| 530 |
x = self.codetest(self.globalconstdict) |
| 531 |
|
| 532 |
def test_dont_write_globals(self): |
| 533 |
def f(): |
| 534 |
global DATA |
| 535 |
DATA = 5 |
| 536 |
with py.test.raises(FlowingError) as excinfo: |
| 537 |
self.codetest(f) |
| 538 |
assert "modify global" in str(excinfo.value) |
| 539 |
assert DATA == {'x': 5, 'y': 6} |
| 540 |
|
| 541 |
|
| 542 |
def dictliteral(name): |
| 543 |
x = {'x': 1} |
| 544 |
return x |
| 545 |
|
| 546 |
def test_dictliteral(self): |
| 547 |
x = self.codetest(self.dictliteral) |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
def specialcases(x): |
| 552 |
operator.lt(x,3) |
| 553 |
operator.le(x,3) |
| 554 |
operator.eq(x,3) |
| 555 |
operator.ne(x,3) |
| 556 |
operator.gt(x,3) |
| 557 |
operator.ge(x,3) |
| 558 |
is_operator(x,3) |
| 559 |
operator.__lt__(x,3) |
| 560 |
operator.__le__(x,3) |
| 561 |
operator.__eq__(x,3) |
| 562 |
operator.__ne__(x,3) |
| 563 |
operator.__gt__(x,3) |
| 564 |
operator.__ge__(x,3) |
| 565 |
operator.xor(x,3) |
| 566 |
|
| 567 |
operator.eq(2,3) |
| 568 |
operator.__gt__(2,3) |
| 569 |
|
| 570 |
def test_specialcases(self): |
| 571 |
x = self.codetest(self.specialcases) |
| 572 |
from rpython.translator.simplify import join_blocks |
| 573 |
join_blocks(x) |
| 574 |
assert len(x.startblock.operations) == 14 |
| 575 |
for op in x.startblock.operations: |
| 576 |
assert op.opname in ['lt', 'le', 'eq', 'ne', |
| 577 |
'gt', 'ge', 'is_', 'xor'] |
| 578 |
assert len(op.args) == 2 |
| 579 |
assert op.args[1].value == 3 |
| 580 |
|
| 581 |
def test_unary_ops(self): |
| 582 |
def f(x): |
| 583 |
return not ~-x |
| 584 |
graph = self.codetest(f) |
| 585 |
assert self.all_operations(graph) == {'bool': 1, 'invert': 1, 'neg': 1} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
def wearetranslated(x): |
| 590 |
from rpython.rlib.objectmodel import we_are_translated |
| 591 |
if we_are_translated(): |
| 592 |
return x |
| 593 |
else: |
| 594 |
some_name_error_here |
| 595 |
|
| 596 |
def test_wearetranslated(self): |
| 597 |
x = self.codetest(self.wearetranslated) |
| 598 |
from rpython.translator.simplify import join_blocks |
| 599 |
join_blocks(x) |
| 600 |
|
| 601 |
assert len(x.startblock.operations) == 0 |
| 602 |
assert len(x.startblock.exits) == 1 |
| 603 |
assert x.startblock.exits[0].target is x.returnblock |
| 604 |
|
| 605 |
|
| 606 |
def jump_target_specialization(x): |
| 607 |
if x: |
| 608 |
n = 5 |
| 609 |
else: |
| 610 |
n = 6 |
| 611 |
return n*2 |
| 612 |
|
| 613 |
def test_jump_target_specialization(self): |
| 614 |
x = self.codetest(self.jump_target_specialization) |
| 615 |
for block in x.iterblocks(): |
| 616 |
for op in block.operations: |
| 617 |
assert op.opname != 'mul', "mul should have disappeared" |
| 618 |
|
| 619 |
|
| 620 |
def highly_branching_example(a,b,c,d,e,f,g,h,i,j): |
| 621 |
if a: |
| 622 |
x1 = 1 |
| 623 |
else: |
| 624 |
x1 = 2 |
| 625 |
if b: |
| 626 |
x2 = 1 |
| 627 |
else: |
| 628 |
x2 = 2 |
| 629 |
if c: |
| 630 |
x3 = 1 |
| 631 |
else: |
| 632 |
x3 = 2 |
| 633 |
if d: |
| 634 |
x4 = 1 |
| 635 |
else: |
| 636 |
x4 = 2 |
| 637 |
if e: |
| 638 |
x5 = 1 |
| 639 |
else: |
| 640 |
x5 = 2 |
| 641 |
if f: |
| 642 |
x6 = 1 |
| 643 |
else: |
| 644 |
x6 = 2 |
| 645 |
if g: |
| 646 |
x7 = 1 |
| 647 |
else: |
| 648 |
x7 = 2 |
| 649 |
if h: |
| 650 |
x8 = 1 |
| 651 |
else: |
| 652 |
x8 = 2 |
| 653 |
if i: |
| 654 |
x9 = 1 |
| 655 |
else: |
| 656 |
x9 = 2 |
| 657 |
if j: |
| 658 |
x10 = 1 |
| 659 |
else: |
| 660 |
x10 = 2 |
| 661 |
return (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) |
| 662 |
|
| 663 |
def test_highly_branching_example(self): |
| 664 |
x = self.codetest(self.highly_branching_example) |
| 665 |
simplify_graph(x) |
| 666 |
|
| 667 |
assert len(list(x.iterblocks())) + len(list(x.iterlinks())) < 60 |
| 668 |
|
| 669 |
|
| 670 |
def test_unfrozen_user_class1(self): |
| 671 |
class C: |
| 672 |
def __nonzero__(self): |
| 673 |
return True |
| 674 |
c = C() |
| 675 |
def f(): |
| 676 |
if c: |
| 677 |
return 1 |
| 678 |
else: |
| 679 |
return 2 |
| 680 |
graph = self.codetest(f) |
| 681 |
|
| 682 |
results = [] |
| 683 |
for link in graph.iterlinks(): |
| 684 |
if link.target == graph.returnblock: |
| 685 |
results.extend(link.args) |
| 686 |
assert len(results) == 2 |
| 687 |
|
| 688 |
def test_unfrozen_user_class2(self): |
| 689 |
class C: |
| 690 |
def __add__(self, other): |
| 691 |
return 4 |
| 692 |
c = C() |
| 693 |
d = C() |
| 694 |
def f(): |
| 695 |
return c+d |
| 696 |
graph = self.codetest(f) |
| 697 |
|
| 698 |
results = [] |
| 699 |
for link in graph.iterlinks(): |
| 700 |
if link.target == graph.returnblock: |
| 701 |
results.extend(link.args) |
| 702 |
assert not isinstance(results[0], Constant) |
| 703 |
|
| 704 |
def test_frozen_user_class1(self): |
| 705 |
class C: |
| 706 |
def __nonzero__(self): |
| 707 |
return True |
| 708 |
def _freeze_(self): |
| 709 |
return True |
| 710 |
c = C() |
| 711 |
def f(): |
| 712 |
if c: |
| 713 |
return 1 |
| 714 |
else: |
| 715 |
return 2 |
| 716 |
|
| 717 |
graph = self.codetest(f) |
| 718 |
|
| 719 |
results = [] |
| 720 |
for link in graph.iterlinks(): |
| 721 |
if link.target == graph.returnblock: |
| 722 |
results.extend(link.args) |
| 723 |
assert len(results) == 1 |
| 724 |
|
| 725 |
def test_frozen_user_class2(self): |
| 726 |
class C: |
| 727 |
def __add__(self, other): |
| 728 |
return 4 |
| 729 |
def _freeze_(self): |
| 730 |
return True |
| 731 |
c = C() |
| 732 |
d = C() |
| 733 |
def f(): |
| 734 |
return c+d |
| 735 |
graph = self.codetest(f) |
| 736 |
|
| 737 |
results = [] |
| 738 |
for link in graph.iterlinks(): |
| 739 |
if link.target == graph.returnblock: |
| 740 |
results.extend(link.args) |
| 741 |
assert results == [Constant(4)] |
| 742 |
|
| 743 |
def test_const_star_call(self): |
| 744 |
def g(a=1,b=2,c=3): |
| 745 |
pass |
| 746 |
def f(): |
| 747 |
return g(1,*(2,3)) |
| 748 |
graph = self.codetest(f) |
| 749 |
for block in graph.iterblocks(): |
| 750 |
for op in block.operations: |
| 751 |
assert not op.opname == "call_args" |
| 752 |
|
| 753 |
def test_starstar_call(self): |
| 754 |
"""Check that CALL_FUNCTION_KW and CALL_FUNCTION_VAR_KW raise a |
| 755 |
useful error. |
| 756 |
""" |
| 757 |
def g(a, b, c): |
| 758 |
return a*b*c |
| 759 |
def f1(): |
| 760 |
return g(**{'a':0}) |
| 761 |
with py.test.raises(FlowingError) as excinfo: |
| 762 |
graph = self.codetest(f1) |
| 763 |
assert 'Dict-unpacking' in str(excinfo.value) |
| 764 |
def f2(): |
| 765 |
return g(*(0,), **{'c':3}) |
| 766 |
with py.test.raises(FlowingError) as excinfo: |
| 767 |
graph = self.codetest(f2) |
| 768 |
assert 'Dict-unpacking' in str(excinfo.value) |
| 769 |
|
| 770 |
def test_kwarg_call(self): |
| 771 |
def g(x): |
| 772 |
return x |
| 773 |
def f(): |
| 774 |
return g(x=2) |
| 775 |
graph = self.codetest(f) |
| 776 |
for block in graph.iterblocks(): |
| 777 |
for op in block.operations: |
| 778 |
assert op.opname == "call_args" |
| 779 |
assert op.args == map(Constant, [g, (0, ('x',), False), 2]) |
| 780 |
|
| 781 |
def test_catch_importerror_1(self): |
| 782 |
def f(): |
| 783 |
try: |
| 784 |
import rpython.this_does_not_exist |
| 785 |
except ImportError: |
| 786 |
return 1 |
| 787 |
graph = self.codetest(f) |
| 788 |
simplify_graph(graph) |
| 789 |
self.show(graph) |
| 790 |
assert not graph.startblock.operations |
| 791 |
assert len(graph.startblock.exits) == 1 |
| 792 |
assert graph.startblock.exits[0].target is graph.returnblock |
| 793 |
|
| 794 |
def test_catch_importerror_2(self): |
| 795 |
def f(): |
| 796 |
try: |
| 797 |
from rpython import this_does_not_exist |
| 798 |
except ImportError: |
| 799 |
return 1 |
| 800 |
graph = self.codetest(f) |
| 801 |
simplify_graph(graph) |
| 802 |
self.show(graph) |
| 803 |
assert not graph.startblock.operations |
| 804 |
assert len(graph.startblock.exits) == 1 |
| 805 |
assert graph.startblock.exits[0].target is graph.returnblock |
| 806 |
|
| 807 |
def test_importerror_1(self): |
| 808 |
def f(): |
| 809 |
import rpython.this_does_not_exist |
| 810 |
py.test.raises(ImportError, 'self.codetest(f)') |
| 811 |
|
| 812 |
def test_importerror_2(self): |
| 813 |
def f(): |
| 814 |
from rpython import this_does_not_exist |
| 815 |
py.test.raises(ImportError, 'self.codetest(f)') |
| 816 |
|
| 817 |
def test_importerror_3(self): |
| 818 |
def f(): |
| 819 |
import rpython.flowspace.test.cant_import |
| 820 |
e = py.test.raises(ImportError, 'self.codetest(f)') |
| 821 |
assert "some explanation here" in str(e.value) |
| 822 |
|
| 823 |
def test_relative_import(self): |
| 824 |
def f(): |
| 825 |
from ..objspace import build_flow |
| 826 |
|
| 827 |
assert f() is None |
| 828 |
self.codetest(f) |
| 829 |
|
| 830 |
def test_mergeable(self): |
| 831 |
def myfunc(x): |
| 832 |
if x: |
| 833 |
from rpython.flowspace.flowcontext import BytecodeCorruption |
| 834 |
s = 12 |
| 835 |
else: |
| 836 |
s = x.abc |
| 837 |
return x[s] |
| 838 |
graph = self.codetest(myfunc) |
| 839 |
|
| 840 |
@py.test.mark.xfail |
| 841 |
def test_unichr_constfold(self): |
| 842 |
def myfunc(): |
| 843 |
return unichr(1234) |
| 844 |
graph = self.codetest(myfunc) |
| 845 |
assert graph.startblock.exits[0].target is graph.returnblock |
| 846 |
|
| 847 |
@py.test.mark.xfail |
| 848 |
def test_unicode_constfold(self): |
| 849 |
def myfunc(): |
| 850 |
return unicode("1234") |
| 851 |
graph = self.codetest(myfunc) |
| 852 |
assert graph.startblock.exits[0].target is graph.returnblock |
| 853 |
|
| 854 |
def test_unicode(self): |
| 855 |
def myfunc(n): |
| 856 |
try: |
| 857 |
return unicode(chr(n)) |
| 858 |
except UnicodeDecodeError: |
| 859 |
return None |
| 860 |
graph = self.codetest(myfunc) |
| 861 |
simplify_graph(graph) |
| 862 |
assert graph.startblock.canraise |
| 863 |
assert graph.startblock.exits[0].target is graph.returnblock |
| 864 |
assert graph.startblock.exits[1].target is graph.returnblock |
| 865 |
|
| 866 |
def test_getitem(self): |
| 867 |
def f(c, x): |
| 868 |
try: |
| 869 |
return c[x] |
| 870 |
except Exception: |
| 871 |
raise |
| 872 |
graph = self.codetest(f) |
| 873 |
simplify_graph(graph) |
| 874 |
assert self.all_operations(graph) == {'getitem_idx': 1} |
| 875 |
|
| 876 |
g = lambda: None |
| 877 |
def f(c, x): |
| 878 |
try: |
| 879 |
return c[x] |
| 880 |
finally: |
| 881 |
g() |
| 882 |
graph = self.codetest(f) |
| 883 |
simplify_graph(graph) |
| 884 |
assert self.all_operations(graph) == {'getitem_idx': 1, |
| 885 |
'simple_call': 2} |
| 886 |
|
| 887 |
def f(c, x): |
| 888 |
try: |
| 889 |
return c[x] |
| 890 |
except IndexError: |
| 891 |
raise |
| 892 |
graph = self.codetest(f) |
| 893 |
simplify_graph(graph) |
| 894 |
assert self.all_operations(graph) == {'getitem_idx': 1} |
| 895 |
|
| 896 |
def f(c, x): |
| 897 |
try: |
| 898 |
return c[x] |
| 899 |
except KeyError: |
| 900 |
raise |
| 901 |
graph = self.codetest(f) |
| 902 |
simplify_graph(graph) |
| 903 |
assert self.all_operations(graph) == {'getitem': 1} |
| 904 |
|
| 905 |
def f(c, x): |
| 906 |
try: |
| 907 |
return c[x] |
| 908 |
except ValueError: |
| 909 |
raise |
| 910 |
graph = self.codetest(f) |
| 911 |
simplify_graph(graph) |
| 912 |
assert self.all_operations(graph) == {'getitem': 1} |
| 913 |
|
| 914 |
def f(c, x): |
| 915 |
try: |
| 916 |
return c[x] |
| 917 |
except Exception: |
| 918 |
return -1 |
| 919 |
graph = self.codetest(f) |
| 920 |
simplify_graph(graph) |
| 921 |
self.show(graph) |
| 922 |
assert self.all_operations(graph) == {'getitem_idx': 1} |
| 923 |
|
| 924 |
def f(c, x): |
| 925 |
try: |
| 926 |
return c[x] |
| 927 |
except IndexError: |
| 928 |
return -1 |
| 929 |
graph = self.codetest(f) |
| 930 |
simplify_graph(graph) |
| 931 |
assert self.all_operations(graph) == {'getitem_idx': 1} |
| 932 |
|
| 933 |
def f(c, x): |
| 934 |
try: |
| 935 |
return c[x] |
| 936 |
except KeyError: |
| 937 |
return -1 |
| 938 |
graph = self.codetest(f) |
| 939 |
simplify_graph(graph) |
| 940 |
assert self.all_operations(graph) == {'getitem': 1} |
| 941 |
|
| 942 |
def f(c, x): |
| 943 |
try: |
| 944 |
return c[x] |
| 945 |
except ValueError: |
| 946 |
return -1 |
| 947 |
graph = self.codetest(f) |
| 948 |
simplify_graph(graph) |
| 949 |
assert self.all_operations(graph) == {'getitem': 1} |
| 950 |
|
| 951 |
def test_delitem(self): |
| 952 |
def f(c, x): |
| 953 |
del c[x] |
| 954 |
graph = self.codetest(f) |
| 955 |
simplify_graph(graph) |
| 956 |
assert self.all_operations(graph) == {'delitem': 1} |
| 957 |
|
| 958 |
def test_context_manager(self): |
| 959 |
def f(c, x): |
| 960 |
with x: |
| 961 |
pass |
| 962 |
graph = self.codetest(f) |
| 963 |
|
| 964 |
assert self.all_operations(graph) == {'getattr': 2, |
| 965 |
'simple_call': 2} |
| 966 |
|
| 967 |
def g(): pass |
| 968 |
def f(c, x): |
| 969 |
with x: |
| 970 |
res = g() |
| 971 |
return res |
| 972 |
graph = self.codetest(f) |
| 973 |
assert self.all_operations(graph) == { |
| 974 |
'getattr': 2, |
| 975 |
'simple_call': 4, |
| 976 |
} |
| 977 |
|
| 978 |
def test_return_in_with(self): |
| 979 |
def f(x): |
| 980 |
with x: |
| 981 |
return 1 |
| 982 |
graph = self.codetest(f) |
| 983 |
simplify_graph(graph) |
| 984 |
assert self.all_operations(graph) == {'getattr': 2, 'simple_call': 2} |
| 985 |
|
| 986 |
def test_break_in_with(self): |
| 987 |
def f(n, x): |
| 988 |
for i in range(n): |
| 989 |
with x: |
| 990 |
break |
| 991 |
return 1 |
| 992 |
self.codetest(f) |
| 993 |
|
| 994 |
def monkey_patch_code(self, code, stacksize, flags, codestring, names, varnames): |
| 995 |
c = code |
| 996 |
return types.CodeType(c.co_argcount, c.co_nlocals, stacksize, flags, |
| 997 |
codestring, c.co_consts, names, varnames, |
| 998 |
c.co_filename, c.co_name, c.co_firstlineno, |
| 999 |
c.co_lnotab) |
| 1000 |
|
| 1001 |
def test_callmethod_opcode(self): |
| 1002 |
""" Tests code generated by pypy-c compiled with CALL_METHOD |
| 1003 |
bytecode |
| 1004 |
""" |
| 1005 |
with patching_opcodes(CALL_METHOD=202, LOOKUP_METHOD=201): |
| 1006 |
class X: |
| 1007 |
def m(self): |
| 1008 |
return 3 |
| 1009 |
|
| 1010 |
def f(): |
| 1011 |
x = X() |
| 1012 |
return x.m() |
| 1013 |
|
| 1014 |
|
| 1015 |
pypy_code = 't\x00\x00\x83\x00\x00}\x00\x00|\x00\x00\xc9\x01\x00\xca\x00\x00S' |
| 1016 |
new_c = self.monkey_patch_code(f.__code__, 3, 3, pypy_code, ('X', 'x', 'm'), ('x',)) |
| 1017 |
f2 = types.FunctionType(new_c, locals(), 'f') |
| 1018 |
|
| 1019 |
graph = self.codetest(f2) |
| 1020 |
all_ops = self.all_operations(graph) |
| 1021 |
assert all_ops['simple_call'] == 2 |
| 1022 |
assert all_ops['getattr'] == 1 |
| 1023 |
|
| 1024 |
@py.test.mark.skipif('sys.version_info < (2, 7)') |
| 1025 |
def test_build_list_from_arg_opcode(self): |
| 1026 |
""" Tests code generated by pypy-c compiled with BUILD_LIST_FROM_ARG |
| 1027 |
bytecode |
| 1028 |
""" |
| 1029 |
with patching_opcodes(BUILD_LIST_FROM_ARG=203): |
| 1030 |
def f(): |
| 1031 |
return [i for i in "abc"] |
| 1032 |
|
| 1033 |
|
| 1034 |
pypy_code = 'd\x01\x00\xcb\x00\x00D]\x0c\x00}\x00\x00|\x00\x00^\x02\x00q\x07\x00S' |
| 1035 |
new_c = self.monkey_patch_code(f.__code__, 3, 67, pypy_code, (), |
| 1036 |
('i',)) |
| 1037 |
f2 = types.FunctionType(new_c, locals(), 'f') |
| 1038 |
|
| 1039 |
graph = self.codetest(f2) |
| 1040 |
all_ops = self.all_operations(graph) |
| 1041 |
assert all_ops == {'newlist': 1, 'getattr': 1, 'simple_call': 1, |
| 1042 |
'iter': 1, 'next': 1} |
| 1043 |
|
| 1044 |
def test_dont_capture_RuntimeError(self): |
| 1045 |
class Foo: |
| 1046 |
def __hash__(self): |
| 1047 |
return hash(self) |
| 1048 |
foolist = [Foo()] |
| 1049 |
def f(): |
| 1050 |
return foolist[0] |
| 1051 |
py.test.raises(RuntimeError, "self.codetest(f)") |
| 1052 |
|
| 1053 |
def test_getslice_constfold(self): |
| 1054 |
def check(f, expected): |
| 1055 |
graph = self.codetest(f) |
| 1056 |
assert graph.startblock.operations == [] |
| 1057 |
[link] = graph.startblock.exits |
| 1058 |
assert link.target is graph.returnblock |
| 1059 |
assert isinstance(link.args[0], Constant) |
| 1060 |
assert link.args[0].value == expected |
| 1061 |
|
| 1062 |
def f1(): |
| 1063 |
s = 'hello' |
| 1064 |
return s[:-2] |
| 1065 |
check(f1, 'hel') |
| 1066 |
|
| 1067 |
def f2(): |
| 1068 |
s = 'hello' |
| 1069 |
return s[:] |
| 1070 |
check(f2, 'hello') |
| 1071 |
|
| 1072 |
def f3(): |
| 1073 |
s = 'hello' |
| 1074 |
return s[-3:] |
| 1075 |
check(f3, 'llo') |
| 1076 |
|
| 1077 |
def test_constfold_attribute_error(self): |
| 1078 |
def f(x): |
| 1079 |
try: |
| 1080 |
"".invalid |
| 1081 |
finally: |
| 1082 |
if x and 0: |
| 1083 |
raise TypeError() |
| 1084 |
with py.test.raises(FlowingError) as excinfo: |
| 1085 |
self.codetest(f) |
| 1086 |
assert 'getattr' in str(excinfo.value) |
| 1087 |
|
| 1088 |
def test_constfold_exception(self): |
| 1089 |
def f(): |
| 1090 |
return (3 + 2) / (4 - 2 * 2) |
| 1091 |
with py.test.raises(FlowingError) as excinfo: |
| 1092 |
self.codetest(f) |
| 1093 |
assert 'div(5, 0)' in str(excinfo.value) |
| 1094 |
|
| 1095 |
def test_nonconstant_except(self): |
| 1096 |
def f(exc_cls): |
| 1097 |
try: |
| 1098 |
raise AttributeError |
| 1099 |
except exc_cls: |
| 1100 |
pass |
| 1101 |
with py.test.raises(FlowingError): |
| 1102 |
self.codetest(f) |
| 1103 |
|
| 1104 |
def test__flowspace_rewrite_directly_as_(self): |
| 1105 |
def g(x): |
| 1106 |
pass |
| 1107 |
def f(x): |
| 1108 |
pass |
| 1109 |
f._flowspace_rewrite_directly_as_ = g |
| 1110 |
def h(x): |
| 1111 |
f(x) |
| 1112 |
graph = self.codetest(h) |
| 1113 |
assert self.all_operations(graph) == {'simple_call': 1} |
| 1114 |
for block in graph.iterblocks(): |
| 1115 |
if block.operations: |
| 1116 |
op = block.operations[0] |
| 1117 |
assert op.opname == 'simple_call' |
| 1118 |
assert op.args[0] == Constant(g) |
| 1119 |
|
| 1120 |
|
| 1121 |
def test_cannot_catch_special_exceptions(self): |
| 1122 |
def f(): |
| 1123 |
try: |
| 1124 |
f() |
| 1125 |
except NotImplementedError: |
| 1126 |
pass |
| 1127 |
py.test.raises(FlowingError, "self.codetest(f)") |
| 1128 |
|
| 1129 |
def f(): |
| 1130 |
try: |
| 1131 |
f() |
| 1132 |
except AssertionError: |
| 1133 |
pass |
| 1134 |
py.test.raises(FlowingError, "self.codetest(f)") |
| 1135 |
|
| 1136 |
def test_cannot_catch_special_exceptions_2(self): |
| 1137 |
class MyNIE(NotImplementedError): |
| 1138 |
pass |
| 1139 |
def f(): |
| 1140 |
try: |
| 1141 |
f() |
| 1142 |
except MyNIE: |
| 1143 |
pass |
| 1144 |
py.test.raises(FlowingError, "self.codetest(f)") |
| 1145 |
|
| 1146 |
def f(): |
| 1147 |
try: |
| 1148 |
f() |
| 1149 |
except (ValueError, MyNIE): |
| 1150 |
pass |
| 1151 |
py.test.raises(FlowingError, "self.codetest(f)") |
| 1152 |
|
| 1153 |
def test_locals_dict(self): |
| 1154 |
def f(): |
| 1155 |
x = 5 |
| 1156 |
return x |
| 1157 |
exec("None") |
| 1158 |
graph = self.codetest(f) |
| 1159 |
assert len(graph.startblock.exits) == 1 |
| 1160 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1161 |
|
| 1162 |
def test_global_variable(self): |
| 1163 |
def global_var_missing(): |
| 1164 |
return a |
| 1165 |
|
| 1166 |
with py.test.raises(FlowingError) as rex: |
| 1167 |
self.codetest(global_var_missing) |
| 1168 |
assert str(rex.exconly()).find("global variable 'a' undeclared") |
| 1169 |
|
| 1170 |
def test_eval(self): |
| 1171 |
exec("def f(): return a") |
| 1172 |
with py.test.raises(FlowingError): |
| 1173 |
self.codetest(f) |
| 1174 |
|
| 1175 |
@py.test.mark.xfail(reason="closures aren't supported") |
| 1176 |
def test_cellvar_store(self): |
| 1177 |
def f(): |
| 1178 |
x = 5 |
| 1179 |
return x |
| 1180 |
lambda: x |
| 1181 |
graph = self.codetest(f) |
| 1182 |
assert len(graph.startblock.exits) == 1 |
| 1183 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1184 |
|
| 1185 |
@py.test.mark.xfail(reason="closures aren't supported") |
| 1186 |
def test_arg_as_cellvar(self): |
| 1187 |
def f(x, y, z): |
| 1188 |
a, b, c = 1, 2, 3 |
| 1189 |
z = b |
| 1190 |
return z |
| 1191 |
lambda: (a, b, x, z) |
| 1192 |
graph = self.codetest(f) |
| 1193 |
assert len(graph.startblock.exits) == 1 |
| 1194 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1195 |
assert not graph.startblock.operations |
| 1196 |
assert graph.startblock.exits[0].args[0].value == 2 |
| 1197 |
|
| 1198 |
def test_lambda(self): |
| 1199 |
def f(): |
| 1200 |
g = lambda m, n: n*m |
| 1201 |
return g |
| 1202 |
graph = self.codetest(f) |
| 1203 |
assert len(graph.startblock.exits) == 1 |
| 1204 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1205 |
g = graph.startblock.exits[0].args[0].value |
| 1206 |
assert g(4, 4) == 16 |
| 1207 |
|
| 1208 |
def test_lambda_with_defaults(self): |
| 1209 |
def f(): |
| 1210 |
g = lambda m, n=5: n*m |
| 1211 |
return g |
| 1212 |
graph = self.codetest(f) |
| 1213 |
assert len(graph.startblock.exits) == 1 |
| 1214 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1215 |
g = graph.startblock.exits[0].args[0].value |
| 1216 |
assert g(4) == 20 |
| 1217 |
|
| 1218 |
def f2(x): |
| 1219 |
g = lambda m, n=x: n*m |
| 1220 |
return g |
| 1221 |
with py.test.raises(FlowingError): |
| 1222 |
self.codetest(f2) |
| 1223 |
|
| 1224 |
@py.test.mark.xfail(reason="closures aren't supported") |
| 1225 |
def test_closure(self): |
| 1226 |
def f(): |
| 1227 |
m = 5 |
| 1228 |
return lambda n: m * n |
| 1229 |
graph = self.codetest(f) |
| 1230 |
assert len(graph.startblock.exits) == 1 |
| 1231 |
assert graph.startblock.exits[0].target == graph.returnblock |
| 1232 |
g = graph.startblock.exits[0].args[0].value |
| 1233 |
assert g(4) == 20 |
| 1234 |
|
| 1235 |
def test_closure_error(self): |
| 1236 |
def f(): |
| 1237 |
m = 5 |
| 1238 |
return lambda n: m * n |
| 1239 |
with py.test.raises(ValueError) as excinfo: |
| 1240 |
self.codetest(f) |
| 1241 |
assert "closure" in str(excinfo.value) |
| 1242 |
|
| 1243 |
def test_unbound_local(self): |
| 1244 |
def f(): |
| 1245 |
x += 1 |
| 1246 |
with py.test.raises(FlowingError): |
| 1247 |
self.codetest(f) |
| 1248 |
|
| 1249 |
def test_aug_assign(self): |
| 1250 |
|
| 1251 |
lst = [2, 3, 4] |
| 1252 |
def f(x, y): |
| 1253 |
lst[x] += y |
| 1254 |
graph = self.codetest(f) |
| 1255 |
assert self.all_operations(graph) == {'getitem': 1, |
| 1256 |
'inplace_add': 1, |
| 1257 |
'setitem': 1} |
| 1258 |
|
| 1259 |
def test_list_append(self): |
| 1260 |
def f(iterable): |
| 1261 |
return [5 for x in iterable] |
| 1262 |
graph = self.codetest(f) |
| 1263 |
assert self.all_operations(graph) == {'getattr': 1, |
| 1264 |
'iter': 1, 'newlist': 1, |
| 1265 |
'next': 1, 'simple_call': 1} |
| 1266 |
|
| 1267 |
def test_mutate_const_list(self): |
| 1268 |
lst = list('abcdef') |
| 1269 |
def f(): |
| 1270 |
lst[0] = 'x' |
| 1271 |
return lst |
| 1272 |
graph = self.codetest(f) |
| 1273 |
assert 'setitem' in self.all_operations(graph) |
| 1274 |
|
| 1275 |
def test_sys_getattr(self): |
| 1276 |
def f(): |
| 1277 |
import sys |
| 1278 |
return sys.modules |
| 1279 |
graph = self.codetest(f) |
| 1280 |
assert 'getattr' in self.all_operations(graph) |
| 1281 |
|
| 1282 |
def test_sys_import_from(self): |
| 1283 |
def f(): |
| 1284 |
from sys import modules |
| 1285 |
return modules |
| 1286 |
graph = self.codetest(f) |
| 1287 |
assert 'getattr' in self.all_operations(graph) |
| 1288 |
|
| 1289 |
def test_empty_cell_unused(self): |
| 1290 |
def test(flag): |
| 1291 |
if flag: |
| 1292 |
b = 5 |
| 1293 |
def g(): |
| 1294 |
if flag: |
| 1295 |
return b |
| 1296 |
else: |
| 1297 |
return 1 |
| 1298 |
return g |
| 1299 |
g1 = test(False) |
| 1300 |
graph = self.codetest(g1) |
| 1301 |
assert not self.all_operations(graph) |
| 1302 |
g2 = test(True) |
| 1303 |
graph = self.codetest(g2) |
| 1304 |
assert not self.all_operations(graph) |
| 1305 |
|
| 1306 |
def test_empty_cell_error(self): |
| 1307 |
def test(flag): |
| 1308 |
if not flag: |
| 1309 |
b = 5 |
| 1310 |
def g(): |
| 1311 |
if flag: |
| 1312 |
return b |
| 1313 |
else: |
| 1314 |
return 1 |
| 1315 |
return g |
| 1316 |
g = test(True) |
| 1317 |
with py.test.raises(FlowingError) as excinfo: |
| 1318 |
graph = self.codetest(g) |
| 1319 |
assert "Undefined closure variable 'b'" in str(excinfo.value) |
| 1320 |
|
| 1321 |
def call_os_remove(msg): |
| 1322 |
os.remove(msg) |
| 1323 |
os.unlink(msg) |
| 1324 |
|
| 1325 |
def test_call_os_remove(self): |
| 1326 |
x = self.codetest(self.call_os_remove) |
| 1327 |
simplify_graph(x) |
| 1328 |
self.show(x) |
| 1329 |
ops = x.startblock.operations |
| 1330 |
assert ops[0].opname == 'simple_call' |
| 1331 |
assert ops[0].args[0].value is os.unlink |
| 1332 |
assert ops[1].opname == 'simple_call' |
| 1333 |
assert ops[1].args[0].value is os.unlink |
| 1334 |
|
| 1335 |
def test_rabspath(self): |
| 1336 |
import os.path |
| 1337 |
def f(s): |
| 1338 |
return os.path.abspath(s) |
| 1339 |
graph = self.codetest(f) |
| 1340 |
simplify_graph(graph) |
| 1341 |
ops = graph.startblock.operations |
| 1342 |
assert ops[0].opname == 'simple_call' |
| 1343 |
|
| 1344 |
from rpython.rlib import rpath |
| 1345 |
assert ops[0].args[0].value is rpath.rabspath |
| 1346 |
|
| 1347 |
def test_constfold_in(self): |
| 1348 |
def f(): |
| 1349 |
if 'x' in "xyz": |
| 1350 |
return 5 |
| 1351 |
else: |
| 1352 |
return 6 |
| 1353 |
graph = self.codetest(f) |
| 1354 |
assert graph.startblock.operations == [] |
| 1355 |
[link] = graph.startblock.exits |
| 1356 |
assert link.target is graph.returnblock |
| 1357 |
assert isinstance(link.args[0], Constant) |
| 1358 |
assert link.args[0].value == 5 |
| 1359 |
|
| 1360 |
def test_remove_dead_ops(self): |
| 1361 |
def f(): |
| 1362 |
a = [1] |
| 1363 |
b = (a, a) |
| 1364 |
c = type(b) |
| 1365 |
graph = self.codetest(f) |
| 1366 |
simplify_graph(graph) |
| 1367 |
assert graph.startblock.operations == [] |
| 1368 |
[link] = graph.startblock.exits |
| 1369 |
assert link.target is graph.returnblock |
| 1370 |
|
| 1371 |
def test_not_combine(self): |
| 1372 |
def f(n): |
| 1373 |
t = not n |
| 1374 |
if not n: |
| 1375 |
t += 1 |
| 1376 |
return t |
| 1377 |
graph = self.codetest(f) |
| 1378 |
simplify_graph(graph) |
| 1379 |
assert self.all_operations(graph) == {'bool': 1, 'inplace_add': 1} |
| 1380 |
|
| 1381 |
def test_unexpected_builtin_function(self): |
| 1382 |
import itertools |
| 1383 |
e = py.test.raises(ValueError, build_flow, itertools.permutations) |
| 1384 |
assert ' is not RPython:' in str(e.value) |
| 1385 |
e = py.test.raises(ValueError, build_flow, itertools.tee) |
| 1386 |
assert ' is not RPython:' in str(e.value) |
| 1387 |
e = py.test.raises(ValueError, build_flow, Exception.__init__) |
| 1388 |
assert ' is not RPython:' in str(e.value) |
| 1389 |
|
| 1390 |
def test_dont_crash_fold_wrong_arity(self): |
| 1391 |
def f(): |
| 1392 |
return str() |
| 1393 |
graph = self.codetest(f) |
| 1394 |
assert graph.startblock.exits[0].args[0].value == '' |
| 1395 |
|
| 1396 |
DATA = {'x': 5, |
| 1397 |
'y': 6} |
| 1398 |
|
| 1399 |
def user_defined_function(): |
| 1400 |
pass |