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