Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | # Minimal tests for dis module |
| 2 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 3 | from test.support import run_unittest, captured_stdout |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 4 | from test.bytecode_helper import BytecodeTestCase |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 5 | import difflib |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import unittest |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 7 | import sys |
| 8 | import dis |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | import io |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 10 | import types |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 11 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 12 | class _C: |
| 13 | def __init__(self, x): |
| 14 | self.x = x == 1 |
| 15 | |
| 16 | dis_c_instance_method = """\ |
| 17 | %-4d 0 LOAD_FAST 1 (x) |
| 18 | 3 LOAD_CONST 1 (1) |
| 19 | 6 COMPARE_OP 2 (==) |
| 20 | 9 LOAD_FAST 0 (self) |
| 21 | 12 STORE_ATTR 0 (x) |
| 22 | 15 LOAD_CONST 0 (None) |
| 23 | 18 RETURN_VALUE |
| 24 | """ % (_C.__init__.__code__.co_firstlineno + 1,) |
| 25 | |
| 26 | dis_c_instance_method_bytes = """\ |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 27 | 0 LOAD_FAST 1 (1) |
| 28 | 3 LOAD_CONST 1 (1) |
| 29 | 6 COMPARE_OP 2 (==) |
| 30 | 9 LOAD_FAST 0 (0) |
| 31 | 12 STORE_ATTR 0 (0) |
| 32 | 15 LOAD_CONST 0 (0) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 33 | 18 RETURN_VALUE |
| 34 | """ |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 35 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 36 | def _f(a): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 37 | print(a) |
Tim Peters | eabafeb | 2003-03-07 15:55:36 +0000 | [diff] [blame] | 38 | return 1 |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 39 | |
| 40 | dis_f = """\ |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 41 | %-4d 0 LOAD_GLOBAL 0 (print) |
| 42 | 3 LOAD_FAST 0 (a) |
Alexander Belopolsky | 7448220 | 2012-06-07 14:28:14 -0400 | [diff] [blame] | 43 | 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 44 | 9 POP_TOP |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 45 | |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 46 | %-4d 10 LOAD_CONST 1 (1) |
| 47 | 13 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 48 | """ % (_f.__code__.co_firstlineno + 1, |
| 49 | _f.__code__.co_firstlineno + 2) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 50 | |
| 51 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 52 | dis_f_co_code = """\ |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 53 | 0 LOAD_GLOBAL 0 (0) |
| 54 | 3 LOAD_FAST 0 (0) |
| 55 | 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 56 | 9 POP_TOP |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 57 | 10 LOAD_CONST 1 (1) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 58 | 13 RETURN_VALUE |
| 59 | """ |
| 60 | |
| 61 | |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 62 | def bug708901(): |
| 63 | for res in range(1, |
| 64 | 10): |
| 65 | pass |
| 66 | |
| 67 | dis_bug708901 = """\ |
| 68 | %-4d 0 SETUP_LOOP 23 (to 26) |
| 69 | 3 LOAD_GLOBAL 0 (range) |
| 70 | 6 LOAD_CONST 1 (1) |
| 71 | |
| 72 | %-4d 9 LOAD_CONST 2 (10) |
Alexander Belopolsky | 7448220 | 2012-06-07 14:28:14 -0400 | [diff] [blame] | 73 | 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 74 | 15 GET_ITER |
| 75 | >> 16 FOR_ITER 6 (to 25) |
| 76 | 19 STORE_FAST 0 (res) |
| 77 | |
| 78 | %-4d 22 JUMP_ABSOLUTE 16 |
| 79 | >> 25 POP_BLOCK |
| 80 | >> 26 LOAD_CONST 0 (None) |
| 81 | 29 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 82 | """ % (bug708901.__code__.co_firstlineno + 1, |
| 83 | bug708901.__code__.co_firstlineno + 2, |
| 84 | bug708901.__code__.co_firstlineno + 3) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 85 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 86 | |
| 87 | def bug1333982(x=[]): |
| 88 | assert 0, ([s for s in x] + |
| 89 | 1) |
| 90 | pass |
| 91 | |
| 92 | dis_bug1333982 = """\ |
| 93 | %-4d 0 LOAD_CONST 1 (0) |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 94 | 3 JUMP_IF_TRUE 33 (to 39) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 95 | 6 POP_TOP |
| 96 | 7 LOAD_GLOBAL 0 (AssertionError) |
| 97 | 10 BUILD_LIST 0 |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 98 | 13 LOAD_FAST 0 (x) |
| 99 | 16 GET_ITER |
| 100 | >> 17 FOR_ITER 12 (to 32) |
| 101 | 20 STORE_FAST 1 (s) |
| 102 | 23 LOAD_FAST 1 (s) |
| 103 | 26 LIST_APPEND 2 |
| 104 | 29 JUMP_ABSOLUTE 17 |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 106 | %-4d >> 32 LOAD_CONST 2 (1) |
| 107 | 35 BINARY_ADD |
| 108 | 36 RAISE_VARARGS 2 |
| 109 | >> 39 POP_TOP |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 110 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 111 | %-4d 40 LOAD_CONST 0 (None) |
| 112 | 43 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 113 | """ % (bug1333982.__code__.co_firstlineno + 1, |
| 114 | bug1333982.__code__.co_firstlineno + 2, |
| 115 | bug1333982.__code__.co_firstlineno + 3) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 116 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 117 | _BIG_LINENO_FORMAT = """\ |
| 118 | %3d 0 LOAD_GLOBAL 0 (spam) |
| 119 | 3 POP_TOP |
| 120 | 4 LOAD_CONST 0 (None) |
| 121 | 7 RETURN_VALUE |
| 122 | """ |
| 123 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 124 | dis_module_expected_results = """\ |
| 125 | Disassembly of f: |
| 126 | 4 0 LOAD_CONST 0 (None) |
| 127 | 3 RETURN_VALUE |
| 128 | |
| 129 | Disassembly of g: |
| 130 | 5 0 LOAD_CONST 0 (None) |
| 131 | 3 RETURN_VALUE |
| 132 | |
| 133 | """ |
| 134 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 135 | expr_str = "x + 1" |
| 136 | |
| 137 | dis_expr_str = """\ |
| 138 | 1 0 LOAD_NAME 0 (x) |
| 139 | 3 LOAD_CONST 0 (1) |
| 140 | 6 BINARY_ADD |
| 141 | 7 RETURN_VALUE |
| 142 | """ |
| 143 | |
| 144 | simple_stmt_str = "x = x + 1" |
| 145 | |
| 146 | dis_simple_stmt_str = """\ |
| 147 | 1 0 LOAD_NAME 0 (x) |
| 148 | 3 LOAD_CONST 0 (1) |
| 149 | 6 BINARY_ADD |
| 150 | 7 STORE_NAME 0 (x) |
| 151 | 10 LOAD_CONST 1 (None) |
| 152 | 13 RETURN_VALUE |
| 153 | """ |
| 154 | |
| 155 | compound_stmt_str = """\ |
| 156 | x = 0 |
| 157 | while 1: |
| 158 | x += 1""" |
| 159 | # Trailing newline has been deliberately omitted |
| 160 | |
| 161 | dis_compound_stmt_str = """\ |
| 162 | 1 0 LOAD_CONST 0 (0) |
| 163 | 3 STORE_NAME 0 (x) |
| 164 | |
| 165 | 2 6 SETUP_LOOP 13 (to 22) |
| 166 | |
| 167 | 3 >> 9 LOAD_NAME 0 (x) |
| 168 | 12 LOAD_CONST 1 (1) |
| 169 | 15 INPLACE_ADD |
| 170 | 16 STORE_NAME 0 (x) |
| 171 | 19 JUMP_ABSOLUTE 9 |
| 172 | >> 22 LOAD_CONST 2 (None) |
| 173 | 25 RETURN_VALUE |
| 174 | """ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 175 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 176 | class DisTests(unittest.TestCase): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 177 | |
| 178 | def get_disassembly(self, func, lasti=-1, wrapper=True): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 179 | s = io.StringIO() |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 180 | save_stdout = sys.stdout |
| 181 | sys.stdout = s |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 182 | try: |
| 183 | if wrapper: |
| 184 | dis.dis(func) |
| 185 | else: |
| 186 | dis.disassemble(func, lasti) |
| 187 | finally: |
| 188 | sys.stdout = save_stdout |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 189 | # Trim trailing blanks (if any). |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 190 | return [line.rstrip() for line in s.getvalue().splitlines()] |
| 191 | |
| 192 | def get_disassemble_as_string(self, func, lasti=-1): |
| 193 | return '\n'.join(self.get_disassembly(func, lasti, False)) |
| 194 | |
| 195 | def do_disassembly_test(self, func, expected): |
| 196 | lines = self.get_disassembly(func) |
| 197 | expected = expected.splitlines() |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 198 | if expected != lines: |
| 199 | self.fail( |
| 200 | "events did not match expectation:\n" + |
| 201 | "\n".join(difflib.ndiff(expected, |
| 202 | lines))) |
| 203 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 204 | def test_opmap(self): |
Benjamin Peterson | 76f7f4d | 2011-07-17 22:49:50 -0500 | [diff] [blame] | 205 | self.assertEqual(dis.opmap["NOP"], 9) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 206 | self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst) |
| 207 | self.assertIn(dis.opmap["STORE_NAME"], dis.hasname) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 208 | |
| 209 | def test_opname(self): |
| 210 | self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
| 211 | |
| 212 | def test_boundaries(self): |
| 213 | self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
| 214 | self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) |
| 215 | |
| 216 | def test_dis(self): |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 217 | self.do_disassembly_test(_f, dis_f) |
| 218 | |
| 219 | def test_bug_708901(self): |
| 220 | self.do_disassembly_test(bug708901, dis_bug708901) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 221 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 222 | def test_bug_1333982(self): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 223 | # XXX: re-enable this test! |
Tim Peters | 83a8c39 | 2005-12-25 22:52:32 +0000 | [diff] [blame] | 224 | # This one is checking bytecodes generated for an `assert` statement, |
| 225 | # so fails if the tests are run with -O. Skip this test then. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 226 | pass # Test has been disabled due to change in the way |
| 227 | # list comps are handled. The byte code now includes |
| 228 | # a memory address and a file location, so they change from |
| 229 | # run to run. |
| 230 | # if __debug__: |
| 231 | # self.do_disassembly_test(bug1333982, dis_bug1333982) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 232 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 233 | def test_big_linenos(self): |
| 234 | def func(count): |
| 235 | namespace = {} |
| 236 | func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 237 | exec(func, namespace) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 238 | return namespace['foo'] |
| 239 | |
| 240 | # Test all small ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 241 | for i in range(1, 300): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 242 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 243 | self.do_disassembly_test(func(i), expected) |
| 244 | |
| 245 | # Test some larger ranges too |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 246 | for i in range(300, 5000, 10): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 247 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 248 | self.do_disassembly_test(func(i), expected) |
| 249 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 250 | from test import dis_module |
| 251 | self.do_disassembly_test(dis_module, dis_module_expected_results) |
| 252 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 253 | def test_disassemble_str(self): |
| 254 | self.do_disassembly_test(expr_str, dis_expr_str) |
| 255 | self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) |
| 256 | self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) |
| 257 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 258 | def test_disassemble_bytes(self): |
| 259 | self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) |
| 260 | |
| 261 | def test_disassemble_method(self): |
| 262 | self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) |
| 263 | |
| 264 | def test_disassemble_method_bytes(self): |
| 265 | method_bytecode = _C(1).__init__.__code__.co_code |
| 266 | self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) |
| 267 | |
| 268 | def test_dis_none(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 269 | try: |
| 270 | del sys.last_traceback |
| 271 | except AttributeError: |
| 272 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 273 | self.assertRaises(RuntimeError, dis.dis, None) |
| 274 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 275 | def test_dis_traceback(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 276 | try: |
| 277 | del sys.last_traceback |
| 278 | except AttributeError: |
| 279 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 280 | |
| 281 | try: |
| 282 | 1/0 |
| 283 | except Exception as e: |
| 284 | tb = e.__traceback__ |
| 285 | sys.last_traceback = tb |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 286 | |
| 287 | tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) |
| 288 | self.do_disassembly_test(None, tb_dis) |
| 289 | |
| 290 | def test_dis_object(self): |
| 291 | self.assertRaises(TypeError, dis.dis, object()) |
| 292 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 293 | code_info_code_info = """\ |
| 294 | Name: code_info |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 295 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 296 | Argument count: 1 |
| 297 | Kw-only arguments: 0 |
| 298 | Number of locals: 1 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 299 | Stack size: 3 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 300 | Flags: OPTIMIZED, NEWLOCALS, NOFREE |
| 301 | Constants: |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 302 | 0: %r |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 303 | Names: |
| 304 | 0: _format_code_info |
| 305 | 1: _get_code_object |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 306 | Variable names: |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 307 | 0: x""" % (('Formatted details of methods, functions, or code.',) |
| 308 | if sys.flags.optimize < 2 else (None,)) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 309 | |
| 310 | @staticmethod |
| 311 | def tricky(x, y, z=True, *args, c, d, e=[], **kwds): |
| 312 | def f(c=c): |
| 313 | print(x, y, z, c, d, e, f) |
| 314 | yield x, y, z, c, d, e, f |
| 315 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 316 | code_info_tricky = """\ |
| 317 | Name: tricky |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 318 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 319 | Argument count: 3 |
| 320 | Kw-only arguments: 3 |
| 321 | Number of locals: 8 |
| 322 | Stack size: 7 |
| 323 | Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR |
| 324 | Constants: |
| 325 | 0: None |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 326 | 1: <code object f at (.*), file "(.*)", line (.*)> |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 327 | 2: 'tricky.<locals>.f' |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 328 | Variable names: |
| 329 | 0: x |
| 330 | 1: y |
| 331 | 2: z |
| 332 | 3: c |
| 333 | 4: d |
| 334 | 5: e |
| 335 | 6: args |
| 336 | 7: kwds |
| 337 | Cell variables: |
Georg Brandl | a108227 | 2012-02-20 21:41:03 +0100 | [diff] [blame] | 338 | 0: [edfxyz] |
| 339 | 1: [edfxyz] |
| 340 | 2: [edfxyz] |
| 341 | 3: [edfxyz] |
| 342 | 4: [edfxyz] |
| 343 | 5: [edfxyz]""" |
| 344 | # NOTE: the order of the cell variables above depends on dictionary order! |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 345 | |
| 346 | co_tricky_nested_f = tricky.__func__.__code__.co_consts[1] |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 347 | |
| 348 | code_info_tricky_nested_f = """\ |
| 349 | Name: f |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 350 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 351 | Argument count: 1 |
| 352 | Kw-only arguments: 0 |
| 353 | Number of locals: 1 |
| 354 | Stack size: 8 |
| 355 | Flags: OPTIMIZED, NEWLOCALS, NESTED |
| 356 | Constants: |
| 357 | 0: None |
| 358 | Names: |
| 359 | 0: print |
| 360 | Variable names: |
| 361 | 0: c |
| 362 | Free variables: |
Georg Brandl | 27fe226 | 2012-02-20 22:03:28 +0100 | [diff] [blame] | 363 | 0: [edfxyz] |
| 364 | 1: [edfxyz] |
| 365 | 2: [edfxyz] |
| 366 | 3: [edfxyz] |
| 367 | 4: [edfxyz] |
| 368 | 5: [edfxyz]""" |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 369 | |
| 370 | code_info_expr_str = """\ |
| 371 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 372 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 373 | Argument count: 0 |
| 374 | Kw-only arguments: 0 |
| 375 | Number of locals: 0 |
| 376 | Stack size: 2 |
| 377 | Flags: NOFREE |
| 378 | Constants: |
| 379 | 0: 1 |
| 380 | Names: |
| 381 | 0: x""" |
| 382 | |
| 383 | code_info_simple_stmt_str = """\ |
| 384 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 385 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 386 | Argument count: 0 |
| 387 | Kw-only arguments: 0 |
| 388 | Number of locals: 0 |
| 389 | Stack size: 2 |
| 390 | Flags: NOFREE |
| 391 | Constants: |
| 392 | 0: 1 |
| 393 | 1: None |
| 394 | Names: |
| 395 | 0: x""" |
| 396 | |
| 397 | code_info_compound_stmt_str = """\ |
| 398 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 399 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 400 | Argument count: 0 |
| 401 | Kw-only arguments: 0 |
| 402 | Number of locals: 0 |
| 403 | Stack size: 2 |
| 404 | Flags: NOFREE |
| 405 | Constants: |
| 406 | 0: 0 |
| 407 | 1: 1 |
| 408 | 2: None |
| 409 | Names: |
| 410 | 0: x""" |
| 411 | |
| 412 | class CodeInfoTests(unittest.TestCase): |
| 413 | test_pairs = [ |
| 414 | (dis.code_info, code_info_code_info), |
| 415 | (tricky, code_info_tricky), |
| 416 | (co_tricky_nested_f, code_info_tricky_nested_f), |
| 417 | (expr_str, code_info_expr_str), |
| 418 | (simple_stmt_str, code_info_simple_stmt_str), |
| 419 | (compound_stmt_str, code_info_compound_stmt_str), |
| 420 | ] |
| 421 | |
| 422 | def test_code_info(self): |
| 423 | self.maxDiff = 1000 |
| 424 | for x, expected in self.test_pairs: |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 425 | self.assertRegex(dis.code_info(x), expected) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 426 | |
| 427 | def test_show_code(self): |
| 428 | self.maxDiff = 1000 |
| 429 | for x, expected in self.test_pairs: |
| 430 | with captured_stdout() as output: |
| 431 | dis.show_code(x) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 432 | self.assertRegex(output.getvalue(), expected+"\n") |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 433 | output = io.StringIO() |
| 434 | dis.show_code(x, file=output) |
| 435 | self.assertRegex(output.getvalue(), expected) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 436 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 437 | def test_code_info_object(self): |
| 438 | self.assertRaises(TypeError, dis.code_info, object()) |
| 439 | |
| 440 | def test_pretty_flags_no_flags(self): |
| 441 | self.assertEqual(dis.pretty_flags(0), '0x0') |
| 442 | |
| 443 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 444 | # Fodder for instruction introspection tests |
| 445 | # Editing any of these may require recalculating the expected output |
| 446 | def outer(a=1, b=2): |
| 447 | def f(c=3, d=4): |
| 448 | def inner(e=5, f=6): |
| 449 | print(a, b, c, d, e, f) |
| 450 | print(a, b, c, d) |
| 451 | return inner |
| 452 | print(a, b, '', 1, [], {}, "Hello world!") |
| 453 | return f |
| 454 | |
| 455 | def jumpy(): |
| 456 | # This won't actually run (but that's OK, we only disassemble it) |
| 457 | for i in range(10): |
| 458 | print(i) |
| 459 | if i < 4: |
| 460 | continue |
| 461 | if i > 6: |
| 462 | break |
| 463 | else: |
| 464 | print("I can haz else clause?") |
| 465 | while i: |
| 466 | print(i) |
| 467 | i -= 1 |
| 468 | if i > 6: |
| 469 | continue |
| 470 | if i < 4: |
| 471 | break |
| 472 | else: |
| 473 | print("Who let lolcatz into this test suite?") |
| 474 | try: |
| 475 | 1 / 0 |
| 476 | except ZeroDivisionError: |
| 477 | print("Here we go, here we go, here we go...") |
| 478 | else: |
| 479 | with i as dodgy: |
| 480 | print("Never reach this") |
| 481 | finally: |
| 482 | print("OK, now we're done") |
| 483 | |
| 484 | # End fodder for opinfo generation tests |
| 485 | expected_outer_offset = 1 - outer.__code__.co_firstlineno |
| 486 | expected_jumpy_offset = 1 - jumpy.__code__.co_firstlineno |
| 487 | code_object_f = outer.__code__.co_consts[3] |
| 488 | code_object_inner = code_object_f.co_consts[3] |
| 489 | |
| 490 | # The following lines are useful to regenerate the expected results after |
| 491 | # either the fodder is modified or the bytecode generation changes |
| 492 | # After regeneration, update the references to code_object_f and |
| 493 | # code_object_inner before rerunning the tests |
| 494 | |
| 495 | #_instructions = dis.get_instructions(outer, line_offset=expected_outer_offset) |
| 496 | #print('expected_opinfo_outer = [\n ', |
| 497 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
| 498 | #_instructions = dis.get_instructions(outer(), line_offset=expected_outer_offset) |
| 499 | #print('expected_opinfo_f = [\n ', |
| 500 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
| 501 | #_instructions = dis.get_instructions(outer()(), line_offset=expected_outer_offset) |
| 502 | #print('expected_opinfo_inner = [\n ', |
| 503 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
| 504 | #_instructions = dis.get_instructions(jumpy, line_offset=expected_jumpy_offset) |
| 505 | #print('expected_opinfo_jumpy = [\n ', |
| 506 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
| 507 | |
| 508 | |
| 509 | Instruction = dis.Instruction |
| 510 | expected_opinfo_outer = [ |
| 511 | Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False), |
| 512 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False), |
| 513 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False), |
| 514 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False), |
| 515 | Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False), |
| 516 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=15, starts_line=None, is_jump_target=False), |
| 517 | 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), |
| 518 | Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False), |
| 519 | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False), |
| 520 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False), |
| 521 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False), |
| 522 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False), |
| 523 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False), |
| 524 | Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False), |
| 525 | Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False), |
| 526 | Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False), |
| 527 | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False), |
| 528 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='7 positional, 0 keyword pair', offset=51, starts_line=None, is_jump_target=False), |
| 529 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False), |
| 530 | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False), |
| 531 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False), |
| 532 | ] |
| 533 | |
| 534 | expected_opinfo_f = [ |
| 535 | Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False), |
| 536 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False), |
| 537 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False), |
| 538 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False), |
| 539 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False), |
| 540 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False), |
| 541 | Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False), |
| 542 | 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=False), |
| 543 | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=24, starts_line=None, is_jump_target=False), |
| 544 | Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False), |
| 545 | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False), |
| 546 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False), |
| 547 | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False), |
| 548 | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False), |
| 549 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False), |
| 550 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False), |
| 551 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='4 positional, 0 keyword pair', offset=48, starts_line=None, is_jump_target=False), |
| 552 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False), |
| 553 | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False), |
| 554 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False), |
| 555 | ] |
| 556 | |
| 557 | expected_opinfo_inner = [ |
| 558 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False), |
| 559 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False), |
| 560 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False), |
| 561 | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False), |
| 562 | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False), |
| 563 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False), |
| 564 | Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False), |
| 565 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='6 positional, 0 keyword pair', offset=21, starts_line=None, is_jump_target=False), |
| 566 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False), |
| 567 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False), |
| 568 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False), |
| 569 | ] |
| 570 | |
| 571 | expected_opinfo_jumpy = [ |
| 572 | Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False), |
| 573 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False), |
| 574 | Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False), |
| 575 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=9, starts_line=None, is_jump_target=False), |
| 576 | Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False), |
| 577 | Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True), |
| 578 | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False), |
| 579 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False), |
| 580 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False), |
| 581 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=25, starts_line=None, is_jump_target=False), |
| 582 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False), |
| 583 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False), |
| 584 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False), |
| 585 | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False), |
| 586 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False), |
| 587 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False), |
| 588 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False), |
| 589 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True), |
| 590 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False), |
| 591 | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False), |
| 592 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False), |
| 593 | Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False), |
| 594 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False), |
| 595 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False), |
| 596 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True), |
| 597 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False), |
| 598 | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=70, starts_line=None, is_jump_target=False), |
| 599 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=73, starts_line=None, is_jump_target=False), |
| 600 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False), |
| 601 | Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True), |
| 602 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True), |
| 603 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False), |
| 604 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False), |
| 605 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False), |
| 606 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=92, starts_line=None, is_jump_target=False), |
| 607 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False), |
| 608 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False), |
| 609 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False), |
| 610 | Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False), |
| 611 | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False), |
| 612 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False), |
| 613 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False), |
| 614 | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False), |
| 615 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False), |
| 616 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False), |
| 617 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False), |
| 618 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True), |
| 619 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False), |
| 620 | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False), |
| 621 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False), |
| 622 | Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False), |
| 623 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False), |
| 624 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False), |
| 625 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True), |
| 626 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False), |
| 627 | Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=147, starts_line=None, is_jump_target=False), |
| 628 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=150, starts_line=None, is_jump_target=False), |
| 629 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False), |
| 630 | Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True), |
| 631 | Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False), |
| 632 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False), |
| 633 | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False), |
| 634 | Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False), |
| 635 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False), |
| 636 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False), |
| 637 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False), |
| 638 | Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True), |
| 639 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False), |
| 640 | Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False), |
| 641 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False), |
| 642 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False), |
| 643 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False), |
| 644 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False), |
| 645 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False), |
| 646 | Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=188, starts_line=None, is_jump_target=False), |
| 647 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=191, starts_line=None, is_jump_target=False), |
| 648 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False), |
| 649 | Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False), |
| 650 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False), |
| 651 | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True), |
| 652 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True), |
| 653 | Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False), |
| 654 | Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False), |
| 655 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False), |
| 656 | 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), |
| 657 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=215, starts_line=None, is_jump_target=False), |
| 658 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False), |
| 659 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False), |
| 660 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False), |
| 661 | Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True), |
| 662 | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False), |
| 663 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True), |
| 664 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False), |
| 665 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True), |
| 666 | Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=232, starts_line=None, is_jump_target=False), |
| 667 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=235, starts_line=None, is_jump_target=False), |
| 668 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False), |
| 669 | Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False), |
| 670 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False), |
| 671 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False), |
| 672 | ] |
| 673 | |
| 674 | class InstructionTests(BytecodeTestCase): |
| 675 | def test_outer(self): |
| 676 | self.assertBytecodeExactlyMatches(outer, expected_opinfo_outer, |
| 677 | line_offset=expected_outer_offset) |
| 678 | |
| 679 | def test_nested(self): |
| 680 | with captured_stdout(): |
| 681 | f = outer() |
| 682 | self.assertBytecodeExactlyMatches(f, expected_opinfo_f, |
| 683 | line_offset=expected_outer_offset) |
| 684 | |
| 685 | def test_doubly_nested(self): |
| 686 | with captured_stdout(): |
| 687 | inner = outer()() |
| 688 | self.assertBytecodeExactlyMatches(inner, expected_opinfo_inner, |
| 689 | line_offset=expected_outer_offset) |
| 690 | |
| 691 | def test_jumpy(self): |
| 692 | self.assertBytecodeExactlyMatches(jumpy, expected_opinfo_jumpy, |
| 693 | line_offset=expected_jumpy_offset) |
| 694 | |
| 695 | class BytecodeTests(unittest.TestCase): |
| 696 | def test_instantiation(self): |
| 697 | # Test with function, method, code string and code object |
| 698 | for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
| 699 | b = dis.Bytecode(obj) |
| 700 | self.assertIsInstance(b.codeobj, types.CodeType) |
| 701 | |
| 702 | self.assertRaises(TypeError, dis.Bytecode, object()) |
| 703 | |
| 704 | def test_iteration(self): |
| 705 | b = dis.Bytecode(_f) |
| 706 | for instr in b: |
| 707 | self.assertIsInstance(instr, dis.Instruction) |
| 708 | |
| 709 | assert len(list(b)) > 0 # Iterating should yield at least 1 instruction |
| 710 | |
| 711 | def test_info(self): |
| 712 | self.maxDiff = 1000 |
| 713 | for x, expected in CodeInfoTests.test_pairs: |
| 714 | b = dis.Bytecode(x) |
| 715 | self.assertRegex(b.info(), expected) |
| 716 | |
| 717 | def test_display_code(self): |
| 718 | b = dis.Bytecode(_f) |
| 719 | output = io.StringIO() |
| 720 | b.display_code(file=output) |
| 721 | result = [line.rstrip() for line in output.getvalue().splitlines()] |
| 722 | self.assertEqual(result, dis_f.splitlines()) |
| 723 | |
| 724 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 725 | def test_main(): |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 726 | run_unittest(DisTests, CodeInfoTests, InstructionTests, BytecodeTests) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 727 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 728 | if __name__ == "__main__": |
| 729 | test_main() |