blob: ed05f5288b44579119eeea27d702d37afe25487c [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
Nick Coghlanefd5df92014-07-25 23:02:56 +1000232def _g(x):
233 yield x
234
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000235class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500236
237 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000238 # We want to test the default printing behaviour, not the file arg
239 output = io.StringIO()
240 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500241 if wrapper:
242 dis.dis(func)
243 else:
244 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000245 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500246
247 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000248 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500249
Zachary Warebb4b7c12013-12-26 09:55:24 -0600250 def strip_addresses(self, text):
251 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500252
253 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600254 got = self.get_disassembly(func)
255 if got != expected:
256 got = self.strip_addresses(got)
257 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000258
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000259 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500260 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000261 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
262 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000263
264 def test_opname(self):
265 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
266
267 def test_boundaries(self):
268 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
269 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
270
271 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000272 self.do_disassembly_test(_f, dis_f)
273
274 def test_bug_708901(self):
275 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000276
Neal Norwitz51abbc72005-12-18 07:06:23 +0000277 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000278 # This one is checking bytecodes generated for an `assert` statement,
279 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600280 if not __debug__:
281 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600282
Zachary Waree80e8062013-12-26 09:53:49 -0600283 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000284
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285 def test_big_linenos(self):
286 def func(count):
287 namespace = {}
288 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000289 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000290 return namespace['foo']
291
292 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000293 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 expected = _BIG_LINENO_FORMAT % (i + 2)
295 self.do_disassembly_test(func(i), expected)
296
297 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000298 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299 expected = _BIG_LINENO_FORMAT % (i + 2)
300 self.do_disassembly_test(func(i), expected)
301
Guido van Rossume7ba4952007-06-06 23:52:48 +0000302 from test import dis_module
303 self.do_disassembly_test(dis_module, dis_module_expected_results)
304
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000305 def test_disassemble_str(self):
306 self.do_disassembly_test(expr_str, dis_expr_str)
307 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
308 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
309
Benjamin Petersond6afe722011-03-15 14:44:52 -0500310 def test_disassemble_bytes(self):
311 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
312
313 def test_disassemble_method(self):
314 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
315
316 def test_disassemble_method_bytes(self):
317 method_bytecode = _C(1).__init__.__code__.co_code
318 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
319
Nick Coghlanefd5df92014-07-25 23:02:56 +1000320 def test_disassemble_generator(self):
321 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
322 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
323 self.assertEqual(gen_disas, gen_func_disas)
324
Benjamin Petersond6afe722011-03-15 14:44:52 -0500325 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500326 try:
327 del sys.last_traceback
328 except AttributeError:
329 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500330 self.assertRaises(RuntimeError, dis.dis, None)
331
Benjamin Petersond6afe722011-03-15 14:44:52 -0500332 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500333 try:
334 del sys.last_traceback
335 except AttributeError:
336 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500337
338 try:
339 1/0
340 except Exception as e:
341 tb = e.__traceback__
342 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500343
344 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
345 self.do_disassembly_test(None, tb_dis)
346
347 def test_dis_object(self):
348 self.assertRaises(TypeError, dis.dis, object())
349
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000350class DisWithFileTests(DisTests):
351
352 # Run the tests again, using the file arg instead of print
353 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000354 output = io.StringIO()
355 if wrapper:
356 dis.dis(func, file=output)
357 else:
358 dis.disassemble(func, lasti, file=output)
359 return output.getvalue()
360
361
362
Nick Coghlaneae2da12010-08-17 08:03:36 +0000363code_info_code_info = """\
364Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000365Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000366Argument count: 1
367Kw-only arguments: 0
368Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000369Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000370Flags: OPTIMIZED, NEWLOCALS, NOFREE
371Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000372 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000373Names:
374 0: _format_code_info
375 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000376Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000377 0: x""" % (('Formatted details of methods, functions, or code.',)
378 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000379
380@staticmethod
381def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
382 def f(c=c):
383 print(x, y, z, c, d, e, f)
384 yield x, y, z, c, d, e, f
385
Nick Coghlaneae2da12010-08-17 08:03:36 +0000386code_info_tricky = """\
387Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000388Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000389Argument count: 3
390Kw-only arguments: 3
391Number of locals: 8
392Stack size: 7
393Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
394Constants:
395 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000396 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100397 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000398Variable names:
399 0: x
400 1: y
401 2: z
402 3: c
403 4: d
404 5: e
405 6: args
406 7: kwds
407Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100408 0: [edfxyz]
409 1: [edfxyz]
410 2: [edfxyz]
411 3: [edfxyz]
412 4: [edfxyz]
413 5: [edfxyz]"""
414# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000415
416co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000417
418code_info_tricky_nested_f = """\
419Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000420Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000421Argument count: 1
422Kw-only arguments: 0
423Number of locals: 1
424Stack size: 8
425Flags: OPTIMIZED, NEWLOCALS, NESTED
426Constants:
427 0: None
428Names:
429 0: print
430Variable names:
431 0: c
432Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100433 0: [edfxyz]
434 1: [edfxyz]
435 2: [edfxyz]
436 3: [edfxyz]
437 4: [edfxyz]
438 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000439
440code_info_expr_str = """\
441Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000442Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000443Argument count: 0
444Kw-only arguments: 0
445Number of locals: 0
446Stack size: 2
447Flags: NOFREE
448Constants:
449 0: 1
450Names:
451 0: x"""
452
453code_info_simple_stmt_str = """\
454Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000455Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000456Argument count: 0
457Kw-only arguments: 0
458Number of locals: 0
459Stack size: 2
460Flags: NOFREE
461Constants:
462 0: 1
463 1: None
464Names:
465 0: x"""
466
467code_info_compound_stmt_str = """\
468Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000469Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000470Argument count: 0
471Kw-only arguments: 0
472Number of locals: 0
473Stack size: 2
474Flags: NOFREE
475Constants:
476 0: 0
477 1: 1
478 2: None
479Names:
480 0: x"""
481
482class CodeInfoTests(unittest.TestCase):
483 test_pairs = [
484 (dis.code_info, code_info_code_info),
485 (tricky, code_info_tricky),
486 (co_tricky_nested_f, code_info_tricky_nested_f),
487 (expr_str, code_info_expr_str),
488 (simple_stmt_str, code_info_simple_stmt_str),
489 (compound_stmt_str, code_info_compound_stmt_str),
490 ]
491
492 def test_code_info(self):
493 self.maxDiff = 1000
494 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000495 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000496
497 def test_show_code(self):
498 self.maxDiff = 1000
499 for x, expected in self.test_pairs:
500 with captured_stdout() as output:
501 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000502 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000503 output = io.StringIO()
504 dis.show_code(x, file=output)
505 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000506
Benjamin Petersond6afe722011-03-15 14:44:52 -0500507 def test_code_info_object(self):
508 self.assertRaises(TypeError, dis.code_info, object())
509
510 def test_pretty_flags_no_flags(self):
511 self.assertEqual(dis.pretty_flags(0), '0x0')
512
513
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000514# Fodder for instruction introspection tests
515# Editing any of these may require recalculating the expected output
516def outer(a=1, b=2):
517 def f(c=3, d=4):
518 def inner(e=5, f=6):
519 print(a, b, c, d, e, f)
520 print(a, b, c, d)
521 return inner
522 print(a, b, '', 1, [], {}, "Hello world!")
523 return f
524
525def jumpy():
526 # This won't actually run (but that's OK, we only disassemble it)
527 for i in range(10):
528 print(i)
529 if i < 4:
530 continue
531 if i > 6:
532 break
533 else:
534 print("I can haz else clause?")
535 while i:
536 print(i)
537 i -= 1
538 if i > 6:
539 continue
540 if i < 4:
541 break
542 else:
543 print("Who let lolcatz into this test suite?")
544 try:
545 1 / 0
546 except ZeroDivisionError:
547 print("Here we go, here we go, here we go...")
548 else:
549 with i as dodgy:
550 print("Never reach this")
551 finally:
552 print("OK, now we're done")
553
554# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000555expected_outer_line = 1
556_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000557code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000558expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000559code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000560expected_inner_line = code_object_inner.co_firstlineno - _line_offset
561expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000562
563# The following lines are useful to regenerate the expected results after
564# either the fodder is modified or the bytecode generation changes
565# After regeneration, update the references to code_object_f and
566# code_object_inner before rerunning the tests
567
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000568#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000569#print('expected_opinfo_outer = [\n ',
570 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200571#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000572#print('expected_opinfo_f = [\n ',
573 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200574#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000575#print('expected_opinfo_inner = [\n ',
576 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000577#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000578#print('expected_opinfo_jumpy = [\n ',
579 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
580
581
582Instruction = dis.Instruction
583expected_opinfo_outer = [
584 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
585 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
586 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
587 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
588 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
589 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),
590 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),
591 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
592 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
593 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
594 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
595 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
596 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
597 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
598 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
599 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
600 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
601 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),
602 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
603 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
604 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
605]
606
607expected_opinfo_f = [
608 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
609 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
610 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
611 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
612 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
613 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
614 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
615 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),
616 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),
617 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
618 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
619 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
620 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
621 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
622 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
623 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
624 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),
625 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
626 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
627 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
628]
629
630expected_opinfo_inner = [
631 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
632 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
633 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
634 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
635 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
636 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
637 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
638 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),
639 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
640 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
641 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
642]
643
644expected_opinfo_jumpy = [
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200645 Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=71, argrepr='to 71', offset=0, starts_line=3, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000646 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
647 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
648 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),
649 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200650 Instruction(opname='FOR_ITER', opcode=93, arg=44, argval=60, argrepr='to 60', offset=13, starts_line=None, is_jump_target=True),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000651 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
652 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
653 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
654 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),
655 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
656 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
657 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
658 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200659 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=44, argval=44, argrepr='', offset=38, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000660 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200661 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=44, starts_line=7, is_jump_target=True),
662 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=47, starts_line=None, is_jump_target=False),
663 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=50, starts_line=None, is_jump_target=False),
664 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=53, starts_line=None, is_jump_target=False),
665 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=56, starts_line=8, is_jump_target=False),
666 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=57, starts_line=None, is_jump_target=False),
667 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=True),
668 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=61, starts_line=10, is_jump_target=False),
669 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=64, starts_line=None, is_jump_target=False),
670 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=67, starts_line=None, is_jump_target=False),
671 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=70, starts_line=None, is_jump_target=False),
672 Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=142, argrepr='to 142', offset=71, starts_line=11, is_jump_target=True),
673 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=True),
674 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=131, argval=131, argrepr='', offset=77, starts_line=None, is_jump_target=False),
675 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=80, starts_line=12, is_jump_target=False),
676 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=83, starts_line=None, is_jump_target=False),
677 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=86, starts_line=None, is_jump_target=False),
678 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=89, starts_line=None, is_jump_target=False),
679 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=90, starts_line=13, is_jump_target=False),
680 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=93, starts_line=None, is_jump_target=False),
681 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
682 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=97, starts_line=None, is_jump_target=False),
683 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=100, starts_line=14, is_jump_target=False),
684 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=103, starts_line=None, is_jump_target=False),
685 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=106, starts_line=None, is_jump_target=False),
686 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=115, argval=115, argrepr='', offset=109, starts_line=None, is_jump_target=False),
687 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=112, starts_line=15, is_jump_target=False),
688 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=115, starts_line=16, is_jump_target=True),
689 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=118, starts_line=None, is_jump_target=False),
690 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=121, starts_line=None, is_jump_target=False),
691 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=74, argval=74, argrepr='', offset=124, starts_line=None, is_jump_target=False),
692 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=127, starts_line=17, is_jump_target=False),
693 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=128, starts_line=None, is_jump_target=False),
694 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=131, starts_line=None, is_jump_target=True),
695 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=19, is_jump_target=False),
696 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=135, starts_line=None, is_jump_target=False),
697 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=138, starts_line=None, is_jump_target=False),
698 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=141, starts_line=None, is_jump_target=False),
699 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=217, argrepr='to 217', offset=142, starts_line=20, is_jump_target=True),
700 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=160, argrepr='to 160', offset=145, starts_line=None, is_jump_target=False),
701 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=148, starts_line=21, is_jump_target=False),
702 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=151, starts_line=None, is_jump_target=False),
703 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
704 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=155, starts_line=None, is_jump_target=False),
705 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
706 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=188, argrepr='to 188', offset=157, starts_line=None, is_jump_target=False),
707 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=22, is_jump_target=True),
708 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=161, starts_line=None, is_jump_target=False),
709 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=164, starts_line=None, is_jump_target=False),
710 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=187, argval=187, argrepr='', offset=167, starts_line=None, is_jump_target=False),
711 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
712 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=171, starts_line=None, is_jump_target=False),
713 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
714 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=173, starts_line=23, is_jump_target=False),
715 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval='Here we go, here we go, here we go...', argrepr="'Here we go, here we go, here we go...'", offset=176, starts_line=None, is_jump_target=False),
716 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=179, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000717 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200718 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
719 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=213, argrepr='to 213', offset=184, starts_line=None, is_jump_target=False),
720 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=187, starts_line=None, is_jump_target=True),
721 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=188, starts_line=25, is_jump_target=True),
722 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=211, argrepr='to 211', offset=191, starts_line=None, is_jump_target=False),
723 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=194, starts_line=None, is_jump_target=False),
724 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=197, starts_line=26, is_jump_target=False),
725 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=200, starts_line=None, is_jump_target=False),
726 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=203, starts_line=None, is_jump_target=False),
727 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
728 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=207, starts_line=None, is_jump_target=False),
729 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=208, starts_line=None, is_jump_target=False),
730 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=211, starts_line=None, is_jump_target=True),
731 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
732 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=213, starts_line=None, is_jump_target=True),
733 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=214, starts_line=None, is_jump_target=False),
734 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=217, starts_line=28, is_jump_target=True),
735 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=220, starts_line=None, is_jump_target=False),
736 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=223, starts_line=None, is_jump_target=False),
737 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=226, starts_line=None, is_jump_target=False),
738 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=227, starts_line=None, is_jump_target=False),
739 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=228, starts_line=None, is_jump_target=False),
740 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=231, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000741]
742
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000743# One last piece of inspect fodder to check the default line number handling
744def simple(): pass
745expected_opinfo_simple = [
746 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
747 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
748]
749
750
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000751class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000752
753 def test_default_first_line(self):
754 actual = dis.get_instructions(simple)
755 self.assertEqual(list(actual), expected_opinfo_simple)
756
757 def test_first_line_set_to_None(self):
758 actual = dis.get_instructions(simple, first_line=None)
759 self.assertEqual(list(actual), expected_opinfo_simple)
760
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000761 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000762 actual = dis.get_instructions(outer, first_line=expected_outer_line)
763 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000764
765 def test_nested(self):
766 with captured_stdout():
767 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000768 actual = dis.get_instructions(f, first_line=expected_f_line)
769 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000770
771 def test_doubly_nested(self):
772 with captured_stdout():
773 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000774 actual = dis.get_instructions(inner, first_line=expected_inner_line)
775 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000776
777 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000778 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
779 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000780
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000781# get_instructions has its own tests above, so can rely on it to validate
782# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000783class BytecodeTests(unittest.TestCase):
784 def test_instantiation(self):
785 # Test with function, method, code string and code object
786 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000787 with self.subTest(obj=obj):
788 b = dis.Bytecode(obj)
789 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000790
791 self.assertRaises(TypeError, dis.Bytecode, object())
792
793 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000794 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
795 with self.subTest(obj=obj):
796 via_object = list(dis.Bytecode(obj))
797 via_generator = list(dis.get_instructions(obj))
798 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000799
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000800 def test_explicit_first_line(self):
801 actual = dis.Bytecode(outer, first_line=expected_outer_line)
802 self.assertEqual(list(actual), expected_opinfo_outer)
803
804 def test_source_line_in_disassembly(self):
805 # Use the line in the source code
806 actual = dis.Bytecode(simple).dis()[:3]
807 expected = "{:>3}".format(simple.__code__.co_firstlineno)
808 self.assertEqual(actual, expected)
809 # Use an explicit first line number
810 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
811 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000812
813 def test_info(self):
814 self.maxDiff = 1000
815 for x, expected in CodeInfoTests.test_pairs:
816 b = dis.Bytecode(x)
817 self.assertRegex(b.info(), expected)
818
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000819 def test_disassembled(self):
820 actual = dis.Bytecode(_f).dis()
821 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000822
Nick Coghlan50c48b82013-11-23 00:57:00 +1000823 def test_from_traceback(self):
824 tb = get_tb()
825 b = dis.Bytecode.from_traceback(tb)
826 while tb.tb_next: tb = tb.tb_next
827
828 self.assertEqual(b.current_offset, tb.tb_lasti)
829
830 def test_from_traceback_dis(self):
831 tb = get_tb()
832 b = dis.Bytecode.from_traceback(tb)
833 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000834
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000835if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600836 unittest.main()