blob: 0fd134802764610b7a85795da82dd2b3a307c845 [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
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030033 @staticmethod
34 def sm(x):
35 x = x == 1
36
37 @classmethod
38 def cm(cls, x):
39 cls.x = x == 1
40
Benjamin Petersond6afe722011-03-15 14:44:52 -050041dis_c_instance_method = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030042%3d 0 LOAD_FAST 1 (x)
Benjamin Petersond6afe722011-03-15 14:44:52 -050043 3 LOAD_CONST 1 (1)
44 6 COMPARE_OP 2 (==)
45 9 LOAD_FAST 0 (self)
46 12 STORE_ATTR 0 (x)
47 15 LOAD_CONST 0 (None)
48 18 RETURN_VALUE
49""" % (_C.__init__.__code__.co_firstlineno + 1,)
50
51dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100052 0 LOAD_FAST 1 (1)
53 3 LOAD_CONST 1 (1)
54 6 COMPARE_OP 2 (==)
55 9 LOAD_FAST 0 (0)
56 12 STORE_ATTR 0 (0)
57 15 LOAD_CONST 0 (0)
Benjamin Petersond6afe722011-03-15 14:44:52 -050058 18 RETURN_VALUE
59"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000060
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030061dis_c_class_method = """\
62%3d 0 LOAD_FAST 1 (x)
63 3 LOAD_CONST 1 (1)
64 6 COMPARE_OP 2 (==)
65 9 LOAD_FAST 0 (cls)
66 12 STORE_ATTR 0 (x)
67 15 LOAD_CONST 0 (None)
68 18 RETURN_VALUE
69""" % (_C.cm.__code__.co_firstlineno + 2,)
70
71dis_c_static_method = """\
72%3d 0 LOAD_FAST 0 (x)
73 3 LOAD_CONST 1 (1)
74 6 COMPARE_OP 2 (==)
75 9 STORE_FAST 0 (x)
76 12 LOAD_CONST 0 (None)
77 15 RETURN_VALUE
78""" % (_C.sm.__code__.co_firstlineno + 2,)
79
80# Class disassembling info has an extra newline at end.
81dis_c = """\
82Disassembly of %s:
83%s
84Disassembly of %s:
85%s
86Disassembly of %s:
87%s
88""" % (_C.__init__.__name__, dis_c_instance_method,
89 _C.cm.__name__, dis_c_class_method,
90 _C.sm.__name__, dis_c_static_method)
91
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000092def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000093 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000094 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000095
96dis_f = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030097%3d 0 LOAD_GLOBAL 0 (print)
Georg Brandl88fc6642007-02-09 21:28:07 +000098 3 LOAD_FAST 0 (a)
Alexander Belopolsky74482202012-06-07 14:28:14 -040099 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Georg Brandl88fc6642007-02-09 21:28:07 +0000100 9 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000101
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300102%3d 10 LOAD_CONST 1 (1)
Georg Brandl88fc6642007-02-09 21:28:07 +0000103 13 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000104""" % (_f.__code__.co_firstlineno + 1,
105 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000106
107
Benjamin Petersond6afe722011-03-15 14:44:52 -0500108dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000109 0 LOAD_GLOBAL 0 (0)
110 3 LOAD_FAST 0 (0)
111 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500112 9 POP_TOP
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000113 10 LOAD_CONST 1 (1)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500114 13 RETURN_VALUE
115"""
116
117
Michael W. Hudson26848a32003-04-29 17:07:36 +0000118def bug708901():
119 for res in range(1,
120 10):
121 pass
122
123dis_bug708901 = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300124%3d 0 SETUP_LOOP 23 (to 26)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000125 3 LOAD_GLOBAL 0 (range)
126 6 LOAD_CONST 1 (1)
127
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300128%3d 9 LOAD_CONST 2 (10)
Alexander Belopolsky74482202012-06-07 14:28:14 -0400129 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000130 15 GET_ITER
131 >> 16 FOR_ITER 6 (to 25)
132 19 STORE_FAST 0 (res)
133
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300134%3d 22 JUMP_ABSOLUTE 16
Michael W. Hudson26848a32003-04-29 17:07:36 +0000135 >> 25 POP_BLOCK
136 >> 26 LOAD_CONST 0 (None)
137 29 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000138""" % (bug708901.__code__.co_firstlineno + 1,
139 bug708901.__code__.co_firstlineno + 2,
140 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000141
Neal Norwitz51abbc72005-12-18 07:06:23 +0000142
143def bug1333982(x=[]):
144 assert 0, ([s for s in x] +
145 1)
146 pass
147
148dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600149%3d 0 LOAD_CONST 1 (0)
Zachary Waree80e8062013-12-26 09:53:49 -0600150 3 POP_JUMP_IF_TRUE 35
151 6 LOAD_GLOBAL 0 (AssertionError)
152 9 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
153 12 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
154 15 MAKE_FUNCTION 0
155 18 LOAD_FAST 0 (x)
156 21 GET_ITER
157 22 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000158
Zachary Warebb4b7c12013-12-26 09:55:24 -0600159%3d 25 LOAD_CONST 4 (1)
Zachary Waree80e8062013-12-26 09:53:49 -0600160 28 BINARY_ADD
161 29 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
162 32 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000163
Zachary Warebb4b7c12013-12-26 09:55:24 -0600164%3d >> 35 LOAD_CONST 0 (None)
Zachary Waree80e8062013-12-26 09:53:49 -0600165 38 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600167 __file__,
168 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000169 bug1333982.__code__.co_firstlineno + 2,
170 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000171
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172_BIG_LINENO_FORMAT = """\
173%3d 0 LOAD_GLOBAL 0 (spam)
174 3 POP_TOP
175 4 LOAD_CONST 0 (None)
176 7 RETURN_VALUE
177"""
178
Guido van Rossume7ba4952007-06-06 23:52:48 +0000179dis_module_expected_results = """\
180Disassembly of f:
181 4 0 LOAD_CONST 0 (None)
182 3 RETURN_VALUE
183
184Disassembly of g:
185 5 0 LOAD_CONST 0 (None)
186 3 RETURN_VALUE
187
188"""
189
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000190expr_str = "x + 1"
191
192dis_expr_str = """\
193 1 0 LOAD_NAME 0 (x)
194 3 LOAD_CONST 0 (1)
195 6 BINARY_ADD
196 7 RETURN_VALUE
197"""
198
199simple_stmt_str = "x = x + 1"
200
201dis_simple_stmt_str = """\
202 1 0 LOAD_NAME 0 (x)
203 3 LOAD_CONST 0 (1)
204 6 BINARY_ADD
205 7 STORE_NAME 0 (x)
206 10 LOAD_CONST 1 (None)
207 13 RETURN_VALUE
208"""
209
210compound_stmt_str = """\
211x = 0
212while 1:
213 x += 1"""
214# Trailing newline has been deliberately omitted
215
216dis_compound_stmt_str = """\
217 1 0 LOAD_CONST 0 (0)
218 3 STORE_NAME 0 (x)
219
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -0500220 2 6 SETUP_LOOP 14 (to 23)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000221
222 3 >> 9 LOAD_NAME 0 (x)
223 12 LOAD_CONST 1 (1)
224 15 INPLACE_ADD
225 16 STORE_NAME 0 (x)
226 19 JUMP_ABSOLUTE 9
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -0500227 22 POP_BLOCK
228 >> 23 LOAD_CONST 2 (None)
229 26 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000230"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000231
Nick Coghlan50c48b82013-11-23 00:57:00 +1000232dis_traceback = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300233%3d 0 SETUP_EXCEPT 12 (to 15)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000234
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300235%3d 3 LOAD_CONST 1 (1)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000236 6 LOAD_CONST 2 (0)
237 --> 9 BINARY_TRUE_DIVIDE
238 10 POP_TOP
239 11 POP_BLOCK
240 12 JUMP_FORWARD 46 (to 61)
241
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300242%3d >> 15 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000243 16 LOAD_GLOBAL 0 (Exception)
244 19 COMPARE_OP 10 (exception match)
245 22 POP_JUMP_IF_FALSE 60
246 25 POP_TOP
247 26 STORE_FAST 0 (e)
248 29 POP_TOP
249 30 SETUP_FINALLY 14 (to 47)
250
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300251%3d 33 LOAD_FAST 0 (e)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000252 36 LOAD_ATTR 1 (__traceback__)
253 39 STORE_FAST 1 (tb)
254 42 POP_BLOCK
255 43 POP_EXCEPT
256 44 LOAD_CONST 0 (None)
257 >> 47 LOAD_CONST 0 (None)
258 50 STORE_FAST 0 (e)
259 53 DELETE_FAST 0 (e)
260 56 END_FINALLY
261 57 JUMP_FORWARD 1 (to 61)
262 >> 60 END_FINALLY
263
Serhiy Storchaka247763d2016-04-12 08:46:28 +0300264%3d >> 61 LOAD_FAST 1 (tb)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000265 64 RETURN_VALUE
266""" % (TRACEBACK_CODE.co_firstlineno + 1,
267 TRACEBACK_CODE.co_firstlineno + 2,
268 TRACEBACK_CODE.co_firstlineno + 3,
269 TRACEBACK_CODE.co_firstlineno + 4,
270 TRACEBACK_CODE.co_firstlineno + 5)
271
Nick Coghlanefd5df92014-07-25 23:02:56 +1000272def _g(x):
273 yield x
274
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000275class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500276
277 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000278 # We want to test the default printing behaviour, not the file arg
279 output = io.StringIO()
280 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500281 if wrapper:
282 dis.dis(func)
283 else:
284 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000285 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500286
287 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000288 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500289
Zachary Warebb4b7c12013-12-26 09:55:24 -0600290 def strip_addresses(self, text):
291 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500292
293 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600294 got = self.get_disassembly(func)
295 if got != expected:
296 got = self.strip_addresses(got)
297 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000298
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000299 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500300 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000301 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
302 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000303
304 def test_opname(self):
305 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
306
307 def test_boundaries(self):
308 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
309 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
310
311 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000312 self.do_disassembly_test(_f, dis_f)
313
314 def test_bug_708901(self):
315 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000316
Neal Norwitz51abbc72005-12-18 07:06:23 +0000317 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000318 # This one is checking bytecodes generated for an `assert` statement,
319 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600320 if not __debug__:
321 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600322
Zachary Waree80e8062013-12-26 09:53:49 -0600323 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000324
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000325 def test_big_linenos(self):
326 def func(count):
327 namespace = {}
328 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000329 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000330 return namespace['foo']
331
332 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000333 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000334 expected = _BIG_LINENO_FORMAT % (i + 2)
335 self.do_disassembly_test(func(i), expected)
336
337 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000338 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000339 expected = _BIG_LINENO_FORMAT % (i + 2)
340 self.do_disassembly_test(func(i), expected)
341
Guido van Rossume7ba4952007-06-06 23:52:48 +0000342 from test import dis_module
343 self.do_disassembly_test(dis_module, dis_module_expected_results)
344
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000345 def test_disassemble_str(self):
346 self.do_disassembly_test(expr_str, dis_expr_str)
347 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
348 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
349
Benjamin Petersond6afe722011-03-15 14:44:52 -0500350 def test_disassemble_bytes(self):
351 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
352
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300353 def test_disassemble_class(self):
354 self.do_disassembly_test(_C, dis_c)
355
356 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500357 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
358
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300359 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500360 method_bytecode = _C(1).__init__.__code__.co_code
361 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
362
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300363 def test_disassemble_static_method(self):
364 self.do_disassembly_test(_C.sm, dis_c_static_method)
365
366 def test_disassemble_class_method(self):
367 self.do_disassembly_test(_C.cm, dis_c_class_method)
368
Nick Coghlanefd5df92014-07-25 23:02:56 +1000369 def test_disassemble_generator(self):
370 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
371 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
372 self.assertEqual(gen_disas, gen_func_disas)
373
Benjamin Petersond6afe722011-03-15 14:44:52 -0500374 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500375 try:
376 del sys.last_traceback
377 except AttributeError:
378 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500379 self.assertRaises(RuntimeError, dis.dis, None)
380
Benjamin Petersond6afe722011-03-15 14:44:52 -0500381 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500382 try:
383 del sys.last_traceback
384 except AttributeError:
385 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500386
387 try:
388 1/0
389 except Exception as e:
390 tb = e.__traceback__
391 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500392
393 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
394 self.do_disassembly_test(None, tb_dis)
395
396 def test_dis_object(self):
397 self.assertRaises(TypeError, dis.dis, object())
398
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000399class DisWithFileTests(DisTests):
400
401 # Run the tests again, using the file arg instead of print
402 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000403 output = io.StringIO()
404 if wrapper:
405 dis.dis(func, file=output)
406 else:
407 dis.disassemble(func, lasti, file=output)
408 return output.getvalue()
409
410
411
Nick Coghlaneae2da12010-08-17 08:03:36 +0000412code_info_code_info = """\
413Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000414Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000415Argument count: 1
416Kw-only arguments: 0
417Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000418Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000419Flags: OPTIMIZED, NEWLOCALS, NOFREE
420Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000421 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000422Names:
423 0: _format_code_info
424 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000425Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000426 0: x""" % (('Formatted details of methods, functions, or code.',)
427 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000428
429@staticmethod
430def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
431 def f(c=c):
432 print(x, y, z, c, d, e, f)
433 yield x, y, z, c, d, e, f
434
Nick Coghlaneae2da12010-08-17 08:03:36 +0000435code_info_tricky = """\
436Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000437Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000438Argument count: 3
439Kw-only arguments: 3
440Number of locals: 8
441Stack size: 7
442Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
443Constants:
444 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000445 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100446 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000447Variable names:
448 0: x
449 1: y
450 2: z
451 3: c
452 4: d
453 5: e
454 6: args
455 7: kwds
456Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100457 0: [edfxyz]
458 1: [edfxyz]
459 2: [edfxyz]
460 3: [edfxyz]
461 4: [edfxyz]
462 5: [edfxyz]"""
463# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000464
465co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000466
467code_info_tricky_nested_f = """\
468Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000469Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000470Argument count: 1
471Kw-only arguments: 0
472Number of locals: 1
473Stack size: 8
474Flags: OPTIMIZED, NEWLOCALS, NESTED
475Constants:
476 0: None
477Names:
478 0: print
479Variable names:
480 0: c
481Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100482 0: [edfxyz]
483 1: [edfxyz]
484 2: [edfxyz]
485 3: [edfxyz]
486 4: [edfxyz]
487 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000488
489code_info_expr_str = """\
490Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000491Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000492Argument count: 0
493Kw-only arguments: 0
494Number of locals: 0
495Stack size: 2
496Flags: NOFREE
497Constants:
498 0: 1
499Names:
500 0: x"""
501
502code_info_simple_stmt_str = """\
503Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000504Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000505Argument count: 0
506Kw-only arguments: 0
507Number of locals: 0
508Stack size: 2
509Flags: NOFREE
510Constants:
511 0: 1
512 1: None
513Names:
514 0: x"""
515
516code_info_compound_stmt_str = """\
517Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000518Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000519Argument count: 0
520Kw-only arguments: 0
521Number of locals: 0
522Stack size: 2
523Flags: NOFREE
524Constants:
525 0: 0
526 1: 1
527 2: None
528Names:
529 0: x"""
530
Yury Selivanov75445082015-05-11 22:57:16 -0400531
532async def async_def():
533 await 1
534 async for a in b: pass
535 async with c as d: pass
536
537code_info_async_def = """\
538Name: async_def
539Filename: (.*)
540Argument count: 0
541Kw-only arguments: 0
542Number of locals: 2
543Stack size: 17
544Flags: OPTIMIZED, NEWLOCALS, GENERATOR, NOFREE, COROUTINE
545Constants:
546 0: None
547 1: 1"""
548
Nick Coghlaneae2da12010-08-17 08:03:36 +0000549class CodeInfoTests(unittest.TestCase):
550 test_pairs = [
551 (dis.code_info, code_info_code_info),
552 (tricky, code_info_tricky),
553 (co_tricky_nested_f, code_info_tricky_nested_f),
554 (expr_str, code_info_expr_str),
555 (simple_stmt_str, code_info_simple_stmt_str),
556 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400557 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000558 ]
559
560 def test_code_info(self):
561 self.maxDiff = 1000
562 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000563 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000564
565 def test_show_code(self):
566 self.maxDiff = 1000
567 for x, expected in self.test_pairs:
568 with captured_stdout() as output:
569 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000570 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000571 output = io.StringIO()
572 dis.show_code(x, file=output)
573 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000574
Benjamin Petersond6afe722011-03-15 14:44:52 -0500575 def test_code_info_object(self):
576 self.assertRaises(TypeError, dis.code_info, object())
577
578 def test_pretty_flags_no_flags(self):
579 self.assertEqual(dis.pretty_flags(0), '0x0')
580
581
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000582# Fodder for instruction introspection tests
583# Editing any of these may require recalculating the expected output
584def outer(a=1, b=2):
585 def f(c=3, d=4):
586 def inner(e=5, f=6):
587 print(a, b, c, d, e, f)
588 print(a, b, c, d)
589 return inner
590 print(a, b, '', 1, [], {}, "Hello world!")
591 return f
592
593def jumpy():
594 # This won't actually run (but that's OK, we only disassemble it)
595 for i in range(10):
596 print(i)
597 if i < 4:
598 continue
599 if i > 6:
600 break
601 else:
602 print("I can haz else clause?")
603 while i:
604 print(i)
605 i -= 1
606 if i > 6:
607 continue
608 if i < 4:
609 break
610 else:
611 print("Who let lolcatz into this test suite?")
612 try:
613 1 / 0
614 except ZeroDivisionError:
615 print("Here we go, here we go, here we go...")
616 else:
617 with i as dodgy:
618 print("Never reach this")
619 finally:
620 print("OK, now we're done")
621
622# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000623expected_outer_line = 1
624_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000625code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000626expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000627code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000628expected_inner_line = code_object_inner.co_firstlineno - _line_offset
629expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000630
631# The following lines are useful to regenerate the expected results after
632# either the fodder is modified or the bytecode generation changes
633# After regeneration, update the references to code_object_f and
634# code_object_inner before rerunning the tests
635
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000636#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000637#print('expected_opinfo_outer = [\n ',
638 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200639#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000640#print('expected_opinfo_f = [\n ',
641 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200642#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000643#print('expected_opinfo_inner = [\n ',
644 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000645#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000646#print('expected_opinfo_jumpy = [\n ',
647 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
648
649
650Instruction = dis.Instruction
651expected_opinfo_outer = [
652 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
653 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
654 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
655 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
656 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
657 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),
658 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),
659 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
660 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
661 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
662 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
663 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
664 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
665 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
666 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
667 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
668 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
669 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),
670 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
671 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
672 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
673]
674
675expected_opinfo_f = [
676 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
677 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
678 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
679 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
680 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
681 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
682 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
683 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),
684 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),
685 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
686 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
687 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
688 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
689 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
690 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
691 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
692 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),
693 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
694 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
695 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
696]
697
698expected_opinfo_inner = [
699 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
700 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
701 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
702 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
703 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
704 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
705 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
706 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),
707 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
708 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
709 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
710]
711
712expected_opinfo_jumpy = [
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200713 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 +1000714 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
715 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
716 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=9, starts_line=None, is_jump_target=False),
717 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 +0200718 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 +1000719 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
720 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
721 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
722 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=25, starts_line=None, is_jump_target=False),
723 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
724 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
725 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
726 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 +0200727 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 +1000728 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 +0200729 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=44, starts_line=7, is_jump_target=True),
730 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=47, starts_line=None, is_jump_target=False),
731 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=50, starts_line=None, is_jump_target=False),
732 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=53, starts_line=None, is_jump_target=False),
733 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=56, starts_line=8, is_jump_target=False),
734 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=57, starts_line=None, is_jump_target=False),
735 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=True),
736 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=61, starts_line=10, is_jump_target=False),
737 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),
738 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),
739 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=70, starts_line=None, is_jump_target=False),
740 Instruction(opname='SETUP_LOOP', opcode=120, arg=68, argval=142, argrepr='to 142', offset=71, starts_line=11, is_jump_target=True),
741 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=True),
742 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=131, argval=131, argrepr='', offset=77, starts_line=None, is_jump_target=False),
743 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=80, starts_line=12, is_jump_target=False),
744 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=83, starts_line=None, is_jump_target=False),
745 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),
746 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=89, starts_line=None, is_jump_target=False),
747 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=90, starts_line=13, is_jump_target=False),
748 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=93, starts_line=None, is_jump_target=False),
749 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
750 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=97, starts_line=None, is_jump_target=False),
751 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=100, starts_line=14, is_jump_target=False),
752 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=103, starts_line=None, is_jump_target=False),
753 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=106, starts_line=None, is_jump_target=False),
754 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=115, argval=115, argrepr='', offset=109, starts_line=None, is_jump_target=False),
755 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=112, starts_line=15, is_jump_target=False),
756 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=115, starts_line=16, is_jump_target=True),
757 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=118, starts_line=None, is_jump_target=False),
758 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=121, starts_line=None, is_jump_target=False),
759 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=74, argval=74, argrepr='', offset=124, starts_line=None, is_jump_target=False),
760 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=127, starts_line=17, is_jump_target=False),
761 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=74, argval=74, argrepr='', offset=128, starts_line=None, is_jump_target=False),
762 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=131, starts_line=None, is_jump_target=True),
763 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=19, is_jump_target=False),
764 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),
765 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),
766 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 -0400767 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 +0200768 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=160, argrepr='to 160', offset=145, starts_line=None, is_jump_target=False),
769 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=148, starts_line=21, is_jump_target=False),
770 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=151, starts_line=None, is_jump_target=False),
771 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
772 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=155, starts_line=None, is_jump_target=False),
773 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
774 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=188, argrepr='to 188', offset=157, starts_line=None, is_jump_target=False),
775 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=22, is_jump_target=True),
776 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=161, starts_line=None, is_jump_target=False),
777 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=164, starts_line=None, is_jump_target=False),
778 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=187, argval=187, argrepr='', offset=167, starts_line=None, is_jump_target=False),
779 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
780 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=171, starts_line=None, is_jump_target=False),
781 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
782 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=173, starts_line=23, is_jump_target=False),
783 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),
784 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 +1000785 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 +0200786 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 -0400787 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 +0200788 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=187, starts_line=None, is_jump_target=True),
789 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=188, starts_line=25, is_jump_target=True),
790 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=211, argrepr='to 211', offset=191, starts_line=None, is_jump_target=False),
791 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=194, starts_line=None, is_jump_target=False),
792 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=197, starts_line=26, is_jump_target=False),
793 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),
794 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),
795 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
796 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=207, starts_line=None, is_jump_target=False),
797 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 -0400798 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=211, starts_line=None, is_jump_target=True),
799 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
800 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=213, starts_line=None, is_jump_target=False),
801 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=True),
802 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=215, starts_line=None, is_jump_target=False),
803 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=218, starts_line=28, is_jump_target=True),
804 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),
805 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),
806 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=227, starts_line=None, is_jump_target=False),
807 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=228, starts_line=None, is_jump_target=False),
808 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=229, starts_line=None, is_jump_target=False),
809 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 +1000810]
811
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000812# One last piece of inspect fodder to check the default line number handling
813def simple(): pass
814expected_opinfo_simple = [
815 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
816 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
817]
818
819
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000820class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000821
822 def test_default_first_line(self):
823 actual = dis.get_instructions(simple)
824 self.assertEqual(list(actual), expected_opinfo_simple)
825
826 def test_first_line_set_to_None(self):
827 actual = dis.get_instructions(simple, first_line=None)
828 self.assertEqual(list(actual), expected_opinfo_simple)
829
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000830 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000831 actual = dis.get_instructions(outer, first_line=expected_outer_line)
832 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000833
834 def test_nested(self):
835 with captured_stdout():
836 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000837 actual = dis.get_instructions(f, first_line=expected_f_line)
838 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000839
840 def test_doubly_nested(self):
841 with captured_stdout():
842 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000843 actual = dis.get_instructions(inner, first_line=expected_inner_line)
844 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000845
846 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000847 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
848 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000849
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000850# get_instructions has its own tests above, so can rely on it to validate
851# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000852class BytecodeTests(unittest.TestCase):
853 def test_instantiation(self):
854 # Test with function, method, code string and code object
855 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000856 with self.subTest(obj=obj):
857 b = dis.Bytecode(obj)
858 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000859
860 self.assertRaises(TypeError, dis.Bytecode, object())
861
862 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000863 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
864 with self.subTest(obj=obj):
865 via_object = list(dis.Bytecode(obj))
866 via_generator = list(dis.get_instructions(obj))
867 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000868
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000869 def test_explicit_first_line(self):
870 actual = dis.Bytecode(outer, first_line=expected_outer_line)
871 self.assertEqual(list(actual), expected_opinfo_outer)
872
873 def test_source_line_in_disassembly(self):
874 # Use the line in the source code
875 actual = dis.Bytecode(simple).dis()[:3]
876 expected = "{:>3}".format(simple.__code__.co_firstlineno)
877 self.assertEqual(actual, expected)
878 # Use an explicit first line number
879 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
880 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000881
882 def test_info(self):
883 self.maxDiff = 1000
884 for x, expected in CodeInfoTests.test_pairs:
885 b = dis.Bytecode(x)
886 self.assertRegex(b.info(), expected)
887
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000888 def test_disassembled(self):
889 actual = dis.Bytecode(_f).dis()
890 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000891
Nick Coghlan50c48b82013-11-23 00:57:00 +1000892 def test_from_traceback(self):
893 tb = get_tb()
894 b = dis.Bytecode.from_traceback(tb)
895 while tb.tb_next: tb = tb.tb_next
896
897 self.assertEqual(b.current_offset, tb.tb_lasti)
898
899 def test_from_traceback_dis(self):
900 tb = get_tb()
901 b = dis.Bytecode.from_traceback(tb)
902 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000903
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000904if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600905 unittest.main()