blob: 254b317e4982437bd36118fa9a2837ac36a2823d [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
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300178_BIG_LINENO_FORMAT2 = """\
179%4d 0 LOAD_GLOBAL 0 (spam)
180 2 POP_TOP
181 4 LOAD_CONST 0 (None)
182 6 RETURN_VALUE
183"""
184
Guido van Rossume7ba4952007-06-06 23:52:48 +0000185dis_module_expected_results = """\
186Disassembly of f:
187 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300188 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000189
190Disassembly of g:
191 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300192 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000193
194"""
195
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000196expr_str = "x + 1"
197
198dis_expr_str = """\
199 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300200 2 LOAD_CONST 0 (1)
201 4 BINARY_ADD
202 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000203"""
204
205simple_stmt_str = "x = x + 1"
206
207dis_simple_stmt_str = """\
208 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300209 2 LOAD_CONST 0 (1)
210 4 BINARY_ADD
211 6 STORE_NAME 0 (x)
212 8 LOAD_CONST 1 (None)
213 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000214"""
215
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700216annot_stmt_str = """\
217
218x: int = 1
219y: fun(1)
220lst[fun(0)]: int = 1
221"""
222# leading newline is for a reason (tests lineno)
223
224dis_annot_stmt_str = """\
225 2 0 SETUP_ANNOTATIONS
226 2 LOAD_CONST 0 (1)
227 4 STORE_NAME 0 (x)
228 6 LOAD_NAME 1 (int)
229 8 STORE_ANNOTATION 0 (x)
230
231 3 10 LOAD_NAME 2 (fun)
232 12 LOAD_CONST 0 (1)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700233 14 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700234 16 STORE_ANNOTATION 3 (y)
235
236 4 18 LOAD_CONST 0 (1)
237 20 LOAD_NAME 4 (lst)
238 22 LOAD_NAME 2 (fun)
239 24 LOAD_CONST 1 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700240 26 CALL_FUNCTION 1
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700241 28 STORE_SUBSCR
242 30 LOAD_NAME 1 (int)
243 32 POP_TOP
244 34 LOAD_CONST 2 (None)
245 36 RETURN_VALUE
246"""
247
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000248compound_stmt_str = """\
249x = 0
250while 1:
251 x += 1"""
252# Trailing newline has been deliberately omitted
253
254dis_compound_stmt_str = """\
255 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300256 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000257
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300258 2 4 SETUP_LOOP 12 (to 18)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000259
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300260 3 >> 6 LOAD_NAME 0 (x)
261 8 LOAD_CONST 1 (1)
262 10 INPLACE_ADD
263 12 STORE_NAME 0 (x)
264 14 JUMP_ABSOLUTE 6
265 16 POP_BLOCK
266 >> 18 LOAD_CONST 2 (None)
267 20 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000268"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000269
Nick Coghlan50c48b82013-11-23 00:57:00 +1000270dis_traceback = """\
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300271%3d 0 SETUP_EXCEPT 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000272
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300273%3d 2 LOAD_CONST 1 (1)
274 4 LOAD_CONST 2 (0)
275 --> 6 BINARY_TRUE_DIVIDE
276 8 POP_TOP
277 10 POP_BLOCK
278 12 JUMP_FORWARD 40 (to 54)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000279
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300280%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000281 16 LOAD_GLOBAL 0 (Exception)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300282 18 COMPARE_OP 10 (exception match)
283 20 POP_JUMP_IF_FALSE 52
284 22 POP_TOP
285 24 STORE_FAST 0 (e)
286 26 POP_TOP
287 28 SETUP_FINALLY 12 (to 42)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000288
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300289%3d 30 LOAD_FAST 0 (e)
290 32 LOAD_ATTR 1 (__traceback__)
291 34 STORE_FAST 1 (tb)
292 36 POP_BLOCK
293 38 POP_EXCEPT
294 40 LOAD_CONST 0 (None)
295 >> 42 LOAD_CONST 0 (None)
296 44 STORE_FAST 0 (e)
297 46 DELETE_FAST 0 (e)
298 48 END_FINALLY
299 50 JUMP_FORWARD 2 (to 54)
300 >> 52 END_FINALLY
Nick Coghlan50c48b82013-11-23 00:57:00 +1000301
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300302%3d >> 54 LOAD_FAST 1 (tb)
303 56 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000304""" % (TRACEBACK_CODE.co_firstlineno + 1,
305 TRACEBACK_CODE.co_firstlineno + 2,
306 TRACEBACK_CODE.co_firstlineno + 3,
307 TRACEBACK_CODE.co_firstlineno + 4,
308 TRACEBACK_CODE.co_firstlineno + 5)
309
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300310def _fstring(a, b, c, d):
311 return f'{a} {b:4} {c!r} {d!r:4}'
312
313dis_fstring = """\
314%3d 0 LOAD_FAST 0 (a)
315 2 FORMAT_VALUE 0
316 4 LOAD_CONST 1 (' ')
317 6 LOAD_FAST 1 (b)
318 8 LOAD_CONST 2 ('4')
319 10 FORMAT_VALUE 4 (with format)
320 12 LOAD_CONST 1 (' ')
321 14 LOAD_FAST 2 (c)
322 16 FORMAT_VALUE 2 (repr)
323 18 LOAD_CONST 1 (' ')
324 20 LOAD_FAST 3 (d)
325 22 LOAD_CONST 2 ('4')
326 24 FORMAT_VALUE 6 (repr, with format)
327 26 BUILD_STRING 7
328 28 RETURN_VALUE
329""" % (_fstring.__code__.co_firstlineno + 1,)
330
Nick Coghlanefd5df92014-07-25 23:02:56 +1000331def _g(x):
332 yield x
333
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300334def _h(y):
335 def foo(x):
336 '''funcdoc'''
337 return [x + z for z in y]
338 return foo
339
340dis_nested_0 = """\
341%3d 0 LOAD_CLOSURE 0 (y)
342 2 BUILD_TUPLE 1
343 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
344 6 LOAD_CONST 2 ('_h.<locals>.foo')
345 8 MAKE_FUNCTION 8
346 10 STORE_FAST 1 (foo)
347
348%3d 12 LOAD_FAST 1 (foo)
349 14 RETURN_VALUE
350""" % (_h.__code__.co_firstlineno + 1,
351 __file__,
352 _h.__code__.co_firstlineno + 1,
353 _h.__code__.co_firstlineno + 4,
354)
355
356dis_nested_1 = """%s
357Disassembly of <code object foo at 0x..., file "%s", line %d>:
358%3d 0 LOAD_CLOSURE 0 (x)
359 2 BUILD_TUPLE 1
360 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
361 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
362 8 MAKE_FUNCTION 8
363 10 LOAD_DEREF 1 (y)
364 12 GET_ITER
365 14 CALL_FUNCTION 1
366 16 RETURN_VALUE
367""" % (dis_nested_0,
368 __file__,
369 _h.__code__.co_firstlineno + 1,
370 _h.__code__.co_firstlineno + 3,
371 __file__,
372 _h.__code__.co_firstlineno + 3,
373)
374
375dis_nested_2 = """%s
376Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
377%3d 0 BUILD_LIST 0
378 2 LOAD_FAST 0 (.0)
379 >> 4 FOR_ITER 12 (to 18)
380 6 STORE_FAST 1 (z)
381 8 LOAD_DEREF 0 (x)
382 10 LOAD_FAST 1 (z)
383 12 BINARY_ADD
384 14 LIST_APPEND 2
385 16 JUMP_ABSOLUTE 4
386 >> 18 RETURN_VALUE
387""" % (dis_nested_1,
388 __file__,
389 _h.__code__.co_firstlineno + 3,
390 _h.__code__.co_firstlineno + 3,
391)
392
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000393class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500394
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300395 maxDiff = None
396
397 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000398 # We want to test the default printing behaviour, not the file arg
399 output = io.StringIO()
400 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500401 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300402 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500403 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300404 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000405 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500406
407 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000408 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500409
Zachary Warebb4b7c12013-12-26 09:55:24 -0600410 def strip_addresses(self, text):
411 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500412
413 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300414 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600415 if got != expected:
416 got = self.strip_addresses(got)
417 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000418
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000419 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500420 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000421 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
422 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000423
424 def test_opname(self):
425 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
426
427 def test_boundaries(self):
428 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
429 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
430
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300431 def test_widths(self):
432 for opcode, opname in enumerate(dis.opname):
433 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
434 'BUILD_TUPLE_UNPACK_WITH_CALL'):
435 continue
436 with self.subTest(opname=opname):
437 width = dis._OPNAME_WIDTH
438 if opcode < dis.HAVE_ARGUMENT:
439 width += 1 + dis._OPARG_WIDTH
440 self.assertLessEqual(len(opname), width)
441
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000442 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000443 self.do_disassembly_test(_f, dis_f)
444
445 def test_bug_708901(self):
446 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000447
Neal Norwitz51abbc72005-12-18 07:06:23 +0000448 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000449 # This one is checking bytecodes generated for an `assert` statement,
450 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600451 if not __debug__:
452 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600453
Zachary Waree80e8062013-12-26 09:53:49 -0600454 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000455
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000456 def test_big_linenos(self):
457 def func(count):
458 namespace = {}
459 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000460 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000461 return namespace['foo']
462
463 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000464 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000465 expected = _BIG_LINENO_FORMAT % (i + 2)
466 self.do_disassembly_test(func(i), expected)
467
468 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300469 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000470 expected = _BIG_LINENO_FORMAT % (i + 2)
471 self.do_disassembly_test(func(i), expected)
472
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300473 for i in range(1000, 5000, 10):
474 expected = _BIG_LINENO_FORMAT2 % (i + 2)
475 self.do_disassembly_test(func(i), expected)
476
Guido van Rossume7ba4952007-06-06 23:52:48 +0000477 from test import dis_module
478 self.do_disassembly_test(dis_module, dis_module_expected_results)
479
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300480 def test_big_offsets(self):
481 def func(count):
482 namespace = {}
483 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
484 exec(func, namespace)
485 return namespace['foo']
486
487 def expected(count, w):
488 s = ['''\
489 %*d LOAD_FAST 0 (x)
490 %*d LOAD_CONST 1 (1)
491 %*d BINARY_ADD
492 %*d STORE_FAST 0 (x)
493''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
494 for i in range(count)]
495 s += ['''\
496
497 3 %*d LOAD_FAST 0 (x)
498 %*d RETURN_VALUE
499''' % (w, 8*count, w, 8*count + 2)]
500 s[0] = ' 2' + s[0][3:]
501 return ''.join(s)
502
503 for i in range(1, 5):
504 self.do_disassembly_test(func(i), expected(i, 4))
505 self.do_disassembly_test(func(1249), expected(1249, 4))
506 self.do_disassembly_test(func(1250), expected(1250, 5))
507
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000508 def test_disassemble_str(self):
509 self.do_disassembly_test(expr_str, dis_expr_str)
510 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700511 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000512 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
513
Benjamin Petersond6afe722011-03-15 14:44:52 -0500514 def test_disassemble_bytes(self):
515 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
516
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300517 def test_disassemble_class(self):
518 self.do_disassembly_test(_C, dis_c)
519
520 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500521 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
522
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300523 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500524 method_bytecode = _C(1).__init__.__code__.co_code
525 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
526
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300527 def test_disassemble_static_method(self):
528 self.do_disassembly_test(_C.sm, dis_c_static_method)
529
530 def test_disassemble_class_method(self):
531 self.do_disassembly_test(_C.cm, dis_c_class_method)
532
Nick Coghlanefd5df92014-07-25 23:02:56 +1000533 def test_disassemble_generator(self):
534 gen_func_disas = self.get_disassembly(_g) # Disassemble generator function
535 gen_disas = self.get_disassembly(_g(1)) # Disassemble generator itself
536 self.assertEqual(gen_disas, gen_func_disas)
537
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300538 def test_disassemble_fstring(self):
539 self.do_disassembly_test(_fstring, dis_fstring)
540
Benjamin Petersond6afe722011-03-15 14:44:52 -0500541 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500542 try:
543 del sys.last_traceback
544 except AttributeError:
545 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500546 self.assertRaises(RuntimeError, dis.dis, None)
547
Benjamin Petersond6afe722011-03-15 14:44:52 -0500548 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500549 try:
550 del sys.last_traceback
551 except AttributeError:
552 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500553
554 try:
555 1/0
556 except Exception as e:
557 tb = e.__traceback__
558 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500559
560 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
561 self.do_disassembly_test(None, tb_dis)
562
563 def test_dis_object(self):
564 self.assertRaises(TypeError, dis.dis, object())
565
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300566 def test_disassemble_recursive(self):
567 def check(expected, **kwargs):
568 dis = self.get_disassembly(_h, **kwargs)
569 dis = self.strip_addresses(dis)
570 self.assertEqual(dis, expected)
571
572 check(dis_nested_0, depth=0)
573 check(dis_nested_1, depth=1)
574 check(dis_nested_2, depth=2)
575 check(dis_nested_2, depth=3)
576 check(dis_nested_2, depth=None)
577 check(dis_nested_2)
578
579
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000580class DisWithFileTests(DisTests):
581
582 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300583 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000584 output = io.StringIO()
585 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300586 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000587 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300588 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000589 return output.getvalue()
590
591
592
Nick Coghlaneae2da12010-08-17 08:03:36 +0000593code_info_code_info = """\
594Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000595Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000596Argument count: 1
597Kw-only arguments: 0
598Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000599Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000600Flags: OPTIMIZED, NEWLOCALS, NOFREE
601Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000602 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000603Names:
604 0: _format_code_info
605 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000606Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000607 0: x""" % (('Formatted details of methods, functions, or code.',)
608 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000609
610@staticmethod
611def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
612 def f(c=c):
613 print(x, y, z, c, d, e, f)
614 yield x, y, z, c, d, e, f
615
Nick Coghlaneae2da12010-08-17 08:03:36 +0000616code_info_tricky = """\
617Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000618Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000619Argument count: 3
620Kw-only arguments: 3
621Number of locals: 8
622Stack size: 7
623Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
624Constants:
625 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000626 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100627 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000628Variable names:
629 0: x
630 1: y
631 2: z
632 3: c
633 4: d
634 5: e
635 6: args
636 7: kwds
637Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100638 0: [edfxyz]
639 1: [edfxyz]
640 2: [edfxyz]
641 3: [edfxyz]
642 4: [edfxyz]
643 5: [edfxyz]"""
644# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000645
646co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000647
648code_info_tricky_nested_f = """\
649Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000650Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000651Argument count: 1
652Kw-only arguments: 0
653Number of locals: 1
654Stack size: 8
655Flags: OPTIMIZED, NEWLOCALS, NESTED
656Constants:
657 0: None
658Names:
659 0: print
660Variable names:
661 0: c
662Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100663 0: [edfxyz]
664 1: [edfxyz]
665 2: [edfxyz]
666 3: [edfxyz]
667 4: [edfxyz]
668 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000669
670code_info_expr_str = """\
671Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000672Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000673Argument count: 0
674Kw-only arguments: 0
675Number of locals: 0
676Stack size: 2
677Flags: NOFREE
678Constants:
679 0: 1
680Names:
681 0: x"""
682
683code_info_simple_stmt_str = """\
684Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000685Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000686Argument count: 0
687Kw-only arguments: 0
688Number of locals: 0
689Stack size: 2
690Flags: NOFREE
691Constants:
692 0: 1
693 1: None
694Names:
695 0: x"""
696
697code_info_compound_stmt_str = """\
698Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000699Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000700Argument count: 0
701Kw-only arguments: 0
702Number of locals: 0
703Stack size: 2
704Flags: NOFREE
705Constants:
706 0: 0
707 1: 1
708 2: None
709Names:
710 0: x"""
711
Yury Selivanov75445082015-05-11 22:57:16 -0400712
713async def async_def():
714 await 1
715 async for a in b: pass
716 async with c as d: pass
717
718code_info_async_def = """\
719Name: async_def
720Filename: (.*)
721Argument count: 0
722Kw-only arguments: 0
723Number of locals: 2
724Stack size: 17
Yury Selivanoveb636452016-09-08 22:01:51 -0700725Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400726Constants:
727 0: None
728 1: 1"""
729
Nick Coghlaneae2da12010-08-17 08:03:36 +0000730class CodeInfoTests(unittest.TestCase):
731 test_pairs = [
732 (dis.code_info, code_info_code_info),
733 (tricky, code_info_tricky),
734 (co_tricky_nested_f, code_info_tricky_nested_f),
735 (expr_str, code_info_expr_str),
736 (simple_stmt_str, code_info_simple_stmt_str),
737 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400738 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000739 ]
740
741 def test_code_info(self):
742 self.maxDiff = 1000
743 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000744 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000745
746 def test_show_code(self):
747 self.maxDiff = 1000
748 for x, expected in self.test_pairs:
749 with captured_stdout() as output:
750 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000751 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000752 output = io.StringIO()
753 dis.show_code(x, file=output)
754 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000755
Benjamin Petersond6afe722011-03-15 14:44:52 -0500756 def test_code_info_object(self):
757 self.assertRaises(TypeError, dis.code_info, object())
758
759 def test_pretty_flags_no_flags(self):
760 self.assertEqual(dis.pretty_flags(0), '0x0')
761
762
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000763# Fodder for instruction introspection tests
764# Editing any of these may require recalculating the expected output
765def outer(a=1, b=2):
766 def f(c=3, d=4):
767 def inner(e=5, f=6):
768 print(a, b, c, d, e, f)
769 print(a, b, c, d)
770 return inner
771 print(a, b, '', 1, [], {}, "Hello world!")
772 return f
773
774def jumpy():
775 # This won't actually run (but that's OK, we only disassemble it)
776 for i in range(10):
777 print(i)
778 if i < 4:
779 continue
780 if i > 6:
781 break
782 else:
783 print("I can haz else clause?")
784 while i:
785 print(i)
786 i -= 1
787 if i > 6:
788 continue
789 if i < 4:
790 break
791 else:
792 print("Who let lolcatz into this test suite?")
793 try:
794 1 / 0
795 except ZeroDivisionError:
796 print("Here we go, here we go, here we go...")
797 else:
798 with i as dodgy:
799 print("Never reach this")
800 finally:
801 print("OK, now we're done")
802
803# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000804expected_outer_line = 1
805_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000806code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000807expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000808code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000809expected_inner_line = code_object_inner.co_firstlineno - _line_offset
810expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000811
812# The following lines are useful to regenerate the expected results after
813# either the fodder is modified or the bytecode generation changes
814# After regeneration, update the references to code_object_f and
815# code_object_inner before rerunning the tests
816
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000817#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000818#print('expected_opinfo_outer = [\n ',
819 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200820#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000821#print('expected_opinfo_f = [\n ',
822 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200823#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000824#print('expected_opinfo_inner = [\n ',
825 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000826#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000827#print('expected_opinfo_jumpy = [\n ',
828 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
829
830
831Instruction = dis.Instruction
832expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300833 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
834 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
835 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
836 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
837 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),
838 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),
839 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=12, starts_line=None, is_jump_target=False),
840 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
841 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
842 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
843 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
844 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
845 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
846 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
847 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
848 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 -0700849 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 +0300850 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
851 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
852 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 +1000853]
854
855expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300856 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
857 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
858 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
859 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
860 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
861 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
862 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),
863 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),
864 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='', offset=16, starts_line=None, is_jump_target=False),
865 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
866 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
867 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
868 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
869 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
870 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 -0700871 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 +0300872 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
873 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
874 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 +1000875]
876
877expected_opinfo_inner = [
878 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 +0300879 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
880 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
881 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
882 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
883 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
884 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 -0700885 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 +0300886 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
887 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
888 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 +1000889]
890
891expected_opinfo_jumpy = [
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300892 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=54, argrepr='to 54', offset=0, starts_line=3, is_jump_target=False),
893 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=2, starts_line=None, is_jump_target=False),
894 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 -0700895 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 +0300896 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=8, starts_line=None, is_jump_target=False),
897 Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=44, argrepr='to 44', offset=10, starts_line=None, is_jump_target=True),
898 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=12, starts_line=None, is_jump_target=False),
899 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=14, starts_line=4, is_jump_target=False),
900 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 -0700901 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 +0300902 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
903 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=5, is_jump_target=False),
904 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=24, starts_line=None, is_jump_target=False),
905 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=26, starts_line=None, is_jump_target=False),
906 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=32, argval=32, argrepr='', offset=28, starts_line=None, is_jump_target=False),
907 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=30, starts_line=6, is_jump_target=False),
908 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=32, starts_line=7, is_jump_target=True),
909 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=34, starts_line=None, is_jump_target=False),
910 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=36, starts_line=None, is_jump_target=False),
911 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=10, argval=10, argrepr='', offset=38, starts_line=None, is_jump_target=False),
912 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=40, starts_line=8, is_jump_target=False),
913 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=10, argval=10, argrepr='', offset=42, starts_line=None, is_jump_target=False),
914 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=44, starts_line=None, is_jump_target=True),
915 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=46, starts_line=10, is_jump_target=False),
916 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 -0700917 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 +0300918 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=52, starts_line=None, is_jump_target=False),
919 Instruction(opname='SETUP_LOOP', opcode=120, arg=52, argval=108, argrepr='to 108', offset=54, starts_line=11, is_jump_target=True),
920 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=True),
921 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=98, argval=98, argrepr='', offset=58, starts_line=None, is_jump_target=False),
922 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=60, starts_line=12, is_jump_target=False),
923 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 -0700924 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 +0300925 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
926 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=68, starts_line=13, is_jump_target=False),
927 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=70, starts_line=None, is_jump_target=False),
928 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=72, starts_line=None, is_jump_target=False),
929 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=74, starts_line=None, is_jump_target=False),
930 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=76, starts_line=14, is_jump_target=False),
931 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=78, starts_line=None, is_jump_target=False),
932 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=80, starts_line=None, is_jump_target=False),
933 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=86, argval=86, argrepr='', offset=82, starts_line=None, is_jump_target=False),
934 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=84, starts_line=15, is_jump_target=False),
935 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=86, starts_line=16, is_jump_target=True),
936 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=88, starts_line=None, is_jump_target=False),
937 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=90, starts_line=None, is_jump_target=False),
938 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=56, argval=56, argrepr='', offset=92, starts_line=None, is_jump_target=False),
939 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=94, starts_line=17, is_jump_target=False),
940 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=56, argval=56, argrepr='', offset=96, starts_line=None, is_jump_target=False),
941 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=98, starts_line=None, is_jump_target=True),
942 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=100, starts_line=19, is_jump_target=False),
943 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 -0700944 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 +0300945 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
946 Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=180, argrepr='to 180', offset=108, starts_line=20, is_jump_target=True),
947 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=124, argrepr='to 124', offset=110, starts_line=None, is_jump_target=False),
948 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=112, starts_line=21, is_jump_target=False),
949 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=114, starts_line=None, is_jump_target=False),
950 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=116, starts_line=None, is_jump_target=False),
951 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=118, starts_line=None, is_jump_target=False),
952 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
953 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=152, argrepr='to 152', offset=122, starts_line=None, is_jump_target=False),
954 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=124, starts_line=22, is_jump_target=True),
955 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=126, starts_line=None, is_jump_target=False),
956 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=128, starts_line=None, is_jump_target=False),
957 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=150, argval=150, argrepr='', offset=130, starts_line=None, is_jump_target=False),
958 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
959 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
960 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
961 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=138, starts_line=23, is_jump_target=False),
962 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 -0700963 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 +0300964 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=False),
965 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=146, starts_line=None, is_jump_target=False),
966 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=176, argrepr='to 176', offset=148, starts_line=None, is_jump_target=False),
967 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=150, starts_line=None, is_jump_target=True),
968 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=152, starts_line=25, is_jump_target=True),
969 Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=170, argrepr='to 170', offset=154, starts_line=None, is_jump_target=False),
970 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=156, starts_line=None, is_jump_target=False),
971 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=158, starts_line=26, is_jump_target=False),
972 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 -0700973 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 +0300974 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
975 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
976 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=168, starts_line=None, is_jump_target=False),
977 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True),
978 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
979 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=False),
980 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=True),
981 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=178, starts_line=None, is_jump_target=False),
982 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=180, starts_line=28, is_jump_target=True),
983 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 -0700984 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 +0300985 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
986 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
987 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=190, starts_line=None, is_jump_target=False),
988 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 +1000989]
990
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000991# One last piece of inspect fodder to check the default line number handling
992def simple(): pass
993expected_opinfo_simple = [
994 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 +0300995 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 +1000996]
997
998
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000999class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001000
1001 def test_default_first_line(self):
1002 actual = dis.get_instructions(simple)
1003 self.assertEqual(list(actual), expected_opinfo_simple)
1004
1005 def test_first_line_set_to_None(self):
1006 actual = dis.get_instructions(simple, first_line=None)
1007 self.assertEqual(list(actual), expected_opinfo_simple)
1008
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001009 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001010 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1011 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001012
1013 def test_nested(self):
1014 with captured_stdout():
1015 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001016 actual = dis.get_instructions(f, first_line=expected_f_line)
1017 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001018
1019 def test_doubly_nested(self):
1020 with captured_stdout():
1021 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001022 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1023 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001024
1025 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001026 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1027 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001028
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001029# get_instructions has its own tests above, so can rely on it to validate
1030# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001031class BytecodeTests(unittest.TestCase):
1032 def test_instantiation(self):
1033 # Test with function, method, code string and code object
1034 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001035 with self.subTest(obj=obj):
1036 b = dis.Bytecode(obj)
1037 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001038
1039 self.assertRaises(TypeError, dis.Bytecode, object())
1040
1041 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001042 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1043 with self.subTest(obj=obj):
1044 via_object = list(dis.Bytecode(obj))
1045 via_generator = list(dis.get_instructions(obj))
1046 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001047
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001048 def test_explicit_first_line(self):
1049 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1050 self.assertEqual(list(actual), expected_opinfo_outer)
1051
1052 def test_source_line_in_disassembly(self):
1053 # Use the line in the source code
1054 actual = dis.Bytecode(simple).dis()[:3]
1055 expected = "{:>3}".format(simple.__code__.co_firstlineno)
1056 self.assertEqual(actual, expected)
1057 # Use an explicit first line number
1058 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
1059 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001060
1061 def test_info(self):
1062 self.maxDiff = 1000
1063 for x, expected in CodeInfoTests.test_pairs:
1064 b = dis.Bytecode(x)
1065 self.assertRegex(b.info(), expected)
1066
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001067 def test_disassembled(self):
1068 actual = dis.Bytecode(_f).dis()
1069 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001070
Nick Coghlan50c48b82013-11-23 00:57:00 +10001071 def test_from_traceback(self):
1072 tb = get_tb()
1073 b = dis.Bytecode.from_traceback(tb)
1074 while tb.tb_next: tb = tb.tb_next
1075
1076 self.assertEqual(b.current_offset, tb.tb_lasti)
1077
1078 def test_from_traceback_dis(self):
1079 tb = get_tb()
1080 b = dis.Bytecode.from_traceback(tb)
1081 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001082
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001083if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001084 unittest.main()