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