blob: 421bbad6c60471f23bb5d6602a469927723048e9 [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Zachary Ware38c707e2015-04-13 15:00:43 -05003from test.support import 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
Nick Coghlanefd5df92014-07-25 23:02:56 +1000233def _g(x):
234 yield x
235
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000236class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500237
238 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000239 # We want to test the default printing behaviour, not the file arg
240 output = io.StringIO()
241 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500242 if wrapper:
243 dis.dis(func)
244 else:
245 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000246 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500247
248 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000249 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500250
Zachary Warebb4b7c12013-12-26 09:55:24 -0600251 def strip_addresses(self, text):
252 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500253
254 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600255 got = self.get_disassembly(func)
256 if got != expected:
257 got = self.strip_addresses(got)
258 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000259
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000260 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500261 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000262 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
263 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000264
265 def test_opname(self):
266 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
267
268 def test_boundaries(self):
269 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
270 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
271
272 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000273 self.do_disassembly_test(_f, dis_f)
274
275 def test_bug_708901(self):
276 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000277
Neal Norwitz51abbc72005-12-18 07:06:23 +0000278 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000279 # This one is checking bytecodes generated for an `assert` statement,
280 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600281 if not __debug__:
282 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600283
Zachary Waree80e8062013-12-26 09:53:49 -0600284 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000285
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286 def test_big_linenos(self):
287 def func(count):
288 namespace = {}
289 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000290 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291 return namespace['foo']
292
293 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000294 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000295 expected = _BIG_LINENO_FORMAT % (i + 2)
296 self.do_disassembly_test(func(i), expected)
297
298 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000299 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000300 expected = _BIG_LINENO_FORMAT % (i + 2)
301 self.do_disassembly_test(func(i), expected)
302
Guido van Rossume7ba4952007-06-06 23:52:48 +0000303 from test import dis_module
304 self.do_disassembly_test(dis_module, dis_module_expected_results)
305
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000306 def test_disassemble_str(self):
307 self.do_disassembly_test(expr_str, dis_expr_str)
308 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
309 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
310
Benjamin Petersond6afe722011-03-15 14:44:52 -0500311 def test_disassemble_bytes(self):
312 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
313
314 def test_disassemble_method(self):
315 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
316
317 def test_disassemble_method_bytes(self):
318 method_bytecode = _C(1).__init__.__code__.co_code
319 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
320
Nick Coghlanefd5df92014-07-25 23:02:56 +1000321 def test_disassemble_generator(self):
322 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
323 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
324 self.assertEqual(gen_disas, gen_func_disas)
325
Benjamin Petersond6afe722011-03-15 14:44:52 -0500326 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500327 try:
328 del sys.last_traceback
329 except AttributeError:
330 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500331 self.assertRaises(RuntimeError, dis.dis, None)
332
Benjamin Petersond6afe722011-03-15 14:44:52 -0500333 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500334 try:
335 del sys.last_traceback
336 except AttributeError:
337 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500338
339 try:
340 1/0
341 except Exception as e:
342 tb = e.__traceback__
343 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500344
345 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
346 self.do_disassembly_test(None, tb_dis)
347
348 def test_dis_object(self):
349 self.assertRaises(TypeError, dis.dis, object())
350
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000351class DisWithFileTests(DisTests):
352
353 # Run the tests again, using the file arg instead of print
354 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000355 output = io.StringIO()
356 if wrapper:
357 dis.dis(func, file=output)
358 else:
359 dis.disassemble(func, lasti, file=output)
360 return output.getvalue()
361
362
363
Nick Coghlaneae2da12010-08-17 08:03:36 +0000364code_info_code_info = """\
365Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000366Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000367Argument count: 1
368Kw-only arguments: 0
369Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000370Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000371Flags: OPTIMIZED, NEWLOCALS, NOFREE
372Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000373 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000374Names:
375 0: _format_code_info
376 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000377Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000378 0: x""" % (('Formatted details of methods, functions, or code.',)
379 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000380
381@staticmethod
382def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
383 def f(c=c):
384 print(x, y, z, c, d, e, f)
385 yield x, y, z, c, d, e, f
386
Nick Coghlaneae2da12010-08-17 08:03:36 +0000387code_info_tricky = """\
388Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000389Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000390Argument count: 3
391Kw-only arguments: 3
392Number of locals: 8
393Stack size: 7
394Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
395Constants:
396 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000397 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100398 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000399Variable names:
400 0: x
401 1: y
402 2: z
403 3: c
404 4: d
405 5: e
406 6: args
407 7: kwds
408Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100409 0: [edfxyz]
410 1: [edfxyz]
411 2: [edfxyz]
412 3: [edfxyz]
413 4: [edfxyz]
414 5: [edfxyz]"""
415# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000416
417co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000418
419code_info_tricky_nested_f = """\
420Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000421Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000422Argument count: 1
423Kw-only arguments: 0
424Number of locals: 1
425Stack size: 8
426Flags: OPTIMIZED, NEWLOCALS, NESTED
427Constants:
428 0: None
429Names:
430 0: print
431Variable names:
432 0: c
433Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100434 0: [edfxyz]
435 1: [edfxyz]
436 2: [edfxyz]
437 3: [edfxyz]
438 4: [edfxyz]
439 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000440
441code_info_expr_str = """\
442Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000443Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000444Argument count: 0
445Kw-only arguments: 0
446Number of locals: 0
447Stack size: 2
448Flags: NOFREE
449Constants:
450 0: 1
451Names:
452 0: x"""
453
454code_info_simple_stmt_str = """\
455Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000456Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000457Argument count: 0
458Kw-only arguments: 0
459Number of locals: 0
460Stack size: 2
461Flags: NOFREE
462Constants:
463 0: 1
464 1: None
465Names:
466 0: x"""
467
468code_info_compound_stmt_str = """\
469Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000470Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000471Argument count: 0
472Kw-only arguments: 0
473Number of locals: 0
474Stack size: 2
475Flags: NOFREE
476Constants:
477 0: 0
478 1: 1
479 2: None
480Names:
481 0: x"""
482
Yury Selivanov75445082015-05-11 22:57:16 -0400483
484async def async_def():
485 await 1
486 async for a in b: pass
487 async with c as d: pass
488
489code_info_async_def = """\
490Name: async_def
491Filename: (.*)
492Argument count: 0
493Kw-only arguments: 0
494Number of locals: 2
495Stack size: 17
496Flags: OPTIMIZED, NEWLOCALS, GENERATOR, NOFREE, COROUTINE
497Constants:
498 0: None
499 1: 1"""
500
Nick Coghlaneae2da12010-08-17 08:03:36 +0000501class CodeInfoTests(unittest.TestCase):
502 test_pairs = [
503 (dis.code_info, code_info_code_info),
504 (tricky, code_info_tricky),
505 (co_tricky_nested_f, code_info_tricky_nested_f),
506 (expr_str, code_info_expr_str),
507 (simple_stmt_str, code_info_simple_stmt_str),
508 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400509 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000510 ]
511
512 def test_code_info(self):
513 self.maxDiff = 1000
514 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000515 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000516
517 def test_show_code(self):
518 self.maxDiff = 1000
519 for x, expected in self.test_pairs:
520 with captured_stdout() as output:
521 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000522 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000523 output = io.StringIO()
524 dis.show_code(x, file=output)
525 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000526
Benjamin Petersond6afe722011-03-15 14:44:52 -0500527 def test_code_info_object(self):
528 self.assertRaises(TypeError, dis.code_info, object())
529
530 def test_pretty_flags_no_flags(self):
531 self.assertEqual(dis.pretty_flags(0), '0x0')
532
533
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000534# Fodder for instruction introspection tests
535# Editing any of these may require recalculating the expected output
536def outer(a=1, b=2):
537 def f(c=3, d=4):
538 def inner(e=5, f=6):
539 print(a, b, c, d, e, f)
540 print(a, b, c, d)
541 return inner
542 print(a, b, '', 1, [], {}, "Hello world!")
543 return f
544
545def jumpy():
546 # This won't actually run (but that's OK, we only disassemble it)
547 for i in range(10):
548 print(i)
549 if i < 4:
550 continue
551 if i > 6:
552 break
553 else:
554 print("I can haz else clause?")
555 while i:
556 print(i)
557 i -= 1
558 if i > 6:
559 continue
560 if i < 4:
561 break
562 else:
563 print("Who let lolcatz into this test suite?")
564 try:
565 1 / 0
566 except ZeroDivisionError:
567 print("Here we go, here we go, here we go...")
568 else:
569 with i as dodgy:
570 print("Never reach this")
571 finally:
572 print("OK, now we're done")
573
574# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000575expected_outer_line = 1
576_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000577code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000578expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000579code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000580expected_inner_line = code_object_inner.co_firstlineno - _line_offset
581expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000582
583# The following lines are useful to regenerate the expected results after
584# either the fodder is modified or the bytecode generation changes
585# After regeneration, update the references to code_object_f and
586# code_object_inner before rerunning the tests
587
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000588#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000589#print('expected_opinfo_outer = [\n ',
590 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200591#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000592#print('expected_opinfo_f = [\n ',
593 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200594#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000595#print('expected_opinfo_inner = [\n ',
596 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000597#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000598#print('expected_opinfo_jumpy = [\n ',
599 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
600
601
602Instruction = dis.Instruction
603expected_opinfo_outer = [
604 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
605 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
606 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
607 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
608 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
609 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),
610 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),
611 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
612 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
613 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
614 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
615 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
616 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
617 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
618 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
619 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
620 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
621 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),
622 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
623 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
624 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
625]
626
627expected_opinfo_f = [
628 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
629 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
630 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
631 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
632 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
633 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
634 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
635 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),
636 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),
637 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
638 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
639 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
640 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
641 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
642 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
643 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
644 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),
645 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
646 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
647 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
648]
649
650expected_opinfo_inner = [
651 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
652 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
653 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
654 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
655 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
656 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
657 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
658 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),
659 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
660 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
661 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
662]
663
664expected_opinfo_jumpy = [
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200665 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 +1000666 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
667 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
668 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),
669 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 +0200670 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 +1000671 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
672 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
673 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
674 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),
675 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
676 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
677 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
678 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 +0200679 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 +1000680 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 +0200681 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=44, starts_line=7, is_jump_target=True),
682 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=47, starts_line=None, is_jump_target=False),
683 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=50, starts_line=None, is_jump_target=False),
684 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=53, starts_line=None, is_jump_target=False),
685 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=56, starts_line=8, is_jump_target=False),
686 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=57, starts_line=None, is_jump_target=False),
687 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=True),
688 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=61, starts_line=10, is_jump_target=False),
689 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),
690 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),
691 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=70, starts_line=None, is_jump_target=False),
692 Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=142, argrepr='to 142', offset=71, starts_line=11, is_jump_target=True),
693 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=True),
694 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=131, argval=131, argrepr='', offset=77, starts_line=None, is_jump_target=False),
695 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=80, starts_line=12, is_jump_target=False),
696 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=83, 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=86, starts_line=None, is_jump_target=False),
698 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=89, starts_line=None, is_jump_target=False),
699 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=90, starts_line=13, is_jump_target=False),
700 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=93, starts_line=None, is_jump_target=False),
701 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
702 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=97, starts_line=None, is_jump_target=False),
703 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=100, starts_line=14, is_jump_target=False),
704 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=103, starts_line=None, is_jump_target=False),
705 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=106, starts_line=None, is_jump_target=False),
706 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=115, argval=115, argrepr='', offset=109, starts_line=None, is_jump_target=False),
707 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=112, starts_line=15, is_jump_target=False),
708 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=115, starts_line=16, is_jump_target=True),
709 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=118, starts_line=None, is_jump_target=False),
710 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=121, starts_line=None, is_jump_target=False),
711 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=74, argval=74, argrepr='', offset=124, starts_line=None, is_jump_target=False),
712 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=127, starts_line=17, is_jump_target=False),
713 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=128, starts_line=None, is_jump_target=False),
714 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=131, starts_line=None, is_jump_target=True),
715 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=19, is_jump_target=False),
716 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),
717 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),
718 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=141, starts_line=None, is_jump_target=False),
Yury Selivanov75445082015-05-11 22:57:16 -0400719 Instruction(opname='SETUP_FINALLY', opcode=122, arg=73, argval=218, argrepr='to 218', offset=142, starts_line=20, is_jump_target=True),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200720 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=160, argrepr='to 160', offset=145, starts_line=None, is_jump_target=False),
721 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=148, starts_line=21, is_jump_target=False),
722 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=151, starts_line=None, is_jump_target=False),
723 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
724 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=155, starts_line=None, is_jump_target=False),
725 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
726 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=188, argrepr='to 188', offset=157, starts_line=None, is_jump_target=False),
727 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=22, is_jump_target=True),
728 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=161, starts_line=None, is_jump_target=False),
729 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=164, starts_line=None, is_jump_target=False),
730 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=187, argval=187, argrepr='', offset=167, starts_line=None, is_jump_target=False),
731 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
732 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=171, starts_line=None, is_jump_target=False),
733 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
734 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=173, starts_line=23, is_jump_target=False),
735 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),
736 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 +1000737 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 +0200738 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
Yury Selivanov75445082015-05-11 22:57:16 -0400739 Instruction(opname='JUMP_FORWARD', opcode=110, arg=27, argval=214, argrepr='to 214', offset=184, starts_line=None, is_jump_target=False),
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200740 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=187, starts_line=None, is_jump_target=True),
741 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=188, starts_line=25, is_jump_target=True),
742 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=211, argrepr='to 211', offset=191, starts_line=None, is_jump_target=False),
743 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=194, starts_line=None, is_jump_target=False),
744 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=197, starts_line=26, is_jump_target=False),
745 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),
746 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),
747 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
748 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=207, starts_line=None, is_jump_target=False),
749 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=208, starts_line=None, is_jump_target=False),
Yury Selivanov75445082015-05-11 22:57:16 -0400750 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=211, starts_line=None, is_jump_target=True),
751 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
752 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=213, starts_line=None, is_jump_target=False),
753 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=True),
754 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=215, starts_line=None, is_jump_target=False),
755 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=218, starts_line=28, is_jump_target=True),
756 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=221, starts_line=None, is_jump_target=False),
757 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=224, starts_line=None, is_jump_target=False),
758 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=227, starts_line=None, is_jump_target=False),
759 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=228, starts_line=None, is_jump_target=False),
760 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=229, starts_line=None, is_jump_target=False),
761 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=232, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000762]
763
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000764# One last piece of inspect fodder to check the default line number handling
765def simple(): pass
766expected_opinfo_simple = [
767 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
768 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
769]
770
771
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000772class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000773
774 def test_default_first_line(self):
775 actual = dis.get_instructions(simple)
776 self.assertEqual(list(actual), expected_opinfo_simple)
777
778 def test_first_line_set_to_None(self):
779 actual = dis.get_instructions(simple, first_line=None)
780 self.assertEqual(list(actual), expected_opinfo_simple)
781
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000782 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000783 actual = dis.get_instructions(outer, first_line=expected_outer_line)
784 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000785
786 def test_nested(self):
787 with captured_stdout():
788 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000789 actual = dis.get_instructions(f, first_line=expected_f_line)
790 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000791
792 def test_doubly_nested(self):
793 with captured_stdout():
794 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000795 actual = dis.get_instructions(inner, first_line=expected_inner_line)
796 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000797
798 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000799 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
800 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000801
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000802# get_instructions has its own tests above, so can rely on it to validate
803# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000804class BytecodeTests(unittest.TestCase):
805 def test_instantiation(self):
806 # Test with function, method, code string and code object
807 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000808 with self.subTest(obj=obj):
809 b = dis.Bytecode(obj)
810 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000811
812 self.assertRaises(TypeError, dis.Bytecode, object())
813
814 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000815 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
816 with self.subTest(obj=obj):
817 via_object = list(dis.Bytecode(obj))
818 via_generator = list(dis.get_instructions(obj))
819 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000820
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000821 def test_explicit_first_line(self):
822 actual = dis.Bytecode(outer, first_line=expected_outer_line)
823 self.assertEqual(list(actual), expected_opinfo_outer)
824
825 def test_source_line_in_disassembly(self):
826 # Use the line in the source code
827 actual = dis.Bytecode(simple).dis()[:3]
828 expected = "{:>3}".format(simple.__code__.co_firstlineno)
829 self.assertEqual(actual, expected)
830 # Use an explicit first line number
831 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
832 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000833
834 def test_info(self):
835 self.maxDiff = 1000
836 for x, expected in CodeInfoTests.test_pairs:
837 b = dis.Bytecode(x)
838 self.assertRegex(b.info(), expected)
839
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000840 def test_disassembled(self):
841 actual = dis.Bytecode(_f).dis()
842 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000843
Nick Coghlan50c48b82013-11-23 00:57:00 +1000844 def test_from_traceback(self):
845 tb = get_tb()
846 b = dis.Bytecode.from_traceback(tb)
847 while tb.tb_next: tb = tb.tb_next
848
849 self.assertEqual(b.current_offset, tb.tb_lasti)
850
851 def test_from_traceback_dis(self):
852 tb = get_tb()
853 b = dis.Bytecode.from_traceback(tb)
854 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000855
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000856if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600857 unittest.main()