blob: 9cd11d3118b604ad876e01dbc699cba664bead3b [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 = """\
Mark Shannon266b4622020-11-17 19:30:14 +0000148%3d 0 LOAD_ASSERTION_ERROR
149 2 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
150 4 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
151 6 MAKE_FUNCTION 0
152 8 LOAD_FAST 0 (x)
153 10 GET_ITER
154 12 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300155
Mark Shannon266b4622020-11-17 19:30:14 +0000156%3d 14 LOAD_CONST 4 (1)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300157
Mark Shannon266b4622020-11-17 19:30:14 +0000158%3d 16 BINARY_ADD
159 18 CALL_FUNCTION 1
160 20 RAISE_VARARGS 1
Georg Brandlebbf63b2010-10-14 07:23:01 +0000161""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600162 __file__,
163 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000164 bug1333982.__code__.co_firstlineno + 2,
Mark Shannon266b4622020-11-17 19:30:14 +0000165 bug1333982.__code__.co_firstlineno + 1)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000166
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167_BIG_LINENO_FORMAT = """\
168%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300169 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000170 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300171 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172"""
173
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300174_BIG_LINENO_FORMAT2 = """\
175%4d 0 LOAD_GLOBAL 0 (spam)
176 2 POP_TOP
177 4 LOAD_CONST 0 (None)
178 6 RETURN_VALUE
179"""
180
Guido van Rossume7ba4952007-06-06 23:52:48 +0000181dis_module_expected_results = """\
182Disassembly of f:
183 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300184 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000185
186Disassembly of g:
187 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300188 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000189
190"""
191
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000192expr_str = "x + 1"
193
194dis_expr_str = """\
195 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300196 2 LOAD_CONST 0 (1)
197 4 BINARY_ADD
198 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000199"""
200
201simple_stmt_str = "x = x + 1"
202
203dis_simple_stmt_str = """\
204 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300205 2 LOAD_CONST 0 (1)
206 4 BINARY_ADD
207 6 STORE_NAME 0 (x)
208 8 LOAD_CONST 1 (None)
209 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000210"""
211
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700212annot_stmt_str = """\
213
214x: int = 1
215y: fun(1)
216lst[fun(0)]: int = 1
217"""
218# leading newline is for a reason (tests lineno)
219
220dis_annot_stmt_str = """\
221 2 0 SETUP_ANNOTATIONS
222 2 LOAD_CONST 0 (1)
223 4 STORE_NAME 0 (x)
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300224 6 LOAD_CONST 1 ('int')
225 8 LOAD_NAME 1 (__annotations__)
226 10 LOAD_CONST 2 ('x')
Mark Shannon332cd5e2018-01-30 00:41:04 +0000227 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700228
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300229 3 14 LOAD_CONST 3 ('fun(1)')
230 16 LOAD_NAME 1 (__annotations__)
231 18 LOAD_CONST 4 ('y')
232 20 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700233
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300234 4 22 LOAD_CONST 0 (1)
235 24 LOAD_NAME 2 (lst)
236 26 LOAD_NAME 3 (fun)
237 28 LOAD_CONST 5 (0)
238 30 CALL_FUNCTION 1
239 32 STORE_SUBSCR
240 34 LOAD_NAME 4 (int)
241 36 POP_TOP
242 38 LOAD_CONST 6 (None)
243 40 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700244"""
245
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000246compound_stmt_str = """\
247x = 0
248while 1:
249 x += 1"""
250# Trailing newline has been deliberately omitted
251
252dis_compound_stmt_str = """\
253 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300254 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000255
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200256 3 >> 4 LOAD_NAME 0 (x)
257 6 LOAD_CONST 1 (1)
258 8 INPLACE_ADD
259 10 STORE_NAME 0 (x)
260 12 JUMP_ABSOLUTE 4
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000261"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000262
Nick Coghlan50c48b82013-11-23 00:57:00 +1000263dis_traceback = """\
Mark Shannoncc75ab72020-11-12 19:49:33 +0000264%3d 0 SETUP_FINALLY 14 (to 16)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000265
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300266%3d 2 LOAD_CONST 1 (1)
267 4 LOAD_CONST 2 (0)
268 --> 6 BINARY_TRUE_DIVIDE
269 8 POP_TOP
270 10 POP_BLOCK
Nick Coghlan50c48b82013-11-23 00:57:00 +1000271
Mark Shannoncc75ab72020-11-12 19:49:33 +0000272%3d 12 LOAD_FAST 1 (tb)
273 14 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000274
Mark Shannoncc75ab72020-11-12 19:49:33 +0000275%3d >> 16 DUP_TOP
276 18 LOAD_GLOBAL 0 (Exception)
277 20 JUMP_IF_NOT_EXC_MATCH 58
278 22 POP_TOP
279 24 STORE_FAST 0 (e)
280 26 POP_TOP
281 28 SETUP_FINALLY 20 (to 50)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282
Mark Shannoncc75ab72020-11-12 19:49:33 +0000283%3d 30 LOAD_FAST 0 (e)
284 32 LOAD_ATTR 1 (__traceback__)
285 34 STORE_FAST 1 (tb)
286 36 POP_BLOCK
287 38 POP_EXCEPT
288 40 LOAD_CONST 0 (None)
289 42 STORE_FAST 0 (e)
290 44 DELETE_FAST 0 (e)
291
292%3d 46 LOAD_FAST 1 (tb)
293 48 RETURN_VALUE
294 >> 50 LOAD_CONST 0 (None)
295 52 STORE_FAST 0 (e)
296 54 DELETE_FAST 0 (e)
297 56 RERAISE
298 >> 58 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000299""" % (TRACEBACK_CODE.co_firstlineno + 1,
300 TRACEBACK_CODE.co_firstlineno + 2,
Mark Shannoncc75ab72020-11-12 19:49:33 +0000301 TRACEBACK_CODE.co_firstlineno + 5,
Nick Coghlan50c48b82013-11-23 00:57:00 +1000302 TRACEBACK_CODE.co_firstlineno + 3,
303 TRACEBACK_CODE.co_firstlineno + 4,
304 TRACEBACK_CODE.co_firstlineno + 5)
305
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300306def _fstring(a, b, c, d):
307 return f'{a} {b:4} {c!r} {d!r:4}'
308
309dis_fstring = """\
310%3d 0 LOAD_FAST 0 (a)
311 2 FORMAT_VALUE 0
312 4 LOAD_CONST 1 (' ')
313 6 LOAD_FAST 1 (b)
314 8 LOAD_CONST 2 ('4')
315 10 FORMAT_VALUE 4 (with format)
316 12 LOAD_CONST 1 (' ')
317 14 LOAD_FAST 2 (c)
318 16 FORMAT_VALUE 2 (repr)
319 18 LOAD_CONST 1 (' ')
320 20 LOAD_FAST 3 (d)
321 22 LOAD_CONST 2 ('4')
322 24 FORMAT_VALUE 6 (repr, with format)
323 26 BUILD_STRING 7
324 28 RETURN_VALUE
325""" % (_fstring.__code__.co_firstlineno + 1,)
326
Mark Shannon88dce262019-12-30 09:53:36 +0000327def _tryfinally(a, b):
328 try:
329 return a
330 finally:
331 b()
332
333def _tryfinallyconst(b):
334 try:
335 return 1
336 finally:
337 b()
338
339dis_tryfinally = """\
340%3d 0 SETUP_FINALLY 12 (to 14)
341
342%3d 2 LOAD_FAST 0 (a)
343 4 POP_BLOCK
344
345%3d 6 LOAD_FAST 1 (b)
346 8 CALL_FUNCTION 0
347 10 POP_TOP
348
349%3d 12 RETURN_VALUE
350
351%3d >> 14 LOAD_FAST 1 (b)
352 16 CALL_FUNCTION 0
353 18 POP_TOP
354 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000355""" % (_tryfinally.__code__.co_firstlineno + 1,
356 _tryfinally.__code__.co_firstlineno + 2,
357 _tryfinally.__code__.co_firstlineno + 4,
358 _tryfinally.__code__.co_firstlineno + 2,
359 _tryfinally.__code__.co_firstlineno + 4,
360 )
361
362dis_tryfinallyconst = """\
363%3d 0 SETUP_FINALLY 12 (to 14)
364
365%3d 2 POP_BLOCK
366
367%3d 4 LOAD_FAST 0 (b)
368 6 CALL_FUNCTION 0
369 8 POP_TOP
370
371%3d 10 LOAD_CONST 1 (1)
372 12 RETURN_VALUE
373
374%3d >> 14 LOAD_FAST 0 (b)
375 16 CALL_FUNCTION 0
376 18 POP_TOP
377 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000378""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
379 _tryfinallyconst.__code__.co_firstlineno + 2,
380 _tryfinallyconst.__code__.co_firstlineno + 4,
381 _tryfinallyconst.__code__.co_firstlineno + 2,
382 _tryfinallyconst.__code__.co_firstlineno + 4,
383 )
384
Nick Coghlanefd5df92014-07-25 23:02:56 +1000385def _g(x):
386 yield x
387
syncosmicfe2b56a2017-08-17 19:29:21 -0700388async def _ag(x):
389 yield x
390
391async def _co(x):
392 async for item in _ag(x):
393 pass
394
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300395def _h(y):
396 def foo(x):
397 '''funcdoc'''
398 return [x + z for z in y]
399 return foo
400
401dis_nested_0 = """\
402%3d 0 LOAD_CLOSURE 0 (y)
403 2 BUILD_TUPLE 1
404 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
405 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200406 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300407 10 STORE_FAST 1 (foo)
408
409%3d 12 LOAD_FAST 1 (foo)
410 14 RETURN_VALUE
411""" % (_h.__code__.co_firstlineno + 1,
412 __file__,
413 _h.__code__.co_firstlineno + 1,
414 _h.__code__.co_firstlineno + 4,
415)
416
417dis_nested_1 = """%s
418Disassembly of <code object foo at 0x..., file "%s", line %d>:
419%3d 0 LOAD_CLOSURE 0 (x)
420 2 BUILD_TUPLE 1
421 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
422 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200423 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300424 10 LOAD_DEREF 1 (y)
425 12 GET_ITER
426 14 CALL_FUNCTION 1
427 16 RETURN_VALUE
428""" % (dis_nested_0,
429 __file__,
430 _h.__code__.co_firstlineno + 1,
431 _h.__code__.co_firstlineno + 3,
432 __file__,
433 _h.__code__.co_firstlineno + 3,
434)
435
436dis_nested_2 = """%s
437Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
438%3d 0 BUILD_LIST 0
439 2 LOAD_FAST 0 (.0)
440 >> 4 FOR_ITER 12 (to 18)
441 6 STORE_FAST 1 (z)
442 8 LOAD_DEREF 0 (x)
443 10 LOAD_FAST 1 (z)
444 12 BINARY_ADD
445 14 LIST_APPEND 2
446 16 JUMP_ABSOLUTE 4
447 >> 18 RETURN_VALUE
448""" % (dis_nested_1,
449 __file__,
450 _h.__code__.co_firstlineno + 3,
451 _h.__code__.co_firstlineno + 3,
452)
453
syncosmicfe2b56a2017-08-17 19:29:21 -0700454
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000455class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500456
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300457 maxDiff = None
458
459 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000460 # We want to test the default printing behaviour, not the file arg
461 output = io.StringIO()
462 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500463 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300464 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500465 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300466 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000467 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500468
469 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000470 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500471
Zachary Warebb4b7c12013-12-26 09:55:24 -0600472 def strip_addresses(self, text):
473 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500474
475 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300476 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600477 if got != expected:
478 got = self.strip_addresses(got)
479 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000480
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000481 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500482 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000483 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
484 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000485
486 def test_opname(self):
487 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
488
489 def test_boundaries(self):
490 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
491 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
492
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300493 def test_widths(self):
494 for opcode, opname in enumerate(dis.opname):
495 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
Mark Shannon9af0e472020-01-14 10:12:45 +0000496 'BUILD_TUPLE_UNPACK_WITH_CALL',
497 'JUMP_IF_NOT_EXC_MATCH'):
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300498 continue
499 with self.subTest(opname=opname):
500 width = dis._OPNAME_WIDTH
501 if opcode < dis.HAVE_ARGUMENT:
502 width += 1 + dis._OPARG_WIDTH
503 self.assertLessEqual(len(opname), width)
504
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000505 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000506 self.do_disassembly_test(_f, dis_f)
507
508 def test_bug_708901(self):
509 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000510
Neal Norwitz51abbc72005-12-18 07:06:23 +0000511 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000512 # This one is checking bytecodes generated for an `assert` statement,
513 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600514 if not __debug__:
515 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600516
Zachary Waree80e8062013-12-26 09:53:49 -0600517 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000518
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 def test_big_linenos(self):
520 def func(count):
521 namespace = {}
522 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000523 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 return namespace['foo']
525
526 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000527 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 expected = _BIG_LINENO_FORMAT % (i + 2)
529 self.do_disassembly_test(func(i), expected)
530
531 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300532 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000533 expected = _BIG_LINENO_FORMAT % (i + 2)
534 self.do_disassembly_test(func(i), expected)
535
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300536 for i in range(1000, 5000, 10):
537 expected = _BIG_LINENO_FORMAT2 % (i + 2)
538 self.do_disassembly_test(func(i), expected)
539
Guido van Rossume7ba4952007-06-06 23:52:48 +0000540 from test import dis_module
541 self.do_disassembly_test(dis_module, dis_module_expected_results)
542
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300543 def test_big_offsets(self):
544 def func(count):
545 namespace = {}
546 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
547 exec(func, namespace)
548 return namespace['foo']
549
550 def expected(count, w):
551 s = ['''\
552 %*d LOAD_FAST 0 (x)
553 %*d LOAD_CONST 1 (1)
554 %*d BINARY_ADD
555 %*d STORE_FAST 0 (x)
556''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
557 for i in range(count)]
558 s += ['''\
559
560 3 %*d LOAD_FAST 0 (x)
561 %*d RETURN_VALUE
562''' % (w, 8*count, w, 8*count + 2)]
563 s[0] = ' 2' + s[0][3:]
564 return ''.join(s)
565
566 for i in range(1, 5):
567 self.do_disassembly_test(func(i), expected(i, 4))
568 self.do_disassembly_test(func(1249), expected(1249, 4))
569 self.do_disassembly_test(func(1250), expected(1250, 5))
570
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000571 def test_disassemble_str(self):
572 self.do_disassembly_test(expr_str, dis_expr_str)
573 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700574 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000575 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
576
Benjamin Petersond6afe722011-03-15 14:44:52 -0500577 def test_disassemble_bytes(self):
578 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
579
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300580 def test_disassemble_class(self):
581 self.do_disassembly_test(_C, dis_c)
582
583 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500584 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
585
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300586 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500587 method_bytecode = _C(1).__init__.__code__.co_code
588 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
589
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300590 def test_disassemble_static_method(self):
591 self.do_disassembly_test(_C.sm, dis_c_static_method)
592
593 def test_disassemble_class_method(self):
594 self.do_disassembly_test(_C.cm, dis_c_class_method)
595
Nick Coghlanefd5df92014-07-25 23:02:56 +1000596 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700597 gen_func_disas = self.get_disassembly(_g) # Generator function
598 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000599 self.assertEqual(gen_disas, gen_func_disas)
600
syncosmicfe2b56a2017-08-17 19:29:21 -0700601 def test_disassemble_async_generator(self):
602 agen_func_disas = self.get_disassembly(_ag) # Async generator function
603 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
604 self.assertEqual(agen_disas, agen_func_disas)
605
606 def test_disassemble_coroutine(self):
607 coro_func_disas = self.get_disassembly(_co) # Coroutine function
608 coro = _co(1) # Coroutine object
609 coro.close() # Avoid a RuntimeWarning (never awaited)
610 coro_disas = self.get_disassembly(coro)
611 self.assertEqual(coro_disas, coro_func_disas)
612
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300613 def test_disassemble_fstring(self):
614 self.do_disassembly_test(_fstring, dis_fstring)
615
Mark Shannon88dce262019-12-30 09:53:36 +0000616 def test_disassemble_try_finally(self):
617 self.do_disassembly_test(_tryfinally, dis_tryfinally)
618 self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
619
Benjamin Petersond6afe722011-03-15 14:44:52 -0500620 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500621 try:
622 del sys.last_traceback
623 except AttributeError:
624 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500625 self.assertRaises(RuntimeError, dis.dis, None)
626
Benjamin Petersond6afe722011-03-15 14:44:52 -0500627 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500628 try:
629 del sys.last_traceback
630 except AttributeError:
631 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500632
633 try:
634 1/0
635 except Exception as e:
636 tb = e.__traceback__
637 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500638
639 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
640 self.do_disassembly_test(None, tb_dis)
641
642 def test_dis_object(self):
643 self.assertRaises(TypeError, dis.dis, object())
644
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300645 def test_disassemble_recursive(self):
646 def check(expected, **kwargs):
647 dis = self.get_disassembly(_h, **kwargs)
648 dis = self.strip_addresses(dis)
649 self.assertEqual(dis, expected)
650
651 check(dis_nested_0, depth=0)
652 check(dis_nested_1, depth=1)
653 check(dis_nested_2, depth=2)
654 check(dis_nested_2, depth=3)
655 check(dis_nested_2, depth=None)
656 check(dis_nested_2)
657
658
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000659class DisWithFileTests(DisTests):
660
661 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300662 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000663 output = io.StringIO()
664 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300665 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000666 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300667 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000668 return output.getvalue()
669
670
Mark Shannon266b4622020-11-17 19:30:14 +0000671if sys.flags.optimize:
672 code_info_consts = "0: None"
673else:
674 code_info_consts = (
675 """0: 'Formatted details of methods, functions, or code.'
676 1: None"""
677)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000678
Mark Shannon266b4622020-11-17 19:30:14 +0000679code_info_code_info = f"""\
Nick Coghlaneae2da12010-08-17 08:03:36 +0000680Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000681Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000682Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100683Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000684Kw-only arguments: 0
685Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000686Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000687Flags: OPTIMIZED, NEWLOCALS, NOFREE
688Constants:
Mark Shannon266b4622020-11-17 19:30:14 +0000689 {code_info_consts}
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000690Names:
691 0: _format_code_info
692 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000693Variable names:
Mark Shannon266b4622020-11-17 19:30:14 +0000694 0: x"""
695
Nick Coghlaneae2da12010-08-17 08:03:36 +0000696
697@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100698def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000699 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100700 print(a, b, x, y, z, c, d, e, f)
701 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000702
Nick Coghlaneae2da12010-08-17 08:03:36 +0000703code_info_tricky = """\
704Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000705Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100706Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100707Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000708Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100709Number of locals: 10
710Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000711Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
712Constants:
713 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000714 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100715 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000716Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100717 0: a
718 1: b
719 2: x
720 3: y
721 4: z
722 5: c
723 6: d
724 7: e
725 8: args
726 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000727Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100728 0: [abedfxyz]
729 1: [abedfxyz]
730 2: [abedfxyz]
731 3: [abedfxyz]
732 4: [abedfxyz]
733 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100734# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000735
736co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000737
738code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000739Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000740Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100741Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000742Kw-only arguments: 0
743Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100744Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000745Flags: OPTIMIZED, NEWLOCALS, NESTED
746Constants:
747 0: None
748Names:
749 0: print
750Variable names:
751 0: c
752Free variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100753 0: [abedfxyz]
754 1: [abedfxyz]
755 2: [abedfxyz]
756 3: [abedfxyz]
757 4: [abedfxyz]
758 5: [abedfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000759
760code_info_expr_str = """\
761Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000762Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000763Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100764Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000765Kw-only arguments: 0
766Number of locals: 0
767Stack size: 2
768Flags: NOFREE
769Constants:
770 0: 1
771Names:
772 0: x"""
773
774code_info_simple_stmt_str = """\
775Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000776Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000777Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100778Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000779Kw-only arguments: 0
780Number of locals: 0
781Stack size: 2
782Flags: NOFREE
783Constants:
784 0: 1
785 1: None
786Names:
787 0: x"""
788
789code_info_compound_stmt_str = """\
790Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000791Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000792Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100793Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000794Kw-only arguments: 0
795Number of locals: 0
796Stack size: 2
797Flags: NOFREE
798Constants:
799 0: 0
800 1: 1
801 2: None
802Names:
803 0: x"""
804
Yury Selivanov75445082015-05-11 22:57:16 -0400805
806async def async_def():
807 await 1
808 async for a in b: pass
809 async with c as d: pass
810
811code_info_async_def = """\
812Name: async_def
813Filename: (.*)
814Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100815Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400816Kw-only arguments: 0
817Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000818Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700819Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400820Constants:
821 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200822 1: 1
823Names:
824 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200825 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200826Variable names:
827 0: a
828 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400829
Nick Coghlaneae2da12010-08-17 08:03:36 +0000830class CodeInfoTests(unittest.TestCase):
831 test_pairs = [
832 (dis.code_info, code_info_code_info),
833 (tricky, code_info_tricky),
834 (co_tricky_nested_f, code_info_tricky_nested_f),
835 (expr_str, code_info_expr_str),
836 (simple_stmt_str, code_info_simple_stmt_str),
837 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400838 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000839 ]
840
841 def test_code_info(self):
842 self.maxDiff = 1000
843 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000844 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000845
846 def test_show_code(self):
847 self.maxDiff = 1000
848 for x, expected in self.test_pairs:
849 with captured_stdout() as output:
850 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000851 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000852 output = io.StringIO()
853 dis.show_code(x, file=output)
854 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000855
Benjamin Petersond6afe722011-03-15 14:44:52 -0500856 def test_code_info_object(self):
857 self.assertRaises(TypeError, dis.code_info, object())
858
859 def test_pretty_flags_no_flags(self):
860 self.assertEqual(dis.pretty_flags(0), '0x0')
861
862
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000863# Fodder for instruction introspection tests
864# Editing any of these may require recalculating the expected output
865def outer(a=1, b=2):
866 def f(c=3, d=4):
867 def inner(e=5, f=6):
868 print(a, b, c, d, e, f)
869 print(a, b, c, d)
870 return inner
871 print(a, b, '', 1, [], {}, "Hello world!")
872 return f
873
874def jumpy():
875 # This won't actually run (but that's OK, we only disassemble it)
876 for i in range(10):
877 print(i)
878 if i < 4:
879 continue
880 if i > 6:
881 break
882 else:
883 print("I can haz else clause?")
884 while i:
885 print(i)
886 i -= 1
887 if i > 6:
888 continue
889 if i < 4:
890 break
891 else:
892 print("Who let lolcatz into this test suite?")
893 try:
894 1 / 0
895 except ZeroDivisionError:
896 print("Here we go, here we go, here we go...")
897 else:
898 with i as dodgy:
899 print("Never reach this")
900 finally:
901 print("OK, now we're done")
902
903# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000904expected_outer_line = 1
905_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000906code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000907expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000908code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000909expected_inner_line = code_object_inner.co_firstlineno - _line_offset
910expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000911
912# The following lines are useful to regenerate the expected results after
913# either the fodder is modified or the bytecode generation changes
914# After regeneration, update the references to code_object_f and
915# code_object_inner before rerunning the tests
916
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000917#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000918#print('expected_opinfo_outer = [\n ',
919 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200920#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000921#print('expected_opinfo_f = [\n ',
922 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200923#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000924#print('expected_opinfo_inner = [\n ',
925 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000926#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000927#print('expected_opinfo_jumpy = [\n ',
928 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
929
930
931Instruction = dis.Instruction
932expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300933 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
934 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
935 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
936 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
937 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),
938 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 +0200939 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 +0300940 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
941 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
942 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
943 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
944 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
945 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
946 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
947 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
948 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 -0700949 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 +0300950 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
951 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
952 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 +1000953]
954
955expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300956 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
957 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
958 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
959 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
960 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
961 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
962 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),
963 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 +0200964 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 +0300965 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
966 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
967 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
968 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
969 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
970 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 -0700971 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 +0300972 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
973 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
974 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 +1000975]
976
977expected_opinfo_inner = [
978 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 +0300979 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
980 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
981 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
982 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
983 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
984 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 -0700985 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 +0300986 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
987 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
988 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 +1000989]
990
991expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200992 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
993 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
994 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
995 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
Mark Shannoncc75ab72020-11-12 19:49:33 +0000996 Instruction(opname='FOR_ITER', opcode=93, arg=32, argval=42, argrepr='to 42', offset=8, starts_line=None, is_jump_target=True),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200997 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
998 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
999 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
1000 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1001 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
1002 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
1003 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
1004 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
1005 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
1006 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
1007 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
1008 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
1009 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
1010 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1011 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
Mark Shannoncc75ab72020-11-12 19:49:33 +00001012 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=40, starts_line=None, is_jump_target=False),
1013 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=42, starts_line=10, is_jump_target=True),
1014 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=44, starts_line=None, is_jump_target=False),
1015 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=46, starts_line=None, is_jump_target=False),
1016 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=48, starts_line=None, is_jump_target=False),
1017 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=50, starts_line=11, is_jump_target=True),
1018 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=90, argval=90, argrepr='', offset=52, starts_line=None, is_jump_target=False),
1019 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=54, starts_line=12, is_jump_target=False),
1020 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=False),
1021 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=58, starts_line=None, is_jump_target=False),
1022 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=False),
1023 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=13, is_jump_target=False),
1024 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=64, starts_line=None, is_jump_target=False),
1025 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
1026 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=68, starts_line=None, is_jump_target=False),
1027 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=70, starts_line=14, is_jump_target=False),
1028 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=72, starts_line=None, is_jump_target=False),
1029 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=74, starts_line=None, is_jump_target=False),
1030 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=76, starts_line=None, is_jump_target=False),
1031 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=78, starts_line=15, is_jump_target=False),
1032 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=16, is_jump_target=True),
1033 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=82, starts_line=None, is_jump_target=False),
1034 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=84, starts_line=None, is_jump_target=False),
1035 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=50, argval=50, argrepr='', offset=86, starts_line=None, is_jump_target=False),
1036 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=98, argval=98, argrepr='', offset=88, starts_line=17, is_jump_target=False),
1037 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=90, starts_line=19, is_jump_target=True),
1038 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=92, starts_line=None, is_jump_target=False),
1039 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=94, starts_line=None, is_jump_target=False),
1040 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
1041 Instruction(opname='SETUP_FINALLY', opcode=122, arg=98, argval=198, argrepr='to 198', offset=98, starts_line=20, is_jump_target=True),
1042 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=114, argrepr='to 114', offset=100, starts_line=None, is_jump_target=False),
1043 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=102, starts_line=21, is_jump_target=False),
1044 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=104, starts_line=None, is_jump_target=False),
1045 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
1046 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=108, starts_line=None, is_jump_target=False),
1047 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
1048 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=140, argrepr='to 140', offset=112, starts_line=None, is_jump_target=False),
1049 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=114, starts_line=22, is_jump_target=True),
1050 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=116, starts_line=None, is_jump_target=False),
1051 Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=138, argval=138, argrepr='', offset=118, starts_line=None, is_jump_target=False),
1052 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
1053 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=122, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001054 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False),
Mark Shannoncc75ab72020-11-12 19:49:33 +00001055 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=126, starts_line=23, is_jump_target=False),
1056 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=128, starts_line=None, is_jump_target=False),
1057 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=130, starts_line=None, is_jump_target=False),
1058 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
1059 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
1060 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=184, argrepr='to 184', offset=136, starts_line=None, is_jump_target=False),
1061 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=True),
1062 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=140, starts_line=25, is_jump_target=True),
1063 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=168, argrepr='to 168', offset=142, starts_line=None, is_jump_target=False),
1064 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=144, starts_line=None, is_jump_target=False),
1065 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=146, starts_line=26, is_jump_target=False),
1066 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=148, starts_line=None, is_jump_target=False),
1067 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=150, starts_line=None, is_jump_target=False),
1068 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=152, starts_line=None, is_jump_target=False),
1069 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
1070 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=156, starts_line=None, is_jump_target=False),
1071 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1072 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False),
1073 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=162, starts_line=None, is_jump_target=False),
1074 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
1075 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=184, argrepr='to 184', offset=166, starts_line=None, is_jump_target=False),
1076 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=True),
1077 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=174, argval=174, argrepr='', offset=170, starts_line=None, is_jump_target=False),
1078 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
1079 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=True),
1080 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1081 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False),
1082 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001083 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
Mark Shannoncc75ab72020-11-12 19:49:33 +00001084 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=True),
1085 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=186, starts_line=28, is_jump_target=False),
1086 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=188, starts_line=None, is_jump_target=False),
1087 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=190, starts_line=None, is_jump_target=False),
1088 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False),
1089 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=194, starts_line=None, is_jump_target=False),
1090 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1091 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=198, starts_line=None, is_jump_target=True),
1092 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=200, starts_line=None, is_jump_target=False),
1093 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=202, starts_line=None, is_jump_target=False),
1094 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False),
1095 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001096]
1097
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001098# One last piece of inspect fodder to check the default line number handling
1099def simple(): pass
1100expected_opinfo_simple = [
1101 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 +03001102 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 +10001103]
1104
1105
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001106class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001107
Mark Shannonfee55262019-11-21 09:11:43 +00001108 def __init__(self, *args):
1109 super().__init__(*args)
1110 self.maxDiff = None
1111
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001112 def test_default_first_line(self):
1113 actual = dis.get_instructions(simple)
1114 self.assertEqual(list(actual), expected_opinfo_simple)
1115
1116 def test_first_line_set_to_None(self):
1117 actual = dis.get_instructions(simple, first_line=None)
1118 self.assertEqual(list(actual), expected_opinfo_simple)
1119
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001120 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001121 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1122 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001123
1124 def test_nested(self):
1125 with captured_stdout():
1126 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001127 actual = dis.get_instructions(f, first_line=expected_f_line)
1128 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001129
1130 def test_doubly_nested(self):
1131 with captured_stdout():
1132 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001133 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1134 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001135
1136 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001137 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1138 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001139
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001140# get_instructions has its own tests above, so can rely on it to validate
1141# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001142class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001143
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001144 def test_instantiation(self):
1145 # Test with function, method, code string and code object
1146 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001147 with self.subTest(obj=obj):
1148 b = dis.Bytecode(obj)
1149 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001150
1151 self.assertRaises(TypeError, dis.Bytecode, object())
1152
1153 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001154 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1155 with self.subTest(obj=obj):
1156 via_object = list(dis.Bytecode(obj))
1157 via_generator = list(dis.get_instructions(obj))
1158 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001159
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001160 def test_explicit_first_line(self):
1161 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1162 self.assertEqual(list(actual), expected_opinfo_outer)
1163
1164 def test_source_line_in_disassembly(self):
1165 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001166 actual = dis.Bytecode(simple).dis()
1167 actual = actual.strip().partition(" ")[0] # extract the line no
1168 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001169 self.assertEqual(actual, expected)
1170 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001171 actual = dis.Bytecode(simple, first_line=350).dis()
1172 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001173 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001174
1175 def test_info(self):
1176 self.maxDiff = 1000
1177 for x, expected in CodeInfoTests.test_pairs:
1178 b = dis.Bytecode(x)
1179 self.assertRegex(b.info(), expected)
1180
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001181 def test_disassembled(self):
1182 actual = dis.Bytecode(_f).dis()
1183 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001184
Nick Coghlan50c48b82013-11-23 00:57:00 +10001185 def test_from_traceback(self):
1186 tb = get_tb()
1187 b = dis.Bytecode.from_traceback(tb)
1188 while tb.tb_next: tb = tb.tb_next
1189
1190 self.assertEqual(b.current_offset, tb.tb_lasti)
1191
1192 def test_from_traceback_dis(self):
1193 tb = get_tb()
1194 b = dis.Bytecode.from_traceback(tb)
1195 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001196
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001197if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001198 unittest.main()