| OLD | NEW |
| 1 # Minimal tests for dis module | 1 # Minimal tests for dis module |
| 2 | 2 |
| 3 from test.test_support import run_unittest | 3 from test.test_support import run_unittest |
| 4 import unittest | 4 import unittest |
| 5 import sys | 5 import sys |
| 6 import dis | 6 import dis |
| 7 import StringIO | 7 import StringIO |
| 8 | 8 |
| 9 | 9 |
| 10 def _f(a): | 10 def _f(a): |
| 11 print a | 11 print a |
| 12 return 1 | 12 return 1 |
| 13 | 13 |
| 14 dis_f = """\ | 14 dis_f = """\ |
| 15 %-4d 0 LOAD_FAST 0 (a) | 15 %-4d 0 LOAD_FAST 0 (a) |
| 16 3 PRINT_ITEM | 16 3 PRINT_ITEM |
| 17 4 PRINT_NEWLINE | 17 4 PRINT_NEWLINE |
| 18 | 18 |
| 19 %-4d 5 LOAD_CONST 1 (1) | 19 %-4d 5 LOAD_CONST 1 (1) |
| 20 8 RETURN_VALUE | 20 8 RETURN_VALUE |
| 21 """%(_f.func_code.co_firstlineno + 1, | 21 """%(_f.func_code.co_firstlineno + 1, |
| 22 _f.func_code.co_firstlineno + 2) | 22 _f.func_code.co_firstlineno + 2) |
| 23 | 23 |
| 24 |
| 25 def loop(a): |
| 26 while a > 0: |
| 27 a -= 1 |
| 28 return a |
| 29 |
| 30 dis_loop = """\ |
| 31 %3d 0 SETUP_LOOP 26 (to 29) |
| 32 3 JUMP_FORWARD 10 (to 16) |
| 33 |
| 34 %3d >> 6 LOAD_FAST 0 (a) |
| 35 9 LOAD_CONST 1 (1) |
| 36 12 INPLACE_SUBTRACT |
| 37 13 STORE_FAST 0 (a) |
| 38 |
| 39 %3d >> 16 LOAD_FAST 0 (a) |
| 40 19 LOAD_CONST 2 (0) |
| 41 22 COMPARE_OP 4 (>) |
| 42 25 POP_JUMP_IF_TRUE 6 |
| 43 28 POP_BLOCK |
| 44 |
| 45 %3d >> 29 LOAD_FAST 0 (a) |
| 46 32 RETURN_VALUE |
| 47 """%(loop.func_code.co_firstlineno + 1, |
| 48 loop.func_code.co_firstlineno + 2, |
| 49 loop.func_code.co_firstlineno + 1, |
| 50 loop.func_code.co_firstlineno + 3) |
| 24 | 51 |
| 25 def bug708901(): | 52 def bug708901(): |
| 26 for res in range(1, | 53 for res in range(1, |
| 27 10): | 54 10): |
| 28 pass | 55 pass |
| 29 | 56 |
| 30 dis_bug708901 = """\ | 57 dis_bug708901 = """\ |
| 31 %-4d 0 SETUP_LOOP 23 (to 26) | 58 %-4d 0 SETUP_LOOP 23 (to 26) |
| 32 3 LOAD_GLOBAL 0 (range) | 59 3 LOAD_GLOBAL 0 (range) |
| 33 6 LOAD_CONST 1 (1) | 60 6 LOAD_CONST 1 (1) |
| 34 | 61 |
| 35 %-4d 9 LOAD_CONST 2 (10) | 62 %-4d 9 LOAD_CONST 2 (10) |
| 36 12 CALL_FUNCTION 2 | 63 12 CALL_FUNCTION 2 |
| 37 15 GET_ITER | 64 15 GET_ITER |
| 38 >> 16 FOR_ITER 6 (to 25) | 65 16 JUMP_FORWARD 3 (to 22) |
| 39 19 STORE_FAST 0 (res) | 66 >> 19 STORE_FAST 0 (res) |
| 40 | 67 >> 22 FOR_ITER 19 |
| 41 %-4d 22 JUMP_ABSOLUTE 16 | 68 25 POP_BLOCK |
| 42 >> 25 POP_BLOCK | |
| 43 >> 26 LOAD_CONST 0 (None) | 69 >> 26 LOAD_CONST 0 (None) |
| 44 29 RETURN_VALUE | 70 29 RETURN_VALUE |
| 45 """%(bug708901.func_code.co_firstlineno + 1, | 71 """%(bug708901.func_code.co_firstlineno + 1, |
| 46 bug708901.func_code.co_firstlineno + 2, | 72 bug708901.func_code.co_firstlineno + 2) |
| 47 bug708901.func_code.co_firstlineno + 3) | |
| 48 | 73 |
| 49 | 74 |
| 50 def bug1333982(x=[]): | 75 def bug1333982(x=[]): |
| 51 assert 0, ([s for s in x] + | 76 assert 0, ([s for s in x] + |
| 52 1) | 77 1) |
| 53 pass | 78 pass |
| 54 | 79 |
| 55 dis_bug1333982 = """\ | 80 dis_bug1333982 = """\ |
| 56 %-4d 0 LOAD_CONST 1 (0) | 81 %-4d 0 LOAD_CONST 1 (0) |
| 57 3 POP_JUMP_IF_TRUE 38 | 82 3 POP_JUMP_IF_TRUE 38 |
| 58 6 LOAD_GLOBAL 0 (AssertionError) | 83 6 LOAD_GLOBAL 0 (AssertionError) |
| 59 9 BUILD_LIST 0 | 84 9 BUILD_LIST 0 |
| 60 12 LOAD_FAST 0 (x) | 85 12 LOAD_FAST 0 (x) |
| 61 15 GET_ITER | 86 15 GET_ITER |
| 62 >> 16 FOR_ITER 12 (to 31) | 87 16 JUMP_FORWARD 9 (to 28) |
| 63 19 STORE_FAST 1 (s) | 88 >> 19 STORE_FAST 1 (s) |
| 64 22 LOAD_FAST 1 (s) | 89 22 LOAD_FAST 1 (s) |
| 65 25 LIST_APPEND 2 | 90 25 LIST_APPEND 2 |
| 66 28 JUMP_ABSOLUTE 16 | 91 >> 28 FOR_ITER 19 |
| 67 | 92 |
| 68 %-4d >> 31 LOAD_CONST 2 (1) | 93 %-4d 31 LOAD_CONST 2 (1) |
| 69 34 BINARY_ADD | 94 34 BINARY_ADD |
| 70 35 RAISE_VARARGS 2 | 95 35 RAISE_VARARGS 2 |
| 71 | 96 |
| 72 %-4d >> 38 LOAD_CONST 0 (None) | 97 %-4d >> 38 LOAD_CONST 0 (None) |
| 73 41 RETURN_VALUE | 98 41 RETURN_VALUE |
| 74 """%(bug1333982.func_code.co_firstlineno + 1, | 99 """%(bug1333982.func_code.co_firstlineno + 1, |
| 75 bug1333982.func_code.co_firstlineno + 2, | 100 bug1333982.func_code.co_firstlineno + 2, |
| 76 bug1333982.func_code.co_firstlineno + 3) | 101 bug1333982.func_code.co_firstlineno + 3) |
| 77 | 102 |
| 78 _BIG_LINENO_FORMAT = """\ | 103 _BIG_LINENO_FORMAT = """\ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 108 | 133 |
| 109 def test_opname(self): | 134 def test_opname(self): |
| 110 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") | 135 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
| 111 | 136 |
| 112 def test_boundaries(self): | 137 def test_boundaries(self): |
| 113 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) | 138 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
| 114 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) | 139 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) |
| 115 | 140 |
| 116 def test_dis(self): | 141 def test_dis(self): |
| 117 self.do_disassembly_test(_f, dis_f) | 142 self.do_disassembly_test(_f, dis_f) |
| 143 |
| 144 def test_lineno_goes_backwards_in_loop(self): |
| 145 self.do_disassembly_test(loop, dis_loop) |
| 118 | 146 |
| 119 def test_bug_708901(self): | 147 def test_bug_708901(self): |
| 120 self.do_disassembly_test(bug708901, dis_bug708901) | 148 self.do_disassembly_test(bug708901, dis_bug708901) |
| 121 | 149 |
| 122 def test_bug_1333982(self): | 150 def test_bug_1333982(self): |
| 123 # This one is checking bytecodes generated for an `assert` statement, | 151 # This one is checking bytecodes generated for an `assert` statement, |
| 124 # so fails if the tests are run with -O. Skip this test then. | 152 # so fails if the tests are run with -O. Skip this test then. |
| 125 if __debug__: | 153 if __debug__: |
| 126 self.do_disassembly_test(bug1333982, dis_bug1333982) | 154 self.do_disassembly_test(bug1333982, dis_bug1333982) |
| 127 | 155 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 141 for i in xrange(300, 5000, 10): | 169 for i in xrange(300, 5000, 10): |
| 142 expected = _BIG_LINENO_FORMAT % (i + 2) | 170 expected = _BIG_LINENO_FORMAT % (i + 2) |
| 143 self.do_disassembly_test(func(i), expected) | 171 self.do_disassembly_test(func(i), expected) |
| 144 | 172 |
| 145 def test_main(): | 173 def test_main(): |
| 146 run_unittest(DisTests) | 174 run_unittest(DisTests) |
| 147 | 175 |
| 148 | 176 |
| 149 if __name__ == "__main__": | 177 if __name__ == "__main__": |
| 150 test_main() | 178 test_main() |
| OLD | NEW |