Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(94)

Side by Side Diff: Lib/test/test_dis.py

Issue 11816: Add functions to return disassembly as string
Patch Set: Created 1 year, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/bytecode_helper.py ('k') | Lib/test/test_peepholer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Minimal tests for dis module 1 # Minimal tests for dis module
2 2
3 from test.support import run_unittest, captured_stdout 3 from test.support import run_unittest, captured_stdout
4 from test.bytecode_helper import BytecodeTestCase
4 import difflib 5 import difflib
5 import unittest 6 import unittest
6 import sys 7 import sys
7 import dis 8 import dis
8 import io 9 import io
9 10
10 class _C: 11 class _C:
11 def __init__(self, x): 12 def __init__(self, x):
12 self.x = x == 1 13 self.x = x == 1
13 14
14 dis_c_instance_method = """\ 15 dis_c_instance_method = """\
15 %-4d 0 LOAD_FAST 1 (x) 16 %-4d 0 LOAD_FAST 1 (x)
16 3 LOAD_CONST 1 (1) 17 3 LOAD_CONST 1 (1)
17 6 COMPARE_OP 2 (==) 18 6 COMPARE_OP 2 (==)
18 9 LOAD_FAST 0 (self) 19 9 LOAD_FAST 0 (self)
19 12 STORE_ATTR 0 (x) 20 12 STORE_ATTR 0 (x)
20 15 LOAD_CONST 0 (None) 21 15 LOAD_CONST 0 (None)
21 18 RETURN_VALUE 22 18 RETURN_VALUE
22 """ % (_C.__init__.__code__.co_firstlineno + 1,) 23 """ % (_C.__init__.__code__.co_firstlineno + 1,)
23 24
24 dis_c_instance_method_bytes = """\ 25 dis_c_instance_method_bytes = """\
25 0 LOAD_FAST 1 (1) 26 0 LOAD_FAST 1 (1)
26 3 LOAD_CONST 1 (1) 27 3 LOAD_CONST 1 (1)
27 6 COMPARE_OP 2 (==) 28 6 COMPARE_OP 2 (==)
28 9 LOAD_FAST 0 (0) 29 9 LOAD_FAST 0 (0)
29 12 STORE_ATTR 0 (0) 30 12 STORE_ATTR 0 (0)
30 15 LOAD_CONST 0 (0) 31 15 LOAD_CONST 0 (0)
31 18 RETURN_VALUE 32 18 RETURN_VALUE
32 """ 33 """
33 34
34 def _f(a): 35 def _f(a):
35 print(a) 36 print(a)
36 return 1 37 return 1
37 38
38 dis_f = """\ 39 dis_f = """\
39 %-4d 0 LOAD_GLOBAL 0 (print) 40 %-4d 0 LOAD_GLOBAL 0 (print)
40 3 LOAD_FAST 0 (a) 41 3 LOAD_FAST 0 (a)
41 6 CALL_FUNCTION 1 42 6 CALL_FUNCTION 1
42 9 POP_TOP 43 9 POP_TOP
43 44
44 %-4d 10 LOAD_CONST 1 (1) 45 %-4d 10 LOAD_CONST 1 (1)
45 13 RETURN_VALUE 46 13 RETURN_VALUE
46 """ % (_f.__code__.co_firstlineno + 1, 47 """ % (_f.__code__.co_firstlineno + 1,
47 _f.__code__.co_firstlineno + 2) 48 _f.__code__.co_firstlineno + 2)
48 49
49 50
50 dis_f_co_code = """\ 51 dis_f_co_code = """\
51 0 LOAD_GLOBAL 0 (0) 52 0 LOAD_GLOBAL 0 (0)
52 3 LOAD_FAST 0 (0) 53 3 LOAD_FAST 0 (0)
53 6 CALL_FUNCTION 1 54 6 CALL_FUNCTION 1
54 9 POP_TOP 55 9 POP_TOP
55 10 LOAD_CONST 1 (1) 56 10 LOAD_CONST 1 (1)
56 13 RETURN_VALUE 57 13 RETURN_VALUE
57 """ 58 """
58 59
59 60
60 def bug708901(): 61 def bug708901():
61 for res in range(1, 62 for res in range(1,
62 10): 63 10):
63 pass 64 pass
64 65
65 dis_bug708901 = """\ 66 dis_bug708901 = """\
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 292
292 def test_dis_object(self): 293 def test_dis_object(self):
293 self.assertRaises(TypeError, dis.dis, object()) 294 self.assertRaises(TypeError, dis.dis, object())
294 295
295 code_info_code_info = """\ 296 code_info_code_info = """\
296 Name: code_info 297 Name: code_info
297 Filename: (.*) 298 Filename: (.*)
298 Argument count: 1 299 Argument count: 1
299 Kw-only arguments: 0 300 Kw-only arguments: 0
300 Number of locals: 1 301 Number of locals: 1
301 Stack size: 4 302 Stack size: 3
302 Flags: OPTIMIZED, NEWLOCALS, NOFREE 303 Flags: OPTIMIZED, NEWLOCALS, NOFREE
303 Constants: 304 Constants:
304 0: %r 305 0: %r
305 1: '__func__' 306 Names:
306 2: '__code__' 307 0: _format_code_info
307 3: '<code_info>' 308 1: _get_code_object
308 4: 'co_code'
309 5: "don't know how to disassemble %%s objects"
310 %sNames:
311 0: hasattr
312 1: __func__
313 2: __code__
314 3: isinstance
315 4: str
316 5: _try_compile
317 6: _format_code_info
318 7: TypeError
319 8: type
320 9: __name__
321 Variable names: 309 Variable names:
322 0: x""" % (('Formatted details of methods, functions, or code.', ' 6: None\ n') 310 0: x""" % (('Formatted details of methods, functions, or code.',)
323 if sys.flags.optimize < 2 else (None, '')) 311 if sys.flags.optimize < 2 else (None,))
324 312
325 @staticmethod 313 @staticmethod
326 def tricky(x, y, z=True, *args, c, d, e=[], **kwds): 314 def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
327 def f(c=c): 315 def f(c=c):
328 print(x, y, z, c, d, e, f) 316 print(x, y, z, c, d, e, f)
329 yield x, y, z, c, d, e, f 317 yield x, y, z, c, d, e, f
330 318
331 code_info_tricky = """\ 319 code_info_tricky = """\
332 Name: tricky 320 Name: tricky
333 Filename: (.*) 321 Filename: (.*)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 Free variables: 364 Free variables:
377 0: e 365 0: e
378 1: d 366 1: d
379 2: f 367 2: f
380 3: y 368 3: y
381 4: x 369 4: x
382 5: z""" 370 5: z"""
383 371
384 code_info_expr_str = """\ 372 code_info_expr_str = """\
385 Name: <module> 373 Name: <module>
386 Filename: <code_info> 374 Filename: <disassembly>
387 Argument count: 0 375 Argument count: 0
388 Kw-only arguments: 0 376 Kw-only arguments: 0
389 Number of locals: 0 377 Number of locals: 0
390 Stack size: 2 378 Stack size: 2
391 Flags: NOFREE 379 Flags: NOFREE
392 Constants: 380 Constants:
393 0: 1 381 0: 1
394 Names: 382 Names:
395 0: x""" 383 0: x"""
396 384
397 code_info_simple_stmt_str = """\ 385 code_info_simple_stmt_str = """\
398 Name: <module> 386 Name: <module>
399 Filename: <code_info> 387 Filename: <disassembly>
400 Argument count: 0 388 Argument count: 0
401 Kw-only arguments: 0 389 Kw-only arguments: 0
402 Number of locals: 0 390 Number of locals: 0
403 Stack size: 2 391 Stack size: 2
404 Flags: NOFREE 392 Flags: NOFREE
405 Constants: 393 Constants:
406 0: 1 394 0: 1
407 1: None 395 1: None
408 Names: 396 Names:
409 0: x""" 397 0: x"""
410 398
411 code_info_compound_stmt_str = """\ 399 code_info_compound_stmt_str = """\
412 Name: <module> 400 Name: <module>
413 Filename: <code_info> 401 Filename: <disassembly>
414 Argument count: 0 402 Argument count: 0
415 Kw-only arguments: 0 403 Kw-only arguments: 0
416 Number of locals: 0 404 Number of locals: 0
417 Stack size: 2 405 Stack size: 2
418 Flags: NOFREE 406 Flags: NOFREE
419 Constants: 407 Constants:
420 0: 0 408 0: 0
421 1: 1 409 1: 1
422 2: None 410 2: None
423 Names: 411 Names:
(...skipping 13 matching lines...) Expand all
437 self.maxDiff = 1000 425 self.maxDiff = 1000
438 for x, expected in self.test_pairs: 426 for x, expected in self.test_pairs:
439 self.assertRegex(dis.code_info(x), expected) 427 self.assertRegex(dis.code_info(x), expected)
440 428
441 def test_show_code(self): 429 def test_show_code(self):
442 self.maxDiff = 1000 430 self.maxDiff = 1000
443 for x, expected in self.test_pairs: 431 for x, expected in self.test_pairs:
444 with captured_stdout() as output: 432 with captured_stdout() as output:
445 dis.show_code(x) 433 dis.show_code(x)
446 self.assertRegex(output.getvalue(), expected+"\n") 434 self.assertRegex(output.getvalue(), expected+"\n")
435 output = io.StringIO()
436 dis.show_code(x, file=output)
437 self.assertRegex(output.getvalue(), expected)
447 438
448 def test_code_info_object(self): 439 def test_code_info_object(self):
449 self.assertRaises(TypeError, dis.code_info, object()) 440 self.assertRaises(TypeError, dis.code_info, object())
450 441
451 def test_pretty_flags_no_flags(self): 442 def test_pretty_flags_no_flags(self):
452 self.assertEqual(dis.pretty_flags(0), '0x0') 443 self.assertEqual(dis.pretty_flags(0), '0x0')
453 444
454 445
446 # Fodder for instruction introspection tests
447 # Editing any of these may require recalculating the expected output
448 def outer(a=1, b=2):
449 def f(c=3, d=4):
450 def inner(e=5, f=6):
451 print(a, b, c, d, e, f)
452 print(a, b, c, d)
453 return inner
454 print(a, b, '', 1, [], {}, "Hello world!")
455 return f
456
457 def jumpy():
458 # This won't actually run (but that's OK, we only disassemble it)
459 for i in range(10):
460 print(i)
461 if i < 4:
462 continue
463 if i > 6:
464 break
465 else:
466 print("I can haz else clause?")
467 while i:
468 print(i)
469 i -= 1
470 if i > 6:
471 continue
472 if i < 4:
473 break
474 else:
475 print("Who let lolcatz into this test suite?")
476 try:
477 1 / 0
478 except ZeroDivisionError:
479 print("Here we go, here we go, here we go...")
480 else:
481 with i as dodgy:
482 print("Never reach this")
483 finally:
484 print("OK, now we're done")
485
486 # End fodder for opinfo generation tests
487 expected_outer_offset = 1 - outer.__code__.co_firstlineno
488 expected_jumpy_offset = 1 - jumpy.__code__.co_firstlineno
489 code_object_f = outer.__code__.co_consts[3]
490 code_object_inner = code_object_f.co_consts[3]
491
492 # The following lines are useful to regenerate the expected results after
493 # either the fodder is modified or the bytecode generation changes
494 # After regeneration, update the references to code_object_f and
495 # code_object_inner before rerunning the tests
496
497 #_instructions = dis.get_instructions(outer, line_offset=expected_outer_offset)
498 #print('expected_opinfo_outer = [\n ',
499 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
500 #_instructions = dis.get_instructions(outer(), line_offset=expected_outer_offset )
501 #print('expected_opinfo_f = [\n ',
502 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
503 #_instructions = dis.get_instructions(outer()(), line_offset=expected_outer_offs et)
504 #print('expected_opinfo_inner = [\n ',
505 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
506 #_instructions = dis.get_instructions(jumpy, line_offset=expected_jumpy_offset)
507 #print('expected_opinfo_jumpy = [\n ',
508 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
509
510
511 Instruction = dis.Instruction
512 expected_opinfo_outer = [
513 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', off set=0, starts_line=2, is_jump_target=False),
514 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', off set=3, starts_line=None, is_jump_target=False),
515 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
516 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
517 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', off set=12, starts_line=None, is_jump_target=False),
518 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argr epr=repr(code_object_f), offset=15, starts_line=None, is_jump_target=False),
519 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr='outer.<locals>.f', offset=18, starts_line=None, is_jump_target=False),
520 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', of fset=21, starts_line=None, is_jump_target=False),
521 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', o ffset=24, starts_line=None, is_jump_target=False),
522 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr=' print', offset=27, starts_line=7, is_jump_target=False),
523 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', o ffset=30, starts_line=None, is_jump_target=False),
524 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', o ffset=33, starts_line=None, is_jump_target=False),
525 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr='', off set=36, starts_line=None, is_jump_target=False),
526 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', off set=39, starts_line=None, is_jump_target=False),
527 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offs et=42, starts_line=None, is_jump_target=False),
528 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offse t=45, starts_line=None, is_jump_target=False),
529 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', arg repr='Hello world!', offset=48, starts_line=None, is_jump_target=False),
530 Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', o ffset=51, starts_line=None, is_jump_target=False),
531 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=54, starts_line=None, is_jump_target=False),
532 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', of fset=55, starts_line=8, is_jump_target=False),
533 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr=' ', offset=58, starts_line=None, is_jump_target=False),
534 ]
535
536 expected_opinfo_f = [
537 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', off set=0, starts_line=3, is_jump_target=False),
538 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', off set=3, starts_line=None, is_jump_target=False),
539 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
540 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
541 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=12, starts_line=None, is_jump_target=False),
542 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
543 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', off set=18, starts_line=None, is_jump_target=False),
544 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=21, starts_line=None, is_jump_target=Fal se),
545 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.< locals>.inner', argrepr='outer.<locals>.f.<locals>.inner', offset=24, starts_lin e=None, is_jump_target=False),
546 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', of fset=27, starts_line=None, is_jump_target=False),
547 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='i nner', offset=30, starts_line=None, is_jump_target=False),
548 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr=' print', offset=33, starts_line=5, is_jump_target=False),
549 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', o ffset=36, starts_line=None, is_jump_target=False),
550 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', o ffset=39, starts_line=None, is_jump_target=False),
551 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', o ffset=42, starts_line=None, is_jump_target=False),
552 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', o ffset=45, starts_line=None, is_jump_target=False),
553 Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', o ffset=48, starts_line=None, is_jump_target=False),
554 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=51, starts_line=None, is_jump_target=False),
555 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='in ner', offset=52, starts_line=6, is_jump_target=False),
556 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr=' ', offset=55, starts_line=None, is_jump_target=False),
557 ]
558
559 expected_opinfo_inner = [
560 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr=' print', offset=0, starts_line=4, is_jump_target=False),
561 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', o ffset=3, starts_line=None, is_jump_target=False),
562 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='b', argrepr='b', o ffset=6, starts_line=None, is_jump_target=False),
563 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='c', argrepr='c', o ffset=9, starts_line=None, is_jump_target=False),
564 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', o ffset=12, starts_line=None, is_jump_target=False),
565 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', of fset=15, starts_line=None, is_jump_target=False),
566 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', of fset=18, starts_line=None, is_jump_target=False),
567 Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', o ffset=21, starts_line=None, is_jump_target=False),
568 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=24, starts_line=None, is_jump_target=False),
569 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None ', offset=25, starts_line=None, is_jump_target=False),
570 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr=' ', offset=28, starts_line=None, is_jump_target=False),
571 ]
572
573 expected_opinfo_jumpy = [
574 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77 ', offset=0, starts_line=3, is_jump_target=False),
575 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr=' range', offset=3, starts_line=None, is_jump_target=False),
576 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', o ffset=6, starts_line=None, is_jump_target=False),
577 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=9, starts_line=None, is_jump_target=False),
578 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', o ffset=12, starts_line=None, is_jump_target=False),
579 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
580 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', o ffset=16, starts_line=None, is_jump_target=False),
581 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=19, starts_line=4, is_jump_target=False),
582 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=22, starts_line=None, is_jump_target=False),
583 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=25, starts_line=None, is_jump_target=False),
584 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=28, starts_line=None, is_jump_target=False),
585 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=29, starts_line=5, is_jump_target=False),
586 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', off set=32, starts_line=None, is_jump_target=False),
587 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', o ffset=35, starts_line=None, is_jump_target=False),
588 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr ='', offset=38, starts_line=None, is_jump_target=False),
589 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
590 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 4 7', offset=44, starts_line=None, is_jump_target=False),
591 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=47, starts_line=7, is_jump_target=True),
592 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', off set=50, starts_line=None, is_jump_target=False),
593 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', o ffset=53, starts_line=None, is_jump_target=False),
594 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr ='', offset=56, starts_line=None, is_jump_target=False),
595 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
596 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
597 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
598 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
599 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=67, starts_line=10, is_jump_target=False),
600 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else cla use?', argrepr='I can haz else clause?', offset=70, starts_line=None, is_jump_ta rget=False),
601 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=73, starts_line=None, is_jump_target=False),
602 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=76, starts_line=None, is_jump_target=False),
603 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 1 54', offset=77, starts_line=11, is_jump_target=True),
604 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=80, starts_line=None, is_jump_target=True),
605 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argre pr='', offset=83, starts_line=None, is_jump_target=False),
606 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=86, starts_line=12, is_jump_target=False),
607 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=89, starts_line=None, is_jump_target=False),
608 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=92, starts_line=None, is_jump_target=False),
609 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=95, starts_line=None, is_jump_target=False),
610 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=96, starts_line=13, is_jump_target=False),
611 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', off set=99, starts_line=None, is_jump_target=False),
612 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argre pr='', offset=102, starts_line=None, is_jump_target=False),
613 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', o ffset=103, starts_line=None, is_jump_target=False),
614 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=106, starts_line=14, is_jump_target=False),
615 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', off set=109, starts_line=None, is_jump_target=False),
616 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', o ffset=112, starts_line=None, is_jump_target=False),
617 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argre pr='', offset=115, starts_line=None, is_jump_target=False),
618 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
619 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
620 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=124, starts_line=16, is_jump_target=True),
621 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', off set=127, starts_line=None, is_jump_target=False),
622 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', o ffset=130, starts_line=None, is_jump_target=False),
623 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr ='', offset=133, starts_line=None, is_jump_target=False),
624 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
625 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
626 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
627 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
628 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=144, starts_line=19, is_jump_target=False),
629 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz in to this test suite?', argrepr='Who let lolcatz into this test suite?', offset=14 7, starts_line=None, is_jump_target=False),
630 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=150, starts_line=None, is_jump_target=False),
631 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=153, starts_line=None, is_jump_target=False),
632 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='t o 229', offset=154, starts_line=20, is_jump_target=True),
633 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
634 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', off set=160, starts_line=21, is_jump_target=False),
635 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', off set=163, starts_line=None, is_jump_target=False),
636 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, arg repr='', offset=166, starts_line=None, is_jump_target=False),
637 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=167, starts_line=None, is_jump_target=False),
638 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
639 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
640 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', off set=172, starts_line=22, is_jump_target=True),
641 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError ', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=Fal se),
642 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
643 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argre pr='', offset=179, starts_line=None, is_jump_target=False),
644 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=182, starts_line=None, is_jump_target=False),
645 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=183, starts_line=None, is_jump_target=False),
646 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=184, starts_line=None, is_jump_target=False),
647 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=185, starts_line=23, is_jump_target=False),
648 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here w e go, here we go...', argrepr='Here we go, here we go, here we go...', offset=18 8, starts_line=None, is_jump_target=False),
649 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=191, starts_line=None, is_jump_target=False),
650 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=194, starts_line=None, is_jump_target=False),
651 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
652 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
653 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='' , offset=199, starts_line=None, is_jump_target=True),
654 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', of fset=200, starts_line=25, is_jump_target=True),
655 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 2 23', offset=203, starts_line=None, is_jump_target=False),
656 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='d odgy', offset=206, starts_line=None, is_jump_target=False),
657 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=209, starts_line=26, is_jump_target=False),
658 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr='Never reach this', offset=212, starts_line=None, is_jump_target=False) ,
659 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=215, starts_line=None, is_jump_target=False),
660 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=218, starts_line=None, is_jump_target=False),
661 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
662 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None ', offset=220, starts_line=None, is_jump_target=False),
663 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr=' ', offset=223, starts_line=None, is_jump_target=True),
664 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='' , offset=224, starts_line=None, is_jump_target=False),
665 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
666 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None ', offset=226, starts_line=None, is_jump_target=False),
667 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr=' print', offset=229, starts_line=28, is_jump_target=True),
668 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're don e", argrepr="OK, now we're done", offset=232, starts_line=None, is_jump_target=F alse),
669 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', o ffset=235, starts_line=None, is_jump_target=False),
670 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', off set=238, starts_line=None, is_jump_target=False),
671 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='' , offset=239, starts_line=None, is_jump_target=False),
672 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None ', offset=240, starts_line=None, is_jump_target=False),
673 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr=' ', offset=243, starts_line=None, is_jump_target=False),
674 ]
675
676 class InstructionTests(BytecodeTestCase):
677 def test_outer(self):
678 self.assertBytecodeExactlyMatches(outer, expected_opinfo_outer,
679 line_offset=expected_outer_offset)
680
681 def test_nested(self):
682 with captured_stdout():
683 f = outer()
684 self.assertBytecodeExactlyMatches(f, expected_opinfo_f,
685 line_offset=expected_outer_offset)
686
687 def test_doubly_nested(self):
688 with captured_stdout():
689 inner = outer()()
690 self.assertBytecodeExactlyMatches(inner, expected_opinfo_inner,
691 line_offset=expected_outer_offset)
692
693 def test_jumpy(self):
694 self.assertBytecodeExactlyMatches(jumpy, expected_opinfo_jumpy,
695 line_offset=expected_jumpy_offset)
696
697
455 def test_main(): 698 def test_main():
456 run_unittest(DisTests, CodeInfoTests) 699 run_unittest(DisTests, CodeInfoTests, InstructionTests)
457 700
458 if __name__ == "__main__": 701 if __name__ == "__main__":
459 test_main() 702 test_main()
OLDNEW
« no previous file with comments | « Lib/test/bytecode_helper.py ('k') | Lib/test/test_peepholer.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7