blob: 5c1595268c50536b9d89ac443b54283a1a12c46a [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Zachary Ware38c707e2015-04-13 15:00:43 -05003from test.support import captured_stdout
Joannah Nanjekye92777d52019-09-12 10:02:59 +01004from test.support.bytecode_helper import BytecodeTestCase
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import unittest
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00006import sys
7import dis
Guido van Rossum34d19282007-08-09 01:03:29 +00008import io
Zachary Waree80e8062013-12-26 09:53:49 -06009import re
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100010import types
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100011import contextlib
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000012
Nick Coghlan50c48b82013-11-23 00:57:00 +100013def get_tb():
14 def _error():
15 try:
16 1 / 0
17 except Exception as e:
18 tb = e.__traceback__
19 return tb
20
21 tb = _error()
22 while tb.tb_next:
23 tb = tb.tb_next
24 return tb
25
26TRACEBACK_CODE = get_tb().tb_frame.f_code
27
Benjamin Petersond6afe722011-03-15 14:44:52 -050028class _C:
29 def __init__(self, x):
30 self.x = x == 1
31
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030032 @staticmethod
33 def sm(x):
34 x = x == 1
35
36 @classmethod
37 def cm(cls, x):
38 cls.x = x == 1
39
Benjamin Petersond6afe722011-03-15 14:44:52 -050040dis_c_instance_method = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030041%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030042 2 LOAD_CONST 1 (1)
43 4 COMPARE_OP 2 (==)
44 6 LOAD_FAST 0 (self)
45 8 STORE_ATTR 0 (x)
46 10 LOAD_CONST 0 (None)
47 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050048""" % (_C.__init__.__code__.co_firstlineno + 1,)
49
50dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100051 0 LOAD_FAST 1 (1)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030052 2 LOAD_CONST 1 (1)
53 4 COMPARE_OP 2 (==)
54 6 LOAD_FAST 0 (0)
55 8 STORE_ATTR 0 (0)
56 10 LOAD_CONST 0 (0)
57 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050058"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000059
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030060dis_c_class_method = """\
61%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030062 2 LOAD_CONST 1 (1)
63 4 COMPARE_OP 2 (==)
64 6 LOAD_FAST 0 (cls)
65 8 STORE_ATTR 0 (x)
66 10 LOAD_CONST 0 (None)
67 12 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030068""" % (_C.cm.__code__.co_firstlineno + 2,)
69
70dis_c_static_method = """\
71%3d 0 LOAD_FAST 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030072 2 LOAD_CONST 1 (1)
73 4 COMPARE_OP 2 (==)
74 6 STORE_FAST 0 (x)
75 8 LOAD_CONST 0 (None)
76 10 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030077""" % (_C.sm.__code__.co_firstlineno + 2,)
78
79# Class disassembling info has an extra newline at end.
80dis_c = """\
81Disassembly of %s:
82%s
83Disassembly of %s:
84%s
85Disassembly of %s:
86%s
87""" % (_C.__init__.__name__, dis_c_instance_method,
88 _C.cm.__name__, dis_c_class_method,
89 _C.sm.__name__, dis_c_static_method)
90
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000091def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000093 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000094
95dis_f = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030096%3d 0 LOAD_GLOBAL 0 (print)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030097 2 LOAD_FAST 0 (a)
Victor Stinnerf9b760f2016-09-09 10:17:08 -070098 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030099 6 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000100
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300101%3d 8 LOAD_CONST 1 (1)
102 10 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000103""" % (_f.__code__.co_firstlineno + 1,
104 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000105
106
Benjamin Petersond6afe722011-03-15 14:44:52 -0500107dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000108 0 LOAD_GLOBAL 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300109 2 LOAD_FAST 0 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700110 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300111 6 POP_TOP
112 8 LOAD_CONST 1 (1)
113 10 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -0500114"""
115
116
Michael W. Hudson26848a32003-04-29 17:07:36 +0000117def bug708901():
118 for res in range(1,
119 10):
120 pass
121
122dis_bug708901 = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200123%3d 0 LOAD_GLOBAL 0 (range)
124 2 LOAD_CONST 1 (1)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000125
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200126%3d 4 LOAD_CONST 2 (10)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300127
128%3d 6 CALL_FUNCTION 2
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200129 8 GET_ITER
130 >> 10 FOR_ITER 4 (to 16)
131 12 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000132
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200133%3d 14 JUMP_ABSOLUTE 10
134 >> 16 LOAD_CONST 0 (None)
135 18 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000136""" % (bug708901.__code__.co_firstlineno + 1,
137 bug708901.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300138 bug708901.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000139 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000140
Neal Norwitz51abbc72005-12-18 07:06:23 +0000141
142def bug1333982(x=[]):
143 assert 0, ([s for s in x] +
144 1)
145 pass
146
147dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600148%3d 0 LOAD_CONST 1 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300149 2 POP_JUMP_IF_TRUE 26
Zackery Spytzce6a0702019-08-25 03:44:09 -0600150 4 LOAD_ASSERTION_ERROR
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300151 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153 10 MAKE_FUNCTION 0
154 12 LOAD_FAST 0 (x)
155 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700156 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300157
158%3d 18 LOAD_CONST 4 (1)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300159
160%3d 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700161 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300162 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000163
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300164%3d >> 26 LOAD_CONST 0 (None)
165 28 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600167 __file__,
168 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000169 bug1333982.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300170 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000171 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000172
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173_BIG_LINENO_FORMAT = """\
174%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300175 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300177 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178"""
179
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300180_BIG_LINENO_FORMAT2 = """\
181%4d 0 LOAD_GLOBAL 0 (spam)
182 2 POP_TOP
183 4 LOAD_CONST 0 (None)
184 6 RETURN_VALUE
185"""
186
Guido van Rossume7ba4952007-06-06 23:52:48 +0000187dis_module_expected_results = """\
188Disassembly of f:
189 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300190 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000191
192Disassembly of g:
193 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300194 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000195
196"""
197
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000198expr_str = "x + 1"
199
200dis_expr_str = """\
201 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300202 2 LOAD_CONST 0 (1)
203 4 BINARY_ADD
204 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000205"""
206
207simple_stmt_str = "x = x + 1"
208
209dis_simple_stmt_str = """\
210 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300211 2 LOAD_CONST 0 (1)
212 4 BINARY_ADD
213 6 STORE_NAME 0 (x)
214 8 LOAD_CONST 1 (None)
215 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000216"""
217
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700218annot_stmt_str = """\
219
220x: int = 1
221y: fun(1)
222lst[fun(0)]: int = 1
223"""
224# leading newline is for a reason (tests lineno)
225
226dis_annot_stmt_str = """\
227 2 0 SETUP_ANNOTATIONS
228 2 LOAD_CONST 0 (1)
229 4 STORE_NAME 0 (x)
230 6 LOAD_NAME 1 (int)
Mark Shannon332cd5e2018-01-30 00:41:04 +0000231 8 LOAD_NAME 2 (__annotations__)
232 10 LOAD_CONST 1 ('x')
233 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700234
Mark Shannon332cd5e2018-01-30 00:41:04 +0000235 3 14 LOAD_NAME 3 (fun)
236 16 LOAD_CONST 0 (1)
237 18 CALL_FUNCTION 1
238 20 LOAD_NAME 2 (__annotations__)
239 22 LOAD_CONST 2 ('y')
240 24 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700241
Mark Shannon332cd5e2018-01-30 00:41:04 +0000242 4 26 LOAD_CONST 0 (1)
243 28 LOAD_NAME 4 (lst)
244 30 LOAD_NAME 3 (fun)
245 32 LOAD_CONST 3 (0)
246 34 CALL_FUNCTION 1
247 36 STORE_SUBSCR
248 38 LOAD_NAME 1 (int)
249 40 POP_TOP
250 42 LOAD_CONST 4 (None)
251 44 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700252"""
253
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000254compound_stmt_str = """\
255x = 0
256while 1:
257 x += 1"""
258# Trailing newline has been deliberately omitted
259
260dis_compound_stmt_str = """\
261 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300262 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000263
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200264 3 >> 4 LOAD_NAME 0 (x)
265 6 LOAD_CONST 1 (1)
266 8 INPLACE_ADD
267 10 STORE_NAME 0 (x)
268 12 JUMP_ABSOLUTE 4
269 14 LOAD_CONST 2 (None)
270 16 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000271"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000272
Nick Coghlan50c48b82013-11-23 00:57:00 +1000273dis_traceback = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200274%3d 0 SETUP_FINALLY 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000275
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300276%3d 2 LOAD_CONST 1 (1)
277 4 LOAD_CONST 2 (0)
278 --> 6 BINARY_TRUE_DIVIDE
279 8 POP_TOP
280 10 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +0000281 12 JUMP_FORWARD 44 (to 58)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300283%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000284 16 LOAD_GLOBAL 0 (Exception)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300285 18 COMPARE_OP 10 (exception match)
Mark Shannonfee55262019-11-21 09:11:43 +0000286 20 POP_JUMP_IF_FALSE 56
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300287 22 POP_TOP
288 24 STORE_FAST 0 (e)
289 26 POP_TOP
Mark Shannonfee55262019-11-21 09:11:43 +0000290 28 SETUP_FINALLY 18 (to 48)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000291
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300292%3d 30 LOAD_FAST 0 (e)
293 32 LOAD_ATTR 1 (__traceback__)
294 34 STORE_FAST 1 (tb)
295 36 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +0000296 38 POP_EXCEPT
297 40 LOAD_CONST 0 (None)
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200298 42 STORE_FAST 0 (e)
299 44 DELETE_FAST 0 (e)
Mark Shannonfee55262019-11-21 09:11:43 +0000300 46 JUMP_FORWARD 10 (to 58)
301 >> 48 LOAD_CONST 0 (None)
302 50 STORE_FAST 0 (e)
303 52 DELETE_FAST 0 (e)
304 54 RERAISE
305 >> 56 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000306
Mark Shannonfee55262019-11-21 09:11:43 +0000307%3d >> 58 LOAD_FAST 1 (tb)
308 60 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000309""" % (TRACEBACK_CODE.co_firstlineno + 1,
310 TRACEBACK_CODE.co_firstlineno + 2,
311 TRACEBACK_CODE.co_firstlineno + 3,
312 TRACEBACK_CODE.co_firstlineno + 4,
313 TRACEBACK_CODE.co_firstlineno + 5)
314
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300315def _fstring(a, b, c, d):
316 return f'{a} {b:4} {c!r} {d!r:4}'
317
318dis_fstring = """\
319%3d 0 LOAD_FAST 0 (a)
320 2 FORMAT_VALUE 0
321 4 LOAD_CONST 1 (' ')
322 6 LOAD_FAST 1 (b)
323 8 LOAD_CONST 2 ('4')
324 10 FORMAT_VALUE 4 (with format)
325 12 LOAD_CONST 1 (' ')
326 14 LOAD_FAST 2 (c)
327 16 FORMAT_VALUE 2 (repr)
328 18 LOAD_CONST 1 (' ')
329 20 LOAD_FAST 3 (d)
330 22 LOAD_CONST 2 ('4')
331 24 FORMAT_VALUE 6 (repr, with format)
332 26 BUILD_STRING 7
333 28 RETURN_VALUE
334""" % (_fstring.__code__.co_firstlineno + 1,)
335
Nick Coghlanefd5df92014-07-25 23:02:56 +1000336def _g(x):
337 yield x
338
syncosmicfe2b56a2017-08-17 19:29:21 -0700339async def _ag(x):
340 yield x
341
342async def _co(x):
343 async for item in _ag(x):
344 pass
345
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300346def _h(y):
347 def foo(x):
348 '''funcdoc'''
349 return [x + z for z in y]
350 return foo
351
352dis_nested_0 = """\
353%3d 0 LOAD_CLOSURE 0 (y)
354 2 BUILD_TUPLE 1
355 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
356 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200357 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300358 10 STORE_FAST 1 (foo)
359
360%3d 12 LOAD_FAST 1 (foo)
361 14 RETURN_VALUE
362""" % (_h.__code__.co_firstlineno + 1,
363 __file__,
364 _h.__code__.co_firstlineno + 1,
365 _h.__code__.co_firstlineno + 4,
366)
367
368dis_nested_1 = """%s
369Disassembly of <code object foo at 0x..., file "%s", line %d>:
370%3d 0 LOAD_CLOSURE 0 (x)
371 2 BUILD_TUPLE 1
372 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
373 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200374 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300375 10 LOAD_DEREF 1 (y)
376 12 GET_ITER
377 14 CALL_FUNCTION 1
378 16 RETURN_VALUE
379""" % (dis_nested_0,
380 __file__,
381 _h.__code__.co_firstlineno + 1,
382 _h.__code__.co_firstlineno + 3,
383 __file__,
384 _h.__code__.co_firstlineno + 3,
385)
386
387dis_nested_2 = """%s
388Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
389%3d 0 BUILD_LIST 0
390 2 LOAD_FAST 0 (.0)
391 >> 4 FOR_ITER 12 (to 18)
392 6 STORE_FAST 1 (z)
393 8 LOAD_DEREF 0 (x)
394 10 LOAD_FAST 1 (z)
395 12 BINARY_ADD
396 14 LIST_APPEND 2
397 16 JUMP_ABSOLUTE 4
398 >> 18 RETURN_VALUE
399""" % (dis_nested_1,
400 __file__,
401 _h.__code__.co_firstlineno + 3,
402 _h.__code__.co_firstlineno + 3,
403)
404
syncosmicfe2b56a2017-08-17 19:29:21 -0700405
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000406class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500407
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300408 maxDiff = None
409
410 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000411 # We want to test the default printing behaviour, not the file arg
412 output = io.StringIO()
413 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500414 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300415 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500416 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300417 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000418 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500419
420 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000421 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500422
Zachary Warebb4b7c12013-12-26 09:55:24 -0600423 def strip_addresses(self, text):
424 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500425
426 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300427 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600428 if got != expected:
429 got = self.strip_addresses(got)
430 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000431
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000432 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500433 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000434 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
435 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000436
437 def test_opname(self):
438 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
439
440 def test_boundaries(self):
441 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
442 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
443
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300444 def test_widths(self):
445 for opcode, opname in enumerate(dis.opname):
446 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
447 'BUILD_TUPLE_UNPACK_WITH_CALL'):
448 continue
449 with self.subTest(opname=opname):
450 width = dis._OPNAME_WIDTH
451 if opcode < dis.HAVE_ARGUMENT:
452 width += 1 + dis._OPARG_WIDTH
453 self.assertLessEqual(len(opname), width)
454
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000455 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000456 self.do_disassembly_test(_f, dis_f)
457
458 def test_bug_708901(self):
459 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000460
Neal Norwitz51abbc72005-12-18 07:06:23 +0000461 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000462 # This one is checking bytecodes generated for an `assert` statement,
463 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600464 if not __debug__:
465 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600466
Zachary Waree80e8062013-12-26 09:53:49 -0600467 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000468
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469 def test_big_linenos(self):
470 def func(count):
471 namespace = {}
472 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000473 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000474 return namespace['foo']
475
476 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000477 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 expected = _BIG_LINENO_FORMAT % (i + 2)
479 self.do_disassembly_test(func(i), expected)
480
481 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300482 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 expected = _BIG_LINENO_FORMAT % (i + 2)
484 self.do_disassembly_test(func(i), expected)
485
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300486 for i in range(1000, 5000, 10):
487 expected = _BIG_LINENO_FORMAT2 % (i + 2)
488 self.do_disassembly_test(func(i), expected)
489
Guido van Rossume7ba4952007-06-06 23:52:48 +0000490 from test import dis_module
491 self.do_disassembly_test(dis_module, dis_module_expected_results)
492
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300493 def test_big_offsets(self):
494 def func(count):
495 namespace = {}
496 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
497 exec(func, namespace)
498 return namespace['foo']
499
500 def expected(count, w):
501 s = ['''\
502 %*d LOAD_FAST 0 (x)
503 %*d LOAD_CONST 1 (1)
504 %*d BINARY_ADD
505 %*d STORE_FAST 0 (x)
506''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
507 for i in range(count)]
508 s += ['''\
509
510 3 %*d LOAD_FAST 0 (x)
511 %*d RETURN_VALUE
512''' % (w, 8*count, w, 8*count + 2)]
513 s[0] = ' 2' + s[0][3:]
514 return ''.join(s)
515
516 for i in range(1, 5):
517 self.do_disassembly_test(func(i), expected(i, 4))
518 self.do_disassembly_test(func(1249), expected(1249, 4))
519 self.do_disassembly_test(func(1250), expected(1250, 5))
520
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000521 def test_disassemble_str(self):
522 self.do_disassembly_test(expr_str, dis_expr_str)
523 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700524 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000525 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
526
Benjamin Petersond6afe722011-03-15 14:44:52 -0500527 def test_disassemble_bytes(self):
528 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
529
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300530 def test_disassemble_class(self):
531 self.do_disassembly_test(_C, dis_c)
532
533 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500534 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
535
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300536 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500537 method_bytecode = _C(1).__init__.__code__.co_code
538 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
539
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300540 def test_disassemble_static_method(self):
541 self.do_disassembly_test(_C.sm, dis_c_static_method)
542
543 def test_disassemble_class_method(self):
544 self.do_disassembly_test(_C.cm, dis_c_class_method)
545
Nick Coghlanefd5df92014-07-25 23:02:56 +1000546 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700547 gen_func_disas = self.get_disassembly(_g) # Generator function
548 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000549 self.assertEqual(gen_disas, gen_func_disas)
550
syncosmicfe2b56a2017-08-17 19:29:21 -0700551 def test_disassemble_async_generator(self):
552 agen_func_disas = self.get_disassembly(_ag) # Async generator function
553 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
554 self.assertEqual(agen_disas, agen_func_disas)
555
556 def test_disassemble_coroutine(self):
557 coro_func_disas = self.get_disassembly(_co) # Coroutine function
558 coro = _co(1) # Coroutine object
559 coro.close() # Avoid a RuntimeWarning (never awaited)
560 coro_disas = self.get_disassembly(coro)
561 self.assertEqual(coro_disas, coro_func_disas)
562
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300563 def test_disassemble_fstring(self):
564 self.do_disassembly_test(_fstring, dis_fstring)
565
Benjamin Petersond6afe722011-03-15 14:44:52 -0500566 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500567 try:
568 del sys.last_traceback
569 except AttributeError:
570 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500571 self.assertRaises(RuntimeError, dis.dis, None)
572
Benjamin Petersond6afe722011-03-15 14:44:52 -0500573 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500574 try:
575 del sys.last_traceback
576 except AttributeError:
577 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500578
579 try:
580 1/0
581 except Exception as e:
582 tb = e.__traceback__
583 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500584
585 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
586 self.do_disassembly_test(None, tb_dis)
587
588 def test_dis_object(self):
589 self.assertRaises(TypeError, dis.dis, object())
590
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300591 def test_disassemble_recursive(self):
592 def check(expected, **kwargs):
593 dis = self.get_disassembly(_h, **kwargs)
594 dis = self.strip_addresses(dis)
595 self.assertEqual(dis, expected)
596
597 check(dis_nested_0, depth=0)
598 check(dis_nested_1, depth=1)
599 check(dis_nested_2, depth=2)
600 check(dis_nested_2, depth=3)
601 check(dis_nested_2, depth=None)
602 check(dis_nested_2)
603
604
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000605class DisWithFileTests(DisTests):
606
607 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300608 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000609 output = io.StringIO()
610 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300611 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000612 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300613 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000614 return output.getvalue()
615
616
617
Nick Coghlaneae2da12010-08-17 08:03:36 +0000618code_info_code_info = """\
619Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000620Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000621Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100622Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000623Kw-only arguments: 0
624Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000625Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000626Flags: OPTIMIZED, NEWLOCALS, NOFREE
627Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000628 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000629Names:
630 0: _format_code_info
631 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000632Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000633 0: x""" % (('Formatted details of methods, functions, or code.',)
634 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000635
636@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100637def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000638 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100639 print(a, b, x, y, z, c, d, e, f)
640 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000641
Nick Coghlaneae2da12010-08-17 08:03:36 +0000642code_info_tricky = """\
643Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000644Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100645Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100646Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000647Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100648Number of locals: 10
649Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000650Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
651Constants:
652 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000653 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100654 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000655Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100656 0: a
657 1: b
658 2: x
659 3: y
660 4: z
661 5: c
662 6: d
663 7: e
664 8: args
665 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000666Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100667 0: [abedfxyz]
668 1: [abedfxyz]
669 2: [abedfxyz]
670 3: [abedfxyz]
671 4: [abedfxyz]
672 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100673# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000674
675co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000676
677code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000678Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000679Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100680Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000681Kw-only arguments: 0
682Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100683Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000684Flags: OPTIMIZED, NEWLOCALS, NESTED
685Constants:
686 0: None
687Names:
688 0: print
689Variable names:
690 0: c
691Free variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100692 0: [abedfxyz]
693 1: [abedfxyz]
694 2: [abedfxyz]
695 3: [abedfxyz]
696 4: [abedfxyz]
697 5: [abedfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000698
699code_info_expr_str = """\
700Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000701Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000702Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100703Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000704Kw-only arguments: 0
705Number of locals: 0
706Stack size: 2
707Flags: NOFREE
708Constants:
709 0: 1
710Names:
711 0: x"""
712
713code_info_simple_stmt_str = """\
714Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000715Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000716Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100717Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000718Kw-only arguments: 0
719Number of locals: 0
720Stack size: 2
721Flags: NOFREE
722Constants:
723 0: 1
724 1: None
725Names:
726 0: x"""
727
728code_info_compound_stmt_str = """\
729Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000730Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000731Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100732Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000733Kw-only arguments: 0
734Number of locals: 0
735Stack size: 2
736Flags: NOFREE
737Constants:
738 0: 0
739 1: 1
740 2: None
741Names:
742 0: x"""
743
Yury Selivanov75445082015-05-11 22:57:16 -0400744
745async def async_def():
746 await 1
747 async for a in b: pass
748 async with c as d: pass
749
750code_info_async_def = """\
751Name: async_def
752Filename: (.*)
753Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100754Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400755Kw-only arguments: 0
756Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000757Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700758Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400759Constants:
760 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200761 1: 1
762Names:
763 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200764 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200765Variable names:
766 0: a
767 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400768
Nick Coghlaneae2da12010-08-17 08:03:36 +0000769class CodeInfoTests(unittest.TestCase):
770 test_pairs = [
771 (dis.code_info, code_info_code_info),
772 (tricky, code_info_tricky),
773 (co_tricky_nested_f, code_info_tricky_nested_f),
774 (expr_str, code_info_expr_str),
775 (simple_stmt_str, code_info_simple_stmt_str),
776 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400777 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000778 ]
779
780 def test_code_info(self):
781 self.maxDiff = 1000
782 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000783 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000784
785 def test_show_code(self):
786 self.maxDiff = 1000
787 for x, expected in self.test_pairs:
788 with captured_stdout() as output:
789 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000790 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000791 output = io.StringIO()
792 dis.show_code(x, file=output)
793 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000794
Benjamin Petersond6afe722011-03-15 14:44:52 -0500795 def test_code_info_object(self):
796 self.assertRaises(TypeError, dis.code_info, object())
797
798 def test_pretty_flags_no_flags(self):
799 self.assertEqual(dis.pretty_flags(0), '0x0')
800
801
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000802# Fodder for instruction introspection tests
803# Editing any of these may require recalculating the expected output
804def outer(a=1, b=2):
805 def f(c=3, d=4):
806 def inner(e=5, f=6):
807 print(a, b, c, d, e, f)
808 print(a, b, c, d)
809 return inner
810 print(a, b, '', 1, [], {}, "Hello world!")
811 return f
812
813def jumpy():
814 # This won't actually run (but that's OK, we only disassemble it)
815 for i in range(10):
816 print(i)
817 if i < 4:
818 continue
819 if i > 6:
820 break
821 else:
822 print("I can haz else clause?")
823 while i:
824 print(i)
825 i -= 1
826 if i > 6:
827 continue
828 if i < 4:
829 break
830 else:
831 print("Who let lolcatz into this test suite?")
832 try:
833 1 / 0
834 except ZeroDivisionError:
835 print("Here we go, here we go, here we go...")
836 else:
837 with i as dodgy:
838 print("Never reach this")
839 finally:
840 print("OK, now we're done")
841
842# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000843expected_outer_line = 1
844_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000845code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000846expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000847code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000848expected_inner_line = code_object_inner.co_firstlineno - _line_offset
849expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000850
851# The following lines are useful to regenerate the expected results after
852# either the fodder is modified or the bytecode generation changes
853# After regeneration, update the references to code_object_f and
854# code_object_inner before rerunning the tests
855
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000856#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000857#print('expected_opinfo_outer = [\n ',
858 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200859#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000860#print('expected_opinfo_f = [\n ',
861 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200862#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000863#print('expected_opinfo_inner = [\n ',
864 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000865#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000866#print('expected_opinfo_jumpy = [\n ',
867 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
868
869
870Instruction = dis.Instruction
871expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300872 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
873 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
874 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
875 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
876 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_f, argrepr=repr(code_object_f), offset=8, starts_line=None, is_jump_target=False),
877 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f', argrepr="'outer.<locals>.f'", offset=10, starts_line=None, is_jump_target=False),
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200878 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=12, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300879 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
880 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
881 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
882 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
883 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
884 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
885 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
886 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
887 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval='Hello world!', argrepr="'Hello world!'", offset=30, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700888 Instruction(opname='CALL_FUNCTION', opcode=131, arg=7, argval=7, argrepr='', offset=32, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300889 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
890 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
891 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=38, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000892]
893
894expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300895 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
896 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
897 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
898 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
899 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
900 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
901 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=code_object_inner, argrepr=repr(code_object_inner), offset=12, starts_line=None, is_jump_target=False),
902 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='outer.<locals>.f.<locals>.inner', argrepr="'outer.<locals>.f.<locals>.inner'", offset=14, starts_line=None, is_jump_target=False),
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200903 Instruction(opname='MAKE_FUNCTION', opcode=132, arg=9, argval=9, argrepr='defaults, closure', offset=16, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300904 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
905 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
906 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
907 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
908 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
909 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='d', argrepr='d', offset=28, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700910 Instruction(opname='CALL_FUNCTION', opcode=131, arg=4, argval=4, argrepr='', offset=30, starts_line=None, is_jump_target=False),
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300911 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
912 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
913 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=36, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000914]
915
916expected_opinfo_inner = [
917 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=0, starts_line=4, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300918 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
919 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
920 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
921 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
922 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
923 Instruction(opname='LOAD_FAST', opcode=124, arg=1, argval='f', argrepr='f', offset=12, starts_line=None, is_jump_target=False),
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700924 Instruction(opname='CALL_FUNCTION', opcode=131, arg=6, argval=6, argrepr='', offset=14, starts_line=None, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300925 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
926 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
927 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=20, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000928]
929
930expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200931 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
932 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
933 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
934 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
935 Instruction(opname='FOR_ITER', opcode=93, arg=34, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True),
936 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
937 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
938 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
939 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
940 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
941 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
942 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
943 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
944 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
945 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
946 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
947 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
948 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
949 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
950 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
951 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=40, starts_line=None, is_jump_target=False),
952 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=42, starts_line=None, is_jump_target=False),
953 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
954 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False),
955 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False),
956 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False),
957 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True),
958 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=94, argval=94, argrepr='', offset=54, starts_line=None, is_jump_target=False),
959 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=False),
960 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False),
961 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False),
962 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False),
963 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False),
964 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False),
965 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False),
966 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False),
967 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False),
968 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False),
969 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False),
970 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=82, argval=82, argrepr='', offset=78, starts_line=None, is_jump_target=False),
971 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=80, starts_line=15, is_jump_target=False),
972 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True),
973 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
974 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
975 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=52, argval=52, argrepr='', offset=88, starts_line=None, is_jump_target=False),
976 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=102, argval=102, argrepr='', offset=90, starts_line=17, is_jump_target=False),
977 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=92, starts_line=None, is_jump_target=False),
978 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=94, starts_line=19, is_jump_target=True),
979 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=96, starts_line=None, is_jump_target=False),
980 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=98, starts_line=None, is_jump_target=False),
981 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=100, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +0000982 Instruction(opname='SETUP_FINALLY', opcode=122, arg=98, argval=202, argrepr='to 202', offset=102, starts_line=20, is_jump_target=True),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200983 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=118, argrepr='to 118', offset=104, starts_line=None, is_jump_target=False),
984 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +0000985 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200986 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
987 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
988 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=114, starts_line=None, is_jump_target=False),
989 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=146, argrepr='to 146', offset=116, starts_line=None, is_jump_target=False),
990 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=True),
991 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False),
992 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=122, starts_line=None, is_jump_target=False),
993 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=144, argval=144, argrepr='', offset=124, starts_line=None, is_jump_target=False),
994 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
995 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False),
996 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=130, starts_line=None, is_jump_target=False),
997 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=23, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +0000998 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=134, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200999 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=136, starts_line=None, is_jump_target=False),
1000 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
1001 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=140, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001002 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=190, argrepr='to 190', offset=142, starts_line=None, is_jump_target=False),
1003 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=True),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001004 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=146, starts_line=25, is_jump_target=True),
Mark Shannonfee55262019-11-21 09:11:43 +00001005 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=174, argrepr='to 174', offset=148, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001006 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=150, starts_line=None, is_jump_target=False),
1007 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=152, starts_line=26, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001008 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=154, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001009 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=156, starts_line=None, is_jump_target=False),
1010 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1011 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001012 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=162, starts_line=None, is_jump_target=False),
1013 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
1014 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
1015 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=168, starts_line=None, is_jump_target=False),
1016 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=False),
1017 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=190, argrepr='to 190', offset=172, starts_line=None, is_jump_target=False),
1018 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=True),
1019 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=180, argval=180, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1020 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False),
1021 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=True),
1022 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
1023 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
1024 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
1025 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=False),
1026 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=190, starts_line=None, is_jump_target=True),
1027 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=192, starts_line=28, is_jump_target=False),
1028 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=194, starts_line=None, is_jump_target=False),
1029 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1030 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=198, starts_line=None, is_jump_target=False),
1031 Instruction(opname='JUMP_FORWARD', opcode=110, arg=10, argval=212, argrepr='to 212', offset=200, starts_line=None, is_jump_target=False),
1032 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=202, starts_line=None, is_jump_target=True),
1033 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=204, starts_line=None, is_jump_target=False),
1034 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=206, starts_line=None, is_jump_target=False),
1035 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=208, starts_line=None, is_jump_target=False),
1036 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=210, starts_line=None, is_jump_target=False),
1037 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=212, starts_line=None, is_jump_target=True),
1038 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=214, starts_line=None, is_jump_target=False)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001039]
1040
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001041# One last piece of inspect fodder to check the default line number handling
1042def simple(): pass
1043expected_opinfo_simple = [
1044 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=0, starts_line=simple.__code__.co_firstlineno, is_jump_target=False),
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001045 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=2, starts_line=None, is_jump_target=False)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001046]
1047
1048
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001049class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001050
Mark Shannonfee55262019-11-21 09:11:43 +00001051 def __init__(self, *args):
1052 super().__init__(*args)
1053 self.maxDiff = None
1054
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001055 def test_default_first_line(self):
1056 actual = dis.get_instructions(simple)
1057 self.assertEqual(list(actual), expected_opinfo_simple)
1058
1059 def test_first_line_set_to_None(self):
1060 actual = dis.get_instructions(simple, first_line=None)
1061 self.assertEqual(list(actual), expected_opinfo_simple)
1062
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001063 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001064 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1065 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001066
1067 def test_nested(self):
1068 with captured_stdout():
1069 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001070 actual = dis.get_instructions(f, first_line=expected_f_line)
1071 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001072
1073 def test_doubly_nested(self):
1074 with captured_stdout():
1075 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001076 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1077 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001078
1079 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001080 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1081 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001082
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001083# get_instructions has its own tests above, so can rely on it to validate
1084# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001085class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001086
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001087 def test_instantiation(self):
1088 # Test with function, method, code string and code object
1089 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001090 with self.subTest(obj=obj):
1091 b = dis.Bytecode(obj)
1092 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001093
1094 self.assertRaises(TypeError, dis.Bytecode, object())
1095
1096 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001097 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1098 with self.subTest(obj=obj):
1099 via_object = list(dis.Bytecode(obj))
1100 via_generator = list(dis.get_instructions(obj))
1101 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001102
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001103 def test_explicit_first_line(self):
1104 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1105 self.assertEqual(list(actual), expected_opinfo_outer)
1106
1107 def test_source_line_in_disassembly(self):
1108 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001109 actual = dis.Bytecode(simple).dis()
1110 actual = actual.strip().partition(" ")[0] # extract the line no
1111 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001112 self.assertEqual(actual, expected)
1113 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001114 actual = dis.Bytecode(simple, first_line=350).dis()
1115 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001116 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001117
1118 def test_info(self):
1119 self.maxDiff = 1000
1120 for x, expected in CodeInfoTests.test_pairs:
1121 b = dis.Bytecode(x)
1122 self.assertRegex(b.info(), expected)
1123
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001124 def test_disassembled(self):
1125 actual = dis.Bytecode(_f).dis()
1126 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001127
Nick Coghlan50c48b82013-11-23 00:57:00 +10001128 def test_from_traceback(self):
1129 tb = get_tb()
1130 b = dis.Bytecode.from_traceback(tb)
1131 while tb.tb_next: tb = tb.tb_next
1132
1133 self.assertEqual(b.current_offset, tb.tb_lasti)
1134
1135 def test_from_traceback_dis(self):
1136 tb = get_tb()
1137 b = dis.Bytecode.from_traceback(tb)
1138 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001139
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001140if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001141 unittest.main()