blob: 94d6be735c4e1c1d102966cf52dbfa3923207d53 [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Nick Coghlaneae2da12010-08-17 08:03:36 +00003from test.support import run_unittest, 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
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
32dis_c_instance_method = """\
33 %-4d 0 LOAD_FAST 1 (x)
34 3 LOAD_CONST 1 (1)
35 6 COMPARE_OP 2 (==)
36 9 LOAD_FAST 0 (self)
37 12 STORE_ATTR 0 (x)
38 15 LOAD_CONST 0 (None)
39 18 RETURN_VALUE
40""" % (_C.__init__.__code__.co_firstlineno + 1,)
41
42dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100043 0 LOAD_FAST 1 (1)
44 3 LOAD_CONST 1 (1)
45 6 COMPARE_OP 2 (==)
46 9 LOAD_FAST 0 (0)
47 12 STORE_ATTR 0 (0)
48 15 LOAD_CONST 0 (0)
Benjamin Petersond6afe722011-03-15 14:44:52 -050049 18 RETURN_VALUE
50"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000051
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000052def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000053 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000054 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000055
56dis_f = """\
Georg Brandl88fc6642007-02-09 21:28:07 +000057 %-4d 0 LOAD_GLOBAL 0 (print)
58 3 LOAD_FAST 0 (a)
Alexander Belopolsky74482202012-06-07 14:28:14 -040059 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Georg Brandl88fc6642007-02-09 21:28:07 +000060 9 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000061
Georg Brandl88fc6642007-02-09 21:28:07 +000062 %-4d 10 LOAD_CONST 1 (1)
63 13 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000064""" % (_f.__code__.co_firstlineno + 1,
65 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +000066
67
Benjamin Petersond6afe722011-03-15 14:44:52 -050068dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100069 0 LOAD_GLOBAL 0 (0)
70 3 LOAD_FAST 0 (0)
71 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Benjamin Petersond6afe722011-03-15 14:44:52 -050072 9 POP_TOP
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100073 10 LOAD_CONST 1 (1)
Benjamin Petersond6afe722011-03-15 14:44:52 -050074 13 RETURN_VALUE
75"""
76
77
Michael W. Hudson26848a32003-04-29 17:07:36 +000078def bug708901():
79 for res in range(1,
80 10):
81 pass
82
83dis_bug708901 = """\
84 %-4d 0 SETUP_LOOP 23 (to 26)
85 3 LOAD_GLOBAL 0 (range)
86 6 LOAD_CONST 1 (1)
87
88 %-4d 9 LOAD_CONST 2 (10)
Alexander Belopolsky74482202012-06-07 14:28:14 -040089 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
Michael W. Hudson26848a32003-04-29 17:07:36 +000090 15 GET_ITER
91 >> 16 FOR_ITER 6 (to 25)
92 19 STORE_FAST 0 (res)
93
94 %-4d 22 JUMP_ABSOLUTE 16
95 >> 25 POP_BLOCK
96 >> 26 LOAD_CONST 0 (None)
97 29 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000098""" % (bug708901.__code__.co_firstlineno + 1,
99 bug708901.__code__.co_firstlineno + 2,
100 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000101
Neal Norwitz51abbc72005-12-18 07:06:23 +0000102
103def bug1333982(x=[]):
104 assert 0, ([s for s in x] +
105 1)
106 pass
107
108dis_bug1333982 = """\
109 %-4d 0 LOAD_CONST 1 (0)
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000110 3 JUMP_IF_TRUE 33 (to 39)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000111 6 POP_TOP
112 7 LOAD_GLOBAL 0 (AssertionError)
113 10 BUILD_LIST 0
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000114 13 LOAD_FAST 0 (x)
115 16 GET_ITER
116 >> 17 FOR_ITER 12 (to 32)
117 20 STORE_FAST 1 (s)
118 23 LOAD_FAST 1 (s)
119 26 LIST_APPEND 2
120 29 JUMP_ABSOLUTE 17
Neal Norwitz51abbc72005-12-18 07:06:23 +0000121
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000122 %-4d >> 32 LOAD_CONST 2 (1)
123 35 BINARY_ADD
124 36 RAISE_VARARGS 2
125 >> 39 POP_TOP
Neal Norwitz51abbc72005-12-18 07:06:23 +0000126
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000127 %-4d 40 LOAD_CONST 0 (None)
128 43 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000129""" % (bug1333982.__code__.co_firstlineno + 1,
130 bug1333982.__code__.co_firstlineno + 2,
131 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000132
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000133_BIG_LINENO_FORMAT = """\
134%3d 0 LOAD_GLOBAL 0 (spam)
135 3 POP_TOP
136 4 LOAD_CONST 0 (None)
137 7 RETURN_VALUE
138"""
139
Guido van Rossume7ba4952007-06-06 23:52:48 +0000140dis_module_expected_results = """\
141Disassembly of f:
142 4 0 LOAD_CONST 0 (None)
143 3 RETURN_VALUE
144
145Disassembly of g:
146 5 0 LOAD_CONST 0 (None)
147 3 RETURN_VALUE
148
149"""
150
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000151expr_str = "x + 1"
152
153dis_expr_str = """\
154 1 0 LOAD_NAME 0 (x)
155 3 LOAD_CONST 0 (1)
156 6 BINARY_ADD
157 7 RETURN_VALUE
158"""
159
160simple_stmt_str = "x = x + 1"
161
162dis_simple_stmt_str = """\
163 1 0 LOAD_NAME 0 (x)
164 3 LOAD_CONST 0 (1)
165 6 BINARY_ADD
166 7 STORE_NAME 0 (x)
167 10 LOAD_CONST 1 (None)
168 13 RETURN_VALUE
169"""
170
171compound_stmt_str = """\
172x = 0
173while 1:
174 x += 1"""
175# Trailing newline has been deliberately omitted
176
177dis_compound_stmt_str = """\
178 1 0 LOAD_CONST 0 (0)
179 3 STORE_NAME 0 (x)
180
181 2 6 SETUP_LOOP 13 (to 22)
182
183 3 >> 9 LOAD_NAME 0 (x)
184 12 LOAD_CONST 1 (1)
185 15 INPLACE_ADD
186 16 STORE_NAME 0 (x)
187 19 JUMP_ABSOLUTE 9
188 >> 22 LOAD_CONST 2 (None)
189 25 RETURN_VALUE
190"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000191
Nick Coghlan50c48b82013-11-23 00:57:00 +1000192dis_traceback = """\
193 %-4d 0 SETUP_EXCEPT 12 (to 15)
194
195 %-4d 3 LOAD_CONST 1 (1)
196 6 LOAD_CONST 2 (0)
197 --> 9 BINARY_TRUE_DIVIDE
198 10 POP_TOP
199 11 POP_BLOCK
200 12 JUMP_FORWARD 46 (to 61)
201
202 %-4d >> 15 DUP_TOP
203 16 LOAD_GLOBAL 0 (Exception)
204 19 COMPARE_OP 10 (exception match)
205 22 POP_JUMP_IF_FALSE 60
206 25 POP_TOP
207 26 STORE_FAST 0 (e)
208 29 POP_TOP
209 30 SETUP_FINALLY 14 (to 47)
210
211 %-4d 33 LOAD_FAST 0 (e)
212 36 LOAD_ATTR 1 (__traceback__)
213 39 STORE_FAST 1 (tb)
214 42 POP_BLOCK
215 43 POP_EXCEPT
216 44 LOAD_CONST 0 (None)
217 >> 47 LOAD_CONST 0 (None)
218 50 STORE_FAST 0 (e)
219 53 DELETE_FAST 0 (e)
220 56 END_FINALLY
221 57 JUMP_FORWARD 1 (to 61)
222 >> 60 END_FINALLY
223
224 %-4d >> 61 LOAD_FAST 1 (tb)
225 64 RETURN_VALUE
226""" % (TRACEBACK_CODE.co_firstlineno + 1,
227 TRACEBACK_CODE.co_firstlineno + 2,
228 TRACEBACK_CODE.co_firstlineno + 3,
229 TRACEBACK_CODE.co_firstlineno + 4,
230 TRACEBACK_CODE.co_firstlineno + 5)
231
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000232class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500233
234 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000235 # We want to test the default printing behaviour, not the file arg
236 output = io.StringIO()
237 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500238 if wrapper:
239 dis.dis(func)
240 else:
241 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000242 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500243
244 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000245 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500246
247 def do_disassembly_test(self, func, expected):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000248 self.assertEqual(self.get_disassembly(func), expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000249
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000250 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500251 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000252 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
253 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000254
255 def test_opname(self):
256 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
257
258 def test_boundaries(self):
259 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
260 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
261
262 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000263 self.do_disassembly_test(_f, dis_f)
264
265 def test_bug_708901(self):
266 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000267
Neal Norwitz51abbc72005-12-18 07:06:23 +0000268 def test_bug_1333982(self):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000269 # XXX: re-enable this test!
Tim Peters83a8c392005-12-25 22:52:32 +0000270 # This one is checking bytecodes generated for an `assert` statement,
271 # so fails if the tests are run with -O. Skip this test then.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000272 pass # Test has been disabled due to change in the way
273 # list comps are handled. The byte code now includes
274 # a memory address and a file location, so they change from
275 # run to run.
276 # if __debug__:
277 # self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000278
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000279 def test_big_linenos(self):
280 def func(count):
281 namespace = {}
282 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000283 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284 return namespace['foo']
285
286 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000287 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288 expected = _BIG_LINENO_FORMAT % (i + 2)
289 self.do_disassembly_test(func(i), expected)
290
291 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000292 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000293 expected = _BIG_LINENO_FORMAT % (i + 2)
294 self.do_disassembly_test(func(i), expected)
295
Guido van Rossume7ba4952007-06-06 23:52:48 +0000296 from test import dis_module
297 self.do_disassembly_test(dis_module, dis_module_expected_results)
298
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000299 def test_disassemble_str(self):
300 self.do_disassembly_test(expr_str, dis_expr_str)
301 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
302 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
303
Benjamin Petersond6afe722011-03-15 14:44:52 -0500304 def test_disassemble_bytes(self):
305 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
306
307 def test_disassemble_method(self):
308 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
309
310 def test_disassemble_method_bytes(self):
311 method_bytecode = _C(1).__init__.__code__.co_code
312 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
313
314 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500315 try:
316 del sys.last_traceback
317 except AttributeError:
318 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500319 self.assertRaises(RuntimeError, dis.dis, None)
320
Benjamin Petersond6afe722011-03-15 14:44:52 -0500321 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500322 try:
323 del sys.last_traceback
324 except AttributeError:
325 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500326
327 try:
328 1/0
329 except Exception as e:
330 tb = e.__traceback__
331 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500332
333 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
334 self.do_disassembly_test(None, tb_dis)
335
336 def test_dis_object(self):
337 self.assertRaises(TypeError, dis.dis, object())
338
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000339class DisWithFileTests(DisTests):
340
341 # Run the tests again, using the file arg instead of print
342 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000343 output = io.StringIO()
344 if wrapper:
345 dis.dis(func, file=output)
346 else:
347 dis.disassemble(func, lasti, file=output)
348 return output.getvalue()
349
350
351
Nick Coghlaneae2da12010-08-17 08:03:36 +0000352code_info_code_info = """\
353Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000354Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000355Argument count: 1
356Kw-only arguments: 0
357Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000358Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000359Flags: OPTIMIZED, NEWLOCALS, NOFREE
360Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000361 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000362Names:
363 0: _format_code_info
364 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000365Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000366 0: x""" % (('Formatted details of methods, functions, or code.',)
367 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000368
369@staticmethod
370def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
371 def f(c=c):
372 print(x, y, z, c, d, e, f)
373 yield x, y, z, c, d, e, f
374
Nick Coghlaneae2da12010-08-17 08:03:36 +0000375code_info_tricky = """\
376Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000377Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000378Argument count: 3
379Kw-only arguments: 3
380Number of locals: 8
381Stack size: 7
382Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
383Constants:
384 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000385 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100386 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000387Variable names:
388 0: x
389 1: y
390 2: z
391 3: c
392 4: d
393 5: e
394 6: args
395 7: kwds
396Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100397 0: [edfxyz]
398 1: [edfxyz]
399 2: [edfxyz]
400 3: [edfxyz]
401 4: [edfxyz]
402 5: [edfxyz]"""
403# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000404
405co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000406
407code_info_tricky_nested_f = """\
408Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000409Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000410Argument count: 1
411Kw-only arguments: 0
412Number of locals: 1
413Stack size: 8
414Flags: OPTIMIZED, NEWLOCALS, NESTED
415Constants:
416 0: None
417Names:
418 0: print
419Variable names:
420 0: c
421Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100422 0: [edfxyz]
423 1: [edfxyz]
424 2: [edfxyz]
425 3: [edfxyz]
426 4: [edfxyz]
427 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000428
429code_info_expr_str = """\
430Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000431Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000432Argument count: 0
433Kw-only arguments: 0
434Number of locals: 0
435Stack size: 2
436Flags: NOFREE
437Constants:
438 0: 1
439Names:
440 0: x"""
441
442code_info_simple_stmt_str = """\
443Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000444Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000445Argument count: 0
446Kw-only arguments: 0
447Number of locals: 0
448Stack size: 2
449Flags: NOFREE
450Constants:
451 0: 1
452 1: None
453Names:
454 0: x"""
455
456code_info_compound_stmt_str = """\
457Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000458Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000459Argument count: 0
460Kw-only arguments: 0
461Number of locals: 0
462Stack size: 2
463Flags: NOFREE
464Constants:
465 0: 0
466 1: 1
467 2: None
468Names:
469 0: x"""
470
471class CodeInfoTests(unittest.TestCase):
472 test_pairs = [
473 (dis.code_info, code_info_code_info),
474 (tricky, code_info_tricky),
475 (co_tricky_nested_f, code_info_tricky_nested_f),
476 (expr_str, code_info_expr_str),
477 (simple_stmt_str, code_info_simple_stmt_str),
478 (compound_stmt_str, code_info_compound_stmt_str),
479 ]
480
481 def test_code_info(self):
482 self.maxDiff = 1000
483 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000484 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000485
486 def test_show_code(self):
487 self.maxDiff = 1000
488 for x, expected in self.test_pairs:
489 with captured_stdout() as output:
490 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000491 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000492 output = io.StringIO()
493 dis.show_code(x, file=output)
494 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000495
Benjamin Petersond6afe722011-03-15 14:44:52 -0500496 def test_code_info_object(self):
497 self.assertRaises(TypeError, dis.code_info, object())
498
499 def test_pretty_flags_no_flags(self):
500 self.assertEqual(dis.pretty_flags(0), '0x0')
501
502
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000503# Fodder for instruction introspection tests
504# Editing any of these may require recalculating the expected output
505def outer(a=1, b=2):
506 def f(c=3, d=4):
507 def inner(e=5, f=6):
508 print(a, b, c, d, e, f)
509 print(a, b, c, d)
510 return inner
511 print(a, b, '', 1, [], {}, "Hello world!")
512 return f
513
514def jumpy():
515 # This won't actually run (but that's OK, we only disassemble it)
516 for i in range(10):
517 print(i)
518 if i < 4:
519 continue
520 if i > 6:
521 break
522 else:
523 print("I can haz else clause?")
524 while i:
525 print(i)
526 i -= 1
527 if i > 6:
528 continue
529 if i < 4:
530 break
531 else:
532 print("Who let lolcatz into this test suite?")
533 try:
534 1 / 0
535 except ZeroDivisionError:
536 print("Here we go, here we go, here we go...")
537 else:
538 with i as dodgy:
539 print("Never reach this")
540 finally:
541 print("OK, now we're done")
542
543# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000544expected_outer_line = 1
545_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000546code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000547expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000548code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000549expected_inner_line = code_object_inner.co_firstlineno - _line_offset
550expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000551
552# The following lines are useful to regenerate the expected results after
553# either the fodder is modified or the bytecode generation changes
554# After regeneration, update the references to code_object_f and
555# code_object_inner before rerunning the tests
556
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000557#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000558#print('expected_opinfo_outer = [\n ',
559 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000560#_instructions = dis.get_instructions(outer(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000561#print('expected_opinfo_f = [\n ',
562 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000563#_instructions = dis.get_instructions(outer()(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000564#print('expected_opinfo_inner = [\n ',
565 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000566#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000567#print('expected_opinfo_jumpy = [\n ',
568 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
569
570
571Instruction = dis.Instruction
572expected_opinfo_outer = [
573 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
574 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
575 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
576 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
577 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
578 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=15, starts_line=None, is_jump_target=False),
579 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=18, starts_line=None, is_jump_target=False),
580 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
581 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
582 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
583 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
584 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
585 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
586 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
587 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
588 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
589 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
590 Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='7 positional, 0 keyword pair', offset=51, starts_line=None, is_jump_target=False),
591 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
592 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
593 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
594]
595
596expected_opinfo_f = [
597 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
598 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
599 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
600 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
601 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
602 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
603 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
604 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=21, starts_line=None, is_jump_target=False),
605 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=24, starts_line=None, is_jump_target=False),
606 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
607 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
608 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
609 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
610 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
611 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
612 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
613 Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='4 positional, 0 keyword pair', offset=48, starts_line=None, is_jump_target=False),
614 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
615 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
616 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
617]
618
619expected_opinfo_inner = [
620 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
621 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
622 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
623 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
624 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
625 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
626 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
627 Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='6 positional, 0 keyword pair', offset=21, starts_line=None, is_jump_target=False),
628 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
629 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
630 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
631]
632
633expected_opinfo_jumpy = [
634 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
635 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
636 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
637 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=9, starts_line=None, is_jump_target=False),
638 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
639 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
640 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
641 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
642 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
643 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=25, starts_line=None, is_jump_target=False),
644 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
645 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
646 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
647 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
648 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
649 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
650 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
651 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
652 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
653 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
654 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
655 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
656 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
657 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
658 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
659 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
660 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=70, starts_line=None, is_jump_target=False),
661 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=73, starts_line=None, is_jump_target=False),
662 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
663 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
664 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
665 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
666 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
667 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
668 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=92, starts_line=None, is_jump_target=False),
669 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
670 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
671 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
672 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
673 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
674 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
675 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
676 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
677 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
678 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
679 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
680 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
681 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
682 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
683 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
684 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
685 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
686 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
687 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
688 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
689 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=147, starts_line=None, is_jump_target=False),
690 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=150, starts_line=None, is_jump_target=False),
691 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
692 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
693 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
694 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
695 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
696 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
697 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
698 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
699 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
700 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
701 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
702 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
703 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
704 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
705 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
706 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
707 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
708 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=188, starts_line=None, is_jump_target=False),
709 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=191, starts_line=None, is_jump_target=False),
710 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
711 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
712 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
713 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
714 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
715 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
716 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
717 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
718 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=212, starts_line=None, is_jump_target=False),
719 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=215, starts_line=None, is_jump_target=False),
720 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
721 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
722 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
723 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
724 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
725 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
726 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
727 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
728 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=232, starts_line=None, is_jump_target=False),
729 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='1 positional, 0 keyword pair', offset=235, starts_line=None, is_jump_target=False),
730 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
731 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
732 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
733 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
734]
735
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000736# One last piece of inspect fodder to check the default line number handling
737def simple(): pass
738expected_opinfo_simple = [
739 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
740 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
741]
742
743
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000744class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000745
746 def test_default_first_line(self):
747 actual = dis.get_instructions(simple)
748 self.assertEqual(list(actual), expected_opinfo_simple)
749
750 def test_first_line_set_to_None(self):
751 actual = dis.get_instructions(simple, first_line=None)
752 self.assertEqual(list(actual), expected_opinfo_simple)
753
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000754 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000755 actual = dis.get_instructions(outer, first_line=expected_outer_line)
756 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000757
758 def test_nested(self):
759 with captured_stdout():
760 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000761 actual = dis.get_instructions(f, first_line=expected_f_line)
762 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000763
764 def test_doubly_nested(self):
765 with captured_stdout():
766 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000767 actual = dis.get_instructions(inner, first_line=expected_inner_line)
768 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000769
770 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000771 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
772 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000773
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000774# get_instructions has its own tests above, so can rely on it to validate
775# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000776class BytecodeTests(unittest.TestCase):
777 def test_instantiation(self):
778 # Test with function, method, code string and code object
779 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000780 with self.subTest(obj=obj):
781 b = dis.Bytecode(obj)
782 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000783
784 self.assertRaises(TypeError, dis.Bytecode, object())
785
786 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000787 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
788 with self.subTest(obj=obj):
789 via_object = list(dis.Bytecode(obj))
790 via_generator = list(dis.get_instructions(obj))
791 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000792
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000793 def test_explicit_first_line(self):
794 actual = dis.Bytecode(outer, first_line=expected_outer_line)
795 self.assertEqual(list(actual), expected_opinfo_outer)
796
797 def test_source_line_in_disassembly(self):
798 # Use the line in the source code
799 actual = dis.Bytecode(simple).dis()[:3]
800 expected = "{:>3}".format(simple.__code__.co_firstlineno)
801 self.assertEqual(actual, expected)
802 # Use an explicit first line number
803 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
804 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000805
806 def test_info(self):
807 self.maxDiff = 1000
808 for x, expected in CodeInfoTests.test_pairs:
809 b = dis.Bytecode(x)
810 self.assertRegex(b.info(), expected)
811
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000812 def test_disassembled(self):
813 actual = dis.Bytecode(_f).dis()
814 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000815
Nick Coghlan50c48b82013-11-23 00:57:00 +1000816 def test_from_traceback(self):
817 tb = get_tb()
818 b = dis.Bytecode.from_traceback(tb)
819 while tb.tb_next: tb = tb.tb_next
820
821 self.assertEqual(b.current_offset, tb.tb_lasti)
822
823 def test_from_traceback_dis(self):
824 tb = get_tb()
825 b = dis.Bytecode.from_traceback(tb)
826 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000827
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000828def test_main():
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000829 run_unittest(DisTests, DisWithFileTests, CodeInfoTests,
830 InstructionTests, BytecodeTests)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000831
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000832if __name__ == "__main__":
833 test_main()