blob: 8610c5dcd81b1d0257a63ede1e00eee979d079b0 [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 from test import dis_module
251 self.do_disassembly_test(dis_module, dis_module_expected_results)
252
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000253 def test_disassemble_str(self):
254 self.do_disassembly_test(expr_str, dis_expr_str)
255 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
256 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
257
Benjamin Petersond6afe722011-03-15 14:44:52 -0500258 def test_disassemble_bytes(self):
259 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
260
261 def test_disassemble_method(self):
262 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
263
264 def test_disassemble_method_bytes(self):
265 method_bytecode = _C(1).__init__.__code__.co_code
266 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
267
268 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500269 try:
270 del sys.last_traceback
271 except AttributeError:
272 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500273 self.assertRaises(RuntimeError, dis.dis, None)
274
Benjamin Petersond6afe722011-03-15 14:44:52 -0500275 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500276 try:
277 del sys.last_traceback
278 except AttributeError:
279 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500280
281 try:
282 1/0
283 except Exception as e:
284 tb = e.__traceback__
285 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500286
287 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
288 self.do_disassembly_test(None, tb_dis)
289
290 def test_dis_object(self):
291 self.assertRaises(TypeError, dis.dis, object())
292
Nick Coghlaneae2da12010-08-17 08:03:36 +0000293code_info_code_info = """\
294Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000295Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000296Argument count: 1
297Kw-only arguments: 0
298Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000299Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000300Flags: OPTIMIZED, NEWLOCALS, NOFREE
301Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000302 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000303Names:
304 0: _format_code_info
305 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000306Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000307 0: x""" % (('Formatted details of methods, functions, or code.',)
308 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000309
310@staticmethod
311def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
312 def f(c=c):
313 print(x, y, z, c, d, e, f)
314 yield x, y, z, c, d, e, f
315
Nick Coghlaneae2da12010-08-17 08:03:36 +0000316code_info_tricky = """\
317Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000318Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000319Argument count: 3
320Kw-only arguments: 3
321Number of locals: 8
322Stack size: 7
323Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
324Constants:
325 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000326 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100327 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000328Variable names:
329 0: x
330 1: y
331 2: z
332 3: c
333 4: d
334 5: e
335 6: args
336 7: kwds
337Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100338 0: [edfxyz]
339 1: [edfxyz]
340 2: [edfxyz]
341 3: [edfxyz]
342 4: [edfxyz]
343 5: [edfxyz]"""
344# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000345
346co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000347
348code_info_tricky_nested_f = """\
349Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000350Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000351Argument count: 1
352Kw-only arguments: 0
353Number of locals: 1
354Stack size: 8
355Flags: OPTIMIZED, NEWLOCALS, NESTED
356Constants:
357 0: None
358Names:
359 0: print
360Variable names:
361 0: c
362Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100363 0: [edfxyz]
364 1: [edfxyz]
365 2: [edfxyz]
366 3: [edfxyz]
367 4: [edfxyz]
368 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000369
370code_info_expr_str = """\
371Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000372Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000373Argument count: 0
374Kw-only arguments: 0
375Number of locals: 0
376Stack size: 2
377Flags: NOFREE
378Constants:
379 0: 1
380Names:
381 0: x"""
382
383code_info_simple_stmt_str = """\
384Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000385Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000386Argument count: 0
387Kw-only arguments: 0
388Number of locals: 0
389Stack size: 2
390Flags: NOFREE
391Constants:
392 0: 1
393 1: None
394Names:
395 0: x"""
396
397code_info_compound_stmt_str = """\
398Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000399Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000400Argument count: 0
401Kw-only arguments: 0
402Number of locals: 0
403Stack size: 2
404Flags: NOFREE
405Constants:
406 0: 0
407 1: 1
408 2: None
409Names:
410 0: x"""
411
412class CodeInfoTests(unittest.TestCase):
413 test_pairs = [
414 (dis.code_info, code_info_code_info),
415 (tricky, code_info_tricky),
416 (co_tricky_nested_f, code_info_tricky_nested_f),
417 (expr_str, code_info_expr_str),
418 (simple_stmt_str, code_info_simple_stmt_str),
419 (compound_stmt_str, code_info_compound_stmt_str),
420 ]
421
422 def test_code_info(self):
423 self.maxDiff = 1000
424 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000425 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000426
427 def test_show_code(self):
428 self.maxDiff = 1000
429 for x, expected in self.test_pairs:
430 with captured_stdout() as output:
431 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000432 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000433 output = io.StringIO()
434 dis.show_code(x, file=output)
435 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000436
Benjamin Petersond6afe722011-03-15 14:44:52 -0500437 def test_code_info_object(self):
438 self.assertRaises(TypeError, dis.code_info, object())
439
440 def test_pretty_flags_no_flags(self):
441 self.assertEqual(dis.pretty_flags(0), '0x0')
442
443
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000444# Fodder for instruction introspection tests
445# Editing any of these may require recalculating the expected output
446def outer(a=1, b=2):
447 def f(c=3, d=4):
448 def inner(e=5, f=6):
449 print(a, b, c, d, e, f)
450 print(a, b, c, d)
451 return inner
452 print(a, b, '', 1, [], {}, "Hello world!")
453 return f
454
455def jumpy():
456 # This won't actually run (but that's OK, we only disassemble it)
457 for i in range(10):
458 print(i)
459 if i < 4:
460 continue
461 if i > 6:
462 break
463 else:
464 print("I can haz else clause?")
465 while i:
466 print(i)
467 i -= 1
468 if i > 6:
469 continue
470 if i < 4:
471 break
472 else:
473 print("Who let lolcatz into this test suite?")
474 try:
475 1 / 0
476 except ZeroDivisionError:
477 print("Here we go, here we go, here we go...")
478 else:
479 with i as dodgy:
480 print("Never reach this")
481 finally:
482 print("OK, now we're done")
483
484# End fodder for opinfo generation tests
485expected_outer_offset = 1 - outer.__code__.co_firstlineno
486expected_jumpy_offset = 1 - jumpy.__code__.co_firstlineno
487code_object_f = outer.__code__.co_consts[3]
488code_object_inner = code_object_f.co_consts[3]
489
490# The following lines are useful to regenerate the expected results after
491# either the fodder is modified or the bytecode generation changes
492# After regeneration, update the references to code_object_f and
493# code_object_inner before rerunning the tests
494
495#_instructions = dis.get_instructions(outer, line_offset=expected_outer_offset)
496#print('expected_opinfo_outer = [\n ',
497 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
498#_instructions = dis.get_instructions(outer(), line_offset=expected_outer_offset)
499#print('expected_opinfo_f = [\n ',
500 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
501#_instructions = dis.get_instructions(outer()(), line_offset=expected_outer_offset)
502#print('expected_opinfo_inner = [\n ',
503 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
504#_instructions = dis.get_instructions(jumpy, line_offset=expected_jumpy_offset)
505#print('expected_opinfo_jumpy = [\n ',
506 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
507
508
509Instruction = dis.Instruction
510expected_opinfo_outer = [
511 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=3, argrepr='3', offset=0, starts_line=2, is_jump_target=False),
512 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=3, starts_line=None, is_jump_target=False),
513 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
514 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
515 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=12, starts_line=None, is_jump_target=False),
516 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),
517 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),
518 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=21, starts_line=None, is_jump_target=False),
519 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=24, starts_line=None, is_jump_target=False),
520 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=27, starts_line=7, is_jump_target=False),
521 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=30, starts_line=None, is_jump_target=False),
522 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=33, starts_line=None, is_jump_target=False),
523 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=36, starts_line=None, is_jump_target=False),
524 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=39, starts_line=None, is_jump_target=False),
525 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=42, starts_line=None, is_jump_target=False),
526 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=45, starts_line=None, is_jump_target=False),
527 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=48, starts_line=None, is_jump_target=False),
528 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),
529 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=54, starts_line=None, is_jump_target=False),
530 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=55, starts_line=8, is_jump_target=False),
531 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=58, starts_line=None, is_jump_target=False),
532]
533
534expected_opinfo_f = [
535 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=5, argrepr='5', offset=0, starts_line=3, is_jump_target=False),
536 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=6, argrepr='6', offset=3, starts_line=None, is_jump_target=False),
537 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=6, starts_line=None, is_jump_target=False),
538 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=9, starts_line=None, is_jump_target=False),
539 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=12, starts_line=None, is_jump_target=False),
540 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=15, starts_line=None, is_jump_target=False),
541 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=18, starts_line=None, is_jump_target=False),
542 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),
543 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),
544 Instruction(opname='MAKE_CLOSURE', opcode=134, arg=2, argval=2, argrepr='', offset=27, starts_line=None, is_jump_target=False),
545 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=30, starts_line=None, is_jump_target=False),
546 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=33, starts_line=5, is_jump_target=False),
547 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=36, starts_line=None, is_jump_target=False),
548 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=39, starts_line=None, is_jump_target=False),
549 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=42, starts_line=None, is_jump_target=False),
550 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=45, starts_line=None, is_jump_target=False),
551 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),
552 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=51, starts_line=None, is_jump_target=False),
553 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=52, starts_line=6, is_jump_target=False),
554 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=55, starts_line=None, is_jump_target=False),
555]
556
557expected_opinfo_inner = [
558 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
559 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=3, starts_line=None, is_jump_target=False),
560 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=6, starts_line=None, is_jump_target=False),
561 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=9, starts_line=None, is_jump_target=False),
562 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=12, starts_line=None, is_jump_target=False),
563 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=15, starts_line=None, is_jump_target=False),
564 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=18, starts_line=None, is_jump_target=False),
565 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),
566 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=24, starts_line=None, is_jump_target=False),
567 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=25, starts_line=None, is_jump_target=False),
568 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
569]
570
571expected_opinfo_jumpy = [
572 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=77, argrepr='to 77', offset=0, starts_line=3, is_jump_target=False),
573 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=3, starts_line=None, is_jump_target=False),
574 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=6, starts_line=None, is_jump_target=False),
575 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),
576 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=12, starts_line=None, is_jump_target=False),
577 Instruction(opname='FOR_ITER', opcode=93, arg=50, argval=66, argrepr='to 66', offset=13, starts_line=None, is_jump_target=True),
578 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=16, starts_line=None, is_jump_target=False),
579 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=19, starts_line=4, is_jump_target=False),
580 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=22, starts_line=None, is_jump_target=False),
581 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),
582 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=28, starts_line=None, is_jump_target=False),
583 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=29, starts_line=5, is_jump_target=False),
584 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=32, starts_line=None, is_jump_target=False),
585 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=35, starts_line=None, is_jump_target=False),
586 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=47, argval=47, argrepr='', offset=38, starts_line=None, is_jump_target=False),
587 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=41, starts_line=6, is_jump_target=False),
588 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=47, argrepr='to 47', offset=44, starts_line=None, is_jump_target=False),
589 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=47, starts_line=7, is_jump_target=True),
590 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=50, starts_line=None, is_jump_target=False),
591 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=53, starts_line=None, is_jump_target=False),
592 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=13, argval=13, argrepr='', offset=56, starts_line=None, is_jump_target=False),
593 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=59, starts_line=8, is_jump_target=False),
594 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=60, starts_line=None, is_jump_target=False),
595 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=13, argval=13, argrepr='', offset=63, starts_line=None, is_jump_target=False),
596 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=True),
597 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=67, starts_line=10, is_jump_target=False),
598 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),
599 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),
600 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=76, starts_line=None, is_jump_target=False),
601 Instruction(opname='SETUP_LOOP', opcode=120, arg=74, argval=154, argrepr='to 154', offset=77, starts_line=11, is_jump_target=True),
602 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=None, is_jump_target=True),
603 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=143, argval=143, argrepr='', offset=83, starts_line=None, is_jump_target=False),
604 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=86, starts_line=12, is_jump_target=False),
605 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=89, 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=92, starts_line=None, is_jump_target=False),
607 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=95, starts_line=None, is_jump_target=False),
608 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=96, starts_line=13, is_jump_target=False),
609 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=99, starts_line=None, is_jump_target=False),
610 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=102, starts_line=None, is_jump_target=False),
611 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=103, starts_line=None, is_jump_target=False),
612 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=106, starts_line=14, is_jump_target=False),
613 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=109, starts_line=None, is_jump_target=False),
614 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=112, starts_line=None, is_jump_target=False),
615 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=124, argval=124, argrepr='', offset=115, starts_line=None, is_jump_target=False),
616 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=118, starts_line=15, is_jump_target=False),
617 Instruction(opname='JUMP_FORWARD', opcode=110, arg=0, argval=124, argrepr='to 124', offset=121, starts_line=None, is_jump_target=False),
618 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=124, starts_line=16, is_jump_target=True),
619 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=127, starts_line=None, is_jump_target=False),
620 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=130, starts_line=None, is_jump_target=False),
621 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=133, starts_line=None, is_jump_target=False),
622 Instruction(opname='BREAK_LOOP', opcode=80, arg=None, argval=None, argrepr='', offset=136, starts_line=17, is_jump_target=False),
623 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=137, starts_line=None, is_jump_target=False),
624 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=80, argval=80, argrepr='', offset=140, starts_line=None, is_jump_target=False),
625 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=143, starts_line=None, is_jump_target=True),
626 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=144, starts_line=19, is_jump_target=False),
627 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),
628 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),
629 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=153, starts_line=None, is_jump_target=False),
630 Instruction(opname='SETUP_FINALLY', opcode=122, arg=72, argval=229, argrepr='to 229', offset=154, starts_line=20, is_jump_target=True),
631 Instruction(opname='SETUP_EXCEPT', opcode=121, arg=12, argval=172, argrepr='to 172', offset=157, starts_line=None, is_jump_target=False),
632 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=160, starts_line=21, is_jump_target=False),
633 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=163, starts_line=None, is_jump_target=False),
634 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
635 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=167, starts_line=None, is_jump_target=False),
636 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
637 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=200, argrepr='to 200', offset=169, starts_line=None, is_jump_target=False),
638 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=172, starts_line=22, is_jump_target=True),
639 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=173, starts_line=None, is_jump_target=False),
640 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=176, starts_line=None, is_jump_target=False),
641 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=199, argval=199, argrepr='', offset=179, starts_line=None, is_jump_target=False),
642 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
643 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=183, starts_line=None, is_jump_target=False),
644 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
645 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=185, starts_line=23, is_jump_target=False),
646 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),
647 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),
648 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=194, starts_line=None, is_jump_target=False),
649 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=195, starts_line=None, is_jump_target=False),
650 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=225, argrepr='to 225', offset=196, starts_line=None, is_jump_target=False),
651 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=199, starts_line=None, is_jump_target=True),
652 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=200, starts_line=25, is_jump_target=True),
653 Instruction(opname='SETUP_WITH', opcode=143, arg=17, argval=223, argrepr='to 223', offset=203, starts_line=None, is_jump_target=False),
654 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=206, starts_line=None, is_jump_target=False),
655 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=209, starts_line=26, is_jump_target=False),
656 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),
657 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),
658 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=218, starts_line=None, is_jump_target=False),
659 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=219, starts_line=None, is_jump_target=False),
660 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=220, starts_line=None, is_jump_target=False),
661 Instruction(opname='WITH_CLEANUP', opcode=81, arg=None, argval=None, argrepr='', offset=223, starts_line=None, is_jump_target=True),
662 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=224, starts_line=None, is_jump_target=False),
663 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=225, starts_line=None, is_jump_target=True),
664 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=226, starts_line=None, is_jump_target=False),
665 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=229, starts_line=28, is_jump_target=True),
666 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),
667 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),
668 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=238, starts_line=None, is_jump_target=False),
669 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=239, starts_line=None, is_jump_target=False),
670 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=240, starts_line=None, is_jump_target=False),
671 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=243, starts_line=None, is_jump_target=False),
672]
673
674class InstructionTests(BytecodeTestCase):
675 def test_outer(self):
676 self.assertBytecodeExactlyMatches(outer, expected_opinfo_outer,
677 line_offset=expected_outer_offset)
678
679 def test_nested(self):
680 with captured_stdout():
681 f = outer()
682 self.assertBytecodeExactlyMatches(f, expected_opinfo_f,
683 line_offset=expected_outer_offset)
684
685 def test_doubly_nested(self):
686 with captured_stdout():
687 inner = outer()()
688 self.assertBytecodeExactlyMatches(inner, expected_opinfo_inner,
689 line_offset=expected_outer_offset)
690
691 def test_jumpy(self):
692 self.assertBytecodeExactlyMatches(jumpy, expected_opinfo_jumpy,
693 line_offset=expected_jumpy_offset)
694
695class BytecodeTests(unittest.TestCase):
696 def test_instantiation(self):
697 # Test with function, method, code string and code object
698 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
699 b = dis.Bytecode(obj)
700 self.assertIsInstance(b.codeobj, types.CodeType)
701
702 self.assertRaises(TypeError, dis.Bytecode, object())
703
704 def test_iteration(self):
705 b = dis.Bytecode(_f)
706 for instr in b:
707 self.assertIsInstance(instr, dis.Instruction)
708
709 assert len(list(b)) > 0 # Iterating should yield at least 1 instruction
710
711 def test_info(self):
712 self.maxDiff = 1000
713 for x, expected in CodeInfoTests.test_pairs:
714 b = dis.Bytecode(x)
715 self.assertRegex(b.info(), expected)
716
717 def test_display_code(self):
718 b = dis.Bytecode(_f)
719 output = io.StringIO()
720 b.display_code(file=output)
721 result = [line.rstrip() for line in output.getvalue().splitlines()]
722 self.assertEqual(result, dis_f.splitlines())
723
724
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000725def test_main():
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000726 run_unittest(DisTests, CodeInfoTests, InstructionTests, BytecodeTests)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000727
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000728if __name__ == "__main__":
729 test_main()