blob: 980ae16c652741af7b3fa3c6f98266bbb6b15640 [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
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import unittest
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00006import sys
7import dis
Guido van Rossum34d19282007-08-09 01:03:29 +00008import io
Zachary Waree80e8062013-12-26 09:53:49 -06009import re
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100010import types
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100011import contextlib
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000012
Nick Coghlan50c48b82013-11-23 00:57:00 +100013def get_tb():
14 def _error():
15 try:
16 1 / 0
17 except Exception as e:
18 tb = e.__traceback__
19 return tb
20
21 tb = _error()
22 while tb.tb_next:
23 tb = tb.tb_next
24 return tb
25
26TRACEBACK_CODE = get_tb().tb_frame.f_code
27
Benjamin Petersond6afe722011-03-15 14:44:52 -050028class _C:
29 def __init__(self, x):
30 self.x = x == 1
31
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030032 @staticmethod
33 def sm(x):
34 x = x == 1
35
36 @classmethod
37 def cm(cls, x):
38 cls.x = x == 1
39
Benjamin Petersond6afe722011-03-15 14:44:52 -050040dis_c_instance_method = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030041%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030042 2 LOAD_CONST 1 (1)
43 4 COMPARE_OP 2 (==)
44 6 LOAD_FAST 0 (self)
45 8 STORE_ATTR 0 (x)
46 10 LOAD_CONST 0 (None)
47 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050048""" % (_C.__init__.__code__.co_firstlineno + 1,)
49
50dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100051 0 LOAD_FAST 1 (1)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030052 2 LOAD_CONST 1 (1)
53 4 COMPARE_OP 2 (==)
54 6 LOAD_FAST 0 (0)
55 8 STORE_ATTR 0 (0)
56 10 LOAD_CONST 0 (0)
57 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050058"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000059
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030060dis_c_class_method = """\
61%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030062 2 LOAD_CONST 1 (1)
63 4 COMPARE_OP 2 (==)
64 6 LOAD_FAST 0 (cls)
65 8 STORE_ATTR 0 (x)
66 10 LOAD_CONST 0 (None)
67 12 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030068""" % (_C.cm.__code__.co_firstlineno + 2,)
69
70dis_c_static_method = """\
71%3d 0 LOAD_FAST 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030072 2 LOAD_CONST 1 (1)
73 4 COMPARE_OP 2 (==)
74 6 STORE_FAST 0 (x)
75 8 LOAD_CONST 0 (None)
76 10 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030077""" % (_C.sm.__code__.co_firstlineno + 2,)
78
79# Class disassembling info has an extra newline at end.
80dis_c = """\
81Disassembly of %s:
82%s
83Disassembly of %s:
84%s
85Disassembly of %s:
86%s
87""" % (_C.__init__.__name__, dis_c_instance_method,
88 _C.cm.__name__, dis_c_class_method,
89 _C.sm.__name__, dis_c_static_method)
90
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000091def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000093 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000094
95dis_f = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030096%3d 0 LOAD_GLOBAL 0 (print)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030097 2 LOAD_FAST 0 (a)
Victor Stinnerf9b760f2016-09-09 10:17:08 -070098 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030099 6 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000100
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300101%3d 8 LOAD_CONST 1 (1)
102 10 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000103""" % (_f.__code__.co_firstlineno + 1,
104 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000105
106
Benjamin Petersond6afe722011-03-15 14:44:52 -0500107dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000108 0 LOAD_GLOBAL 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300109 2 LOAD_FAST 0 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700110 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300111 6 POP_TOP
112 8 LOAD_CONST 1 (1)
113 10 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -0500114"""
115
116
Michael W. Hudson26848a32003-04-29 17:07:36 +0000117def bug708901():
118 for res in range(1,
119 10):
120 pass
121
122dis_bug708901 = """\
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300123%3d 0 SETUP_LOOP 18 (to 20)
124 2 LOAD_GLOBAL 0 (range)
125 4 LOAD_CONST 1 (1)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000126
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300127%3d 6 LOAD_CONST 2 (10)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700128 8 CALL_FUNCTION 2
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300129 10 GET_ITER
130 >> 12 FOR_ITER 4 (to 18)
131 14 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000132
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300133%3d 16 JUMP_ABSOLUTE 12
134 >> 18 POP_BLOCK
135 >> 20 LOAD_CONST 0 (None)
136 22 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000137""" % (bug708901.__code__.co_firstlineno + 1,
138 bug708901.__code__.co_firstlineno + 2,
139 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000140
Neal Norwitz51abbc72005-12-18 07:06:23 +0000141
142def bug1333982(x=[]):
143 assert 0, ([s for s in x] +
144 1)
145 pass
146
147dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600148%3d 0 LOAD_CONST 1 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300149 2 POP_JUMP_IF_TRUE 26
150 4 LOAD_GLOBAL 0 (AssertionError)
151 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153 10 MAKE_FUNCTION 0
154 12 LOAD_FAST 0 (x)
155 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700156 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300157
158%3d 18 LOAD_CONST 4 (1)
159 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700160 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300161 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000162
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300163%3d >> 26 LOAD_CONST 0 (None)
164 28 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000165""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600166 __file__,
167 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000168 bug1333982.__code__.co_firstlineno + 2,
169 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000170
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000171_BIG_LINENO_FORMAT = """\
172%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300173 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000174 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300175 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176"""
177
Guido van Rossume7ba4952007-06-06 23:52:48 +0000178dis_module_expected_results = """\
179Disassembly of f:
180 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300181 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000182
183Disassembly of g:
184 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300185 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000186
187"""
188
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000189expr_str = "x + 1"
190
191dis_expr_str = """\
192 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300193 2 LOAD_CONST 0 (1)
194 4 BINARY_ADD
195 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000196"""
197
198simple_stmt_str = "x = x + 1"
199
200dis_simple_stmt_str = """\
201 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300202 2 LOAD_CONST 0 (1)
203 4 BINARY_ADD
204 6 STORE_NAME 0 (x)
205 8 LOAD_CONST 1 (None)
206 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000207"""
208
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700209annot_stmt_str = """\
210
211x: int = 1
212y: fun(1)
213lst[fun(0)]: int = 1
214"""
215# leading newline is for a reason (tests lineno)
216
217dis_annot_stmt_str = """\
218 2 0 SETUP_ANNOTATIONS
219 2 LOAD_CONST 0 (1)
220 4 STORE_NAME 0 (x)
221 6 LOAD_NAME 1 (int)
222 8 STORE_ANNOTATION 0 (x)
223
224 3 10 LOAD_NAME 2 (fun)
225 12 LOAD_CONST 0 (1)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700226 14 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700227 16 STORE_ANNOTATION 3 (y)
228
229 4 18 LOAD_CONST 0 (1)
230 20 LOAD_NAME 4 (lst)
231 22 LOAD_NAME 2 (fun)
232 24 LOAD_CONST 1 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700233 26 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700234 28 STORE_SUBSCR
235 30 LOAD_NAME 1 (int)
236 32 POP_TOP
237 34 LOAD_CONST 2 (None)
238 36 RETURN_VALUE
239"""
240
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000241compound_stmt_str = """\
242x = 0
243while 1:
244 x += 1"""
245# Trailing newline has been deliberately omitted
246
247dis_compound_stmt_str = """\
248 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300249 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000250
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300251 2 4 SETUP_LOOP 12 (to 18)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000252
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300253 3 >> 6 LOAD_NAME 0 (x)
254 8 LOAD_CONST 1 (1)
255 10 INPLACE_ADD
256 12 STORE_NAME 0 (x)
257 14 JUMP_ABSOLUTE 6
258 16 POP_BLOCK
259 >> 18 LOAD_CONST 2 (None)
260 20 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000261"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000262
Nick Coghlan50c48b82013-11-23 00:57:00 +1000263dis_traceback = """\
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300264%3d 0 SETUP_EXCEPT 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000265
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300266%3d 2 LOAD_CONST 1 (1)
267 4 LOAD_CONST 2 (0)
268 --> 6 BINARY_TRUE_DIVIDE
269 8 POP_TOP
270 10 POP_BLOCK
271 12 JUMP_FORWARD 40 (to 54)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000272
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300273%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000274 16 LOAD_GLOBAL 0 (Exception)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300275 18 COMPARE_OP 10 (exception match)
276 20 POP_JUMP_IF_FALSE 52
277 22 POP_TOP
278 24 STORE_FAST 0 (e)
279 26 POP_TOP
280 28 SETUP_FINALLY 12 (to 42)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000281
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300282%3d 30 LOAD_FAST 0 (e)
283 32 LOAD_ATTR 1 (__traceback__)
284 34 STORE_FAST 1 (tb)
285 36 POP_BLOCK
286 38 POP_EXCEPT
287 40 LOAD_CONST 0 (None)
288 >> 42 LOAD_CONST 0 (None)
289 44 STORE_FAST 0 (e)
290 46 DELETE_FAST 0 (e)
291 48 END_FINALLY
292 50 JUMP_FORWARD 2 (to 54)
293 >> 52 END_FINALLY
Nick Coghlan50c48b82013-11-23 00:57:00 +1000294
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300295%3d >> 54 LOAD_FAST 1 (tb)
296 56 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000297""" % (TRACEBACK_CODE.co_firstlineno + 1,
298 TRACEBACK_CODE.co_firstlineno + 2,
299 TRACEBACK_CODE.co_firstlineno + 3,
300 TRACEBACK_CODE.co_firstlineno + 4,
301 TRACEBACK_CODE.co_firstlineno + 5)
302
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300303def _fstring(a, b, c, d):
304 return f'{a} {b:4} {c!r} {d!r:4}'
305
306dis_fstring = """\
307%3d 0 LOAD_FAST 0 (a)
308 2 FORMAT_VALUE 0
309 4 LOAD_CONST 1 (' ')
310 6 LOAD_FAST 1 (b)
311 8 LOAD_CONST 2 ('4')
312 10 FORMAT_VALUE 4 (with format)
313 12 LOAD_CONST 1 (' ')
314 14 LOAD_FAST 2 (c)
315 16 FORMAT_VALUE 2 (repr)
316 18 LOAD_CONST 1 (' ')
317 20 LOAD_FAST 3 (d)
318 22 LOAD_CONST 2 ('4')
319 24 FORMAT_VALUE 6 (repr, with format)
320 26 BUILD_STRING 7
321 28 RETURN_VALUE
322""" % (_fstring.__code__.co_firstlineno + 1,)
323
Nick Coghlanefd5df92014-07-25 23:02:56 +1000324def _g(x):
325 yield x
326
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000327class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500328
329 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000330 # We want to test the default printing behaviour, not the file arg
331 output = io.StringIO()
332 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500333 if wrapper:
334 dis.dis(func)
335 else:
336 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000337 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500338
339 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000340 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500341
Zachary Warebb4b7c12013-12-26 09:55:24 -0600342 def strip_addresses(self, text):
343 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500344
345 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600346 got = self.get_disassembly(func)
347 if got != expected:
348 got = self.strip_addresses(got)
349 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000350
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000351 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500352 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000353 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
354 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000355
356 def test_opname(self):
357 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
358
359 def test_boundaries(self):
360 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
361 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
362
363 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000364 self.do_disassembly_test(_f, dis_f)
365
366 def test_bug_708901(self):
367 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000368
Neal Norwitz51abbc72005-12-18 07:06:23 +0000369 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000370 # This one is checking bytecodes generated for an `assert` statement,
371 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600372 if not __debug__:
373 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600374
Zachary Waree80e8062013-12-26 09:53:49 -0600375 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000376
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000377 def test_big_linenos(self):
378 def func(count):
379 namespace = {}
380 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000381 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382 return namespace['foo']
383
384 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000385 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000386 expected = _BIG_LINENO_FORMAT % (i + 2)
387 self.do_disassembly_test(func(i), expected)
388
389 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000390 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000391 expected = _BIG_LINENO_FORMAT % (i + 2)
392 self.do_disassembly_test(func(i), expected)
393
Guido van Rossume7ba4952007-06-06 23:52:48 +0000394 from test import dis_module
395 self.do_disassembly_test(dis_module, dis_module_expected_results)
396
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000397 def test_disassemble_str(self):
398 self.do_disassembly_test(expr_str, dis_expr_str)
399 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700400 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000401 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
402
Benjamin Petersond6afe722011-03-15 14:44:52 -0500403 def test_disassemble_bytes(self):
404 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
405
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300406 def test_disassemble_class(self):
407 self.do_disassembly_test(_C, dis_c)
408
409 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500410 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
411
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300412 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500413 method_bytecode = _C(1).__init__.__code__.co_code
414 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
415
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300416 def test_disassemble_static_method(self):
417 self.do_disassembly_test(_C.sm, dis_c_static_method)
418
419 def test_disassemble_class_method(self):
420 self.do_disassembly_test(_C.cm, dis_c_class_method)
421
Nick Coghlanefd5df92014-07-25 23:02:56 +1000422 def test_disassemble_generator(self):
423 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
424 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
425 self.assertEqual(gen_disas, gen_func_disas)
426
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300427 def test_disassemble_fstring(self):
428 self.do_disassembly_test(_fstring, dis_fstring)
429
Benjamin Petersond6afe722011-03-15 14:44:52 -0500430 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500431 try:
432 del sys.last_traceback
433 except AttributeError:
434 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500435 self.assertRaises(RuntimeError, dis.dis, None)
436
Benjamin Petersond6afe722011-03-15 14:44:52 -0500437 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500438 try:
439 del sys.last_traceback
440 except AttributeError:
441 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500442
443 try:
444 1/0
445 except Exception as e:
446 tb = e.__traceback__
447 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500448
449 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
450 self.do_disassembly_test(None, tb_dis)
451
452 def test_dis_object(self):
453 self.assertRaises(TypeError, dis.dis, object())
454
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000455class DisWithFileTests(DisTests):
456
457 # Run the tests again, using the file arg instead of print
458 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000459 output = io.StringIO()
460 if wrapper:
461 dis.dis(func, file=output)
462 else:
463 dis.disassemble(func, lasti, file=output)
464 return output.getvalue()
465
466
467
Nick Coghlaneae2da12010-08-17 08:03:36 +0000468code_info_code_info = """\
469Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000470Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000471Argument count: 1
472Kw-only arguments: 0
473Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000474Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000475Flags: OPTIMIZED, NEWLOCALS, NOFREE
476Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000477 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000478Names:
479 0: _format_code_info
480 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000481Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000482 0: x""" % (('Formatted details of methods, functions, or code.',)
483 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000484
485@staticmethod
486def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
487 def f(c=c):
488 print(x, y, z, c, d, e, f)
489 yield x, y, z, c, d, e, f
490
Nick Coghlaneae2da12010-08-17 08:03:36 +0000491code_info_tricky = """\
492Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000493Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000494Argument count: 3
495Kw-only arguments: 3
496Number of locals: 8
497Stack size: 7
498Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
499Constants:
500 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000501 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100502 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000503Variable names:
504 0: x
505 1: y
506 2: z
507 3: c
508 4: d
509 5: e
510 6: args
511 7: kwds
512Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100513 0: [edfxyz]
514 1: [edfxyz]
515 2: [edfxyz]
516 3: [edfxyz]
517 4: [edfxyz]
518 5: [edfxyz]"""
519# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000520
521co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000522
523code_info_tricky_nested_f = """\
524Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000525Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000526Argument count: 1
527Kw-only arguments: 0
528Number of locals: 1
529Stack size: 8
530Flags: OPTIMIZED, NEWLOCALS, NESTED
531Constants:
532 0: None
533Names:
534 0: print
535Variable names:
536 0: c
537Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100538 0: [edfxyz]
539 1: [edfxyz]
540 2: [edfxyz]
541 3: [edfxyz]
542 4: [edfxyz]
543 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000544
545code_info_expr_str = """\
546Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000547Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000548Argument count: 0
549Kw-only arguments: 0
550Number of locals: 0
551Stack size: 2
552Flags: NOFREE
553Constants:
554 0: 1
555Names:
556 0: x"""
557
558code_info_simple_stmt_str = """\
559Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000560Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000561Argument count: 0
562Kw-only arguments: 0
563Number of locals: 0
564Stack size: 2
565Flags: NOFREE
566Constants:
567 0: 1
568 1: None
569Names:
570 0: x"""
571
572code_info_compound_stmt_str = """\
573Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000574Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000575Argument count: 0
576Kw-only arguments: 0
577Number of locals: 0
578Stack size: 2
579Flags: NOFREE
580Constants:
581 0: 0
582 1: 1
583 2: None
584Names:
585 0: x"""
586
Yury Selivanov75445082015-05-11 22:57:16 -0400587
588async def async_def():
589 await 1
590 async for a in b: pass
591 async with c as d: pass
592
593code_info_async_def = """\
594Name: async_def
595Filename: (.*)
596Argument count: 0
597Kw-only arguments: 0
598Number of locals: 2
599Stack size: 17
Yury Selivanoveb636452016-09-08 22:01:51 -0700600Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400601Constants:
602 0: None
603 1: 1"""
604
Nick Coghlaneae2da12010-08-17 08:03:36 +0000605class CodeInfoTests(unittest.TestCase):
606 test_pairs = [
607 (dis.code_info, code_info_code_info),
608 (tricky, code_info_tricky),
609 (co_tricky_nested_f, code_info_tricky_nested_f),
610 (expr_str, code_info_expr_str),
611 (simple_stmt_str, code_info_simple_stmt_str),
612 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400613 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000614 ]
615
616 def test_code_info(self):
617 self.maxDiff = 1000
618 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000619 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000620
621 def test_show_code(self):
622 self.maxDiff = 1000
623 for x, expected in self.test_pairs:
624 with captured_stdout() as output:
625 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000626 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000627 output = io.StringIO()
628 dis.show_code(x, file=output)
629 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000630
Benjamin Petersond6afe722011-03-15 14:44:52 -0500631 def test_code_info_object(self):
632 self.assertRaises(TypeError, dis.code_info, object())
633
634 def test_pretty_flags_no_flags(self):
635 self.assertEqual(dis.pretty_flags(0), '0x0')
636
637
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000638# Fodder for instruction introspection tests
639# Editing any of these may require recalculating the expected output
640def outer(a=1, b=2):
641 def f(c=3, d=4):
642 def inner(e=5, f=6):
643 print(a, b, c, d, e, f)
644 print(a, b, c, d)
645 return inner
646 print(a, b, '', 1, [], {}, "Hello world!")
647 return f
648
649def jumpy():
650 # This won't actually run (but that's OK, we only disassemble it)
651 for i in range(10):
652 print(i)
653 if i < 4:
654 continue
655 if i > 6:
656 break
657 else:
658 print("I can haz else clause?")
659 while i:
660 print(i)
661 i -= 1
662 if i > 6:
663 continue
664 if i < 4:
665 break
666 else:
667 print("Who let lolcatz into this test suite?")
668 try:
669 1 / 0
670 except ZeroDivisionError:
671 print("Here we go, here we go, here we go...")
672 else:
673 with i as dodgy:
674 print("Never reach this")
675 finally:
676 print("OK, now we're done")
677
678# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000679expected_outer_line = 1
680_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000681code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000682expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000683code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000684expected_inner_line = code_object_inner.co_firstlineno - _line_offset
685expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000686
687# The following lines are useful to regenerate the expected results after
688# either the fodder is modified or the bytecode generation changes
689# After regeneration, update the references to code_object_f and
690# code_object_inner before rerunning the tests
691
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000692#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000693#print('expected_opinfo_outer = [\n ',
694 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200695#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000696#print('expected_opinfo_f = [\n ',
697 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200698#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000699#print('expected_opinfo_inner = [\n ',
700 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000701#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000702#print('expected_opinfo_jumpy = [\n ',
703 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
704
705
706Instruction = dis.Instruction
707expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300708 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
709 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
710 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
711 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
712 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False),
713 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False),
714 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False),
715 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
716 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
717 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
718 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
719 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
720 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
721 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
722 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
723 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=30, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700724 Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=32, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300725 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
726 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
727 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000728]
729
730expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300731 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
732 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
733 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
734 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
735 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
736 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
737 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False),
738 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False),
739 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False),
740 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
741 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
742 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
743 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
744 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
745 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=28, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700746 Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=30, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300747 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
748 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
749 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000750]
751
752expected_opinfo_inner = [
753 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300754 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
755 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
756 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
757 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
758 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
759 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700760 Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300761 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
762 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
763 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000764]
765
766expected_opinfo_jumpy = [
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300767 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=54, argrepr='to 54', offset=0, starts_line=3, is_jump_target=False),
768 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=2, starts_line=None, is_jump_target=False),
769 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=4, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700770 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=6, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300771 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False),
772 Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=44, argrepr='to 44', offset=10, starts_line=None, is_jump_target=True),
773 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=12, starts_line=None, is_jump_target=False),
774 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=14, starts_line=4, is_jump_target=False),
775 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700776 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=18, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300777 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
778 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=5, is_jump_target=False),
779 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=24, starts_line=None, is_jump_target=False),
780 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=26, starts_line=None, is_jump_target=False),
781 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=32, argval=32, argrepr='', offset=28, starts_line=None, is_jump_target=False),
782 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=30, starts_line=6, is_jump_target=False),
783 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=32, starts_line=7, is_jump_target=True),
784 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=34, starts_line=None, is_jump_target=False),
785 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=36, starts_line=None, is_jump_target=False),
786 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=10, argval=10, argrepr='', offset=38, starts_line=None, is_jump_target=False),
787 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=40, starts_line=8, is_jump_target=False),
788 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=42, starts_line=None, is_jump_target=False),
789 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=44, starts_line=None, is_jump_target=True),
790 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=46, starts_line=10, is_jump_target=False),
791 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=48, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700792 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=50, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300793 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=52, starts_line=None, is_jump_target=False),
794 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=108, argrepr='to 108', offset=54, starts_line=11, is_jump_target=True),
795 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=True),
796 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=98, argval=98, argrepr='', offset=58, starts_line=None, is_jump_target=False),
797 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=60, starts_line=12, is_jump_target=False),
798 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700799 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=64, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300800 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
801 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=13, is_jump_target=False),
802 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=70, starts_line=None, is_jump_target=False),
803 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=72, starts_line=None, is_jump_target=False),
804 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=False),
805 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=76, starts_line=14, is_jump_target=False),
806 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=78, starts_line=None, is_jump_target=False),
807 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=80, starts_line=None, is_jump_target=False),
808 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=86, argval=86, argrepr='', offset=82, starts_line=None, is_jump_target=False),
809 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=84, starts_line=15, is_jump_target=False),
810 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=86, starts_line=16, is_jump_target=True),
811 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=88, starts_line=None, is_jump_target=False),
812 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=90, starts_line=None, is_jump_target=False),
813 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=56, argval=56, argrepr='', offset=92, starts_line=None, is_jump_target=False),
814 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=94, starts_line=17, is_jump_target=False),
815 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=96, starts_line=None, is_jump_target=False),
816 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=98, starts_line=None, is_jump_target=True),
817 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=100, starts_line=19, is_jump_target=False),
818 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=102, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700819 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=104, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300820 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
821 Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=180, argrepr='to 180', offset=108, starts_line=20, is_jump_target=True),
822 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=124, argrepr='to 124', offset=110, starts_line=None, is_jump_target=False),
823 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=112, starts_line=21, is_jump_target=False),
824 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=114, starts_line=None, is_jump_target=False),
825 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False),
826 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=118, starts_line=None, is_jump_target=False),
827 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
828 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=152, argrepr='to 152', offset=122, starts_line=None, is_jump_target=False),
829 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=124, starts_line=22, is_jump_target=True),
830 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=126, starts_line=None, is_jump_target=False),
831 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=128, starts_line=None, is_jump_target=False),
832 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=150, argval=150, argrepr='', offset=130, starts_line=None, is_jump_target=False),
833 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
834 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
835 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
836 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=138, starts_line=23, is_jump_target=False),
837 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=140, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700838 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=142, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300839 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=False),
840 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False),
841 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=176, argrepr='to 176', offset=148, starts_line=None, is_jump_target=False),
842 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=150, starts_line=None, is_jump_target=True),
843 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, starts_line=25, is_jump_target=True),
844 Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=170, argrepr='to 170', offset=154, starts_line=None, is_jump_target=False),
845 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=156, starts_line=None, is_jump_target=False),
846 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=158, starts_line=26, is_jump_target=False),
847 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=160, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700848 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=162, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300849 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
850 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
851 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=168, starts_line=None, is_jump_target=False),
852 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True),
853 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
854 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=False),
855 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=True),
856 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=178, starts_line=None, is_jump_target=False),
857 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=180, starts_line=28, is_jump_target=True),
858 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=182, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700859 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=184, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300860 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
861 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
862 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=190, starts_line=None, is_jump_target=False),
863 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000864]
865
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000866# One last piece of inspect fodder to check the default line number handling
867def simple(): pass
868expected_opinfo_simple = [
869 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300870 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=2, starts_line=None, is_jump_target=False)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000871]
872
873
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000874class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000875
876 def test_default_first_line(self):
877 actual = dis.get_instructions(simple)
878 self.assertEqual(list(actual), expected_opinfo_simple)
879
880 def test_first_line_set_to_None(self):
881 actual = dis.get_instructions(simple, first_line=None)
882 self.assertEqual(list(actual), expected_opinfo_simple)
883
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000884 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000885 actual = dis.get_instructions(outer, first_line=expected_outer_line)
886 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000887
888 def test_nested(self):
889 with captured_stdout():
890 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000891 actual = dis.get_instructions(f, first_line=expected_f_line)
892 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000893
894 def test_doubly_nested(self):
895 with captured_stdout():
896 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000897 actual = dis.get_instructions(inner, first_line=expected_inner_line)
898 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000899
900 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000901 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
902 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000903
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000904# get_instructions has its own tests above, so can rely on it to validate
905# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000906class BytecodeTests(unittest.TestCase):
907 def test_instantiation(self):
908 # Test with function, method, code string and code object
909 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000910 with self.subTest(obj=obj):
911 b = dis.Bytecode(obj)
912 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000913
914 self.assertRaises(TypeError, dis.Bytecode, object())
915
916 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000917 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
918 with self.subTest(obj=obj):
919 via_object = list(dis.Bytecode(obj))
920 via_generator = list(dis.get_instructions(obj))
921 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000922
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000923 def test_explicit_first_line(self):
924 actual = dis.Bytecode(outer, first_line=expected_outer_line)
925 self.assertEqual(list(actual), expected_opinfo_outer)
926
927 def test_source_line_in_disassembly(self):
928 # Use the line in the source code
929 actual = dis.Bytecode(simple).dis()[:3]
930 expected = "{:>3}".format(simple.__code__.co_firstlineno)
931 self.assertEqual(actual, expected)
932 # Use an explicit first line number
933 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
934 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000935
936 def test_info(self):
937 self.maxDiff = 1000
938 for x, expected in CodeInfoTests.test_pairs:
939 b = dis.Bytecode(x)
940 self.assertRegex(b.info(), expected)
941
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000942 def test_disassembled(self):
943 actual = dis.Bytecode(_f).dis()
944 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000945
Nick Coghlan50c48b82013-11-23 00:57:00 +1000946 def test_from_traceback(self):
947 tb = get_tb()
948 b = dis.Bytecode.from_traceback(tb)
949 while tb.tb_next: tb = tb.tb_next
950
951 self.assertEqual(b.current_offset, tb.tb_lasti)
952
953 def test_from_traceback_dis(self):
954 tb = get_tb()
955 b = dis.Bytecode.from_traceback(tb)
956 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000957
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000958if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600959 unittest.main()