blob: 2b546785a1116b4bc92053bfaeb64b1659249863 [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
Benjamin Petersond6afe722011-03-15 14:44:52 -050013class _C:
14 def __init__(self, x):
15 self.x = x == 1
16
17dis_c_instance_method = """\
18 %-4d 0 LOAD_FAST 1 (x)
19 3 LOAD_CONST 1 (1)
20 6 COMPARE_OP 2 (==)
21 9 LOAD_FAST 0 (self)
22 12 STORE_ATTR 0 (x)
23 15 LOAD_CONST 0 (None)
24 18 RETURN_VALUE
25""" % (_C.__init__.__code__.co_firstlineno + 1,)
26
27dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100028 0 LOAD_FAST 1 (1)
29 3 LOAD_CONST 1 (1)
30 6 COMPARE_OP 2 (==)
31 9 LOAD_FAST 0 (0)
32 12 STORE_ATTR 0 (0)
33 15 LOAD_CONST 0 (0)
Benjamin Petersond6afe722011-03-15 14:44:52 -050034 18 RETURN_VALUE
35"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000036
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000037def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000038 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000039 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000040
41dis_f = """\
Georg Brandl88fc6642007-02-09 21:28:07 +000042 %-4d 0 LOAD_GLOBAL 0 (print)
43 3 LOAD_FAST 0 (a)
Alexander Belopolsky74482202012-06-07 14:28:14 -040044 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Georg Brandl88fc6642007-02-09 21:28:07 +000045 9 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000046
Georg Brandl88fc6642007-02-09 21:28:07 +000047 %-4d 10 LOAD_CONST 1 (1)
48 13 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000049""" % (_f.__code__.co_firstlineno + 1,
50 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +000051
52
Benjamin Petersond6afe722011-03-15 14:44:52 -050053dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100054 0 LOAD_GLOBAL 0 (0)
55 3 LOAD_FAST 0 (0)
56 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Benjamin Petersond6afe722011-03-15 14:44:52 -050057 9 POP_TOP
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100058 10 LOAD_CONST 1 (1)
Benjamin Petersond6afe722011-03-15 14:44:52 -050059 13 RETURN_VALUE
60"""
61
62
Michael W. Hudson26848a32003-04-29 17:07:36 +000063def bug708901():
64 for res in range(1,
65 10):
66 pass
67
68dis_bug708901 = """\
69 %-4d 0 SETUP_LOOP 23 (to 26)
70 3 LOAD_GLOBAL 0 (range)
71 6 LOAD_CONST 1 (1)
72
73 %-4d 9 LOAD_CONST 2 (10)
Alexander Belopolsky74482202012-06-07 14:28:14 -040074 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
Michael W. Hudson26848a32003-04-29 17:07:36 +000075 15 GET_ITER
76 >> 16 FOR_ITER 6 (to 25)
77 19 STORE_FAST 0 (res)
78
79 %-4d 22 JUMP_ABSOLUTE 16
80 >> 25 POP_BLOCK
81 >> 26 LOAD_CONST 0 (None)
82 29 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000083""" % (bug708901.__code__.co_firstlineno + 1,
84 bug708901.__code__.co_firstlineno + 2,
85 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000086
Neal Norwitz51abbc72005-12-18 07:06:23 +000087
88def bug1333982(x=[]):
89 assert 0, ([s for s in x] +
90 1)
91 pass
92
93dis_bug1333982 = """\
94 %-4d 0 LOAD_CONST 1 (0)
Antoine Pitrouf289ae62008-12-18 11:06:25 +000095 3 JUMP_IF_TRUE 33 (to 39)
Neal Norwitz51abbc72005-12-18 07:06:23 +000096 6 POP_TOP
97 7 LOAD_GLOBAL 0 (AssertionError)
98 10 BUILD_LIST 0
Antoine Pitrouf289ae62008-12-18 11:06:25 +000099 13 LOAD_FAST 0 (x)
100 16 GET_ITER
101 >> 17 FOR_ITER 12 (to 32)
102 20 STORE_FAST 1 (s)
103 23 LOAD_FAST 1 (s)
104 26 LIST_APPEND 2
105 29 JUMP_ABSOLUTE 17
Neal Norwitz51abbc72005-12-18 07:06:23 +0000106
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000107 %-4d >> 32 LOAD_CONST 2 (1)
108 35 BINARY_ADD
109 36 RAISE_VARARGS 2
110 >> 39 POP_TOP
Neal Norwitz51abbc72005-12-18 07:06:23 +0000111
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000112 %-4d 40 LOAD_CONST 0 (None)
113 43 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000114""" % (bug1333982.__code__.co_firstlineno + 1,
115 bug1333982.__code__.co_firstlineno + 2,
116 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000117
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000118_BIG_LINENO_FORMAT = """\
119%3d 0 LOAD_GLOBAL 0 (spam)
120 3 POP_TOP
121 4 LOAD_CONST 0 (None)
122 7 RETURN_VALUE
123"""
124
Guido van Rossume7ba4952007-06-06 23:52:48 +0000125dis_module_expected_results = """\
126Disassembly of f:
127 4 0 LOAD_CONST 0 (None)
128 3 RETURN_VALUE
129
130Disassembly of g:
131 5 0 LOAD_CONST 0 (None)
132 3 RETURN_VALUE
133
134"""
135
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000136expr_str = "x + 1"
137
138dis_expr_str = """\
139 1 0 LOAD_NAME 0 (x)
140 3 LOAD_CONST 0 (1)
141 6 BINARY_ADD
142 7 RETURN_VALUE
143"""
144
145simple_stmt_str = "x = x + 1"
146
147dis_simple_stmt_str = """\
148 1 0 LOAD_NAME 0 (x)
149 3 LOAD_CONST 0 (1)
150 6 BINARY_ADD
151 7 STORE_NAME 0 (x)
152 10 LOAD_CONST 1 (None)
153 13 RETURN_VALUE
154"""
155
156compound_stmt_str = """\
157x = 0
158while 1:
159 x += 1"""
160# Trailing newline has been deliberately omitted
161
162dis_compound_stmt_str = """\
163 1 0 LOAD_CONST 0 (0)
164 3 STORE_NAME 0 (x)
165
166 2 6 SETUP_LOOP 13 (to 22)
167
168 3 >> 9 LOAD_NAME 0 (x)
169 12 LOAD_CONST 1 (1)
170 15 INPLACE_ADD
171 16 STORE_NAME 0 (x)
172 19 JUMP_ABSOLUTE 9
173 >> 22 LOAD_CONST 2 (None)
174 25 RETURN_VALUE
175"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000176
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000177class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500178
179 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000180 # We want to test the default printing behaviour, not the file arg
181 output = io.StringIO()
182 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500183 if wrapper:
184 dis.dis(func)
185 else:
186 dis.disassemble(func, lasti)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000187 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500188
189 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000190 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500191
192 def do_disassembly_test(self, func, expected):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000193 self.assertEqual(self.get_disassembly(func), expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000194
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000195 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500196 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000197 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
198 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000199
200 def test_opname(self):
201 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
202
203 def test_boundaries(self):
204 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
205 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
206
207 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000208 self.do_disassembly_test(_f, dis_f)
209
210 def test_bug_708901(self):
211 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000212
Neal Norwitz51abbc72005-12-18 07:06:23 +0000213 def test_bug_1333982(self):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000214 # XXX: re-enable this test!
Tim Peters83a8c392005-12-25 22:52:32 +0000215 # This one is checking bytecodes generated for an `assert` statement,
216 # so fails if the tests are run with -O. Skip this test then.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000217 pass # Test has been disabled due to change in the way
218 # list comps are handled. The byte code now includes
219 # a memory address and a file location, so they change from
220 # run to run.
221 # if __debug__:
222 # self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000223
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000224 def test_big_linenos(self):
225 def func(count):
226 namespace = {}
227 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000228 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000229 return namespace['foo']
230
231 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000232 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 expected = _BIG_LINENO_FORMAT % (i + 2)
234 self.do_disassembly_test(func(i), expected)
235
236 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000237 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 expected = _BIG_LINENO_FORMAT % (i + 2)
239 self.do_disassembly_test(func(i), expected)
240
Guido van Rossume7ba4952007-06-06 23:52:48 +0000241 from test import dis_module
242 self.do_disassembly_test(dis_module, dis_module_expected_results)
243
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000244 def test_disassemble_str(self):
245 self.do_disassembly_test(expr_str, dis_expr_str)
246 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
247 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
248
Benjamin Petersond6afe722011-03-15 14:44:52 -0500249 def test_disassemble_bytes(self):
250 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
251
252 def test_disassemble_method(self):
253 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
254
255 def test_disassemble_method_bytes(self):
256 method_bytecode = _C(1).__init__.__code__.co_code
257 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
258
259 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500260 try:
261 del sys.last_traceback
262 except AttributeError:
263 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500264 self.assertRaises(RuntimeError, dis.dis, None)
265
Benjamin Petersond6afe722011-03-15 14:44:52 -0500266 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500267 try:
268 del sys.last_traceback
269 except AttributeError:
270 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500271
272 try:
273 1/0
274 except Exception as e:
275 tb = e.__traceback__
276 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500277
278 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
279 self.do_disassembly_test(None, tb_dis)
280
281 def test_dis_object(self):
282 self.assertRaises(TypeError, dis.dis, object())
283
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000284class DisWithFileTests(DisTests):
285
286 # Run the tests again, using the file arg instead of print
287 def get_disassembly(self, func, lasti=-1, wrapper=True):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000288 output = io.StringIO()
289 if wrapper:
290 dis.dis(func, file=output)
291 else:
292 dis.disassemble(func, lasti, file=output)
293 return output.getvalue()
294
295
296
Nick Coghlaneae2da12010-08-17 08:03:36 +0000297code_info_code_info = """\
298Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000299Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000300Argument count: 1
301Kw-only arguments: 0
302Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000303Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000304Flags: OPTIMIZED, NEWLOCALS, NOFREE
305Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000306 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000307Names:
308 0: _format_code_info
309 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000310Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000311 0: x""" % (('Formatted details of methods, functions, or code.',)
312 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000313
314@staticmethod
315def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
316 def f(c=c):
317 print(x, y, z, c, d, e, f)
318 yield x, y, z, c, d, e, f
319
Nick Coghlaneae2da12010-08-17 08:03:36 +0000320code_info_tricky = """\
321Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000322Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000323Argument count: 3
324Kw-only arguments: 3
325Number of locals: 8
326Stack size: 7
327Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
328Constants:
329 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000330 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100331 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000332Variable names:
333 0: x
334 1: y
335 2: z
336 3: c
337 4: d
338 5: e
339 6: args
340 7: kwds
341Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100342 0: [edfxyz]
343 1: [edfxyz]
344 2: [edfxyz]
345 3: [edfxyz]
346 4: [edfxyz]
347 5: [edfxyz]"""
348# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000349
350co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000351
352code_info_tricky_nested_f = """\
353Name: f
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
358Stack size: 8
359Flags: OPTIMIZED, NEWLOCALS, NESTED
360Constants:
361 0: None
362Names:
363 0: print
364Variable names:
365 0: c
366Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100367 0: [edfxyz]
368 1: [edfxyz]
369 2: [edfxyz]
370 3: [edfxyz]
371 4: [edfxyz]
372 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000373
374code_info_expr_str = """\
375Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000376Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000377Argument count: 0
378Kw-only arguments: 0
379Number of locals: 0
380Stack size: 2
381Flags: NOFREE
382Constants:
383 0: 1
384Names:
385 0: x"""
386
387code_info_simple_stmt_str = """\
388Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000389Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000390Argument count: 0
391Kw-only arguments: 0
392Number of locals: 0
393Stack size: 2
394Flags: NOFREE
395Constants:
396 0: 1
397 1: None
398Names:
399 0: x"""
400
401code_info_compound_stmt_str = """\
402Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000403Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000404Argument count: 0
405Kw-only arguments: 0
406Number of locals: 0
407Stack size: 2
408Flags: NOFREE
409Constants:
410 0: 0
411 1: 1
412 2: None
413Names:
414 0: x"""
415
416class CodeInfoTests(unittest.TestCase):
417 test_pairs = [
418 (dis.code_info, code_info_code_info),
419 (tricky, code_info_tricky),
420 (co_tricky_nested_f, code_info_tricky_nested_f),
421 (expr_str, code_info_expr_str),
422 (simple_stmt_str, code_info_simple_stmt_str),
423 (compound_stmt_str, code_info_compound_stmt_str),
424 ]
425
426 def test_code_info(self):
427 self.maxDiff = 1000
428 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000429 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000430
431 def test_show_code(self):
432 self.maxDiff = 1000
433 for x, expected in self.test_pairs:
434 with captured_stdout() as output:
435 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000436 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000437 output = io.StringIO()
438 dis.show_code(x, file=output)
439 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000440
Benjamin Petersond6afe722011-03-15 14:44:52 -0500441 def test_code_info_object(self):
442 self.assertRaises(TypeError, dis.code_info, object())
443
444 def test_pretty_flags_no_flags(self):
445 self.assertEqual(dis.pretty_flags(0), '0x0')
446
447
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000448# Fodder for instruction introspection tests
449# Editing any of these may require recalculating the expected output
450def outer(a=1, b=2):
451 def f(c=3, d=4):
452 def inner(e=5, f=6):
453 print(a, b, c, d, e, f)
454 print(a, b, c, d)
455 return inner
456 print(a, b, '', 1, [], {}, "Hello world!")
457 return f
458
459def jumpy():
460 # This won't actually run (but that's OK, we only disassemble it)
461 for i in range(10):
462 print(i)
463 if i < 4:
464 continue
465 if i > 6:
466 break
467 else:
468 print("I can haz else clause?")
469 while i:
470 print(i)
471 i -= 1
472 if i > 6:
473 continue
474 if i < 4:
475 break
476 else:
477 print("Who let lolcatz into this test suite?")
478 try:
479 1 / 0
480 except ZeroDivisionError:
481 print("Here we go, here we go, here we go...")
482 else:
483 with i as dodgy:
484 print("Never reach this")
485 finally:
486 print("OK, now we're done")
487
488# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000489expected_outer_line = 1
490_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000491code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000492expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000493code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000494expected_inner_line = code_object_inner.co_firstlineno - _line_offset
495expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000496
497# The following lines are useful to regenerate the expected results after
498# either the fodder is modified or the bytecode generation changes
499# After regeneration, update the references to code_object_f and
500# code_object_inner before rerunning the tests
501
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000502#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000503#print('expected_opinfo_outer = [\n ',
504 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000505#_instructions = dis.get_instructions(outer(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000506#print('expected_opinfo_f = [\n ',
507 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000508#_instructions = dis.get_instructions(outer()(), first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000509#print('expected_opinfo_inner = [\n ',
510 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000511#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000512#print('expected_opinfo_jumpy = [\n ',
513 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
514
515
516Instruction = dis.Instruction
517expected_opinfo_outer = [
518 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
519 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
520 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
521 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
522 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
523 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),
524 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),
525 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
526 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
527 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
528 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
529 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
530 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
531 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
532 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
533 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
534 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
535 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),
536 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
537 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
538 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
539]
540
541expected_opinfo_f = [
542 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
543 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
544 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
545 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
546 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
547 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
548 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
549 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),
550 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),
551 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
552 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
553 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
554 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
555 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
556 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
557 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
558 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),
559 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
560 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
561 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
562]
563
564expected_opinfo_inner = [
565 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
566 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
567 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
568 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
569 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
570 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
571 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
572 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),
573 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
574 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
575 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
576]
577
578expected_opinfo_jumpy = [
579 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
580 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
581 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
582 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),
583 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
584 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
585 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
586 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
587 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
588 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),
589 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
590 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
591 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
592 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
593 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
594 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
595 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
596 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
597 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
598 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
599 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
600 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
601 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
602 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
603 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
604 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
605 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),
606 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),
607 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
608 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
609 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
610 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
611 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
612 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
613 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),
614 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
615 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
616 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
617 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
618 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
619 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
620 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
621 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
622 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
623 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
624 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
625 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
626 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
627 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
628 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
629 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
630 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
631 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
632 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
633 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
634 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),
635 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),
636 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
637 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
638 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
639 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
640 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
641 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
642 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
643 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
644 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
645 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
646 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
647 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
648 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
649 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
650 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
651 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
652 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
653 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),
654 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),
655 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
656 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
657 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
658 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
659 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
660 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
661 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
662 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
663 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),
664 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),
665 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
666 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
667 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
668 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
669 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
670 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
671 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
672 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
673 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),
674 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),
675 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
676 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
677 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
678 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
679]
680
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000681# One last piece of inspect fodder to check the default line number handling
682def simple(): pass
683expected_opinfo_simple = [
684 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
685 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=3, starts_line=None, is_jump_target=False)
686]
687
688
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000689class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000690
691 def test_default_first_line(self):
692 actual = dis.get_instructions(simple)
693 self.assertEqual(list(actual), expected_opinfo_simple)
694
695 def test_first_line_set_to_None(self):
696 actual = dis.get_instructions(simple, first_line=None)
697 self.assertEqual(list(actual), expected_opinfo_simple)
698
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000699 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000700 actual = dis.get_instructions(outer, first_line=expected_outer_line)
701 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000702
703 def test_nested(self):
704 with captured_stdout():
705 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000706 actual = dis.get_instructions(f, first_line=expected_f_line)
707 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000708
709 def test_doubly_nested(self):
710 with captured_stdout():
711 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000712 actual = dis.get_instructions(inner, first_line=expected_inner_line)
713 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000714
715 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000716 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
717 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000718
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000719# get_instructions has its own tests above, so can rely on it to validate
720# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000721class BytecodeTests(unittest.TestCase):
722 def test_instantiation(self):
723 # Test with function, method, code string and code object
724 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000725 with self.subTest(obj=obj):
726 b = dis.Bytecode(obj)
727 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000728
729 self.assertRaises(TypeError, dis.Bytecode, object())
730
731 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000732 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
733 with self.subTest(obj=obj):
734 via_object = list(dis.Bytecode(obj))
735 via_generator = list(dis.get_instructions(obj))
736 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000737
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000738 def test_explicit_first_line(self):
739 actual = dis.Bytecode(outer, first_line=expected_outer_line)
740 self.assertEqual(list(actual), expected_opinfo_outer)
741
742 def test_source_line_in_disassembly(self):
743 # Use the line in the source code
744 actual = dis.Bytecode(simple).dis()[:3]
745 expected = "{:>3}".format(simple.__code__.co_firstlineno)
746 self.assertEqual(actual, expected)
747 # Use an explicit first line number
748 actual = dis.Bytecode(simple, first_line=350).dis()[:3]
749 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000750
751 def test_info(self):
752 self.maxDiff = 1000
753 for x, expected in CodeInfoTests.test_pairs:
754 b = dis.Bytecode(x)
755 self.assertRegex(b.info(), expected)
756
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000757 def test_disassembled(self):
758 actual = dis.Bytecode(_f).dis()
759 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000760
761
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000762def test_main():
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000763 run_unittest(DisTests, DisWithFileTests, CodeInfoTests,
764 InstructionTests, BytecodeTests)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000765
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000766if __name__ == "__main__":
767 test_main()