blob: b9b5ac2667ee0a0e56cb70c0a9e78f7bf4fd47fe [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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030043 2 LOAD_CONST 1 (1)
44 4 COMPARE_OP 2 (==)
45 6 LOAD_FAST 0 (self)
46 8 STORE_ATTR 0 (x)
47 10 LOAD_CONST 0 (None)
48 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050049""" % (_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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030053 2 LOAD_CONST 1 (1)
54 4 COMPARE_OP 2 (==)
55 6 LOAD_FAST 0 (0)
56 8 STORE_ATTR 0 (0)
57 10 LOAD_CONST 0 (0)
58 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050059"""
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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030063 2 LOAD_CONST 1 (1)
64 4 COMPARE_OP 2 (==)
65 6 LOAD_FAST 0 (cls)
66 8 STORE_ATTR 0 (x)
67 10 LOAD_CONST 0 (None)
68 12 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030069""" % (_C.cm.__code__.co_firstlineno + 2,)
70
71dis_c_static_method = """\
72%3d 0 LOAD_FAST 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030073 2 LOAD_CONST 1 (1)
74 4 COMPARE_OP 2 (==)
75 6 STORE_FAST 0 (x)
76 8 LOAD_CONST 0 (None)
77 10 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030078""" % (_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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030098 2 LOAD_FAST 0 (a)
Victor Stinnerf9b760f2016-09-09 10:17:08 -070099 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300100 6 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000101
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300102%3d 8 LOAD_CONST 1 (1)
103 10 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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300110 2 LOAD_FAST 0 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700111 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300112 6 POP_TOP
113 8 LOAD_CONST 1 (1)
114 10 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -0500115"""
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 Storchakab0f80b02016-05-24 09:15:14 +0300124%3d 0 SETUP_LOOP 18 (to 20)
125 2 LOAD_GLOBAL 0 (range)
126 4 LOAD_CONST 1 (1)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000127
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300128%3d 6 LOAD_CONST 2 (10)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700129 8 CALL_FUNCTION 2
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300130 10 GET_ITER
131 >> 12 FOR_ITER 4 (to 18)
132 14 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000133
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300134%3d 16 JUMP_ABSOLUTE 12
135 >> 18 POP_BLOCK
136 >> 20 LOAD_CONST 0 (None)
137 22 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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300150 2 POP_JUMP_IF_TRUE 26
151 4 LOAD_GLOBAL 0 (AssertionError)
152 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
153 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
154 10 MAKE_FUNCTION 0
155 12 LOAD_FAST 0 (x)
156 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700157 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300158
159%3d 18 LOAD_CONST 4 (1)
160 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700161 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300162 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000163
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300164%3d >> 26 LOAD_CONST 0 (None)
165 28 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)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300174 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000175 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300176 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000177"""
178
Guido van Rossume7ba4952007-06-06 23:52:48 +0000179dis_module_expected_results = """\
180Disassembly of f:
181 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300182 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000183
184Disassembly of g:
185 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300186 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000187
188"""
189
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000190expr_str = "x + 1"
191
192dis_expr_str = """\
193 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300194 2 LOAD_CONST 0 (1)
195 4 BINARY_ADD
196 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000197"""
198
199simple_stmt_str = "x = x + 1"
200
201dis_simple_stmt_str = """\
202 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300203 2 LOAD_CONST 0 (1)
204 4 BINARY_ADD
205 6 STORE_NAME 0 (x)
206 8 LOAD_CONST 1 (None)
207 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000208"""
209
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700210annot_stmt_str = """\
211
212x: int = 1
213y: fun(1)
214lst[fun(0)]: int = 1
215"""
216# leading newline is for a reason (tests lineno)
217
218dis_annot_stmt_str = """\
219 2 0 SETUP_ANNOTATIONS
220 2 LOAD_CONST 0 (1)
221 4 STORE_NAME 0 (x)
222 6 LOAD_NAME 1 (int)
223 8 STORE_ANNOTATION 0 (x)
224
225 3 10 LOAD_NAME 2 (fun)
226 12 LOAD_CONST 0 (1)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700227 14 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700228 16 STORE_ANNOTATION 3 (y)
229
230 4 18 LOAD_CONST 0 (1)
231 20 LOAD_NAME 4 (lst)
232 22 LOAD_NAME 2 (fun)
233 24 LOAD_CONST 1 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700234 26 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700235 28 STORE_SUBSCR
236 30 LOAD_NAME 1 (int)
237 32 POP_TOP
238 34 LOAD_CONST 2 (None)
239 36 RETURN_VALUE
240"""
241
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000242compound_stmt_str = """\
243x = 0
244while 1:
245 x += 1"""
246# Trailing newline has been deliberately omitted
247
248dis_compound_stmt_str = """\
249 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300250 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000251
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300252 2 4 SETUP_LOOP 12 (to 18)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000253
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300254 3 >> 6 LOAD_NAME 0 (x)
255 8 LOAD_CONST 1 (1)
256 10 INPLACE_ADD
257 12 STORE_NAME 0 (x)
258 14 JUMP_ABSOLUTE 6
259 16 POP_BLOCK
260 >> 18 LOAD_CONST 2 (None)
261 20 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000262"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000263
Nick Coghlan50c48b82013-11-23 00:57:00 +1000264dis_traceback = """\
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300265%3d 0 SETUP_EXCEPT 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000266
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300267%3d 2 LOAD_CONST 1 (1)
268 4 LOAD_CONST 2 (0)
269 --> 6 BINARY_TRUE_DIVIDE
270 8 POP_TOP
271 10 POP_BLOCK
272 12 JUMP_FORWARD 40 (to 54)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000273
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300274%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000275 16 LOAD_GLOBAL 0 (Exception)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300276 18 COMPARE_OP 10 (exception match)
277 20 POP_JUMP_IF_FALSE 52
278 22 POP_TOP
279 24 STORE_FAST 0 (e)
280 26 POP_TOP
281 28 SETUP_FINALLY 12 (to 42)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300283%3d 30 LOAD_FAST 0 (e)
284 32 LOAD_ATTR 1 (__traceback__)
285 34 STORE_FAST 1 (tb)
286 36 POP_BLOCK
287 38 POP_EXCEPT
288 40 LOAD_CONST 0 (None)
289 >> 42 LOAD_CONST 0 (None)
290 44 STORE_FAST 0 (e)
291 46 DELETE_FAST 0 (e)
292 48 END_FINALLY
293 50 JUMP_FORWARD 2 (to 54)
294 >> 52 END_FINALLY
Nick Coghlan50c48b82013-11-23 00:57:00 +1000295
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300296%3d >> 54 LOAD_FAST 1 (tb)
297 56 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000298""" % (TRACEBACK_CODE.co_firstlineno + 1,
299 TRACEBACK_CODE.co_firstlineno + 2,
300 TRACEBACK_CODE.co_firstlineno + 3,
301 TRACEBACK_CODE.co_firstlineno + 4,
302 TRACEBACK_CODE.co_firstlineno + 5)
303
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300304def _fstring(a, b, c, d):
305 return f'{a} {b:4} {c!r} {d!r:4}'
306
307dis_fstring = """\
308%3d 0 LOAD_FAST 0 (a)
309 2 FORMAT_VALUE 0
310 4 LOAD_CONST 1 (' ')
311 6 LOAD_FAST 1 (b)
312 8 LOAD_CONST 2 ('4')
313 10 FORMAT_VALUE 4 (with format)
314 12 LOAD_CONST 1 (' ')
315 14 LOAD_FAST 2 (c)
316 16 FORMAT_VALUE 2 (repr)
317 18 LOAD_CONST 1 (' ')
318 20 LOAD_FAST 3 (d)
319 22 LOAD_CONST 2 ('4')
320 24 FORMAT_VALUE 6 (repr, with format)
321 26 BUILD_STRING 7
322 28 RETURN_VALUE
323""" % (_fstring.__code__.co_firstlineno + 1,)
324
Nick Coghlanefd5df92014-07-25 23:02:56 +1000325def _g(x):
326 yield x
327
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000328class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500329
330 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000331 # We want to test the default printing behaviour, not the file arg
332 output = io.StringIO()
333 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500334 if wrapper:
335 dis.dis(func)
336 else:
337 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000338 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500339
340 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000341 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500342
Zachary Warebb4b7c12013-12-26 09:55:24 -0600343 def strip_addresses(self, text):
344 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500345
346 def do_disassembly_test(self, func, expected):
Zachary Warebb4b7c12013-12-26 09:55:24 -0600347 got = self.get_disassembly(func)
348 if got != expected:
349 got = self.strip_addresses(got)
350 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000351
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000352 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500353 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000354 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
355 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000356
357 def test_opname(self):
358 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
359
360 def test_boundaries(self):
361 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
362 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
363
364 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000365 self.do_disassembly_test(_f, dis_f)
366
367 def test_bug_708901(self):
368 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000369
Neal Norwitz51abbc72005-12-18 07:06:23 +0000370 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000371 # This one is checking bytecodes generated for an `assert` statement,
372 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600373 if not __debug__:
374 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600375
Zachary Waree80e8062013-12-26 09:53:49 -0600376 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000377
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000378 def test_big_linenos(self):
379 def func(count):
380 namespace = {}
381 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000382 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000383 return namespace['foo']
384
385 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000386 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000387 expected = _BIG_LINENO_FORMAT % (i + 2)
388 self.do_disassembly_test(func(i), expected)
389
390 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000391 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000392 expected = _BIG_LINENO_FORMAT % (i + 2)
393 self.do_disassembly_test(func(i), expected)
394
Guido van Rossume7ba4952007-06-06 23:52:48 +0000395 from test import dis_module
396 self.do_disassembly_test(dis_module, dis_module_expected_results)
397
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000398 def test_disassemble_str(self):
399 self.do_disassembly_test(expr_str, dis_expr_str)
400 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700401 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000402 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
403
Benjamin Petersond6afe722011-03-15 14:44:52 -0500404 def test_disassemble_bytes(self):
405 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
406
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300407 def test_disassemble_class(self):
408 self.do_disassembly_test(_C, dis_c)
409
410 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500411 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
412
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300413 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500414 method_bytecode = _C(1).__init__.__code__.co_code
415 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
416
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300417 def test_disassemble_static_method(self):
418 self.do_disassembly_test(_C.sm, dis_c_static_method)
419
420 def test_disassemble_class_method(self):
421 self.do_disassembly_test(_C.cm, dis_c_class_method)
422
Nick Coghlanefd5df92014-07-25 23:02:56 +1000423 def test_disassemble_generator(self):
424 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
425 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
426 self.assertEqual(gen_disas, gen_func_disas)
427
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300428 def test_disassemble_fstring(self):
429 self.do_disassembly_test(_fstring, dis_fstring)
430
Benjamin Petersond6afe722011-03-15 14:44:52 -0500431 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500432 try:
433 del sys.last_traceback
434 except AttributeError:
435 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500436 self.assertRaises(RuntimeError, dis.dis, None)
437
Benjamin Petersond6afe722011-03-15 14:44:52 -0500438 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500439 try:
440 del sys.last_traceback
441 except AttributeError:
442 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500443
444 try:
445 1/0
446 except Exception as e:
447 tb = e.__traceback__
448 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500449
450 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
451 self.do_disassembly_test(None, tb_dis)
452
453 def test_dis_object(self):
454 self.assertRaises(TypeError, dis.dis, object())
455
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000456class DisWithFileTests(DisTests):
457
458 # Run the tests again, using the file arg instead of print
459 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000460 output = io.StringIO()
461 if wrapper:
462 dis.dis(func, file=output)
463 else:
464 dis.disassemble(func, lasti, file=output)
465 return output.getvalue()
466
467
468
Nick Coghlaneae2da12010-08-17 08:03:36 +0000469code_info_code_info = """\
470Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000471Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000472Argument count: 1
473Kw-only arguments: 0
474Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000475Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000476Flags: OPTIMIZED, NEWLOCALS, NOFREE
477Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000478 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000479Names:
480 0: _format_code_info
481 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000482Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000483 0: x""" % (('Formatted details of methods, functions, or code.',)
484 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000485
486@staticmethod
487def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
488 def f(c=c):
489 print(x, y, z, c, d, e, f)
490 yield x, y, z, c, d, e, f
491
Nick Coghlaneae2da12010-08-17 08:03:36 +0000492code_info_tricky = """\
493Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000494Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000495Argument count: 3
496Kw-only arguments: 3
497Number of locals: 8
498Stack size: 7
499Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
500Constants:
501 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000502 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100503 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000504Variable names:
505 0: x
506 1: y
507 2: z
508 3: c
509 4: d
510 5: e
511 6: args
512 7: kwds
513Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100514 0: [edfxyz]
515 1: [edfxyz]
516 2: [edfxyz]
517 3: [edfxyz]
518 4: [edfxyz]
519 5: [edfxyz]"""
520# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000521
522co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000523
524code_info_tricky_nested_f = """\
525Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000526Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000527Argument count: 1
528Kw-only arguments: 0
529Number of locals: 1
530Stack size: 8
531Flags: OPTIMIZED, NEWLOCALS, NESTED
532Constants:
533 0: None
534Names:
535 0: print
536Variable names:
537 0: c
538Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100539 0: [edfxyz]
540 1: [edfxyz]
541 2: [edfxyz]
542 3: [edfxyz]
543 4: [edfxyz]
544 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000545
546code_info_expr_str = """\
547Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000548Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000549Argument count: 0
550Kw-only arguments: 0
551Number of locals: 0
552Stack size: 2
553Flags: NOFREE
554Constants:
555 0: 1
556Names:
557 0: x"""
558
559code_info_simple_stmt_str = """\
560Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000561Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000562Argument count: 0
563Kw-only arguments: 0
564Number of locals: 0
565Stack size: 2
566Flags: NOFREE
567Constants:
568 0: 1
569 1: None
570Names:
571 0: x"""
572
573code_info_compound_stmt_str = """\
574Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000575Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000576Argument count: 0
577Kw-only arguments: 0
578Number of locals: 0
579Stack size: 2
580Flags: NOFREE
581Constants:
582 0: 0
583 1: 1
584 2: None
585Names:
586 0: x"""
587
Yury Selivanov75445082015-05-11 22:57:16 -0400588
589async def async_def():
590 await 1
591 async for a in b: pass
592 async with c as d: pass
593
594code_info_async_def = """\
595Name: async_def
596Filename: (.*)
597Argument count: 0
598Kw-only arguments: 0
599Number of locals: 2
600Stack size: 17
Yury Selivanoveb636452016-09-08 22:01:51 -0700601Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400602Constants:
603 0: None
604 1: 1"""
605
Nick Coghlaneae2da12010-08-17 08:03:36 +0000606class CodeInfoTests(unittest.TestCase):
607 test_pairs = [
608 (dis.code_info, code_info_code_info),
609 (tricky, code_info_tricky),
610 (co_tricky_nested_f, code_info_tricky_nested_f),
611 (expr_str, code_info_expr_str),
612 (simple_stmt_str, code_info_simple_stmt_str),
613 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400614 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000615 ]
616
617 def test_code_info(self):
618 self.maxDiff = 1000
619 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000620 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000621
622 def test_show_code(self):
623 self.maxDiff = 1000
624 for x, expected in self.test_pairs:
625 with captured_stdout() as output:
626 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000627 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000628 output = io.StringIO()
629 dis.show_code(x, file=output)
630 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000631
Benjamin Petersond6afe722011-03-15 14:44:52 -0500632 def test_code_info_object(self):
633 self.assertRaises(TypeError, dis.code_info, object())
634
635 def test_pretty_flags_no_flags(self):
636 self.assertEqual(dis.pretty_flags(0), '0x0')
637
638
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000639# Fodder for instruction introspection tests
640# Editing any of these may require recalculating the expected output
641def outer(a=1, b=2):
642 def f(c=3, d=4):
643 def inner(e=5, f=6):
644 print(a, b, c, d, e, f)
645 print(a, b, c, d)
646 return inner
647 print(a, b, '', 1, [], {}, "Hello world!")
648 return f
649
650def jumpy():
651 # This won't actually run (but that's OK, we only disassemble it)
652 for i in range(10):
653 print(i)
654 if i < 4:
655 continue
656 if i > 6:
657 break
658 else:
659 print("I can haz else clause?")
660 while i:
661 print(i)
662 i -= 1
663 if i > 6:
664 continue
665 if i < 4:
666 break
667 else:
668 print("Who let lolcatz into this test suite?")
669 try:
670 1 / 0
671 except ZeroDivisionError:
672 print("Here we go, here we go, here we go...")
673 else:
674 with i as dodgy:
675 print("Never reach this")
676 finally:
677 print("OK, now we're done")
678
679# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000680expected_outer_line = 1
681_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000682code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000683expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000684code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000685expected_inner_line = code_object_inner.co_firstlineno - _line_offset
686expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000687
688# The following lines are useful to regenerate the expected results after
689# either the fodder is modified or the bytecode generation changes
690# After regeneration, update the references to code_object_f and
691# code_object_inner before rerunning the tests
692
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000693#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000694#print('expected_opinfo_outer = [\n ',
695 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200696#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000697#print('expected_opinfo_f = [\n ',
698 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200699#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000700#print('expected_opinfo_inner = [\n ',
701 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000702#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000703#print('expected_opinfo_jumpy = [\n ',
704 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
705
706
707Instruction = dis.Instruction
708expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300709 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
710 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
711 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
712 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
713 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),
714 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),
715 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False),
716 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
717 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
718 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
719 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
720 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
721 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
722 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
723 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
724 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 -0700725 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 +0300726 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
727 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
728 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 +1000729]
730
731expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300732 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
733 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
734 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
735 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
736 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
737 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
738 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),
739 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),
740 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False),
741 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
742 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
743 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
744 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
745 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
746 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 -0700747 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 +0300748 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
749 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
750 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 +1000751]
752
753expected_opinfo_inner = [
754 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 +0300755 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
756 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
757 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
758 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
759 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
760 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 -0700761 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 +0300762 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
763 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
764 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 +1000765]
766
767expected_opinfo_jumpy = [
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300768 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=54, argrepr='to 54', offset=0, starts_line=3, is_jump_target=False),
769 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=2, starts_line=None, is_jump_target=False),
770 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 -0700771 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 +0300772 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False),
773 Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=44, argrepr='to 44', offset=10, starts_line=None, is_jump_target=True),
774 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=12, starts_line=None, is_jump_target=False),
775 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=14, starts_line=4, is_jump_target=False),
776 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 -0700777 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 +0300778 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
779 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=5, is_jump_target=False),
780 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=24, starts_line=None, is_jump_target=False),
781 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=26, starts_line=None, is_jump_target=False),
782 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=32, argval=32, argrepr='', offset=28, starts_line=None, is_jump_target=False),
783 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=30, starts_line=6, is_jump_target=False),
784 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=32, starts_line=7, is_jump_target=True),
785 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=34, starts_line=None, is_jump_target=False),
786 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=36, starts_line=None, is_jump_target=False),
787 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=10, argval=10, argrepr='', offset=38, starts_line=None, is_jump_target=False),
788 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=40, starts_line=8, is_jump_target=False),
789 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=42, starts_line=None, is_jump_target=False),
790 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=44, starts_line=None, is_jump_target=True),
791 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=46, starts_line=10, is_jump_target=False),
792 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 -0700793 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 +0300794 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=52, starts_line=None, is_jump_target=False),
795 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=108, argrepr='to 108', offset=54, starts_line=11, is_jump_target=True),
796 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=True),
797 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=98, argval=98, argrepr='', offset=58, starts_line=None, is_jump_target=False),
798 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=60, starts_line=12, is_jump_target=False),
799 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 -0700800 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 +0300801 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
802 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=13, is_jump_target=False),
803 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=70, starts_line=None, is_jump_target=False),
804 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=72, starts_line=None, is_jump_target=False),
805 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=False),
806 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=76, starts_line=14, is_jump_target=False),
807 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=78, starts_line=None, is_jump_target=False),
808 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=80, starts_line=None, is_jump_target=False),
809 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=86, argval=86, argrepr='', offset=82, starts_line=None, is_jump_target=False),
810 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=84, starts_line=15, is_jump_target=False),
811 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=86, starts_line=16, is_jump_target=True),
812 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=88, starts_line=None, is_jump_target=False),
813 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=90, starts_line=None, is_jump_target=False),
814 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=56, argval=56, argrepr='', offset=92, starts_line=None, is_jump_target=False),
815 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=94, starts_line=17, is_jump_target=False),
816 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=96, starts_line=None, is_jump_target=False),
817 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=98, starts_line=None, is_jump_target=True),
818 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=100, starts_line=19, is_jump_target=False),
819 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 -0700820 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 +0300821 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
822 Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=180, argrepr='to 180', offset=108, starts_line=20, is_jump_target=True),
823 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=124, argrepr='to 124', offset=110, starts_line=None, is_jump_target=False),
824 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=112, starts_line=21, is_jump_target=False),
825 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=114, starts_line=None, is_jump_target=False),
826 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False),
827 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=118, starts_line=None, is_jump_target=False),
828 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
829 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=152, argrepr='to 152', offset=122, starts_line=None, is_jump_target=False),
830 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=124, starts_line=22, is_jump_target=True),
831 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=126, starts_line=None, is_jump_target=False),
832 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=128, starts_line=None, is_jump_target=False),
833 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=150, argval=150, argrepr='', offset=130, starts_line=None, is_jump_target=False),
834 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
835 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
836 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
837 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=138, starts_line=23, is_jump_target=False),
838 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 -0700839 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 +0300840 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=False),
841 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False),
842 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=176, argrepr='to 176', offset=148, starts_line=None, is_jump_target=False),
843 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=150, starts_line=None, is_jump_target=True),
844 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, starts_line=25, is_jump_target=True),
845 Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=170, argrepr='to 170', offset=154, starts_line=None, is_jump_target=False),
846 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=156, starts_line=None, is_jump_target=False),
847 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=158, starts_line=26, is_jump_target=False),
848 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 -0700849 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 +0300850 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
851 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
852 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=168, starts_line=None, is_jump_target=False),
853 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True),
854 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
855 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=False),
856 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=True),
857 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=178, starts_line=None, is_jump_target=False),
858 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=180, starts_line=28, is_jump_target=True),
859 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 -0700860 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 +0300861 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
862 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
863 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=190, starts_line=None, is_jump_target=False),
864 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 +1000865]
866
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000867# One last piece of inspect fodder to check the default line number handling
868def simple(): pass
869expected_opinfo_simple = [
870 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 +0300871 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 +1000872]
873
874
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000875class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000876
877 def test_default_first_line(self):
878 actual = dis.get_instructions(simple)
879 self.assertEqual(list(actual), expected_opinfo_simple)
880
881 def test_first_line_set_to_None(self):
882 actual = dis.get_instructions(simple, first_line=None)
883 self.assertEqual(list(actual), expected_opinfo_simple)
884
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000885 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000886 actual = dis.get_instructions(outer, first_line=expected_outer_line)
887 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000888
889 def test_nested(self):
890 with captured_stdout():
891 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000892 actual = dis.get_instructions(f, first_line=expected_f_line)
893 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000894
895 def test_doubly_nested(self):
896 with captured_stdout():
897 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000898 actual = dis.get_instructions(inner, first_line=expected_inner_line)
899 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000900
901 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000902 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
903 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000904
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000905# get_instructions has its own tests above, so can rely on it to validate
906# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000907class BytecodeTests(unittest.TestCase):
908 def test_instantiation(self):
909 # Test with function, method, code string and code object
910 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000911 with self.subTest(obj=obj):
912 b = dis.Bytecode(obj)
913 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000914
915 self.assertRaises(TypeError, dis.Bytecode, object())
916
917 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000918 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
919 with self.subTest(obj=obj):
920 via_object = list(dis.Bytecode(obj))
921 via_generator = list(dis.get_instructions(obj))
922 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000923
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000924 def test_explicit_first_line(self):
925 actual = dis.Bytecode(outer, first_line=expected_outer_line)
926 self.assertEqual(list(actual), expected_opinfo_outer)
927
928 def test_source_line_in_disassembly(self):
929 # Use the line in the source code
930 actual = dis.Bytecode(simple).dis()[:3]
931 expected = "{:>3}".format(simple.__code__.co_firstlineno)
932 self.assertEqual(actual, expected)
933 # Use an explicit first line number
934 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
935 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000936
937 def test_info(self):
938 self.maxDiff = 1000
939 for x, expected in CodeInfoTests.test_pairs:
940 b = dis.Bytecode(x)
941 self.assertRegex(b.info(), expected)
942
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000943 def test_disassembled(self):
944 actual = dis.Bytecode(_f).dis()
945 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000946
Nick Coghlan50c48b82013-11-23 00:57:00 +1000947 def test_from_traceback(self):
948 tb = get_tb()
949 b = dis.Bytecode.from_traceback(tb)
950 while tb.tb_next: tb = tb.tb_next
951
952 self.assertEqual(b.current_offset, tb.tb_lasti)
953
954 def test_from_traceback_dis(self):
955 tb = get_tb()
956 b = dis.Bytecode.from_traceback(tb)
957 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000958
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000959if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -0600960 unittest.main()