blob: a1c658269791d6a5eb15f8b0c2c03f2528ad0b3e [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
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000011
Benjamin Petersond6afe722011-03-15 14:44:52 -050012class _C:
13 def __init__(self, x):
14 self.x = x == 1
15
16dis_c_instance_method = """\
17 %-4d 0 LOAD_FAST 1 (x)
18 3 LOAD_CONST 1 (1)
19 6 COMPARE_OP 2 (==)
20 9 LOAD_FAST 0 (self)
21 12 STORE_ATTR 0 (x)
22 15 LOAD_CONST 0 (None)
23 18 RETURN_VALUE
24""" % (_C.__init__.__code__.co_firstlineno + 1,)
25
26dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100027 0 LOAD_FAST 1 (1)
28 3 LOAD_CONST 1 (1)
29 6 COMPARE_OP 2 (==)
30 9 LOAD_FAST 0 (0)
31 12 STORE_ATTR 0 (0)
32 15 LOAD_CONST 0 (0)
Benjamin Petersond6afe722011-03-15 14:44:52 -050033 18 RETURN_VALUE
34"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000035
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000036def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000037 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000038 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000039
40dis_f = """\
Georg Brandl88fc6642007-02-09 21:28:07 +000041 %-4d 0 LOAD_GLOBAL 0 (print)
42 3 LOAD_FAST 0 (a)
Alexander Belopolsky74482202012-06-07 14:28:14 -040043 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Georg Brandl88fc6642007-02-09 21:28:07 +000044 9 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000045
Georg Brandl88fc6642007-02-09 21:28:07 +000046 %-4d 10 LOAD_CONST 1 (1)
47 13 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000048""" % (_f.__code__.co_firstlineno + 1,
49 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +000050
51
Benjamin Petersond6afe722011-03-15 14:44:52 -050052dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100053 0 LOAD_GLOBAL 0 (0)
54 3 LOAD_FAST 0 (0)
55 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
Benjamin Petersond6afe722011-03-15 14:44:52 -050056 9 POP_TOP
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100057 10 LOAD_CONST 1 (1)
Benjamin Petersond6afe722011-03-15 14:44:52 -050058 13 RETURN_VALUE
59"""
60
61
Michael W. Hudson26848a32003-04-29 17:07:36 +000062def bug708901():
63 for res in range(1,
64 10):
65 pass
66
67dis_bug708901 = """\
68 %-4d 0 SETUP_LOOP 23 (to 26)
69 3 LOAD_GLOBAL 0 (range)
70 6 LOAD_CONST 1 (1)
71
72 %-4d 9 LOAD_CONST 2 (10)
Alexander Belopolsky74482202012-06-07 14:28:14 -040073 12 CALL_FUNCTION 2 (2 positional, 0 keyword pair)
Michael W. Hudson26848a32003-04-29 17:07:36 +000074 15 GET_ITER
75 >> 16 FOR_ITER 6 (to 25)
76 19 STORE_FAST 0 (res)
77
78 %-4d 22 JUMP_ABSOLUTE 16
79 >> 25 POP_BLOCK
80 >> 26 LOAD_CONST 0 (None)
81 29 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +000082""" % (bug708901.__code__.co_firstlineno + 1,
83 bug708901.__code__.co_firstlineno + 2,
84 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000085
Neal Norwitz51abbc72005-12-18 07:06:23 +000086
87def bug1333982(x=[]):
88 assert 0, ([s for s in x] +
89 1)
90 pass
91
92dis_bug1333982 = """\
93 %-4d 0 LOAD_CONST 1 (0)
Antoine Pitrouf289ae62008-12-18 11:06:25 +000094 3 JUMP_IF_TRUE 33 (to 39)
Neal Norwitz51abbc72005-12-18 07:06:23 +000095 6 POP_TOP
96 7 LOAD_GLOBAL 0 (AssertionError)
97 10 BUILD_LIST 0
Antoine Pitrouf289ae62008-12-18 11:06:25 +000098 13 LOAD_FAST 0 (x)
99 16 GET_ITER
100 >> 17 FOR_ITER 12 (to 32)
101 20 STORE_FAST 1 (s)
102 23 LOAD_FAST 1 (s)
103 26 LIST_APPEND 2
104 29 JUMP_ABSOLUTE 17
Neal Norwitz51abbc72005-12-18 07:06:23 +0000105
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000106 %-4d >> 32 LOAD_CONST 2 (1)
107 35 BINARY_ADD
108 36 RAISE_VARARGS 2
109 >> 39 POP_TOP
Neal Norwitz51abbc72005-12-18 07:06:23 +0000110
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000111 %-4d 40 LOAD_CONST 0 (None)
112 43 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000113""" % (bug1333982.__code__.co_firstlineno + 1,
114 bug1333982.__code__.co_firstlineno + 2,
115 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000116
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000117_BIG_LINENO_FORMAT = """\
118%3d 0 LOAD_GLOBAL 0 (spam)
119 3 POP_TOP
120 4 LOAD_CONST 0 (None)
121 7 RETURN_VALUE
122"""
123
Guido van Rossume7ba4952007-06-06 23:52:48 +0000124dis_module_expected_results = """\
125Disassembly of f:
126 4 0 LOAD_CONST 0 (None)
127 3 RETURN_VALUE
128
129Disassembly of g:
130 5 0 LOAD_CONST 0 (None)
131 3 RETURN_VALUE
132
133"""
134
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000135expr_str = "x + 1"
136
137dis_expr_str = """\
138 1 0 LOAD_NAME 0 (x)
139 3 LOAD_CONST 0 (1)
140 6 BINARY_ADD
141 7 RETURN_VALUE
142"""
143
144simple_stmt_str = "x = x + 1"
145
146dis_simple_stmt_str = """\
147 1 0 LOAD_NAME 0 (x)
148 3 LOAD_CONST 0 (1)
149 6 BINARY_ADD
150 7 STORE_NAME 0 (x)
151 10 LOAD_CONST 1 (None)
152 13 RETURN_VALUE
153"""
154
155compound_stmt_str = """\
156x = 0
157while 1:
158 x += 1"""
159# Trailing newline has been deliberately omitted
160
161dis_compound_stmt_str = """\
162 1 0 LOAD_CONST 0 (0)
163 3 STORE_NAME 0 (x)
164
165 2 6 SETUP_LOOP 13 (to 22)
166
167 3 >> 9 LOAD_NAME 0 (x)
168 12 LOAD_CONST 1 (1)
169 15 INPLACE_ADD
170 16 STORE_NAME 0 (x)
171 19 JUMP_ABSOLUTE 9
172 >> 22 LOAD_CONST 2 (None)
173 25 RETURN_VALUE
174"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000175
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000176class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500177
178 def get_disassembly(self, func, lasti=-1, wrapper=True):
Guido van Rossum34d19282007-08-09 01:03:29 +0000179 s = io.StringIO()
Michael W. Hudson26848a32003-04-29 17:07:36 +0000180 save_stdout = sys.stdout
181 sys.stdout = s
Benjamin Petersond6afe722011-03-15 14:44:52 -0500182 try:
183 if wrapper:
184 dis.dis(func)
185 else:
186 dis.disassemble(func, lasti)
187 finally:
188 sys.stdout = save_stdout
Michael W. Hudson26848a32003-04-29 17:07:36 +0000189 # Trim trailing blanks (if any).
Benjamin Petersond6afe722011-03-15 14:44:52 -0500190 return [line.rstrip() for line in s.getvalue().splitlines()]
191
192 def get_disassemble_as_string(self, func, lasti=-1):
193 return '\n'.join(self.get_disassembly(func, lasti, False))
194
195 def do_disassembly_test(self, func, expected):
196 lines = self.get_disassembly(func)
197 expected = expected.splitlines()
Michael W. Hudson26848a32003-04-29 17:07:36 +0000198 if expected != lines:
199 self.fail(
200 "events did not match expectation:\n" +
201 "\n".join(difflib.ndiff(expected,
202 lines)))
203
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000204 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500205 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000206 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
207 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000208
209 def test_opname(self):
210 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
211
212 def test_boundaries(self):
213 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
214 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
215
216 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000217 self.do_disassembly_test(_f, dis_f)
218
219 def test_bug_708901(self):
220 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000221
Neal Norwitz51abbc72005-12-18 07:06:23 +0000222 def test_bug_1333982(self):
Guido van Rossume7ba4952007-06-06 23:52:48 +0000223 # XXX: re-enable this test!
Tim Peters83a8c392005-12-25 22:52:32 +0000224 # This one is checking bytecodes generated for an `assert` statement,
225 # so fails if the tests are run with -O. Skip this test then.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000226 pass # Test has been disabled due to change in the way
227 # list comps are handled. The byte code now includes
228 # a memory address and a file location, so they change from
229 # run to run.
230 # if __debug__:
231 # self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000232
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 def test_big_linenos(self):
234 def func(count):
235 namespace = {}
236 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000237 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 return namespace['foo']
239
240 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000241 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242 expected = _BIG_LINENO_FORMAT % (i + 2)
243 self.do_disassembly_test(func(i), expected)
244
245 # Test some larger ranges too
Guido van Rossum805365e2007-05-07 22:24:25 +0000246 for i in range(300, 5000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000247 expected = _BIG_LINENO_FORMAT % (i + 2)
248 self.do_disassembly_test(func(i), expected)
249
Guido van Rossume7ba4952007-06-06 23:52:48 +0000250 def test_big_linenos(self):
251 from test import dis_module
252 self.do_disassembly_test(dis_module, dis_module_expected_results)
253
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000254 def test_disassemble_str(self):
255 self.do_disassembly_test(expr_str, dis_expr_str)
256 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
257 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
258
Benjamin Petersond6afe722011-03-15 14:44:52 -0500259 def test_disassemble_bytes(self):
260 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
261
262 def test_disassemble_method(self):
263 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
264
265 def test_disassemble_method_bytes(self):
266 method_bytecode = _C(1).__init__.__code__.co_code
267 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
268
269 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500270 try:
271 del sys.last_traceback
272 except AttributeError:
273 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500274 self.assertRaises(RuntimeError, dis.dis, None)
275
276 def test_dis_object(self):
277 self.assertRaises(TypeError, dis.dis, object())
278
279 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500280 try:
281 del sys.last_traceback
282 except AttributeError:
283 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500284
285 try:
286 1/0
287 except Exception as e:
288 tb = e.__traceback__
289 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500290
291 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
292 self.do_disassembly_test(None, tb_dis)
293
294 def test_dis_object(self):
295 self.assertRaises(TypeError, dis.dis, object())
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
489expected_outer_offset = 1 - outer.__code__.co_firstlineno
490expected_jumpy_offset = 1 - jumpy.__code__.co_firstlineno
491code_object_f = outer.__code__.co_consts[3]
492code_object_inner = code_object_f.co_consts[3]
493
494# The following lines are useful to regenerate the expected results after
495# either the fodder is modified or the bytecode generation changes
496# After regeneration, update the references to code_object_f and
497# code_object_inner before rerunning the tests
498
499#_instructions = dis.get_instructions(outer, line_offset=expected_outer_offset)
500#print('expected_opinfo_outer = [\n ',
501 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
502#_instructions = dis.get_instructions(outer(), line_offset=expected_outer_offset)
503#print('expected_opinfo_f = [\n ',
504 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
505#_instructions = dis.get_instructions(outer()(), line_offset=expected_outer_offset)
506#print('expected_opinfo_inner = [\n ',
507 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
508#_instructions = dis.get_instructions(jumpy, line_offset=expected_jumpy_offset)
509#print('expected_opinfo_jumpy = [\n ',
510 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
511
512
513Instruction = dis.Instruction
514expected_opinfo_outer = [
515 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
516 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
517 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
518 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
519 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
520 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),
521 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),
522 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
523 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
524 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
525 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
526 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
527 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
528 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
529 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
530 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
531 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
532 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),
533 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
534 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
535 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
536]
537
538expected_opinfo_f = [
539 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
540 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
541 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
542 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
543 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
544 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
545 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
546 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),
547 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),
548 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
549 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
550 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
551 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
552 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
553 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
554 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
555 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),
556 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
557 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
558 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
559]
560
561expected_opinfo_inner = [
562 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
563 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
564 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
565 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
566 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
567 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
568 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
569 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),
570 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
571 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
572 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
573]
574
575expected_opinfo_jumpy = [
576 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
577 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
578 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
579 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),
580 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
581 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
582 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
583 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
584 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
585 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),
586 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
587 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
588 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
589 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
590 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
591 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
592 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
593 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
594 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
595 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
596 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
597 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
598 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
599 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
600 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
601 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
602 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),
603 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),
604 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
605 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
606 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
607 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
608 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
609 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, starts_line=None, is_jump_target=False),
610 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),
611 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
612 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
613 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
614 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
615 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
616 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
617 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
618 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
619 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
620 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
621 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
622 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
623 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
624 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
625 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
626 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
627 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
628 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
629 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
630 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
631 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),
632 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),
633 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
634 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
635 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
636 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
637 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
638 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
639 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
640 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
641 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
642 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
643 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
644 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
645 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
646 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
647 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
648 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
649 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
650 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),
651 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),
652 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
653 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
654 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
655 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
656 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
657 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
658 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
659 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
660 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),
661 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),
662 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
663 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
664 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
665 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
666 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
667 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
668 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
669 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
670 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),
671 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),
672 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
673 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
674 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
675 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
676]
677
678class InstructionTests(BytecodeTestCase):
679 def test_outer(self):
680 self.assertBytecodeExactlyMatches(outer, expected_opinfo_outer,
681 line_offset=expected_outer_offset)
682
683 def test_nested(self):
684 with captured_stdout():
685 f = outer()
686 self.assertBytecodeExactlyMatches(f, expected_opinfo_f,
687 line_offset=expected_outer_offset)
688
689 def test_doubly_nested(self):
690 with captured_stdout():
691 inner = outer()()
692 self.assertBytecodeExactlyMatches(inner, expected_opinfo_inner,
693 line_offset=expected_outer_offset)
694
695 def test_jumpy(self):
696 self.assertBytecodeExactlyMatches(jumpy, expected_opinfo_jumpy,
697 line_offset=expected_jumpy_offset)
698
699class BytecodeTests(unittest.TestCase):
700 def test_instantiation(self):
701 # Test with function, method, code string and code object
702 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
703 b = dis.Bytecode(obj)
704 self.assertIsInstance(b.codeobj, types.CodeType)
705
706 self.assertRaises(TypeError, dis.Bytecode, object())
707
708 def test_iteration(self):
709 b = dis.Bytecode(_f)
710 for instr in b:
711 self.assertIsInstance(instr, dis.Instruction)
712
713 assert len(list(b)) > 0 # Iterating should yield at least 1 instruction
714
715 def test_info(self):
716 self.maxDiff = 1000
717 for x, expected in CodeInfoTests.test_pairs:
718 b = dis.Bytecode(x)
719 self.assertRegex(b.info(), expected)
720
721 def test_display_code(self):
722 b = dis.Bytecode(_f)
723 output = io.StringIO()
724 b.display_code(file=output)
725 result = [line.rstrip() for line in output.getvalue().splitlines()]
726 self.assertEqual(result, dis_f.splitlines())
727
728
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000729def test_main():
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000730 run_unittest(DisTests, CodeInfoTests, InstructionTests, BytecodeTests)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000731
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000732if __name__ == "__main__":
733 test_main()