blob: b8daff725ccb541c87afc77dd0245d8773a2ef06 [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
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -0500181 2 6 SETUP_LOOP 14 (to 23)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000182
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
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -0500188 22 POP_BLOCK
189 >> 23 LOAD_CONST 2 (None)
190 26 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000191"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000192
Nick Coghlan50c48b82013-11-23 00:57:00 +1000193dis_traceback = """\
194 %-4d 0 SETUP_EXCEPT 12 (to 15)
195
196 %-4d 3 LOAD_CONST 1 (1)
197 6 LOAD_CONST 2 (0)
198 --> 9 BINARY_TRUE_DIVIDE
199 10 POP_TOP
200 11 POP_BLOCK
201 12 JUMP_FORWARD 46 (to 61)
202
203 %-4d >> 15 DUP_TOP
204 16 LOAD_GLOBAL 0 (Exception)
205 19 COMPARE_OP 10 (exception match)
206 22 POP_JUMP_IF_FALSE 60
207 25 POP_TOP
208 26 STORE_FAST 0 (e)
209 29 POP_TOP
210 30 SETUP_FINALLY 14 (to 47)
211
212 %-4d 33 LOAD_FAST 0 (e)
213 36 LOAD_ATTR 1 (__traceback__)
214 39 STORE_FAST 1 (tb)
215 42 POP_BLOCK
216 43 POP_EXCEPT
217 44 LOAD_CONST 0 (None)
218 >> 47 LOAD_CONST 0 (None)
219 50 STORE_FAST 0 (e)
220 53 DELETE_FAST 0 (e)
221 56 END_FINALLY
222 57 JUMP_FORWARD 1 (to 61)
223 >> 60 END_FINALLY
224
225 %-4d >> 61 LOAD_FAST 1 (tb)
226 64 RETURN_VALUE
227""" % (TRACEBACK_CODE.co_firstlineno + 1,
228 TRACEBACK_CODE.co_firstlineno + 2,
229 TRACEBACK_CODE.co_firstlineno + 3,
230 TRACEBACK_CODE.co_firstlineno + 4,
231 TRACEBACK_CODE.co_firstlineno + 5)
232
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000233class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500234
235 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000236 # We want to test the default printing behaviour, not the file arg
237 output = io.StringIO()
238 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500239 if wrapper:
240 dis.dis(func)
241 else:
242 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000243 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500244
245 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000246 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500247
Zachary Warebb4b7c12013-12-26 09:55:24 -0600248 def strip_addresses(self, text):
249 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500250
251 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600252 got = self.get_disassembly(func)
253 if got != expected:
254 got = self.strip_addresses(got)
255 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000256
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000257 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500258 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000259 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
260 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000261
262 def test_opname(self):
263 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
264
265 def test_boundaries(self):
266 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
267 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
268
269 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000270 self.do_disassembly_test(_f, dis_f)
271
272 def test_bug_708901(self):
273 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000274
Neal Norwitz51abbc72005-12-18 07:06:23 +0000275 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000276 # This one is checking bytecodes generated for an `assert` statement,
277 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600278 if not __debug__:
279 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600280
Zachary Waree80e8062013-12-26 09:53:49 -0600281 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000282
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283 def test_big_linenos(self):
284 def func(count):
285 namespace = {}
286 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000287 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288 return namespace['foo']
289
290 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000291 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292 expected = _BIG_LINENO_FORMAT % (i + 2)
293 self.do_disassembly_test(func(i), expected)
294
295 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000296 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000297 expected = _BIG_LINENO_FORMAT % (i + 2)
298 self.do_disassembly_test(func(i), expected)
299
Guido van Rossume7ba4952007-06-06 23:52:48 +0000300 from test import dis_module
301 self.do_disassembly_test(dis_module, dis_module_expected_results)
302
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000303 def test_disassemble_str(self):
304 self.do_disassembly_test(expr_str, dis_expr_str)
305 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
306 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
307
Benjamin Petersond6afe722011-03-15 14:44:52 -0500308 def test_disassemble_bytes(self):
309 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
310
311 def test_disassemble_method(self):
312 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
313
314 def test_disassemble_method_bytes(self):
315 method_bytecode = _C(1).__init__.__code__.co_code
316 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
317
318 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500319 try:
320 del sys.last_traceback
321 except AttributeError:
322 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500323 self.assertRaises(RuntimeError, dis.dis, None)
324
Benjamin Petersond6afe722011-03-15 14:44:52 -0500325 def test_dis_traceback(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
331 try:
332 1/0
333 except Exception as e:
334 tb = e.__traceback__
335 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500336
337 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
338 self.do_disassembly_test(None, tb_dis)
339
340 def test_dis_object(self):
341 self.assertRaises(TypeError, dis.dis, object())
342
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000343class DisWithFileTests(DisTests):
344
345 # Run the tests again, using the file arg instead of print
346 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000347 output = io.StringIO()
348 if wrapper:
349 dis.dis(func, file=output)
350 else:
351 dis.disassemble(func, lasti, file=output)
352 return output.getvalue()
353
354
355
Nick Coghlaneae2da12010-08-17 08:03:36 +0000356code_info_code_info = """\
357Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000358Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000359Argument count: 1
360Kw-only arguments: 0
361Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000362Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000363Flags: OPTIMIZED, NEWLOCALS, NOFREE
364Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000365 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000366Names:
367 0: _format_code_info
368 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000369Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000370 0: x""" % (('Formatted details of methods, functions, or code.',)
371 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000372
373@staticmethod
374def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
375 def f(c=c):
376 print(x, y, z, c, d, e, f)
377 yield x, y, z, c, d, e, f
378
Nick Coghlaneae2da12010-08-17 08:03:36 +0000379code_info_tricky = """\
380Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000381Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000382Argument count: 3
383Kw-only arguments: 3
384Number of locals: 8
385Stack size: 7
386Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
387Constants:
388 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000389 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100390 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000391Variable names:
392 0: x
393 1: y
394 2: z
395 3: c
396 4: d
397 5: e
398 6: args
399 7: kwds
400Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100401 0: [edfxyz]
402 1: [edfxyz]
403 2: [edfxyz]
404 3: [edfxyz]
405 4: [edfxyz]
406 5: [edfxyz]"""
407# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000408
409co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000410
411code_info_tricky_nested_f = """\
412Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000413Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000414Argument count: 1
415Kw-only arguments: 0
416Number of locals: 1
417Stack size: 8
418Flags: OPTIMIZED, NEWLOCALS, NESTED
419Constants:
420 0: None
421Names:
422 0: print
423Variable names:
424 0: c
425Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100426 0: [edfxyz]
427 1: [edfxyz]
428 2: [edfxyz]
429 3: [edfxyz]
430 4: [edfxyz]
431 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000432
433code_info_expr_str = """\
434Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000435Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000436Argument count: 0
437Kw-only arguments: 0
438Number of locals: 0
439Stack size: 2
440Flags: NOFREE
441Constants:
442 0: 1
443Names:
444 0: x"""
445
446code_info_simple_stmt_str = """\
447Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000448Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000449Argument count: 0
450Kw-only arguments: 0
451Number of locals: 0
452Stack size: 2
453Flags: NOFREE
454Constants:
455 0: 1
456 1: None
457Names:
458 0: x"""
459
460code_info_compound_stmt_str = """\
461Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000462Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000463Argument count: 0
464Kw-only arguments: 0
465Number of locals: 0
466Stack size: 2
467Flags: NOFREE
468Constants:
469 0: 0
470 1: 1
471 2: None
472Names:
473 0: x"""
474
475class CodeInfoTests(unittest.TestCase):
476 test_pairs = [
477 (dis.code_info, code_info_code_info),
478 (tricky, code_info_tricky),
479 (co_tricky_nested_f, code_info_tricky_nested_f),
480 (expr_str, code_info_expr_str),
481 (simple_stmt_str, code_info_simple_stmt_str),
482 (compound_stmt_str, code_info_compound_stmt_str),
483 ]
484
485 def test_code_info(self):
486 self.maxDiff = 1000
487 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000488 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000489
490 def test_show_code(self):
491 self.maxDiff = 1000
492 for x, expected in self.test_pairs:
493 with captured_stdout() as output:
494 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000495 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000496 output = io.StringIO()
497 dis.show_code(x, file=output)
498 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000499
Benjamin Petersond6afe722011-03-15 14:44:52 -0500500 def test_code_info_object(self):
501 self.assertRaises(TypeError, dis.code_info, object())
502
503 def test_pretty_flags_no_flags(self):
504 self.assertEqual(dis.pretty_flags(0), '0x0')
505
506
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000507# Fodder for instruction introspection tests
508# Editing any of these may require recalculating the expected output
509def outer(a=1, b=2):
510 def f(c=3, d=4):
511 def inner(e=5, f=6):
512 print(a, b, c, d, e, f)
513 print(a, b, c, d)
514 return inner
515 print(a, b, '', 1, [], {}, "Hello world!")
516 return f
517
518def jumpy():
519 # This won't actually run (but that's OK, we only disassemble it)
520 for i in range(10):
521 print(i)
522 if i < 4:
523 continue
524 if i > 6:
525 break
526 else:
527 print("I can haz else clause?")
528 while i:
529 print(i)
530 i -= 1
531 if i > 6:
532 continue
533 if i < 4:
534 break
535 else:
536 print("Who let lolcatz into this test suite?")
537 try:
538 1 / 0
539 except ZeroDivisionError:
540 print("Here we go, here we go, here we go...")
541 else:
542 with i as dodgy:
543 print("Never reach this")
544 finally:
545 print("OK, now we're done")
546
547# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000548expected_outer_line = 1
549_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000550code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000551expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000552code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000553expected_inner_line = code_object_inner.co_firstlineno - _line_offset
554expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000555
556# The following lines are useful to regenerate the expected results after
557# either the fodder is modified or the bytecode generation changes
558# After regeneration, update the references to code_object_f and
559# code_object_inner before rerunning the tests
560
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000561#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000562#print('expected_opinfo_outer = [\n ',
563 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000564#_instructions = dis.get_instructions(outer(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000565#print('expected_opinfo_f = [\n ',
566 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000567#_instructions = dis.get_instructions(outer()(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000568#print('expected_opinfo_inner = [\n ',
569 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000570#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000571#print('expected_opinfo_jumpy = [\n ',
572 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
573
574
575Instruction = dis.Instruction
576expected_opinfo_outer = [
577 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
578 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
579 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
580 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
581 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
582 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),
583 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),
584 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
585 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
586 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
587 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
588 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
589 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
590 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
591 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
592 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
593 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
594 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),
595 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
596 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
597 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
598]
599
600expected_opinfo_f = [
601 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
602 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
603 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
604 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
605 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
606 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
607 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
608 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),
609 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),
610 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
611 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
612 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
613 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
614 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
615 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
616 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
617 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),
618 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
619 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
620 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
621]
622
623expected_opinfo_inner = [
624 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
625 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
626 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
627 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
628 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
629 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
630 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
631 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),
632 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
633 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
634 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
635]
636
637expected_opinfo_jumpy = [
638 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
639 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
640 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
641 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),
642 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
643 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
644 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
645 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
646 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
647 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),
648 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
649 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
650 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
651 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
652 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
653 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
654 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
655 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
656 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
657 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
658 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
659 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
660 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
661 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
662 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
663 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
664 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),
665 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),
666 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
667 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
668 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
669 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
670 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
671 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
672 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),
673 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
674 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
675 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
676 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
677 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
678 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
679 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
680 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
681 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
682 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
683 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
684 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
685 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
686 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
687 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
688 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
689 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
690 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
691 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
692 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
693 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),
694 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),
695 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
696 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
697 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
698 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
699 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
700 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
701 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
702 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
703 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
704 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
705 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
706 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
707 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
708 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
709 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
710 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
711 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
712 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),
713 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),
714 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
715 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
716 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
717 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
718 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
719 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
720 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
721 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
722 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),
723 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),
724 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
725 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
726 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
727 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
728 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
729 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
730 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
731 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
732 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),
733 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),
734 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
735 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
736 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
737 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
738]
739
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000740# One last piece of inspect fodder to check the default line number handling
741def simple(): pass
742expected_opinfo_simple = [
743 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
744 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
745]
746
747
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000748class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000749
750 def test_default_first_line(self):
751 actual = dis.get_instructions(simple)
752 self.assertEqual(list(actual), expected_opinfo_simple)
753
754 def test_first_line_set_to_None(self):
755 actual = dis.get_instructions(simple, first_line=None)
756 self.assertEqual(list(actual), expected_opinfo_simple)
757
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000758 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000759 actual = dis.get_instructions(outer, first_line=expected_outer_line)
760 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000761
762 def test_nested(self):
763 with captured_stdout():
764 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000765 actual = dis.get_instructions(f, first_line=expected_f_line)
766 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000767
768 def test_doubly_nested(self):
769 with captured_stdout():
770 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000771 actual = dis.get_instructions(inner, first_line=expected_inner_line)
772 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000773
774 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000775 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
776 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000777
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000778# get_instructions has its own tests above, so can rely on it to validate
779# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000780class BytecodeTests(unittest.TestCase):
781 def test_instantiation(self):
782 # Test with function, method, code string and code object
783 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000784 with self.subTest(obj=obj):
785 b = dis.Bytecode(obj)
786 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000787
788 self.assertRaises(TypeError, dis.Bytecode, object())
789
790 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000791 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
792 with self.subTest(obj=obj):
793 via_object = list(dis.Bytecode(obj))
794 via_generator = list(dis.get_instructions(obj))
795 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000796
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000797 def test_explicit_first_line(self):
798 actual = dis.Bytecode(outer, first_line=expected_outer_line)
799 self.assertEqual(list(actual), expected_opinfo_outer)
800
801 def test_source_line_in_disassembly(self):
802 # Use the line in the source code
803 actual = dis.Bytecode(simple).dis()[:3]
804 expected = "{:>3}".format(simple.__code__.co_firstlineno)
805 self.assertEqual(actual, expected)
806 # Use an explicit first line number
807 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
808 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000809
810 def test_info(self):
811 self.maxDiff = 1000
812 for x, expected in CodeInfoTests.test_pairs:
813 b = dis.Bytecode(x)
814 self.assertRegex(b.info(), expected)
815
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000816 def test_disassembled(self):
817 actual = dis.Bytecode(_f).dis()
818 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000819
Nick Coghlan50c48b82013-11-23 00:57:00 +1000820 def test_from_traceback(self):
821 tb = get_tb()
822 b = dis.Bytecode.from_traceback(tb)
823 while tb.tb_next: tb = tb.tb_next
824
825 self.assertEqual(b.current_offset, tb.tb_lasti)
826
827 def test_from_traceback_dis(self):
828 tb = get_tb()
829 b = dis.Bytecode.from_traceback(tb)
830 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000831
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000832if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600833 unittest.main()