Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | # Minimal tests for dis module |
| 2 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 3 | from test.support import run_unittest, captured_stdout |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 4 | import difflib |
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 |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 10 | class _C: |
| 11 | def __init__(self, x): |
| 12 | self.x = x == 1 |
| 13 | |
| 14 | dis_c_instance_method = """\ |
| 15 | %-4d 0 LOAD_FAST 1 (x) |
| 16 | 3 LOAD_CONST 1 (1) |
| 17 | 6 COMPARE_OP 2 (==) |
| 18 | 9 LOAD_FAST 0 (self) |
| 19 | 12 STORE_ATTR 0 (x) |
| 20 | 15 LOAD_CONST 0 (None) |
| 21 | 18 RETURN_VALUE |
| 22 | """ % (_C.__init__.__code__.co_firstlineno + 1,) |
| 23 | |
| 24 | dis_c_instance_method_bytes = """\ |
| 25 | 0 LOAD_FAST 1 (1) |
| 26 | 3 LOAD_CONST 1 (1) |
| 27 | 6 COMPARE_OP 2 (==) |
| 28 | 9 LOAD_FAST 0 (0) |
| 29 | 12 STORE_ATTR 0 (0) |
| 30 | 15 LOAD_CONST 0 (0) |
| 31 | 18 RETURN_VALUE |
| 32 | """ |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 33 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 34 | def _f(a): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 35 | print(a) |
Tim Peters | eabafeb | 2003-03-07 15:55:36 +0000 | [diff] [blame] | 36 | return 1 |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 37 | |
| 38 | dis_f = """\ |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 39 | %-4d 0 LOAD_GLOBAL 0 (print) |
| 40 | 3 LOAD_FAST 0 (a) |
| 41 | 6 CALL_FUNCTION 1 |
| 42 | 9 POP_TOP |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 43 | |
Georg Brandl | 88fc664 | 2007-02-09 21:28:07 +0000 | [diff] [blame] | 44 | %-4d 10 LOAD_CONST 1 (1) |
| 45 | 13 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 46 | """ % (_f.__code__.co_firstlineno + 1, |
| 47 | _f.__code__.co_firstlineno + 2) |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 48 | |
| 49 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 50 | dis_f_co_code = """\ |
| 51 | 0 LOAD_GLOBAL 0 (0) |
| 52 | 3 LOAD_FAST 0 (0) |
| 53 | 6 CALL_FUNCTION 1 |
| 54 | 9 POP_TOP |
| 55 | 10 LOAD_CONST 1 (1) |
| 56 | 13 RETURN_VALUE |
| 57 | """ |
| 58 | |
| 59 | |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 60 | def bug708901(): |
| 61 | for res in range(1, |
| 62 | 10): |
| 63 | pass |
| 64 | |
| 65 | dis_bug708901 = """\ |
| 66 | %-4d 0 SETUP_LOOP 23 (to 26) |
| 67 | 3 LOAD_GLOBAL 0 (range) |
| 68 | 6 LOAD_CONST 1 (1) |
| 69 | |
| 70 | %-4d 9 LOAD_CONST 2 (10) |
| 71 | 12 CALL_FUNCTION 2 |
| 72 | 15 GET_ITER |
| 73 | >> 16 FOR_ITER 6 (to 25) |
| 74 | 19 STORE_FAST 0 (res) |
| 75 | |
| 76 | %-4d 22 JUMP_ABSOLUTE 16 |
| 77 | >> 25 POP_BLOCK |
| 78 | >> 26 LOAD_CONST 0 (None) |
| 79 | 29 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 80 | """ % (bug708901.__code__.co_firstlineno + 1, |
| 81 | bug708901.__code__.co_firstlineno + 2, |
| 82 | bug708901.__code__.co_firstlineno + 3) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 83 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 84 | |
| 85 | def bug1333982(x=[]): |
| 86 | assert 0, ([s for s in x] + |
| 87 | 1) |
| 88 | pass |
| 89 | |
| 90 | dis_bug1333982 = """\ |
| 91 | %-4d 0 LOAD_CONST 1 (0) |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 92 | 3 JUMP_IF_TRUE 33 (to 39) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 93 | 6 POP_TOP |
| 94 | 7 LOAD_GLOBAL 0 (AssertionError) |
| 95 | 10 BUILD_LIST 0 |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 96 | 13 LOAD_FAST 0 (x) |
| 97 | 16 GET_ITER |
| 98 | >> 17 FOR_ITER 12 (to 32) |
| 99 | 20 STORE_FAST 1 (s) |
| 100 | 23 LOAD_FAST 1 (s) |
| 101 | 26 LIST_APPEND 2 |
| 102 | 29 JUMP_ABSOLUTE 17 |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 103 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 104 | %-4d >> 32 LOAD_CONST 2 (1) |
| 105 | 35 BINARY_ADD |
| 106 | 36 RAISE_VARARGS 2 |
| 107 | >> 39 POP_TOP |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f289ae6 | 2008-12-18 11:06:25 +0000 | [diff] [blame] | 109 | %-4d 40 LOAD_CONST 0 (None) |
| 110 | 43 RETURN_VALUE |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 111 | """ % (bug1333982.__code__.co_firstlineno + 1, |
| 112 | bug1333982.__code__.co_firstlineno + 2, |
| 113 | bug1333982.__code__.co_firstlineno + 3) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 114 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 115 | _BIG_LINENO_FORMAT = """\ |
| 116 | %3d 0 LOAD_GLOBAL 0 (spam) |
| 117 | 3 POP_TOP |
| 118 | 4 LOAD_CONST 0 (None) |
| 119 | 7 RETURN_VALUE |
| 120 | """ |
| 121 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 122 | dis_module_expected_results = """\ |
| 123 | Disassembly of f: |
| 124 | 4 0 LOAD_CONST 0 (None) |
| 125 | 3 RETURN_VALUE |
| 126 | |
| 127 | Disassembly of g: |
| 128 | 5 0 LOAD_CONST 0 (None) |
| 129 | 3 RETURN_VALUE |
| 130 | |
| 131 | """ |
| 132 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 133 | expr_str = "x + 1" |
| 134 | |
| 135 | dis_expr_str = """\ |
| 136 | 1 0 LOAD_NAME 0 (x) |
| 137 | 3 LOAD_CONST 0 (1) |
| 138 | 6 BINARY_ADD |
| 139 | 7 RETURN_VALUE |
| 140 | """ |
| 141 | |
| 142 | simple_stmt_str = "x = x + 1" |
| 143 | |
| 144 | dis_simple_stmt_str = """\ |
| 145 | 1 0 LOAD_NAME 0 (x) |
| 146 | 3 LOAD_CONST 0 (1) |
| 147 | 6 BINARY_ADD |
| 148 | 7 STORE_NAME 0 (x) |
| 149 | 10 LOAD_CONST 1 (None) |
| 150 | 13 RETURN_VALUE |
| 151 | """ |
| 152 | |
| 153 | compound_stmt_str = """\ |
| 154 | x = 0 |
| 155 | while 1: |
| 156 | x += 1""" |
| 157 | # Trailing newline has been deliberately omitted |
| 158 | |
| 159 | dis_compound_stmt_str = """\ |
| 160 | 1 0 LOAD_CONST 0 (0) |
| 161 | 3 STORE_NAME 0 (x) |
| 162 | |
| 163 | 2 6 SETUP_LOOP 13 (to 22) |
| 164 | |
| 165 | 3 >> 9 LOAD_NAME 0 (x) |
| 166 | 12 LOAD_CONST 1 (1) |
| 167 | 15 INPLACE_ADD |
| 168 | 16 STORE_NAME 0 (x) |
| 169 | 19 JUMP_ABSOLUTE 9 |
| 170 | >> 22 LOAD_CONST 2 (None) |
| 171 | 25 RETURN_VALUE |
| 172 | """ |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 173 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 174 | class DisTests(unittest.TestCase): |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 175 | |
| 176 | def get_disassembly(self, func, lasti=-1, wrapper=True): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 177 | s = io.StringIO() |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 178 | save_stdout = sys.stdout |
| 179 | sys.stdout = s |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 180 | try: |
| 181 | if wrapper: |
| 182 | dis.dis(func) |
| 183 | else: |
| 184 | dis.disassemble(func, lasti) |
| 185 | finally: |
| 186 | sys.stdout = save_stdout |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 187 | # Trim trailing blanks (if any). |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 188 | return [line.rstrip() for line in s.getvalue().splitlines()] |
| 189 | |
| 190 | def get_disassemble_as_string(self, func, lasti=-1): |
| 191 | return '\n'.join(self.get_disassembly(func, lasti, False)) |
| 192 | |
| 193 | def do_disassembly_test(self, func, expected): |
| 194 | lines = self.get_disassembly(func) |
| 195 | expected = expected.splitlines() |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 196 | if expected != lines: |
| 197 | self.fail( |
| 198 | "events did not match expectation:\n" + |
| 199 | "\n".join(difflib.ndiff(expected, |
| 200 | lines))) |
| 201 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 202 | def test_opmap(self): |
| 203 | self.assertEqual(dis.opmap["STOP_CODE"], 0) |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 204 | self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst) |
| 205 | self.assertIn(dis.opmap["STORE_NAME"], dis.hasname) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 206 | |
| 207 | def test_opname(self): |
| 208 | self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST") |
| 209 | |
| 210 | def test_boundaries(self): |
| 211 | self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG) |
| 212 | self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT) |
| 213 | |
| 214 | def test_dis(self): |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 215 | self.do_disassembly_test(_f, dis_f) |
| 216 | |
| 217 | def test_bug_708901(self): |
| 218 | self.do_disassembly_test(bug708901, dis_bug708901) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 219 | |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 220 | def test_bug_1333982(self): |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 221 | # XXX: re-enable this test! |
Tim Peters | 83a8c39 | 2005-12-25 22:52:32 +0000 | [diff] [blame] | 222 | # This one is checking bytecodes generated for an `assert` statement, |
| 223 | # so fails if the tests are run with -O. Skip this test then. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 224 | pass # Test has been disabled due to change in the way |
| 225 | # list comps are handled. The byte code now includes |
| 226 | # a memory address and a file location, so they change from |
| 227 | # run to run. |
| 228 | # if __debug__: |
| 229 | # self.do_disassembly_test(bug1333982, dis_bug1333982) |
Neal Norwitz | 51abbc7 | 2005-12-18 07:06:23 +0000 | [diff] [blame] | 230 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 231 | def test_big_linenos(self): |
| 232 | def func(count): |
| 233 | namespace = {} |
| 234 | func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"]) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 235 | exec(func, namespace) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 236 | return namespace['foo'] |
| 237 | |
| 238 | # Test all small ranges |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 239 | for i in range(1, 300): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 240 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 241 | self.do_disassembly_test(func(i), expected) |
| 242 | |
| 243 | # Test some larger ranges too |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 244 | for i in range(300, 5000, 10): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 245 | expected = _BIG_LINENO_FORMAT % (i + 2) |
| 246 | self.do_disassembly_test(func(i), expected) |
| 247 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 248 | def test_big_linenos(self): |
| 249 | from test import dis_module |
| 250 | self.do_disassembly_test(dis_module, dis_module_expected_results) |
| 251 | |
Nick Coghlan | 5c8b54e | 2010-07-03 07:36:51 +0000 | [diff] [blame] | 252 | def test_disassemble_str(self): |
| 253 | self.do_disassembly_test(expr_str, dis_expr_str) |
| 254 | self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str) |
| 255 | self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str) |
| 256 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 257 | def test_disassemble_bytes(self): |
| 258 | self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code) |
| 259 | |
| 260 | def test_disassemble_method(self): |
| 261 | self.do_disassembly_test(_C(1).__init__, dis_c_instance_method) |
| 262 | |
| 263 | def test_disassemble_method_bytes(self): |
| 264 | method_bytecode = _C(1).__init__.__code__.co_code |
| 265 | self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes) |
| 266 | |
| 267 | def test_dis_none(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 268 | try: |
| 269 | del sys.last_traceback |
| 270 | except AttributeError: |
| 271 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 272 | self.assertRaises(RuntimeError, dis.dis, None) |
| 273 | |
| 274 | def test_dis_object(self): |
| 275 | self.assertRaises(TypeError, dis.dis, object()) |
| 276 | |
| 277 | def test_dis_traceback(self): |
Benjamin Peterson | 47afc2a | 2011-03-15 15:54:50 -0500 | [diff] [blame] | 278 | try: |
| 279 | del sys.last_traceback |
| 280 | except AttributeError: |
| 281 | pass |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 282 | |
| 283 | try: |
| 284 | 1/0 |
| 285 | except Exception as e: |
| 286 | tb = e.__traceback__ |
| 287 | sys.last_traceback = tb |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 288 | |
| 289 | tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti) |
| 290 | self.do_disassembly_test(None, tb_dis) |
| 291 | |
| 292 | def test_dis_object(self): |
| 293 | self.assertRaises(TypeError, dis.dis, object()) |
| 294 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 295 | code_info_code_info = """\ |
| 296 | Name: code_info |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 297 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 298 | Argument count: 1 |
| 299 | Kw-only arguments: 0 |
| 300 | Number of locals: 1 |
| 301 | Stack size: 4 |
| 302 | Flags: OPTIMIZED, NEWLOCALS, NOFREE |
| 303 | Constants: |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 304 | 0: %r |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 305 | 1: '__func__' |
| 306 | 2: '__code__' |
| 307 | 3: '<code_info>' |
| 308 | 4: 'co_code' |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 309 | 5: "don't know how to disassemble %%s objects" |
| 310 | %sNames: |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 311 | 0: hasattr |
| 312 | 1: __func__ |
| 313 | 2: __code__ |
| 314 | 3: isinstance |
| 315 | 4: str |
| 316 | 5: _try_compile |
| 317 | 6: _format_code_info |
| 318 | 7: TypeError |
| 319 | 8: type |
| 320 | 9: __name__ |
| 321 | Variable names: |
Georg Brandl | ebbf63b | 2010-10-14 07:23:01 +0000 | [diff] [blame] | 322 | 0: x""" % (('Formatted details of methods, functions, or code.', ' 6: None\n') |
| 323 | if sys.flags.optimize < 2 else (None, '')) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 324 | |
| 325 | @staticmethod |
| 326 | def tricky(x, y, z=True, *args, c, d, e=[], **kwds): |
| 327 | def f(c=c): |
| 328 | print(x, y, z, c, d, e, f) |
| 329 | yield x, y, z, c, d, e, f |
| 330 | |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 331 | code_info_tricky = """\ |
| 332 | Name: tricky |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 333 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 334 | Argument count: 3 |
| 335 | Kw-only arguments: 3 |
| 336 | Number of locals: 8 |
| 337 | Stack size: 7 |
| 338 | Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR |
| 339 | Constants: |
| 340 | 0: None |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 341 | 1: <code object f at (.*), file "(.*)", line (.*)> |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 342 | Variable names: |
| 343 | 0: x |
| 344 | 1: y |
| 345 | 2: z |
| 346 | 3: c |
| 347 | 4: d |
| 348 | 5: e |
| 349 | 6: args |
| 350 | 7: kwds |
| 351 | Cell variables: |
| 352 | 0: e |
| 353 | 1: d |
| 354 | 2: f |
| 355 | 3: y |
| 356 | 4: x |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 357 | 5: z""" |
| 358 | |
| 359 | co_tricky_nested_f = tricky.__func__.__code__.co_consts[1] |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 360 | |
| 361 | code_info_tricky_nested_f = """\ |
| 362 | Name: f |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 363 | Filename: (.*) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 364 | Argument count: 1 |
| 365 | Kw-only arguments: 0 |
| 366 | Number of locals: 1 |
| 367 | Stack size: 8 |
| 368 | Flags: OPTIMIZED, NEWLOCALS, NESTED |
| 369 | Constants: |
| 370 | 0: None |
| 371 | Names: |
| 372 | 0: print |
| 373 | Variable names: |
| 374 | 0: c |
| 375 | Free variables: |
| 376 | 0: e |
| 377 | 1: d |
| 378 | 2: f |
| 379 | 3: y |
| 380 | 4: x |
Nick Coghlan | 46e6380 | 2010-08-17 11:28:07 +0000 | [diff] [blame] | 381 | 5: z""" |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 382 | |
| 383 | code_info_expr_str = """\ |
| 384 | Name: <module> |
| 385 | Filename: <code_info> |
| 386 | Argument count: 0 |
| 387 | Kw-only arguments: 0 |
| 388 | Number of locals: 0 |
| 389 | Stack size: 2 |
| 390 | Flags: NOFREE |
| 391 | Constants: |
| 392 | 0: 1 |
| 393 | Names: |
| 394 | 0: x""" |
| 395 | |
| 396 | code_info_simple_stmt_str = """\ |
| 397 | Name: <module> |
| 398 | Filename: <code_info> |
| 399 | Argument count: 0 |
| 400 | Kw-only arguments: 0 |
| 401 | Number of locals: 0 |
| 402 | Stack size: 2 |
| 403 | Flags: NOFREE |
| 404 | Constants: |
| 405 | 0: 1 |
| 406 | 1: None |
| 407 | Names: |
| 408 | 0: x""" |
| 409 | |
| 410 | code_info_compound_stmt_str = """\ |
| 411 | Name: <module> |
| 412 | Filename: <code_info> |
| 413 | Argument count: 0 |
| 414 | Kw-only arguments: 0 |
| 415 | Number of locals: 0 |
| 416 | Stack size: 2 |
| 417 | Flags: NOFREE |
| 418 | Constants: |
| 419 | 0: 0 |
| 420 | 1: 1 |
| 421 | 2: None |
| 422 | Names: |
| 423 | 0: x""" |
| 424 | |
| 425 | class CodeInfoTests(unittest.TestCase): |
| 426 | test_pairs = [ |
| 427 | (dis.code_info, code_info_code_info), |
| 428 | (tricky, code_info_tricky), |
| 429 | (co_tricky_nested_f, code_info_tricky_nested_f), |
| 430 | (expr_str, code_info_expr_str), |
| 431 | (simple_stmt_str, code_info_simple_stmt_str), |
| 432 | (compound_stmt_str, code_info_compound_stmt_str), |
| 433 | ] |
| 434 | |
| 435 | def test_code_info(self): |
| 436 | self.maxDiff = 1000 |
| 437 | for x, expected in self.test_pairs: |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 438 | self.assertRegex(dis.code_info(x), expected) |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 439 | |
| 440 | def test_show_code(self): |
| 441 | self.maxDiff = 1000 |
| 442 | for x, expected in self.test_pairs: |
| 443 | with captured_stdout() as output: |
| 444 | dis.show_code(x) |
Ezio Melotti | ed3a7d2 | 2010-12-01 02:32:32 +0000 | [diff] [blame] | 445 | self.assertRegex(output.getvalue(), expected+"\n") |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 446 | |
Benjamin Peterson | d6afe72 | 2011-03-15 14:44:52 -0500 | [diff] [blame] | 447 | def test_code_info_object(self): |
| 448 | self.assertRaises(TypeError, dis.code_info, object()) |
| 449 | |
| 450 | def test_pretty_flags_no_flags(self): |
| 451 | self.assertEqual(dis.pretty_flags(0), '0x0') |
| 452 | |
| 453 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 454 | def test_main(): |
Nick Coghlan | eae2da1 | 2010-08-17 08:03:36 +0000 | [diff] [blame] | 455 | run_unittest(DisTests, CodeInfoTests) |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 456 | |
Skip Montanaro | add0ccc | 2003-02-27 21:27:07 +0000 | [diff] [blame] | 457 | if __name__ == "__main__": |
| 458 | test_main() |