blob: d1229fba6dbe65355f9e97988aeb2a5f87c9b243 [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Nick Coghlaneae2da12010-08-17 08:03:36 +00003from test.support import run_unittest, captured_stdout
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10004from test.bytecode_helper import BytecodeTestCase
Benjamin Petersond6afe722011-03-15 14:44:52 -05005import difflib
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import unittest
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00007import sys
8import dis
Guido van Rossum34d19282007-08-09 01:03:29 +00009import io
Zachary Waree80e8062013-12-26 09:53:49 -060010import re
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100011import types
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100012import contextlib
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000013
Nick Coghlan50c48b82013-11-23 00:57:00 +100014def get_tb():
15 def _error():
16 try:
17 1 / 0
18 except Exception as e:
19 tb = e.__traceback__
20 return tb
21
22 tb = _error()
23 while tb.tb_next:
24 tb = tb.tb_next
25 return tb
26
27TRACEBACK_CODE = get_tb().tb_frame.f_code
28
Benjamin Petersond6afe722011-03-15 14:44:52 -050029class _C:
30 def __init__(self, x):
31 self.x = x == 1
32
33dis_c_instance_method = """\
34 %-4d 0 LOAD_FAST 1 (x)
35 3 LOAD_CONST 1 (1)
36 6 COMPARE_OP 2 (==)
37 9 LOAD_FAST 0 (self)
38 12 STORE_ATTR 0 (x)
39 15 LOAD_CONST 0 (None)
40 18 RETURN_VALUE
41""" % (_C.__init__.__code__.co_firstlineno + 1,)
42
43dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100044 0 LOAD_FAST 1 (1)
45 3 LOAD_CONST 1 (1)
46 6 COMPARE_OP 2 (==)
47 9 LOAD_FAST 0 (0)
48 12 STORE_ATTR 0 (0)
49 15 LOAD_CONST 0 (0)
Benjamin Petersond6afe722011-03-15 14:44:52 -050050 18 RETURN_VALUE
51"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000052
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000053def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000054 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000055 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000056
57dis_f = """\
Georg Brandl88fc6642007-02-09 21:28:07 +000058 %-4d 0 LOAD_GLOBAL 0 (print)
59 3 LOAD_FAST 0 (a)
Alexander Belopolsky74482202012-06-07 14:28:14 -040060 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Georg Brandl88fc6642007-02-09 21:28:07 +000061 9 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000062
Georg Brandl88fc6642007-02-09 21:28:07 +000063 %-4d 10 LOAD_CONST 1 (1)
64 13 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000065""" % (_f.__code__.co_firstlineno + 1,
66 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +000067
68
Benjamin Petersond6afe722011-03-15 14:44:52 -050069dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100070 0 LOAD_GLOBAL 0 (0)
71 3 LOAD_FAST 0 (0)
72 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Benjamin Petersond6afe722011-03-15 14:44:52 -050073 9 POP_TOP
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100074 10 LOAD_CONST 1 (1)
Benjamin Petersond6afe722011-03-15 14:44:52 -050075 13 RETURN_VALUE
76"""
77
78
Michael W. Hudson26848a32003-04-29 17:07:36 +000079def bug708901():
80 for res in range(1,
81 10):
82 pass
83
84dis_bug708901 = """\
85 %-4d 0 SETUP_LOOP 23 (to 26)
86 3 LOAD_GLOBAL 0 (range)
87 6 LOAD_CONST 1 (1)
88
89 %-4d 9 LOAD_CONST 2 (10)
Alexander Belopolsky74482202012-06-07 14:28:14 -040090 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
Michael W. Hudson26848a32003-04-29 17:07:36 +000091 15 GET_ITER
92 >> 16 FOR_ITER 6 (to 25)
93 19 STORE_FAST 0 (res)
94
95 %-4d 22 JUMP_ABSOLUTE 16
96 >> 25 POP_BLOCK
97 >> 26 LOAD_CONST 0 (None)
98 29 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000099""" % (bug708901.__code__.co_firstlineno + 1,
100 bug708901.__code__.co_firstlineno + 2,
101 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000102
Neal Norwitz51abbc72005-12-18 07:06:23 +0000103
104def bug1333982(x=[]):
105 assert 0, ([s for s in x] +
106 1)
107 pass
108
109dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600110%3d 0 LOAD_CONST 1 (0)
Zachary Waree80e8062013-12-26 09:53:49 -0600111 3 POP_JUMP_IF_TRUE 35
112 6 LOAD_GLOBAL 0 (AssertionError)
113 9 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
114 12 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
115 15 MAKE_FUNCTION 0
116 18 LOAD_FAST 0 (x)
117 21 GET_ITER
118 22 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000119
Zachary Warebb4b7c12013-12-26 09:55:24 -0600120%3d 25 LOAD_CONST 4 (1)
Zachary Waree80e8062013-12-26 09:53:49 -0600121 28 BINARY_ADD
122 29 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
123 32 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000124
Zachary Warebb4b7c12013-12-26 09:55:24 -0600125%3d >> 35 LOAD_CONST 0 (None)
Zachary Waree80e8062013-12-26 09:53:49 -0600126 38 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000127""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600128 __file__,
129 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000130 bug1333982.__code__.co_firstlineno + 2,
131 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000132
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133_BIG_LINENO_FORMAT = """\
134%3d 0 LOAD_GLOBAL 0 (spam)
135 3 POP_TOP
136 4 LOAD_CONST 0 (None)
137 7 RETURN_VALUE
138"""
139
Guido van Rossume7ba4952007-06-06 23:52:48 +0000140dis_module_expected_results = """\
141Disassembly of f:
142 4 0 LOAD_CONST 0 (None)
143 3 RETURN_VALUE
144
145Disassembly of g:
146 5 0 LOAD_CONST 0 (None)
147 3 RETURN_VALUE
148
149"""
150
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000151expr_str = "x + 1"
152
153dis_expr_str = """\
154 1 0 LOAD_NAME 0 (x)
155 3 LOAD_CONST 0 (1)
156 6 BINARY_ADD
157 7 RETURN_VALUE
158"""
159
160simple_stmt_str = "x = x + 1"
161
162dis_simple_stmt_str = """\
163 1 0 LOAD_NAME 0 (x)
164 3 LOAD_CONST 0 (1)
165 6 BINARY_ADD
166 7 STORE_NAME 0 (x)
167 10 LOAD_CONST 1 (None)
168 13 RETURN_VALUE
169"""
170
171compound_stmt_str = """\
172x = 0
173while 1:
174 x += 1"""
175# Trailing newline has been deliberately omitted
176
177dis_compound_stmt_str = """\
178 1 0 LOAD_CONST 0 (0)
179 3 STORE_NAME 0 (x)
180
181 2 6 SETUP_LOOP 13 (to 22)
182
183 3 >> 9 LOAD_NAME 0 (x)
184 12 LOAD_CONST 1 (1)
185 15 INPLACE_ADD
186 16 STORE_NAME 0 (x)
187 19 JUMP_ABSOLUTE 9
188 >> 22 LOAD_CONST 2 (None)
189 25 RETURN_VALUE
190"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000191
Nick Coghlan50c48b82013-11-23 00:57:00 +1000192dis_traceback = """\
193 %-4d 0 SETUP_EXCEPT 12 (to 15)
194
195 %-4d 3 LOAD_CONST 1 (1)
196 6 LOAD_CONST 2 (0)
197 --> 9 BINARY_TRUE_DIVIDE
198 10 POP_TOP
199 11 POP_BLOCK
200 12 JUMP_FORWARD 46 (to 61)
201
202 %-4d >> 15 DUP_TOP
203 16 LOAD_GLOBAL 0 (Exception)
204 19 COMPARE_OP 10 (exception match)
205 22 POP_JUMP_IF_FALSE 60
206 25 POP_TOP
207 26 STORE_FAST 0 (e)
208 29 POP_TOP
209 30 SETUP_FINALLY 14 (to 47)
210
211 %-4d 33 LOAD_FAST 0 (e)
212 36 LOAD_ATTR 1 (__traceback__)
213 39 STORE_FAST 1 (tb)
214 42 POP_BLOCK
215 43 POP_EXCEPT
216 44 LOAD_CONST 0 (None)
217 >> 47 LOAD_CONST 0 (None)
218 50 STORE_FAST 0 (e)
219 53 DELETE_FAST 0 (e)
220 56 END_FINALLY
221 57 JUMP_FORWARD 1 (to 61)
222 >> 60 END_FINALLY
223
224 %-4d >> 61 LOAD_FAST 1 (tb)
225 64 RETURN_VALUE
226""" % (TRACEBACK_CODE.co_firstlineno + 1,
227 TRACEBACK_CODE.co_firstlineno + 2,
228 TRACEBACK_CODE.co_firstlineno + 3,
229 TRACEBACK_CODE.co_firstlineno + 4,
230 TRACEBACK_CODE.co_firstlineno + 5)
231
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000232class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500233
234 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000235 # We want to test the default printing behaviour, not the file arg
236 output = io.StringIO()
237 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500238 if wrapper:
239 dis.dis(func)
240 else:
241 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000242 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500243
244 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000245 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500246
Zachary Warebb4b7c12013-12-26 09:55:24 -0600247 def strip_addresses(self, text):
248 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500249
250 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600251 got = self.get_disassembly(func)
252 if got != expected:
253 got = self.strip_addresses(got)
254 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000255
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000256 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500257 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000258 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
259 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000260
261 def test_opname(self):
262 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
263
264 def test_boundaries(self):
265 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
266 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
267
268 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000269 self.do_disassembly_test(_f, dis_f)
270
271 def test_bug_708901(self):
272 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000273
Neal Norwitz51abbc72005-12-18 07:06:23 +0000274 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000275 # This one is checking bytecodes generated for an `assert` statement,
276 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600277 if not __debug__:
278 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600279
Zachary Waree80e8062013-12-26 09:53:49 -0600280 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000281
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282 def test_big_linenos(self):
283 def func(count):
284 namespace = {}
285 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000286 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 return namespace['foo']
288
289 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000290 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291 expected = _BIG_LINENO_FORMAT % (i + 2)
292 self.do_disassembly_test(func(i), expected)
293
294 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000295 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296 expected = _BIG_LINENO_FORMAT % (i + 2)
297 self.do_disassembly_test(func(i), expected)
298
Guido van Rossume7ba4952007-06-06 23:52:48 +0000299 from test import dis_module
300 self.do_disassembly_test(dis_module, dis_module_expected_results)
301
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000302 def test_disassemble_str(self):
303 self.do_disassembly_test(expr_str, dis_expr_str)
304 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
305 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
306
Benjamin Petersond6afe722011-03-15 14:44:52 -0500307 def test_disassemble_bytes(self):
308 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
309
310 def test_disassemble_method(self):
311 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
312
313 def test_disassemble_method_bytes(self):
314 method_bytecode = _C(1).__init__.__code__.co_code
315 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
316
317 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500318 try:
319 del sys.last_traceback
320 except AttributeError:
321 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500322 self.assertRaises(RuntimeError, dis.dis, None)
323
Benjamin Petersond6afe722011-03-15 14:44:52 -0500324 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500325 try:
326 del sys.last_traceback
327 except AttributeError:
328 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500329
330 try:
331 1/0
332 except Exception as e:
333 tb = e.__traceback__
334 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500335
336 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
337 self.do_disassembly_test(None, tb_dis)
338
339 def test_dis_object(self):
340 self.assertRaises(TypeError, dis.dis, object())
341
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000342class DisWithFileTests(DisTests):
343
344 # Run the tests again, using the file arg instead of print
345 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000346 output = io.StringIO()
347 if wrapper:
348 dis.dis(func, file=output)
349 else:
350 dis.disassemble(func, lasti, file=output)
351 return output.getvalue()
352
353
354
Nick Coghlaneae2da12010-08-17 08:03:36 +0000355code_info_code_info = """\
356Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000357Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000358Argument count: 1
359Kw-only arguments: 0
360Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000361Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000362Flags: OPTIMIZED, NEWLOCALS, NOFREE
363Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000364 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000365Names:
366 0: _format_code_info
367 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000368Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000369 0: x""" % (('Formatted details of methods, functions, or code.',)
370 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000371
372@staticmethod
373def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
374 def f(c=c):
375 print(x, y, z, c, d, e, f)
376 yield x, y, z, c, d, e, f
377
Nick Coghlaneae2da12010-08-17 08:03:36 +0000378code_info_tricky = """\
379Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000380Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000381Argument count: 3
382Kw-only arguments: 3
383Number of locals: 8
384Stack size: 7
385Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
386Constants:
387 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000388 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100389 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000390Variable names:
391 0: x
392 1: y
393 2: z
394 3: c
395 4: d
396 5: e
397 6: args
398 7: kwds
399Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100400 0: [edfxyz]
401 1: [edfxyz]
402 2: [edfxyz]
403 3: [edfxyz]
404 4: [edfxyz]
405 5: [edfxyz]"""
406# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000407
408co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000409
410code_info_tricky_nested_f = """\
411Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000412Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000413Argument count: 1
414Kw-only arguments: 0
415Number of locals: 1
416Stack size: 8
417Flags: OPTIMIZED, NEWLOCALS, NESTED
418Constants:
419 0: None
420Names:
421 0: print
422Variable names:
423 0: c
424Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100425 0: [edfxyz]
426 1: [edfxyz]
427 2: [edfxyz]
428 3: [edfxyz]
429 4: [edfxyz]
430 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000431
432code_info_expr_str = """\
433Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000434Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000435Argument count: 0
436Kw-only arguments: 0
437Number of locals: 0
438Stack size: 2
439Flags: NOFREE
440Constants:
441 0: 1
442Names:
443 0: x"""
444
445code_info_simple_stmt_str = """\
446Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000447Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000448Argument count: 0
449Kw-only arguments: 0
450Number of locals: 0
451Stack size: 2
452Flags: NOFREE
453Constants:
454 0: 1
455 1: None
456Names:
457 0: x"""
458
459code_info_compound_stmt_str = """\
460Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000461Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000462Argument count: 0
463Kw-only arguments: 0
464Number of locals: 0
465Stack size: 2
466Flags: NOFREE
467Constants:
468 0: 0
469 1: 1
470 2: None
471Names:
472 0: x"""
473
474class CodeInfoTests(unittest.TestCase):
475 test_pairs = [
476 (dis.code_info, code_info_code_info),
477 (tricky, code_info_tricky),
478 (co_tricky_nested_f, code_info_tricky_nested_f),
479 (expr_str, code_info_expr_str),
480 (simple_stmt_str, code_info_simple_stmt_str),
481 (compound_stmt_str, code_info_compound_stmt_str),
482 ]
483
484 def test_code_info(self):
485 self.maxDiff = 1000
486 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000487 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000488
489 def test_show_code(self):
490 self.maxDiff = 1000
491 for x, expected in self.test_pairs:
492 with captured_stdout() as output:
493 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000494 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000495 output = io.StringIO()
496 dis.show_code(x, file=output)
497 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000498
Benjamin Petersond6afe722011-03-15 14:44:52 -0500499 def test_code_info_object(self):
500 self.assertRaises(TypeError, dis.code_info, object())
501
502 def test_pretty_flags_no_flags(self):
503 self.assertEqual(dis.pretty_flags(0), '0x0')
504
505
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000506# Fodder for instruction introspection tests
507# Editing any of these may require recalculating the expected output
508def outer(a=1, b=2):
509 def f(c=3, d=4):
510 def inner(e=5, f=6):
511 print(a, b, c, d, e, f)
512 print(a, b, c, d)
513 return inner
514 print(a, b, '', 1, [], {}, "Hello world!")
515 return f
516
517def jumpy():
518 # This won't actually run (but that's OK, we only disassemble it)
519 for i in range(10):
520 print(i)
521 if i < 4:
522 continue
523 if i > 6:
524 break
525 else:
526 print("I can haz else clause?")
527 while i:
528 print(i)
529 i -= 1
530 if i > 6:
531 continue
532 if i < 4:
533 break
534 else:
535 print("Who let lolcatz into this test suite?")
536 try:
537 1 / 0
538 except ZeroDivisionError:
539 print("Here we go, here we go, here we go...")
540 else:
541 with i as dodgy:
542 print("Never reach this")
543 finally:
544 print("OK, now we're done")
545
546# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000547expected_outer_line = 1
548_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000549code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000550expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000551code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000552expected_inner_line = code_object_inner.co_firstlineno - _line_offset
553expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000554
555# The following lines are useful to regenerate the expected results after
556# either the fodder is modified or the bytecode generation changes
557# After regeneration, update the references to code_object_f and
558# code_object_inner before rerunning the tests
559
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000560#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000561#print('expected_opinfo_outer = [\n ',
562 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000563#_instructions = dis.get_instructions(outer(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000564#print('expected_opinfo_f = [\n ',
565 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000566#_instructions = dis.get_instructions(outer()(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000567#print('expected_opinfo_inner = [\n ',
568 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000569#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000570#print('expected_opinfo_jumpy = [\n ',
571 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
572
573
574Instruction = dis.Instruction
575expected_opinfo_outer = [
576 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
577 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
578 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
579 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
580 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
581 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=15, starts_line=None, is_jump_target=False),
582 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=18, starts_line=None, is_jump_target=False),
583 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
584 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
585 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
586 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
587 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
588 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
589 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
590 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
591 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
592 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
593 Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='7 positional, 0 keyword pair', offset=51, starts_line=None, is_jump_target=False),
594 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
595 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
596 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
597]
598
599expected_opinfo_f = [
600 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
601 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
602 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
603 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
604 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
605 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
606 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
607 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=21, starts_line=None, is_jump_target=False),
608 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=24, starts_line=None, is_jump_target=False),
609 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
610 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
611 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
612 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
613 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
614 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
615 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
616 Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='4 positional, 0 keyword pair', offset=48, starts_line=None, is_jump_target=False),
617 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
618 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
619 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
620]
621
622expected_opinfo_inner = [
623 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
624 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
625 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
626 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
627 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
628 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
629 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
630 Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='6 positional, 0 keyword pair', offset=21, starts_line=None, is_jump_target=False),
631 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
632 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
633 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
634]
635
636expected_opinfo_jumpy = [
637 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
638 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
639 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
640 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=9, starts_line=None, is_jump_target=False),
641 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
642 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
643 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
644 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
645 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
646 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=25, starts_line=None, is_jump_target=False),
647 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
648 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
649 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
650 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
651 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
652 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
653 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
654 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
655 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
656 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
657 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
658 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
659 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
660 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
661 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
662 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
663 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=70, starts_line=None, is_jump_target=False),
664 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=73, starts_line=None, is_jump_target=False),
665 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
666 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
667 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
668 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
669 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
670 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
671 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=92, starts_line=None, is_jump_target=False),
672 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
673 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
674 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
675 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
676 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
677 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
678 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
679 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
680 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
681 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
682 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
683 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
684 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
685 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
686 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
687 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
688 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
689 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
690 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
691 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
692 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=147, starts_line=None, is_jump_target=False),
693 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=150, starts_line=None, is_jump_target=False),
694 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
695 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
696 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
697 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
698 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
699 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
700 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
701 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
702 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
703 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
704 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
705 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
706 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
707 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
708 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
709 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
710 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
711 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=188, starts_line=None, is_jump_target=False),
712 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=191, starts_line=None, is_jump_target=False),
713 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
714 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
715 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
716 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
717 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
718 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
719 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
720 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
721 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=212, starts_line=None, is_jump_target=False),
722 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=215, starts_line=None, is_jump_target=False),
723 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
724 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
725 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
726 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
727 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
728 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
729 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
730 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
731 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=232, starts_line=None, is_jump_target=False),
732 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=235, starts_line=None, is_jump_target=False),
733 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
734 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
735 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
736 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
737]
738
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000739# One last piece of inspect fodder to check the default line number handling
740def simple(): pass
741expected_opinfo_simple = [
742 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
743 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
744]
745
746
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000747class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000748
749 def test_default_first_line(self):
750 actual = dis.get_instructions(simple)
751 self.assertEqual(list(actual), expected_opinfo_simple)
752
753 def test_first_line_set_to_None(self):
754 actual = dis.get_instructions(simple, first_line=None)
755 self.assertEqual(list(actual), expected_opinfo_simple)
756
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000757 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000758 actual = dis.get_instructions(outer, first_line=expected_outer_line)
759 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000760
761 def test_nested(self):
762 with captured_stdout():
763 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000764 actual = dis.get_instructions(f, first_line=expected_f_line)
765 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000766
767 def test_doubly_nested(self):
768 with captured_stdout():
769 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000770 actual = dis.get_instructions(inner, first_line=expected_inner_line)
771 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000772
773 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000774 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
775 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000776
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000777# get_instructions has its own tests above, so can rely on it to validate
778# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000779class BytecodeTests(unittest.TestCase):
780 def test_instantiation(self):
781 # Test with function, method, code string and code object
782 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000783 with self.subTest(obj=obj):
784 b = dis.Bytecode(obj)
785 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000786
787 self.assertRaises(TypeError, dis.Bytecode, object())
788
789 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000790 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
791 with self.subTest(obj=obj):
792 via_object = list(dis.Bytecode(obj))
793 via_generator = list(dis.get_instructions(obj))
794 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000795
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000796 def test_explicit_first_line(self):
797 actual = dis.Bytecode(outer, first_line=expected_outer_line)
798 self.assertEqual(list(actual), expected_opinfo_outer)
799
800 def test_source_line_in_disassembly(self):
801 # Use the line in the source code
802 actual = dis.Bytecode(simple).dis()[:3]
803 expected = "{:>3}".format(simple.__code__.co_firstlineno)
804 self.assertEqual(actual, expected)
805 # Use an explicit first line number
806 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
807 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000808
809 def test_info(self):
810 self.maxDiff = 1000
811 for x, expected in CodeInfoTests.test_pairs:
812 b = dis.Bytecode(x)
813 self.assertRegex(b.info(), expected)
814
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000815 def test_disassembled(self):
816 actual = dis.Bytecode(_f).dis()
817 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000818
Nick Coghlan50c48b82013-11-23 00:57:00 +1000819 def test_from_traceback(self):
820 tb = get_tb()
821 b = dis.Bytecode.from_traceback(tb)
822 while tb.tb_next: tb = tb.tb_next
823
824 self.assertEqual(b.current_offset, tb.tb_lasti)
825
826 def test_from_traceback_dis(self):
827 tb = get_tb()
828 b = dis.Bytecode.from_traceback(tb)
829 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000830
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000831if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600832 unittest.main()