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 |
Joannah Nanjekye | 92777d5 | 2019-09-12 10:02:59 +0100 | [diff] [blame] | 4 | from test.support.bytecode_helper import BytecodeTestCase |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5 | import unittest |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 6 | import sys |
| 7 | import dis |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 8 | import io |
Zachary Ware | e80e806 | 2013-12-26 09:53:49 -0600 | [diff] [blame] | 9 | import re |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 10 | import types |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 11 | import contextlib |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 12 | |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 13 | def get_tb(): |
| 14 | def _error(): |
| 15 | try: |
| 16 | 1 / 0 |
| 17 | except Exception as e: |
| 18 | tb = e.__traceback__ |
| 19 | return tb |
| 20 | |
| 21 | tb = _error() |
| 22 | while tb.tb_next: |
| 23 | tb = tb.tb_next |
| 24 | return tb |
| 25 | |
| 26 | TRACEBACK_CODE = get_tb().tb_frame.f_code |
| 27 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 28 | class _C: |
| 29 | def __init__(self, x): |
| 30 | self.x = x == 1 |
| 31 | |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 32 | @staticmethod |
| 33 | def sm(x): |
| 34 | x = x == 1 |
| 35 | |
| 36 | @classmethod |
| 37 | def cm(cls, x): |
| 38 | cls.x = x == 1 |
| 39 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 40 | dis_c_instance_method = """\ |
Serhiy Storchaka | 247763d | 2016-04-12 08:46:28 +0300 | [diff] [blame] | 41 | %3d 0 LOAD_FAST 1 (x) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 42 | 2 LOAD_CONST 1 (1) |
| 43 | 4 COMPARE_OP 2 (==) |
| 44 | 6 LOAD_FAST 0 (self) |
| 45 | 8 STORE_ATTR 0 (x) |
| 46 | 10 LOAD_CONST 0 (None) |
| 47 | 12 RETURN_VALUE |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 48 | """ % (_C.__init__.__code__.co_firstlineno + 1,) |
| 49 | |
| 50 | dis_c_instance_method_bytes = """\ |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 51 | 0 LOAD_FAST 1 (1) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 52 | 2 LOAD_CONST 1 (1) |
| 53 | 4 COMPARE_OP 2 (==) |
| 54 | 6 LOAD_FAST 0 (0) |
| 55 | 8 STORE_ATTR 0 (0) |
| 56 | 10 LOAD_CONST 0 (0) |
| 57 | 12 RETURN_VALUE |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 58 | """ |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 59 | |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 60 | dis_c_class_method = """\ |
| 61 | %3d 0 LOAD_FAST 1 (x) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 62 | 2 LOAD_CONST 1 (1) |
| 63 | 4 COMPARE_OP 2 (==) |
| 64 | 6 LOAD_FAST 0 (cls) |
| 65 | 8 STORE_ATTR 0 (x) |
| 66 | 10 LOAD_CONST 0 (None) |
| 67 | 12 RETURN_VALUE |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 68 | """ % (_C.cm.__code__.co_firstlineno + 2,) |
| 69 | |
| 70 | dis_c_static_method = """\ |
| 71 | %3d 0 LOAD_FAST 0 (x) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 72 | 2 LOAD_CONST 1 (1) |
| 73 | 4 COMPARE_OP 2 (==) |
| 74 | 6 STORE_FAST 0 (x) |
| 75 | 8 LOAD_CONST 0 (None) |
| 76 | 10 RETURN_VALUE |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 77 | """ % (_C.sm.__code__.co_firstlineno + 2,) |
| 78 | |
| 79 | # Class disassembling info has an extra newline at end. |
| 80 | dis_c = """\ |
| 81 | Disassembly of %s: |
| 82 | %s |
| 83 | Disassembly of %s: |
| 84 | %s |
| 85 | Disassembly of %s: |
| 86 | %s |
| 87 | """ % (_C.__init__.__name__, dis_c_instance_method, |
| 88 | _C.cm.__name__, dis_c_class_method, |
| 89 | _C.sm.__name__, dis_c_static_method) |
| 90 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 91 | def _f(a): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 92 | print(a) |
Tim Peters | eabafeb | 2003-03-07 15:55:36 +0000 | [diff] [blame] | 93 | return 1 |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 94 | |
| 95 | dis_f = """\ |
Serhiy Storchaka | 247763d | 2016-04-12 08:46:28 +0300 | [diff] [blame] | 96 | %3d 0 LOAD_GLOBAL 0 (print) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 97 | 2 LOAD_FAST 0 (a) |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 98 | 4 CALL_FUNCTION 1 |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 99 | 6 POP_TOP |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 100 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 101 | %3d 8 LOAD_CONST 1 (1) |
| 102 | 10 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 103 | """ % (_f.__code__.co_firstlineno + 1, |
| 104 | _f.__code__.co_firstlineno + 2) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 105 | |
| 106 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 107 | dis_f_co_code = """\ |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 108 | 0 LOAD_GLOBAL 0 (0) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 109 | 2 LOAD_FAST 0 (0) |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 110 | 4 CALL_FUNCTION 1 |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 111 | 6 POP_TOP |
| 112 | 8 LOAD_CONST 1 (1) |
| 113 | 10 RETURN_VALUE |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 114 | """ |
| 115 | |
| 116 | |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 117 | def bug708901(): |
| 118 | for res in range(1, |
| 119 | 10): |
| 120 | pass |
| 121 | |
| 122 | dis_bug708901 = """\ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 123 | %3d 0 LOAD_GLOBAL 0 (range) |
| 124 | 2 LOAD_CONST 1 (1) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 125 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 126 | %3d 4 LOAD_CONST 2 (10) |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 127 | |
| 128 | %3d 6 CALL_FUNCTION 2 |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 129 | 8 GET_ITER |
| 130 | >> 10 FOR_ITER 4 (to 16) |
| 131 | 12 STORE_FAST 0 (res) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 132 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 133 | %3d 14 JUMP_ABSOLUTE 10 |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 134 | |
| 135 | %3d >> 16 LOAD_CONST 0 (None) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 136 | 18 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 137 | """ % (bug708901.__code__.co_firstlineno + 1, |
| 138 | bug708901.__code__.co_firstlineno + 2, |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 139 | bug708901.__code__.co_firstlineno + 1, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 140 | bug708901.__code__.co_firstlineno + 3, |
| 141 | bug708901.__code__.co_firstlineno + 1) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 142 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 143 | |
| 144 | def bug1333982(x=[]): |
| 145 | assert 0, ([s for s in x] + |
| 146 | 1) |
| 147 | pass |
| 148 | |
| 149 | dis_bug1333982 = """\ |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 150 | %3d 0 LOAD_ASSERTION_ERROR |
| 151 | 2 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>) |
| 152 | 4 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>') |
| 153 | 6 MAKE_FUNCTION 0 |
| 154 | 8 LOAD_FAST 0 (x) |
| 155 | 10 GET_ITER |
| 156 | 12 CALL_FUNCTION 1 |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 157 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 158 | %3d 14 LOAD_CONST 4 (1) |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 159 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 160 | %3d 16 BINARY_ADD |
| 161 | 18 CALL_FUNCTION 1 |
| 162 | 20 RAISE_VARARGS 1 |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 163 | """ % (bug1333982.__code__.co_firstlineno + 1, |
Zachary Ware | e80e806 | 2013-12-26 09:53:49 -0600 | [diff] [blame] | 164 | __file__, |
| 165 | bug1333982.__code__.co_firstlineno + 1, |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 166 | bug1333982.__code__.co_firstlineno + 2, |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 167 | bug1333982.__code__.co_firstlineno + 1) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 169 | _BIG_LINENO_FORMAT = """\ |
| 170 | %3d 0 LOAD_GLOBAL 0 (spam) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 171 | 2 POP_TOP |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 172 | 4 LOAD_CONST 0 (None) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 173 | 6 RETURN_VALUE |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 174 | """ |
| 175 | |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 176 | _BIG_LINENO_FORMAT2 = """\ |
| 177 | %4d 0 LOAD_GLOBAL 0 (spam) |
| 178 | 2 POP_TOP |
| 179 | 4 LOAD_CONST 0 (None) |
| 180 | 6 RETURN_VALUE |
| 181 | """ |
| 182 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 183 | dis_module_expected_results = """\ |
| 184 | Disassembly of f: |
| 185 | 4 0 LOAD_CONST 0 (None) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 186 | 2 RETURN_VALUE |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 187 | |
| 188 | Disassembly of g: |
| 189 | 5 0 LOAD_CONST 0 (None) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 190 | 2 RETURN_VALUE |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 191 | |
| 192 | """ |
| 193 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 194 | expr_str = "x + 1" |
| 195 | |
| 196 | dis_expr_str = """\ |
| 197 | 1 0 LOAD_NAME 0 (x) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 198 | 2 LOAD_CONST 0 (1) |
| 199 | 4 BINARY_ADD |
| 200 | 6 RETURN_VALUE |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 201 | """ |
| 202 | |
| 203 | simple_stmt_str = "x = x + 1" |
| 204 | |
| 205 | dis_simple_stmt_str = """\ |
| 206 | 1 0 LOAD_NAME 0 (x) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 207 | 2 LOAD_CONST 0 (1) |
| 208 | 4 BINARY_ADD |
| 209 | 6 STORE_NAME 0 (x) |
| 210 | 8 LOAD_CONST 1 (None) |
| 211 | 10 RETURN_VALUE |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 212 | """ |
| 213 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 214 | annot_stmt_str = """\ |
| 215 | |
| 216 | x: int = 1 |
| 217 | y: fun(1) |
| 218 | lst[fun(0)]: int = 1 |
| 219 | """ |
| 220 | # leading newline is for a reason (tests lineno) |
| 221 | |
| 222 | dis_annot_stmt_str = """\ |
| 223 | 2 0 SETUP_ANNOTATIONS |
| 224 | 2 LOAD_CONST 0 (1) |
| 225 | 4 STORE_NAME 0 (x) |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 226 | 6 LOAD_CONST 1 ('int') |
| 227 | 8 LOAD_NAME 1 (__annotations__) |
| 228 | 10 LOAD_CONST 2 ('x') |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 229 | 12 STORE_SUBSCR |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 230 | |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 231 | 3 14 LOAD_CONST 3 ('fun(1)') |
| 232 | 16 LOAD_NAME 1 (__annotations__) |
| 233 | 18 LOAD_CONST 4 ('y') |
| 234 | 20 STORE_SUBSCR |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 235 | |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 236 | 4 22 LOAD_CONST 0 (1) |
| 237 | 24 LOAD_NAME 2 (lst) |
| 238 | 26 LOAD_NAME 3 (fun) |
| 239 | 28 LOAD_CONST 5 (0) |
| 240 | 30 CALL_FUNCTION 1 |
| 241 | 32 STORE_SUBSCR |
| 242 | 34 LOAD_NAME 4 (int) |
| 243 | 36 POP_TOP |
| 244 | 38 LOAD_CONST 6 (None) |
| 245 | 40 RETURN_VALUE |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 246 | """ |
| 247 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 248 | compound_stmt_str = """\ |
| 249 | x = 0 |
| 250 | while 1: |
| 251 | x += 1""" |
| 252 | # Trailing newline has been deliberately omitted |
| 253 | |
| 254 | dis_compound_stmt_str = """\ |
| 255 | 1 0 LOAD_CONST 0 (0) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 256 | 2 STORE_NAME 0 (x) |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 257 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 258 | 3 >> 4 LOAD_NAME 0 (x) |
| 259 | 6 LOAD_CONST 1 (1) |
| 260 | 8 INPLACE_ADD |
| 261 | 10 STORE_NAME 0 (x) |
| 262 | 12 JUMP_ABSOLUTE 4 |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 263 | """ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 264 | |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 265 | dis_traceback = """\ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 266 | %3d 0 SETUP_FINALLY 14 (to 16) |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 267 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 268 | %3d 2 LOAD_CONST 1 (1) |
| 269 | 4 LOAD_CONST 2 (0) |
| 270 | --> 6 BINARY_TRUE_DIVIDE |
| 271 | 8 POP_TOP |
| 272 | 10 POP_BLOCK |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 273 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 274 | %3d 12 LOAD_FAST 1 (tb) |
| 275 | 14 RETURN_VALUE |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 276 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 277 | %3d >> 16 DUP_TOP |
| 278 | 18 LOAD_GLOBAL 0 (Exception) |
| 279 | 20 JUMP_IF_NOT_EXC_MATCH 58 |
| 280 | 22 POP_TOP |
| 281 | 24 STORE_FAST 0 (e) |
| 282 | 26 POP_TOP |
| 283 | 28 SETUP_FINALLY 20 (to 50) |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 284 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 285 | %3d 30 LOAD_FAST 0 (e) |
| 286 | 32 LOAD_ATTR 1 (__traceback__) |
| 287 | 34 STORE_FAST 1 (tb) |
| 288 | 36 POP_BLOCK |
| 289 | 38 POP_EXCEPT |
| 290 | 40 LOAD_CONST 0 (None) |
| 291 | 42 STORE_FAST 0 (e) |
| 292 | 44 DELETE_FAST 0 (e) |
| 293 | |
| 294 | %3d 46 LOAD_FAST 1 (tb) |
| 295 | 48 RETURN_VALUE |
| 296 | >> 50 LOAD_CONST 0 (None) |
| 297 | 52 STORE_FAST 0 (e) |
| 298 | 54 DELETE_FAST 0 (e) |
| 299 | 56 RERAISE |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 300 | |
| 301 | %3d >> 58 RERAISE |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 302 | """ % (TRACEBACK_CODE.co_firstlineno + 1, |
| 303 | TRACEBACK_CODE.co_firstlineno + 2, |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 304 | TRACEBACK_CODE.co_firstlineno + 5, |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 305 | TRACEBACK_CODE.co_firstlineno + 3, |
| 306 | TRACEBACK_CODE.co_firstlineno + 4, |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 307 | TRACEBACK_CODE.co_firstlineno + 5, |
| 308 | TRACEBACK_CODE.co_firstlineno + 3) |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 309 | |
Serhiy Storchaka | dd102f7 | 2016-10-08 12:34:25 +0300 | [diff] [blame] | 310 | def _fstring(a, b, c, d): |
| 311 | return f'{a} {b:4} {c!r} {d!r:4}' |
| 312 | |
| 313 | dis_fstring = """\ |
| 314 | %3d 0 LOAD_FAST 0 (a) |
| 315 | 2 FORMAT_VALUE 0 |
| 316 | 4 LOAD_CONST 1 (' ') |
| 317 | 6 LOAD_FAST 1 (b) |
| 318 | 8 LOAD_CONST 2 ('4') |
| 319 | 10 FORMAT_VALUE 4 (with format) |
| 320 | 12 LOAD_CONST 1 (' ') |
| 321 | 14 LOAD_FAST 2 (c) |
| 322 | 16 FORMAT_VALUE 2 (repr) |
| 323 | 18 LOAD_CONST 1 (' ') |
| 324 | 20 LOAD_FAST 3 (d) |
| 325 | 22 LOAD_CONST 2 ('4') |
| 326 | 24 FORMAT_VALUE 6 (repr, with format) |
| 327 | 26 BUILD_STRING 7 |
| 328 | 28 RETURN_VALUE |
| 329 | """ % (_fstring.__code__.co_firstlineno + 1,) |
| 330 | |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 331 | def _tryfinally(a, b): |
| 332 | try: |
| 333 | return a |
| 334 | finally: |
| 335 | b() |
| 336 | |
| 337 | def _tryfinallyconst(b): |
| 338 | try: |
| 339 | return 1 |
| 340 | finally: |
| 341 | b() |
| 342 | |
| 343 | dis_tryfinally = """\ |
| 344 | %3d 0 SETUP_FINALLY 12 (to 14) |
| 345 | |
| 346 | %3d 2 LOAD_FAST 0 (a) |
| 347 | 4 POP_BLOCK |
| 348 | |
| 349 | %3d 6 LOAD_FAST 1 (b) |
| 350 | 8 CALL_FUNCTION 0 |
| 351 | 10 POP_TOP |
| 352 | |
| 353 | %3d 12 RETURN_VALUE |
| 354 | |
| 355 | %3d >> 14 LOAD_FAST 1 (b) |
| 356 | 16 CALL_FUNCTION 0 |
| 357 | 18 POP_TOP |
| 358 | 20 RERAISE |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 359 | """ % (_tryfinally.__code__.co_firstlineno + 1, |
| 360 | _tryfinally.__code__.co_firstlineno + 2, |
| 361 | _tryfinally.__code__.co_firstlineno + 4, |
| 362 | _tryfinally.__code__.co_firstlineno + 2, |
| 363 | _tryfinally.__code__.co_firstlineno + 4, |
| 364 | ) |
| 365 | |
| 366 | dis_tryfinallyconst = """\ |
| 367 | %3d 0 SETUP_FINALLY 12 (to 14) |
| 368 | |
| 369 | %3d 2 POP_BLOCK |
| 370 | |
| 371 | %3d 4 LOAD_FAST 0 (b) |
| 372 | 6 CALL_FUNCTION 0 |
| 373 | 8 POP_TOP |
| 374 | |
| 375 | %3d 10 LOAD_CONST 1 (1) |
| 376 | 12 RETURN_VALUE |
| 377 | |
| 378 | %3d >> 14 LOAD_FAST 0 (b) |
| 379 | 16 CALL_FUNCTION 0 |
| 380 | 18 POP_TOP |
| 381 | 20 RERAISE |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 382 | """ % (_tryfinallyconst.__code__.co_firstlineno + 1, |
| 383 | _tryfinallyconst.__code__.co_firstlineno + 2, |
| 384 | _tryfinallyconst.__code__.co_firstlineno + 4, |
| 385 | _tryfinallyconst.__code__.co_firstlineno + 2, |
| 386 | _tryfinallyconst.__code__.co_firstlineno + 4, |
| 387 | ) |
| 388 | |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 389 | def _g(x): |
| 390 | yield x |
| 391 | |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 392 | async def _ag(x): |
| 393 | yield x |
| 394 | |
| 395 | async def _co(x): |
| 396 | async for item in _ag(x): |
| 397 | pass |
| 398 | |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 399 | def _h(y): |
| 400 | def foo(x): |
| 401 | '''funcdoc''' |
| 402 | return [x + z for z in y] |
| 403 | return foo |
| 404 | |
| 405 | dis_nested_0 = """\ |
| 406 | %3d 0 LOAD_CLOSURE 0 (y) |
| 407 | 2 BUILD_TUPLE 1 |
| 408 | 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>) |
| 409 | 6 LOAD_CONST 2 ('_h.<locals>.foo') |
Serhiy Storchaka | e2732d3 | 2018-03-11 11:07:06 +0200 | [diff] [blame] | 410 | 8 MAKE_FUNCTION 8 (closure) |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 411 | 10 STORE_FAST 1 (foo) |
| 412 | |
| 413 | %3d 12 LOAD_FAST 1 (foo) |
| 414 | 14 RETURN_VALUE |
| 415 | """ % (_h.__code__.co_firstlineno + 1, |
| 416 | __file__, |
| 417 | _h.__code__.co_firstlineno + 1, |
| 418 | _h.__code__.co_firstlineno + 4, |
| 419 | ) |
| 420 | |
| 421 | dis_nested_1 = """%s |
| 422 | Disassembly of <code object foo at 0x..., file "%s", line %d>: |
| 423 | %3d 0 LOAD_CLOSURE 0 (x) |
| 424 | 2 BUILD_TUPLE 1 |
| 425 | 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>) |
| 426 | 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>') |
Serhiy Storchaka | e2732d3 | 2018-03-11 11:07:06 +0200 | [diff] [blame] | 427 | 8 MAKE_FUNCTION 8 (closure) |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 428 | 10 LOAD_DEREF 1 (y) |
| 429 | 12 GET_ITER |
| 430 | 14 CALL_FUNCTION 1 |
| 431 | 16 RETURN_VALUE |
| 432 | """ % (dis_nested_0, |
| 433 | __file__, |
| 434 | _h.__code__.co_firstlineno + 1, |
| 435 | _h.__code__.co_firstlineno + 3, |
| 436 | __file__, |
| 437 | _h.__code__.co_firstlineno + 3, |
| 438 | ) |
| 439 | |
| 440 | dis_nested_2 = """%s |
| 441 | Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>: |
| 442 | %3d 0 BUILD_LIST 0 |
| 443 | 2 LOAD_FAST 0 (.0) |
| 444 | >> 4 FOR_ITER 12 (to 18) |
| 445 | 6 STORE_FAST 1 (z) |
| 446 | 8 LOAD_DEREF 0 (x) |
| 447 | 10 LOAD_FAST 1 (z) |
| 448 | 12 BINARY_ADD |
| 449 | 14 LIST_APPEND 2 |
| 450 | 16 JUMP_ABSOLUTE 4 |
| 451 | >> 18 RETURN_VALUE |
| 452 | """ % (dis_nested_1, |
| 453 | __file__, |
| 454 | _h.__code__.co_firstlineno + 3, |
| 455 | _h.__code__.co_firstlineno + 3, |
| 456 | ) |
| 457 | |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 458 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 459 | class DisTests(unittest.TestCase): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 460 | |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 461 | maxDiff = None |
| 462 | |
| 463 | def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 464 | # We want to test the default printing behaviour, not the file arg |
| 465 | output = io.StringIO() |
| 466 | with contextlib.redirect_stdout(output): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 467 | if wrapper: |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 468 | dis.dis(func, **kwargs) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 469 | else: |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 470 | dis.disassemble(func, lasti, **kwargs) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 471 | return output.getvalue() |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 472 | |
| 473 | def get_disassemble_as_string(self, func, lasti=-1): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 474 | return self.get_disassembly(func, lasti, False) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 475 | |
Zachary Ware | bb4b7c1 | 2013-12-26 09:55:24 -0600 | [diff] [blame] | 476 | def strip_addresses(self, text): |
| 477 | return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text) |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 478 | |
| 479 | def do_disassembly_test(self, func, expected): |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 480 | got = self.get_disassembly(func, depth=0) |
Zachary Ware | bb4b7c1 | 2013-12-26 09:55:24 -0600 | [diff] [blame] | 481 | if got != expected: |
| 482 | got = self.strip_addresses(got) |
| 483 | self.assertEqual(got, expected) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 484 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 485 | def test_opmap(self): |
Benjamin Peterson | 76f7f4d | 2011-07-17 22:49:50 -0500 | [diff] [blame] | 486 | self.assertEqual(dis.opmap["NOP"], 9) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 487 | self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst) |
| 488 | self.assertIn(dis.opmap["STORE_NAME"], dis.hasname) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 489 | |
| 490 | def test_opname(self): |
| 491 | self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
| 492 | |
| 493 | def test_boundaries(self): |
| 494 | self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
| 495 | self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) |
| 496 | |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 497 | def test_widths(self): |
| 498 | for opcode, opname in enumerate(dis.opname): |
| 499 | if opname in ('BUILD_MAP_UNPACK_WITH_CALL', |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 500 | 'BUILD_TUPLE_UNPACK_WITH_CALL', |
| 501 | 'JUMP_IF_NOT_EXC_MATCH'): |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 502 | continue |
| 503 | with self.subTest(opname=opname): |
| 504 | width = dis._OPNAME_WIDTH |
| 505 | if opcode < dis.HAVE_ARGUMENT: |
| 506 | width += 1 + dis._OPARG_WIDTH |
| 507 | self.assertLessEqual(len(opname), width) |
| 508 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 509 | def test_dis(self): |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 510 | self.do_disassembly_test(_f, dis_f) |
| 511 | |
| 512 | def test_bug_708901(self): |
| 513 | self.do_disassembly_test(bug708901, dis_bug708901) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 514 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 515 | def test_bug_1333982(self): |
Tim Peters | 83a8c39 | 2005-12-25 22:52:32 +0000 | [diff] [blame] | 516 | # This one is checking bytecodes generated for an `assert` statement, |
| 517 | # 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] | 518 | if not __debug__: |
| 519 | self.skipTest('need asserts, run without -O') |
Zachary Ware | 9fe6d86 | 2013-12-08 00:20:35 -0600 | [diff] [blame] | 520 | |
Zachary Ware | e80e806 | 2013-12-26 09:53:49 -0600 | [diff] [blame] | 521 | self.do_disassembly_test(bug1333982, dis_bug1333982) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 522 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 523 | def test_big_linenos(self): |
| 524 | def func(count): |
| 525 | namespace = {} |
| 526 | func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 527 | exec(func, namespace) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 528 | return namespace['foo'] |
| 529 | |
| 530 | # Test all small ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 531 | for i in range(1, 300): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 532 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 533 | self.do_disassembly_test(func(i), expected) |
| 534 | |
| 535 | # Test some larger ranges too |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 536 | for i in range(300, 1000, 10): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 537 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 538 | self.do_disassembly_test(func(i), expected) |
| 539 | |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 540 | for i in range(1000, 5000, 10): |
| 541 | expected = _BIG_LINENO_FORMAT2 % (i + 2) |
| 542 | self.do_disassembly_test(func(i), expected) |
| 543 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 544 | from test import dis_module |
| 545 | self.do_disassembly_test(dis_module, dis_module_expected_results) |
| 546 | |
Serhiy Storchaka | d90045f | 2017-04-19 20:36:31 +0300 | [diff] [blame] | 547 | def test_big_offsets(self): |
| 548 | def func(count): |
| 549 | namespace = {} |
| 550 | func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x" |
| 551 | exec(func, namespace) |
| 552 | return namespace['foo'] |
| 553 | |
| 554 | def expected(count, w): |
| 555 | s = ['''\ |
| 556 | %*d LOAD_FAST 0 (x) |
| 557 | %*d LOAD_CONST 1 (1) |
| 558 | %*d BINARY_ADD |
| 559 | %*d STORE_FAST 0 (x) |
| 560 | ''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6) |
| 561 | for i in range(count)] |
| 562 | s += ['''\ |
| 563 | |
| 564 | 3 %*d LOAD_FAST 0 (x) |
| 565 | %*d RETURN_VALUE |
| 566 | ''' % (w, 8*count, w, 8*count + 2)] |
| 567 | s[0] = ' 2' + s[0][3:] |
| 568 | return ''.join(s) |
| 569 | |
| 570 | for i in range(1, 5): |
| 571 | self.do_disassembly_test(func(i), expected(i, 4)) |
| 572 | self.do_disassembly_test(func(1249), expected(1249, 4)) |
| 573 | self.do_disassembly_test(func(1250), expected(1250, 5)) |
| 574 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 575 | def test_disassemble_str(self): |
| 576 | self.do_disassembly_test(expr_str, dis_expr_str) |
| 577 | self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 578 | self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str) |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 579 | self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) |
| 580 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 581 | def test_disassemble_bytes(self): |
| 582 | self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) |
| 583 | |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 584 | def test_disassemble_class(self): |
| 585 | self.do_disassembly_test(_C, dis_c) |
| 586 | |
| 587 | def test_disassemble_instance_method(self): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 588 | self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) |
| 589 | |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 590 | def test_disassemble_instance_method_bytes(self): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 591 | method_bytecode = _C(1).__init__.__code__.co_code |
| 592 | self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) |
| 593 | |
Serhiy Storchaka | 585c93d | 2016-04-23 09:23:52 +0300 | [diff] [blame] | 594 | def test_disassemble_static_method(self): |
| 595 | self.do_disassembly_test(_C.sm, dis_c_static_method) |
| 596 | |
| 597 | def test_disassemble_class_method(self): |
| 598 | self.do_disassembly_test(_C.cm, dis_c_class_method) |
| 599 | |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 600 | def test_disassemble_generator(self): |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 601 | gen_func_disas = self.get_disassembly(_g) # Generator function |
| 602 | gen_disas = self.get_disassembly(_g(1)) # Generator iterator |
Nick Coghlan | efd5df9 | 2014-07-25 23:02:56 +1000 | [diff] [blame] | 603 | self.assertEqual(gen_disas, gen_func_disas) |
| 604 | |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 605 | def test_disassemble_async_generator(self): |
| 606 | agen_func_disas = self.get_disassembly(_ag) # Async generator function |
| 607 | agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator |
| 608 | self.assertEqual(agen_disas, agen_func_disas) |
| 609 | |
| 610 | def test_disassemble_coroutine(self): |
| 611 | coro_func_disas = self.get_disassembly(_co) # Coroutine function |
| 612 | coro = _co(1) # Coroutine object |
| 613 | coro.close() # Avoid a RuntimeWarning (never awaited) |
| 614 | coro_disas = self.get_disassembly(coro) |
| 615 | self.assertEqual(coro_disas, coro_func_disas) |
| 616 | |
Serhiy Storchaka | dd102f7 | 2016-10-08 12:34:25 +0300 | [diff] [blame] | 617 | def test_disassemble_fstring(self): |
| 618 | self.do_disassembly_test(_fstring, dis_fstring) |
| 619 | |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 620 | def test_disassemble_try_finally(self): |
| 621 | self.do_disassembly_test(_tryfinally, dis_tryfinally) |
| 622 | self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst) |
| 623 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 624 | def test_dis_none(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 625 | try: |
| 626 | del sys.last_traceback |
| 627 | except AttributeError: |
| 628 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 629 | self.assertRaises(RuntimeError, dis.dis, None) |
| 630 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 631 | def test_dis_traceback(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 632 | try: |
| 633 | del sys.last_traceback |
| 634 | except AttributeError: |
| 635 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 636 | |
| 637 | try: |
| 638 | 1/0 |
| 639 | except Exception as e: |
| 640 | tb = e.__traceback__ |
| 641 | sys.last_traceback = tb |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 642 | |
| 643 | tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) |
| 644 | self.do_disassembly_test(None, tb_dis) |
| 645 | |
| 646 | def test_dis_object(self): |
| 647 | self.assertRaises(TypeError, dis.dis, object()) |
| 648 | |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 649 | def test_disassemble_recursive(self): |
| 650 | def check(expected, **kwargs): |
| 651 | dis = self.get_disassembly(_h, **kwargs) |
| 652 | dis = self.strip_addresses(dis) |
| 653 | self.assertEqual(dis, expected) |
| 654 | |
| 655 | check(dis_nested_0, depth=0) |
| 656 | check(dis_nested_1, depth=1) |
| 657 | check(dis_nested_2, depth=2) |
| 658 | check(dis_nested_2, depth=3) |
| 659 | check(dis_nested_2, depth=None) |
| 660 | check(dis_nested_2) |
| 661 | |
| 662 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 663 | class DisWithFileTests(DisTests): |
| 664 | |
| 665 | # Run the tests again, using the file arg instead of print |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 666 | def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 667 | output = io.StringIO() |
| 668 | if wrapper: |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 669 | dis.dis(func, file=output, **kwargs) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 670 | else: |
Serhiy Storchaka | 1efbf92 | 2017-06-11 14:09:39 +0300 | [diff] [blame] | 671 | dis.disassemble(func, lasti, file=output, **kwargs) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 672 | return output.getvalue() |
| 673 | |
| 674 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 675 | if sys.flags.optimize: |
| 676 | code_info_consts = "0: None" |
| 677 | else: |
| 678 | code_info_consts = ( |
| 679 | """0: 'Formatted details of methods, functions, or code.' |
| 680 | 1: None""" |
| 681 | ) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 682 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 683 | code_info_code_info = f"""\ |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 684 | Name: code_info |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 685 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 686 | Argument count: 1 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 687 | Positional-only arguments: 0 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 688 | Kw-only arguments: 0 |
| 689 | Number of locals: 1 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 690 | Stack size: 3 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 691 | Flags: OPTIMIZED, NEWLOCALS, NOFREE |
| 692 | Constants: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 693 | {code_info_consts} |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 694 | Names: |
| 695 | 0: _format_code_info |
| 696 | 1: _get_code_object |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 697 | Variable names: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 698 | 0: x""" |
| 699 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 700 | |
| 701 | @staticmethod |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 702 | def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds): |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 703 | def f(c=c): |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 704 | print(a, b, x, y, z, c, d, e, f) |
| 705 | yield a, b, x, y, z, c, d, e, f |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 706 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 707 | code_info_tricky = """\ |
| 708 | Name: tricky |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 709 | Filename: (.*) |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 710 | Argument count: 5 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 711 | Positional-only arguments: 2 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 712 | Kw-only arguments: 3 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 713 | Number of locals: 10 |
| 714 | Stack size: 9 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 715 | Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR |
| 716 | Constants: |
| 717 | 0: None |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 718 | 1: <code object f at (.*), file "(.*)", line (.*)> |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 719 | 2: 'tricky.<locals>.f' |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 720 | Variable names: |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 721 | 0: a |
| 722 | 1: b |
| 723 | 2: x |
| 724 | 3: y |
| 725 | 4: z |
| 726 | 5: c |
| 727 | 6: d |
| 728 | 7: e |
| 729 | 8: args |
| 730 | 9: kwds |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 731 | Cell variables: |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 732 | 0: [abedfxyz] |
| 733 | 1: [abedfxyz] |
| 734 | 2: [abedfxyz] |
| 735 | 3: [abedfxyz] |
| 736 | 4: [abedfxyz] |
| 737 | 5: [abedfxyz]""" |
Georg Brandl | a108227 | 2012-02-20 21:41:03 +0100 | [diff] [blame] | 738 | # NOTE: the order of the cell variables above depends on dictionary order! |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 739 | |
| 740 | co_tricky_nested_f = tricky.__func__.__code__.co_consts[1] |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 741 | |
| 742 | code_info_tricky_nested_f = """\ |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 743 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 744 | Argument count: 1 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 745 | Positional-only arguments: 0 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 746 | Kw-only arguments: 0 |
| 747 | Number of locals: 1 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 748 | Stack size: 10 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 749 | Flags: OPTIMIZED, NEWLOCALS, NESTED |
| 750 | Constants: |
| 751 | 0: None |
| 752 | Names: |
| 753 | 0: print |
| 754 | Variable names: |
| 755 | 0: c |
| 756 | Free variables: |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 757 | 0: [abedfxyz] |
| 758 | 1: [abedfxyz] |
| 759 | 2: [abedfxyz] |
| 760 | 3: [abedfxyz] |
| 761 | 4: [abedfxyz] |
| 762 | 5: [abedfxyz]""" |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 763 | |
| 764 | code_info_expr_str = """\ |
| 765 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 766 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 767 | Argument count: 0 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 768 | Positional-only arguments: 0 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 769 | Kw-only arguments: 0 |
| 770 | Number of locals: 0 |
| 771 | Stack size: 2 |
| 772 | Flags: NOFREE |
| 773 | Constants: |
| 774 | 0: 1 |
| 775 | Names: |
| 776 | 0: x""" |
| 777 | |
| 778 | code_info_simple_stmt_str = """\ |
| 779 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 780 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 781 | Argument count: 0 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 782 | Positional-only arguments: 0 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 783 | Kw-only arguments: 0 |
| 784 | Number of locals: 0 |
| 785 | Stack size: 2 |
| 786 | Flags: NOFREE |
| 787 | Constants: |
| 788 | 0: 1 |
| 789 | 1: None |
| 790 | Names: |
| 791 | 0: x""" |
| 792 | |
| 793 | code_info_compound_stmt_str = """\ |
| 794 | Name: <module> |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 795 | Filename: <disassembly> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 796 | Argument count: 0 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 797 | Positional-only arguments: 0 |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 798 | Kw-only arguments: 0 |
| 799 | Number of locals: 0 |
| 800 | Stack size: 2 |
| 801 | Flags: NOFREE |
| 802 | Constants: |
| 803 | 0: 0 |
| 804 | 1: 1 |
| 805 | 2: None |
| 806 | Names: |
| 807 | 0: x""" |
| 808 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 809 | |
| 810 | async def async_def(): |
| 811 | await 1 |
| 812 | async for a in b: pass |
| 813 | async with c as d: pass |
| 814 | |
| 815 | code_info_async_def = """\ |
| 816 | Name: async_def |
| 817 | Filename: (.*) |
| 818 | Argument count: 0 |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 819 | Positional-only arguments: 0 |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 820 | Kw-only arguments: 0 |
| 821 | Number of locals: 2 |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 822 | Stack size: 9 |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 823 | Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 824 | Constants: |
| 825 | 0: None |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 826 | 1: 1 |
| 827 | Names: |
| 828 | 0: b |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 829 | 1: c |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 830 | Variable names: |
| 831 | 0: a |
| 832 | 1: d""" |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 833 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 834 | class CodeInfoTests(unittest.TestCase): |
| 835 | test_pairs = [ |
| 836 | (dis.code_info, code_info_code_info), |
| 837 | (tricky, code_info_tricky), |
| 838 | (co_tricky_nested_f, code_info_tricky_nested_f), |
| 839 | (expr_str, code_info_expr_str), |
| 840 | (simple_stmt_str, code_info_simple_stmt_str), |
| 841 | (compound_stmt_str, code_info_compound_stmt_str), |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 842 | (async_def, code_info_async_def) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 843 | ] |
| 844 | |
| 845 | def test_code_info(self): |
| 846 | self.maxDiff = 1000 |
| 847 | for x, expected in self.test_pairs: |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 848 | self.assertRegex(dis.code_info(x), expected) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 849 | |
| 850 | def test_show_code(self): |
| 851 | self.maxDiff = 1000 |
| 852 | for x, expected in self.test_pairs: |
| 853 | with captured_stdout() as output: |
| 854 | dis.show_code(x) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 855 | self.assertRegex(output.getvalue(), expected+"\n") |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 856 | output = io.StringIO() |
| 857 | dis.show_code(x, file=output) |
| 858 | self.assertRegex(output.getvalue(), expected) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 859 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 860 | def test_code_info_object(self): |
| 861 | self.assertRaises(TypeError, dis.code_info, object()) |
| 862 | |
| 863 | def test_pretty_flags_no_flags(self): |
| 864 | self.assertEqual(dis.pretty_flags(0), '0x0') |
| 865 | |
| 866 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 867 | # Fodder for instruction introspection tests |
| 868 | # Editing any of these may require recalculating the expected output |
| 869 | def outer(a=1, b=2): |
| 870 | def f(c=3, d=4): |
| 871 | def inner(e=5, f=6): |
| 872 | print(a, b, c, d, e, f) |
| 873 | print(a, b, c, d) |
| 874 | return inner |
| 875 | print(a, b, '', 1, [], {}, "Hello world!") |
| 876 | return f |
| 877 | |
| 878 | def jumpy(): |
| 879 | # This won't actually run (but that's OK, we only disassemble it) |
| 880 | for i in range(10): |
| 881 | print(i) |
| 882 | if i < 4: |
| 883 | continue |
| 884 | if i > 6: |
| 885 | break |
| 886 | else: |
| 887 | print("I can haz else clause?") |
| 888 | while i: |
| 889 | print(i) |
| 890 | i -= 1 |
| 891 | if i > 6: |
| 892 | continue |
| 893 | if i < 4: |
| 894 | break |
| 895 | else: |
| 896 | print("Who let lolcatz into this test suite?") |
| 897 | try: |
| 898 | 1 / 0 |
| 899 | except ZeroDivisionError: |
| 900 | print("Here we go, here we go, here we go...") |
| 901 | else: |
| 902 | with i as dodgy: |
| 903 | print("Never reach this") |
| 904 | finally: |
| 905 | print("OK, now we're done") |
| 906 | |
| 907 | # End fodder for opinfo generation tests |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 908 | expected_outer_line = 1 |
| 909 | _line_offset = outer.__code__.co_firstlineno - 1 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 910 | code_object_f = outer.__code__.co_consts[3] |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 911 | expected_f_line = code_object_f.co_firstlineno - _line_offset |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 912 | code_object_inner = code_object_f.co_consts[3] |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 913 | expected_inner_line = code_object_inner.co_firstlineno - _line_offset |
| 914 | expected_jumpy_line = 1 |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 915 | |
| 916 | # The following lines are useful to regenerate the expected results after |
| 917 | # either the fodder is modified or the bytecode generation changes |
| 918 | # After regeneration, update the references to code_object_f and |
| 919 | # code_object_inner before rerunning the tests |
| 920 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 921 | #_instructions = dis.get_instructions(outer, first_line=expected_outer_line) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 922 | #print('expected_opinfo_outer = [\n ', |
| 923 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 924 | #_instructions = dis.get_instructions(outer(), first_line=expected_f_line) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 925 | #print('expected_opinfo_f = [\n ', |
| 926 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 927 | #_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 928 | #print('expected_opinfo_inner = [\n ', |
| 929 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 930 | #_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 931 | #print('expected_opinfo_jumpy = [\n ', |
| 932 | #',\n '.join(map(str, _instructions)), ',\n]', sep='') |
| 933 | |
| 934 | |
| 935 | Instruction = dis.Instruction |
| 936 | expected_opinfo_outer = [ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 937 | Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False), |
| 938 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
| 939 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
| 940 | Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False), |
| 941 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False), |
| 942 | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | e2732d3 | 2018-03-11 11:07:06 +0200 | [diff] [blame] | 943 | Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=12, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 944 | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False), |
| 945 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False), |
| 946 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False), |
| 947 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False), |
| 948 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False), |
| 949 | Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False), |
| 950 | Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False), |
| 951 | Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False), |
| 952 | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=30, starts_line=None, is_jump_target=False), |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 953 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=32, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 954 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False), |
| 955 | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False), |
| 956 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False), |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 957 | ] |
| 958 | |
| 959 | expected_opinfo_f = [ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 960 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False), |
| 961 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
| 962 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
| 963 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False), |
| 964 | Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False), |
| 965 | Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False), |
| 966 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False), |
| 967 | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | e2732d3 | 2018-03-11 11:07:06 +0200 | [diff] [blame] | 968 | Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 969 | Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False), |
| 970 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False), |
| 971 | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False), |
| 972 | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False), |
| 973 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False), |
| 974 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=28, starts_line=None, is_jump_target=False), |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 975 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=30, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 976 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False), |
| 977 | Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False), |
| 978 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False), |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 979 | ] |
| 980 | |
| 981 | expected_opinfo_inner = [ |
| 982 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False), |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 983 | Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False), |
| 984 | Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False), |
| 985 | Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False), |
| 986 | Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False), |
| 987 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False), |
| 988 | Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False), |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 989 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False), |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 990 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False), |
| 991 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False), |
| 992 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False), |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 993 | ] |
| 994 | |
| 995 | expected_opinfo_jumpy = [ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 996 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False), |
| 997 | Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False), |
| 998 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False), |
| 999 | Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False), |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 1000 | Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=42, argrepr='to 42', offset=8, starts_line=None, is_jump_target=True), |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1001 | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False), |
| 1002 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False), |
| 1003 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False), |
| 1004 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False), |
| 1005 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False), |
| 1006 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False), |
| 1007 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False), |
| 1008 | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False), |
| 1009 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False), |
| 1010 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False), |
| 1011 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True), |
| 1012 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False), |
| 1013 | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False), |
| 1014 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False), |
| 1015 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False), |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 1016 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=40, starts_line=None, is_jump_target=False), |
| 1017 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=42, starts_line=10, is_jump_target=True), |
| 1018 | Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=44, starts_line=None, is_jump_target=False), |
| 1019 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=46, starts_line=None, is_jump_target=False), |
| 1020 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=48, starts_line=None, is_jump_target=False), |
| 1021 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=50, starts_line=11, is_jump_target=True), |
| 1022 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=90, argval=90, argrepr='', offset=52, starts_line=None, is_jump_target=False), |
| 1023 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=54, starts_line=12, is_jump_target=False), |
| 1024 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=False), |
| 1025 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=58, starts_line=None, is_jump_target=False), |
| 1026 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=False), |
| 1027 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=13, is_jump_target=False), |
| 1028 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=64, starts_line=None, is_jump_target=False), |
| 1029 | Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False), |
| 1030 | Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=68, starts_line=None, is_jump_target=False), |
| 1031 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=70, starts_line=14, is_jump_target=False), |
| 1032 | Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=72, starts_line=None, is_jump_target=False), |
| 1033 | Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=74, starts_line=None, is_jump_target=False), |
| 1034 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=76, starts_line=None, is_jump_target=False), |
| 1035 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=78, starts_line=15, is_jump_target=False), |
| 1036 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=16, is_jump_target=True), |
| 1037 | Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=82, starts_line=None, is_jump_target=False), |
| 1038 | Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=84, starts_line=None, is_jump_target=False), |
| 1039 | Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=50, argval=50, argrepr='', offset=86, starts_line=None, is_jump_target=False), |
| 1040 | Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=98, argval=98, argrepr='', offset=88, starts_line=17, is_jump_target=False), |
| 1041 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=90, starts_line=19, is_jump_target=True), |
| 1042 | 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=92, starts_line=None, is_jump_target=False), |
| 1043 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=94, starts_line=None, is_jump_target=False), |
| 1044 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False), |
| 1045 | Instruction(opname='SETUP_FINALLY', opcode=122, arg=98, argval=198, argrepr='to 198', offset=98, starts_line=20, is_jump_target=True), |
| 1046 | Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=114, argrepr='to 114', offset=100, starts_line=None, is_jump_target=False), |
| 1047 | Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=102, starts_line=21, is_jump_target=False), |
| 1048 | Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=104, starts_line=None, is_jump_target=False), |
| 1049 | Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False), |
| 1050 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=108, starts_line=None, is_jump_target=False), |
| 1051 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False), |
| 1052 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=140, argrepr='to 140', offset=112, starts_line=None, is_jump_target=False), |
| 1053 | Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=114, starts_line=22, is_jump_target=True), |
| 1054 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=116, starts_line=None, is_jump_target=False), |
| 1055 | Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=138, argval=138, argrepr='', offset=118, starts_line=None, is_jump_target=False), |
| 1056 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False), |
| 1057 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=122, starts_line=None, is_jump_target=False), |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1058 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False), |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 1059 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=126, starts_line=23, is_jump_target=False), |
| 1060 | 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=128, starts_line=None, is_jump_target=False), |
| 1061 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=130, starts_line=None, is_jump_target=False), |
| 1062 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False), |
| 1063 | Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False), |
| 1064 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=184, argrepr='to 184', offset=136, starts_line=None, is_jump_target=False), |
| 1065 | Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=True), |
| 1066 | Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=140, starts_line=25, is_jump_target=True), |
| 1067 | Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=168, argrepr='to 168', offset=142, starts_line=None, is_jump_target=False), |
| 1068 | Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=144, starts_line=None, is_jump_target=False), |
| 1069 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=146, starts_line=26, is_jump_target=False), |
| 1070 | Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=148, starts_line=None, is_jump_target=False), |
| 1071 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=150, starts_line=None, is_jump_target=False), |
| 1072 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=152, starts_line=None, is_jump_target=False), |
| 1073 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False), |
| 1074 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=156, starts_line=None, is_jump_target=False), |
| 1075 | Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False), |
| 1076 | Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False), |
| 1077 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=162, starts_line=None, is_jump_target=False), |
| 1078 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False), |
| 1079 | Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=184, argrepr='to 184', offset=166, starts_line=None, is_jump_target=False), |
| 1080 | Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=True), |
| 1081 | Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=174, argval=174, argrepr='', offset=170, starts_line=None, is_jump_target=False), |
| 1082 | Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False), |
| 1083 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=True), |
| 1084 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False), |
| 1085 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False), |
| 1086 | Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=False), |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1087 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False), |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 1088 | Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=True), |
| 1089 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=186, starts_line=28, is_jump_target=False), |
| 1090 | Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=188, starts_line=None, is_jump_target=False), |
| 1091 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=190, starts_line=None, is_jump_target=False), |
| 1092 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False), |
| 1093 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=194, starts_line=None, is_jump_target=False), |
| 1094 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False), |
| 1095 | Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=198, starts_line=None, is_jump_target=True), |
| 1096 | Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=200, starts_line=None, is_jump_target=False), |
| 1097 | Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=202, starts_line=None, is_jump_target=False), |
| 1098 | Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False), |
| 1099 | Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False), |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1100 | ] |
| 1101 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1102 | # One last piece of inspect fodder to check the default line number handling |
| 1103 | def simple(): pass |
| 1104 | expected_opinfo_simple = [ |
| 1105 | Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False), |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1106 | Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=2, starts_line=None, is_jump_target=False) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1107 | ] |
| 1108 | |
| 1109 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1110 | class InstructionTests(BytecodeTestCase): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1111 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1112 | def __init__(self, *args): |
| 1113 | super().__init__(*args) |
| 1114 | self.maxDiff = None |
| 1115 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1116 | def test_default_first_line(self): |
| 1117 | actual = dis.get_instructions(simple) |
| 1118 | self.assertEqual(list(actual), expected_opinfo_simple) |
| 1119 | |
| 1120 | def test_first_line_set_to_None(self): |
| 1121 | actual = dis.get_instructions(simple, first_line=None) |
| 1122 | self.assertEqual(list(actual), expected_opinfo_simple) |
| 1123 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1124 | def test_outer(self): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1125 | actual = dis.get_instructions(outer, first_line=expected_outer_line) |
| 1126 | self.assertEqual(list(actual), expected_opinfo_outer) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1127 | |
| 1128 | def test_nested(self): |
| 1129 | with captured_stdout(): |
| 1130 | f = outer() |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1131 | actual = dis.get_instructions(f, first_line=expected_f_line) |
| 1132 | self.assertEqual(list(actual), expected_opinfo_f) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1133 | |
| 1134 | def test_doubly_nested(self): |
| 1135 | with captured_stdout(): |
| 1136 | inner = outer()() |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1137 | actual = dis.get_instructions(inner, first_line=expected_inner_line) |
| 1138 | self.assertEqual(list(actual), expected_opinfo_inner) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1139 | |
| 1140 | def test_jumpy(self): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1141 | actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line) |
| 1142 | self.assertEqual(list(actual), expected_opinfo_jumpy) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1143 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1144 | # get_instructions has its own tests above, so can rely on it to validate |
| 1145 | # the object oriented API |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1146 | class BytecodeTests(unittest.TestCase): |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1147 | |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1148 | def test_instantiation(self): |
| 1149 | # Test with function, method, code string and code object |
| 1150 | for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1151 | with self.subTest(obj=obj): |
| 1152 | b = dis.Bytecode(obj) |
| 1153 | self.assertIsInstance(b.codeobj, types.CodeType) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1154 | |
| 1155 | self.assertRaises(TypeError, dis.Bytecode, object()) |
| 1156 | |
| 1157 | def test_iteration(self): |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1158 | for obj in [_f, _C(1).__init__, "a=1", _f.__code__]: |
| 1159 | with self.subTest(obj=obj): |
| 1160 | via_object = list(dis.Bytecode(obj)) |
| 1161 | via_generator = list(dis.get_instructions(obj)) |
| 1162 | self.assertEqual(via_object, via_generator) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1163 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1164 | def test_explicit_first_line(self): |
| 1165 | actual = dis.Bytecode(outer, first_line=expected_outer_line) |
| 1166 | self.assertEqual(list(actual), expected_opinfo_outer) |
| 1167 | |
| 1168 | def test_source_line_in_disassembly(self): |
| 1169 | # Use the line in the source code |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 1170 | actual = dis.Bytecode(simple).dis() |
| 1171 | actual = actual.strip().partition(" ")[0] # extract the line no |
| 1172 | expected = str(simple.__code__.co_firstlineno) |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1173 | self.assertEqual(actual, expected) |
| 1174 | # Use an explicit first line number |
syncosmic | fe2b56a | 2017-08-17 19:29:21 -0700 | [diff] [blame] | 1175 | actual = dis.Bytecode(simple, first_line=350).dis() |
| 1176 | actual = actual.strip().partition(" ")[0] # extract the line no |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1177 | self.assertEqual(actual, "350") |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1178 | |
| 1179 | def test_info(self): |
| 1180 | self.maxDiff = 1000 |
| 1181 | for x, expected in CodeInfoTests.test_pairs: |
| 1182 | b = dis.Bytecode(x) |
| 1183 | self.assertRegex(b.info(), expected) |
| 1184 | |
Nick Coghlan | 90b8e7d | 2013-11-06 22:08:36 +1000 | [diff] [blame] | 1185 | def test_disassembled(self): |
| 1186 | actual = dis.Bytecode(_f).dis() |
| 1187 | self.assertEqual(actual, dis_f) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1188 | |
Nick Coghlan | 50c48b8 | 2013-11-23 00:57:00 +1000 | [diff] [blame] | 1189 | def test_from_traceback(self): |
| 1190 | tb = get_tb() |
| 1191 | b = dis.Bytecode.from_traceback(tb) |
| 1192 | while tb.tb_next: tb = tb.tb_next |
| 1193 | |
| 1194 | self.assertEqual(b.current_offset, tb.tb_lasti) |
| 1195 | |
| 1196 | def test_from_traceback_dis(self): |
| 1197 | tb = get_tb() |
| 1198 | b = dis.Bytecode.from_traceback(tb) |
| 1199 | self.assertEqual(b.dis(), dis_traceback) |
Nick Coghlan | b39fd0c | 2013-05-06 23:59:20 +1000 | [diff] [blame] | 1200 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 1201 | if __name__ == "__main__": |
Zachary Ware | e80e806 | 2013-12-26 09:53:49 -0600 | [diff] [blame] | 1202 | unittest.main() |