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