blob: 56d877151838f0970f220a80c28b9953088c6395 [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
Mark Shannon5977a792020-12-02 13:31:40 +0000134
135%3d >> 16 LOAD_CONST 0 (None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200136 18 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000137""" % (bug708901.__code__.co_firstlineno + 1,
138 bug708901.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300139 bug708901.__code__.co_firstlineno + 1,
Mark Shannon5977a792020-12-02 13:31:40 +0000140 bug708901.__code__.co_firstlineno + 3,
141 bug708901.__code__.co_firstlineno + 1)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000142
Neal Norwitz51abbc72005-12-18 07:06:23 +0000143
144def bug1333982(x=[]):
145 assert 0, ([s for s in x] +
146 1)
147 pass
148
149dis_bug1333982 = """\
Mark Shannon266b4622020-11-17 19:30:14 +0000150%3d 0 LOAD_ASSERTION_ERROR
151 2 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152 4 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153 6 MAKE_FUNCTION 0
154 8 LOAD_FAST 0 (x)
155 10 GET_ITER
156 12 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300157
Mark Shannon266b4622020-11-17 19:30:14 +0000158%3d 14 LOAD_CONST 4 (1)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300159
Mark Shannon266b4622020-11-17 19:30:14 +0000160%3d 16 BINARY_ADD
161 18 CALL_FUNCTION 1
162 20 RAISE_VARARGS 1
Georg Brandlebbf63b2010-10-14 07:23:01 +0000163""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600164 __file__,
165 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166 bug1333982.__code__.co_firstlineno + 2,
Mark Shannon266b4622020-11-17 19:30:14 +0000167 bug1333982.__code__.co_firstlineno + 1)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000168
Yurii Karabasf24b8102020-12-04 17:20:53 +0200169
170def bug42562():
171 pass
172
173
174# Set line number for 'pass' to None
175bug42562.__code__ = bug42562.__code__.replace(co_linetable=b'\x04\x80\xff\x80')
176
177
178dis_bug42562 = """\
179 0 LOAD_CONST 0 (None)
180 2 RETURN_VALUE
181"""
182
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000183_BIG_LINENO_FORMAT = """\
184%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300185 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300187 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000188"""
189
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300190_BIG_LINENO_FORMAT2 = """\
191%4d 0 LOAD_GLOBAL 0 (spam)
192 2 POP_TOP
193 4 LOAD_CONST 0 (None)
194 6 RETURN_VALUE
195"""
196
Guido van Rossume7ba4952007-06-06 23:52:48 +0000197dis_module_expected_results = """\
198Disassembly of f:
199 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300200 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000201
202Disassembly of g:
203 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300204 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000205
206"""
207
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000208expr_str = "x + 1"
209
210dis_expr_str = """\
211 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300212 2 LOAD_CONST 0 (1)
213 4 BINARY_ADD
214 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000215"""
216
217simple_stmt_str = "x = x + 1"
218
219dis_simple_stmt_str = """\
220 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300221 2 LOAD_CONST 0 (1)
222 4 BINARY_ADD
223 6 STORE_NAME 0 (x)
224 8 LOAD_CONST 1 (None)
225 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000226"""
227
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700228annot_stmt_str = """\
229
230x: int = 1
231y: fun(1)
232lst[fun(0)]: int = 1
233"""
234# leading newline is for a reason (tests lineno)
235
236dis_annot_stmt_str = """\
237 2 0 SETUP_ANNOTATIONS
238 2 LOAD_CONST 0 (1)
239 4 STORE_NAME 0 (x)
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300240 6 LOAD_CONST 1 ('int')
241 8 LOAD_NAME 1 (__annotations__)
242 10 LOAD_CONST 2 ('x')
Mark Shannon332cd5e2018-01-30 00:41:04 +0000243 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700244
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300245 3 14 LOAD_CONST 3 ('fun(1)')
246 16 LOAD_NAME 1 (__annotations__)
247 18 LOAD_CONST 4 ('y')
248 20 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700249
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300250 4 22 LOAD_CONST 0 (1)
251 24 LOAD_NAME 2 (lst)
252 26 LOAD_NAME 3 (fun)
253 28 LOAD_CONST 5 (0)
254 30 CALL_FUNCTION 1
255 32 STORE_SUBSCR
256 34 LOAD_NAME 4 (int)
257 36 POP_TOP
258 38 LOAD_CONST 6 (None)
259 40 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700260"""
261
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000262compound_stmt_str = """\
263x = 0
264while 1:
265 x += 1"""
266# Trailing newline has been deliberately omitted
267
268dis_compound_stmt_str = """\
269 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300270 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000271
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200272 3 >> 4 LOAD_NAME 0 (x)
273 6 LOAD_CONST 1 (1)
274 8 INPLACE_ADD
275 10 STORE_NAME 0 (x)
276 12 JUMP_ABSOLUTE 4
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000277"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000278
Nick Coghlan50c48b82013-11-23 00:57:00 +1000279dis_traceback = """\
Mark Shannoncc75ab72020-11-12 19:49:33 +0000280%3d 0 SETUP_FINALLY 14 (to 16)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000281
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300282%3d 2 LOAD_CONST 1 (1)
283 4 LOAD_CONST 2 (0)
284 --> 6 BINARY_TRUE_DIVIDE
285 8 POP_TOP
286 10 POP_BLOCK
Nick Coghlan50c48b82013-11-23 00:57:00 +1000287
Mark Shannoncc75ab72020-11-12 19:49:33 +0000288%3d 12 LOAD_FAST 1 (tb)
289 14 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000290
Mark Shannoncc75ab72020-11-12 19:49:33 +0000291%3d >> 16 DUP_TOP
292 18 LOAD_GLOBAL 0 (Exception)
293 20 JUMP_IF_NOT_EXC_MATCH 58
294 22 POP_TOP
295 24 STORE_FAST 0 (e)
296 26 POP_TOP
297 28 SETUP_FINALLY 20 (to 50)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000298
Mark Shannoncc75ab72020-11-12 19:49:33 +0000299%3d 30 LOAD_FAST 0 (e)
300 32 LOAD_ATTR 1 (__traceback__)
301 34 STORE_FAST 1 (tb)
302 36 POP_BLOCK
303 38 POP_EXCEPT
304 40 LOAD_CONST 0 (None)
305 42 STORE_FAST 0 (e)
306 44 DELETE_FAST 0 (e)
307
308%3d 46 LOAD_FAST 1 (tb)
309 48 RETURN_VALUE
310 >> 50 LOAD_CONST 0 (None)
311 52 STORE_FAST 0 (e)
312 54 DELETE_FAST 0 (e)
313 56 RERAISE
Mark Shannon5977a792020-12-02 13:31:40 +0000314
315%3d >> 58 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000316""" % (TRACEBACK_CODE.co_firstlineno + 1,
317 TRACEBACK_CODE.co_firstlineno + 2,
Mark Shannoncc75ab72020-11-12 19:49:33 +0000318 TRACEBACK_CODE.co_firstlineno + 5,
Nick Coghlan50c48b82013-11-23 00:57:00 +1000319 TRACEBACK_CODE.co_firstlineno + 3,
320 TRACEBACK_CODE.co_firstlineno + 4,
Mark Shannon5977a792020-12-02 13:31:40 +0000321 TRACEBACK_CODE.co_firstlineno + 5,
322 TRACEBACK_CODE.co_firstlineno + 3)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000323
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300324def _fstring(a, b, c, d):
325 return f'{a} {b:4} {c!r} {d!r:4}'
326
327dis_fstring = """\
328%3d 0 LOAD_FAST 0 (a)
329 2 FORMAT_VALUE 0
330 4 LOAD_CONST 1 (' ')
331 6 LOAD_FAST 1 (b)
332 8 LOAD_CONST 2 ('4')
333 10 FORMAT_VALUE 4 (with format)
334 12 LOAD_CONST 1 (' ')
335 14 LOAD_FAST 2 (c)
336 16 FORMAT_VALUE 2 (repr)
337 18 LOAD_CONST 1 (' ')
338 20 LOAD_FAST 3 (d)
339 22 LOAD_CONST 2 ('4')
340 24 FORMAT_VALUE 6 (repr, with format)
341 26 BUILD_STRING 7
342 28 RETURN_VALUE
343""" % (_fstring.__code__.co_firstlineno + 1,)
344
Mark Shannon88dce262019-12-30 09:53:36 +0000345def _tryfinally(a, b):
346 try:
347 return a
348 finally:
349 b()
350
351def _tryfinallyconst(b):
352 try:
353 return 1
354 finally:
355 b()
356
357dis_tryfinally = """\
358%3d 0 SETUP_FINALLY 12 (to 14)
359
360%3d 2 LOAD_FAST 0 (a)
361 4 POP_BLOCK
362
363%3d 6 LOAD_FAST 1 (b)
364 8 CALL_FUNCTION 0
365 10 POP_TOP
366
367%3d 12 RETURN_VALUE
368
369%3d >> 14 LOAD_FAST 1 (b)
370 16 CALL_FUNCTION 0
371 18 POP_TOP
372 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000373""" % (_tryfinally.__code__.co_firstlineno + 1,
374 _tryfinally.__code__.co_firstlineno + 2,
375 _tryfinally.__code__.co_firstlineno + 4,
376 _tryfinally.__code__.co_firstlineno + 2,
377 _tryfinally.__code__.co_firstlineno + 4,
378 )
379
380dis_tryfinallyconst = """\
381%3d 0 SETUP_FINALLY 12 (to 14)
382
383%3d 2 POP_BLOCK
384
385%3d 4 LOAD_FAST 0 (b)
386 6 CALL_FUNCTION 0
387 8 POP_TOP
388
389%3d 10 LOAD_CONST 1 (1)
390 12 RETURN_VALUE
391
392%3d >> 14 LOAD_FAST 0 (b)
393 16 CALL_FUNCTION 0
394 18 POP_TOP
395 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000396""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
397 _tryfinallyconst.__code__.co_firstlineno + 2,
398 _tryfinallyconst.__code__.co_firstlineno + 4,
399 _tryfinallyconst.__code__.co_firstlineno + 2,
400 _tryfinallyconst.__code__.co_firstlineno + 4,
401 )
402
Nick Coghlanefd5df92014-07-25 23:02:56 +1000403def _g(x):
404 yield x
405
syncosmicfe2b56a2017-08-17 19:29:21 -0700406async def _ag(x):
407 yield x
408
409async def _co(x):
410 async for item in _ag(x):
411 pass
412
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300413def _h(y):
414 def foo(x):
415 '''funcdoc'''
416 return [x + z for z in y]
417 return foo
418
419dis_nested_0 = """\
420%3d 0 LOAD_CLOSURE 0 (y)
421 2 BUILD_TUPLE 1
422 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
423 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200424 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300425 10 STORE_FAST 1 (foo)
426
427%3d 12 LOAD_FAST 1 (foo)
428 14 RETURN_VALUE
429""" % (_h.__code__.co_firstlineno + 1,
430 __file__,
431 _h.__code__.co_firstlineno + 1,
432 _h.__code__.co_firstlineno + 4,
433)
434
435dis_nested_1 = """%s
436Disassembly of <code object foo at 0x..., file "%s", line %d>:
437%3d 0 LOAD_CLOSURE 0 (x)
438 2 BUILD_TUPLE 1
439 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
440 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200441 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300442 10 LOAD_DEREF 1 (y)
443 12 GET_ITER
444 14 CALL_FUNCTION 1
445 16 RETURN_VALUE
446""" % (dis_nested_0,
447 __file__,
448 _h.__code__.co_firstlineno + 1,
449 _h.__code__.co_firstlineno + 3,
450 __file__,
451 _h.__code__.co_firstlineno + 3,
452)
453
454dis_nested_2 = """%s
455Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
456%3d 0 BUILD_LIST 0
457 2 LOAD_FAST 0 (.0)
458 >> 4 FOR_ITER 12 (to 18)
459 6 STORE_FAST 1 (z)
460 8 LOAD_DEREF 0 (x)
461 10 LOAD_FAST 1 (z)
462 12 BINARY_ADD
463 14 LIST_APPEND 2
464 16 JUMP_ABSOLUTE 4
465 >> 18 RETURN_VALUE
466""" % (dis_nested_1,
467 __file__,
468 _h.__code__.co_firstlineno + 3,
469 _h.__code__.co_firstlineno + 3,
470)
471
syncosmicfe2b56a2017-08-17 19:29:21 -0700472
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000473class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500474
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300475 maxDiff = None
476
477 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000478 # We want to test the default printing behaviour, not the file arg
479 output = io.StringIO()
480 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500481 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300482 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500483 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300484 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000485 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500486
487 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000488 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500489
Zachary Warebb4b7c12013-12-26 09:55:24 -0600490 def strip_addresses(self, text):
491 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500492
493 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300494 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600495 if got != expected:
496 got = self.strip_addresses(got)
497 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000498
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000499 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500500 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000501 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
502 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000503
504 def test_opname(self):
505 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
506
507 def test_boundaries(self):
508 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
509 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
510
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300511 def test_widths(self):
512 for opcode, opname in enumerate(dis.opname):
513 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
Mark Shannon9af0e472020-01-14 10:12:45 +0000514 'BUILD_TUPLE_UNPACK_WITH_CALL',
515 'JUMP_IF_NOT_EXC_MATCH'):
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300516 continue
517 with self.subTest(opname=opname):
518 width = dis._OPNAME_WIDTH
519 if opcode < dis.HAVE_ARGUMENT:
520 width += 1 + dis._OPARG_WIDTH
521 self.assertLessEqual(len(opname), width)
522
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000523 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000524 self.do_disassembly_test(_f, dis_f)
525
526 def test_bug_708901(self):
527 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000528
Neal Norwitz51abbc72005-12-18 07:06:23 +0000529 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000530 # This one is checking bytecodes generated for an `assert` statement,
531 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600532 if not __debug__:
533 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600534
Zachary Waree80e8062013-12-26 09:53:49 -0600535 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000536
Yurii Karabasf24b8102020-12-04 17:20:53 +0200537 def test_bug_42562(self):
538 self.do_disassembly_test(bug42562, dis_bug42562)
539
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540 def test_big_linenos(self):
541 def func(count):
542 namespace = {}
543 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000544 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000545 return namespace['foo']
546
547 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000548 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000549 expected = _BIG_LINENO_FORMAT % (i + 2)
550 self.do_disassembly_test(func(i), expected)
551
552 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300553 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000554 expected = _BIG_LINENO_FORMAT % (i + 2)
555 self.do_disassembly_test(func(i), expected)
556
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300557 for i in range(1000, 5000, 10):
558 expected = _BIG_LINENO_FORMAT2 % (i + 2)
559 self.do_disassembly_test(func(i), expected)
560
Guido van Rossume7ba4952007-06-06 23:52:48 +0000561 from test import dis_module
562 self.do_disassembly_test(dis_module, dis_module_expected_results)
563
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300564 def test_big_offsets(self):
565 def func(count):
566 namespace = {}
567 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
568 exec(func, namespace)
569 return namespace['foo']
570
571 def expected(count, w):
572 s = ['''\
573 %*d LOAD_FAST 0 (x)
574 %*d LOAD_CONST 1 (1)
575 %*d BINARY_ADD
576 %*d STORE_FAST 0 (x)
577''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
578 for i in range(count)]
579 s += ['''\
580
581 3 %*d LOAD_FAST 0 (x)
582 %*d RETURN_VALUE
583''' % (w, 8*count, w, 8*count + 2)]
584 s[0] = ' 2' + s[0][3:]
585 return ''.join(s)
586
587 for i in range(1, 5):
588 self.do_disassembly_test(func(i), expected(i, 4))
589 self.do_disassembly_test(func(1249), expected(1249, 4))
590 self.do_disassembly_test(func(1250), expected(1250, 5))
591
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000592 def test_disassemble_str(self):
593 self.do_disassembly_test(expr_str, dis_expr_str)
594 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700595 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000596 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
597
Benjamin Petersond6afe722011-03-15 14:44:52 -0500598 def test_disassemble_bytes(self):
599 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
600
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300601 def test_disassemble_class(self):
602 self.do_disassembly_test(_C, dis_c)
603
604 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500605 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
606
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300607 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500608 method_bytecode = _C(1).__init__.__code__.co_code
609 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
610
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300611 def test_disassemble_static_method(self):
612 self.do_disassembly_test(_C.sm, dis_c_static_method)
613
614 def test_disassemble_class_method(self):
615 self.do_disassembly_test(_C.cm, dis_c_class_method)
616
Nick Coghlanefd5df92014-07-25 23:02:56 +1000617 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700618 gen_func_disas = self.get_disassembly(_g) # Generator function
619 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000620 self.assertEqual(gen_disas, gen_func_disas)
621
syncosmicfe2b56a2017-08-17 19:29:21 -0700622 def test_disassemble_async_generator(self):
623 agen_func_disas = self.get_disassembly(_ag) # Async generator function
624 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
625 self.assertEqual(agen_disas, agen_func_disas)
626
627 def test_disassemble_coroutine(self):
628 coro_func_disas = self.get_disassembly(_co) # Coroutine function
629 coro = _co(1) # Coroutine object
630 coro.close() # Avoid a RuntimeWarning (never awaited)
631 coro_disas = self.get_disassembly(coro)
632 self.assertEqual(coro_disas, coro_func_disas)
633
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300634 def test_disassemble_fstring(self):
635 self.do_disassembly_test(_fstring, dis_fstring)
636
Mark Shannon88dce262019-12-30 09:53:36 +0000637 def test_disassemble_try_finally(self):
638 self.do_disassembly_test(_tryfinally, dis_tryfinally)
639 self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
640
Benjamin Petersond6afe722011-03-15 14:44:52 -0500641 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500642 try:
643 del sys.last_traceback
644 except AttributeError:
645 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500646 self.assertRaises(RuntimeError, dis.dis, None)
647
Benjamin Petersond6afe722011-03-15 14:44:52 -0500648 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500649 try:
650 del sys.last_traceback
651 except AttributeError:
652 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500653
654 try:
655 1/0
656 except Exception as e:
657 tb = e.__traceback__
658 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500659
660 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
661 self.do_disassembly_test(None, tb_dis)
662
663 def test_dis_object(self):
664 self.assertRaises(TypeError, dis.dis, object())
665
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300666 def test_disassemble_recursive(self):
667 def check(expected, **kwargs):
668 dis = self.get_disassembly(_h, **kwargs)
669 dis = self.strip_addresses(dis)
670 self.assertEqual(dis, expected)
671
672 check(dis_nested_0, depth=0)
673 check(dis_nested_1, depth=1)
674 check(dis_nested_2, depth=2)
675 check(dis_nested_2, depth=3)
676 check(dis_nested_2, depth=None)
677 check(dis_nested_2)
678
679
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000680class DisWithFileTests(DisTests):
681
682 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300683 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000684 output = io.StringIO()
685 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300686 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000687 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300688 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000689 return output.getvalue()
690
691
Mark Shannon266b4622020-11-17 19:30:14 +0000692if sys.flags.optimize:
693 code_info_consts = "0: None"
694else:
695 code_info_consts = (
696 """0: 'Formatted details of methods, functions, or code.'
697 1: None"""
698)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000699
Mark Shannon266b4622020-11-17 19:30:14 +0000700code_info_code_info = f"""\
Nick Coghlaneae2da12010-08-17 08:03:36 +0000701Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000702Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000703Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100704Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000705Kw-only arguments: 0
706Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000707Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000708Flags: OPTIMIZED, NEWLOCALS, NOFREE
709Constants:
Mark Shannon266b4622020-11-17 19:30:14 +0000710 {code_info_consts}
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000711Names:
712 0: _format_code_info
713 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000714Variable names:
Mark Shannon266b4622020-11-17 19:30:14 +0000715 0: x"""
716
Nick Coghlaneae2da12010-08-17 08:03:36 +0000717
718@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100719def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000720 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100721 print(a, b, x, y, z, c, d, e, f)
722 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000723
Nick Coghlaneae2da12010-08-17 08:03:36 +0000724code_info_tricky = """\
725Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000726Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100727Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100728Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000729Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100730Number of locals: 10
731Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000732Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
733Constants:
734 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000735 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100736 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000737Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100738 0: a
739 1: b
740 2: x
741 3: y
742 4: z
743 5: c
744 6: d
745 7: e
746 8: args
747 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000748Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100749 0: [abedfxyz]
750 1: [abedfxyz]
751 2: [abedfxyz]
752 3: [abedfxyz]
753 4: [abedfxyz]
754 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100755# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000756
757co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000758
759code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000760Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000761Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100762Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000763Kw-only arguments: 0
764Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100765Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000766Flags: OPTIMIZED, NEWLOCALS, NESTED
767Constants:
768 0: None
769Names:
770 0: print
771Variable names:
772 0: c
773Free variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100774 0: [abedfxyz]
775 1: [abedfxyz]
776 2: [abedfxyz]
777 3: [abedfxyz]
778 4: [abedfxyz]
779 5: [abedfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000780
781code_info_expr_str = """\
782Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000783Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000784Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100785Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000786Kw-only arguments: 0
787Number of locals: 0
788Stack size: 2
789Flags: NOFREE
790Constants:
791 0: 1
792Names:
793 0: x"""
794
795code_info_simple_stmt_str = """\
796Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000797Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000798Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100799Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000800Kw-only arguments: 0
801Number of locals: 0
802Stack size: 2
803Flags: NOFREE
804Constants:
805 0: 1
806 1: None
807Names:
808 0: x"""
809
810code_info_compound_stmt_str = """\
811Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000812Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000813Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100814Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000815Kw-only arguments: 0
816Number of locals: 0
817Stack size: 2
818Flags: NOFREE
819Constants:
820 0: 0
821 1: 1
822 2: None
823Names:
824 0: x"""
825
Yury Selivanov75445082015-05-11 22:57:16 -0400826
827async def async_def():
828 await 1
829 async for a in b: pass
830 async with c as d: pass
831
832code_info_async_def = """\
833Name: async_def
834Filename: (.*)
835Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100836Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400837Kw-only arguments: 0
838Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000839Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700840Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400841Constants:
842 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200843 1: 1
844Names:
845 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200846 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200847Variable names:
848 0: a
849 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400850
Nick Coghlaneae2da12010-08-17 08:03:36 +0000851class CodeInfoTests(unittest.TestCase):
852 test_pairs = [
853 (dis.code_info, code_info_code_info),
854 (tricky, code_info_tricky),
855 (co_tricky_nested_f, code_info_tricky_nested_f),
856 (expr_str, code_info_expr_str),
857 (simple_stmt_str, code_info_simple_stmt_str),
858 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400859 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000860 ]
861
862 def test_code_info(self):
863 self.maxDiff = 1000
864 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000865 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000866
867 def test_show_code(self):
868 self.maxDiff = 1000
869 for x, expected in self.test_pairs:
870 with captured_stdout() as output:
871 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000872 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000873 output = io.StringIO()
874 dis.show_code(x, file=output)
875 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000876
Benjamin Petersond6afe722011-03-15 14:44:52 -0500877 def test_code_info_object(self):
878 self.assertRaises(TypeError, dis.code_info, object())
879
880 def test_pretty_flags_no_flags(self):
881 self.assertEqual(dis.pretty_flags(0), '0x0')
882
883
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000884# Fodder for instruction introspection tests
885# Editing any of these may require recalculating the expected output
886def outer(a=1, b=2):
887 def f(c=3, d=4):
888 def inner(e=5, f=6):
889 print(a, b, c, d, e, f)
890 print(a, b, c, d)
891 return inner
892 print(a, b, '', 1, [], {}, "Hello world!")
893 return f
894
895def jumpy():
896 # This won't actually run (but that's OK, we only disassemble it)
897 for i in range(10):
898 print(i)
899 if i < 4:
900 continue
901 if i > 6:
902 break
903 else:
904 print("I can haz else clause?")
905 while i:
906 print(i)
907 i -= 1
908 if i > 6:
909 continue
910 if i < 4:
911 break
912 else:
913 print("Who let lolcatz into this test suite?")
914 try:
915 1 / 0
916 except ZeroDivisionError:
917 print("Here we go, here we go, here we go...")
918 else:
919 with i as dodgy:
920 print("Never reach this")
921 finally:
922 print("OK, now we're done")
923
924# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000925expected_outer_line = 1
926_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000927code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000928expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000929code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000930expected_inner_line = code_object_inner.co_firstlineno - _line_offset
931expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000932
933# The following lines are useful to regenerate the expected results after
934# either the fodder is modified or the bytecode generation changes
935# After regeneration, update the references to code_object_f and
936# code_object_inner before rerunning the tests
937
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000938#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000939#print('expected_opinfo_outer = [\n ',
940 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200941#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000942#print('expected_opinfo_f = [\n ',
943 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200944#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000945#print('expected_opinfo_inner = [\n ',
946 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000947#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000948#print('expected_opinfo_jumpy = [\n ',
949 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
950
951
952Instruction = dis.Instruction
953expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300954 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
955 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
956 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
957 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
958 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),
959 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 +0200960 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 +0300961 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
962 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
963 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
964 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
965 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
966 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
967 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
968 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
969 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 -0700970 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 +0300971 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
972 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
973 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 +1000974]
975
976expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300977 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
978 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
979 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
980 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
981 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
982 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
983 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),
984 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 +0200985 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 +0300986 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
987 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
988 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
989 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
990 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
991 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 -0700992 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 +0300993 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
994 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
995 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 +1000996]
997
998expected_opinfo_inner = [
999 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 +03001000 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
1001 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
1002 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
1003 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
1004 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
1005 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 -07001006 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 +03001007 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1008 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
1009 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 +10001010]
1011
1012expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001013 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
1014 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
1015 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
1016 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 +00001017 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 +02001018 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
1019 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
1020 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
1021 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1022 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
1023 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
1024 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
1025 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
1026 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
1027 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
1028 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
1029 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
1030 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
1031 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1032 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 +00001033 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=40, starts_line=None, is_jump_target=False),
1034 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=42, starts_line=10, is_jump_target=True),
1035 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),
1036 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=46, starts_line=None, is_jump_target=False),
1037 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=48, starts_line=None, is_jump_target=False),
1038 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=50, starts_line=11, is_jump_target=True),
1039 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=90, argval=90, argrepr='', offset=52, starts_line=None, is_jump_target=False),
1040 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=54, starts_line=12, is_jump_target=False),
1041 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=False),
1042 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=58, starts_line=None, is_jump_target=False),
1043 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=False),
1044 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=13, is_jump_target=False),
1045 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=64, starts_line=None, is_jump_target=False),
1046 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
1047 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=68, starts_line=None, is_jump_target=False),
1048 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=70, starts_line=14, is_jump_target=False),
1049 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=72, starts_line=None, is_jump_target=False),
1050 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=74, starts_line=None, is_jump_target=False),
1051 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=76, starts_line=None, is_jump_target=False),
1052 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=78, starts_line=15, is_jump_target=False),
1053 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=16, is_jump_target=True),
1054 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=82, starts_line=None, is_jump_target=False),
1055 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=84, starts_line=None, is_jump_target=False),
1056 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=50, argval=50, argrepr='', offset=86, starts_line=None, is_jump_target=False),
1057 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=98, argval=98, argrepr='', offset=88, starts_line=17, is_jump_target=False),
1058 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=90, starts_line=19, is_jump_target=True),
1059 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),
1060 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=94, starts_line=None, is_jump_target=False),
1061 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=96, starts_line=None, is_jump_target=False),
1062 Instruction(opname='SETUP_FINALLY', opcode=122, arg=98, argval=198, argrepr='to 198', offset=98, starts_line=20, is_jump_target=True),
1063 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=114, argrepr='to 114', offset=100, starts_line=None, is_jump_target=False),
1064 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=102, starts_line=21, is_jump_target=False),
1065 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=104, starts_line=None, is_jump_target=False),
1066 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=106, starts_line=None, is_jump_target=False),
1067 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=108, starts_line=None, is_jump_target=False),
1068 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
1069 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=140, argrepr='to 140', offset=112, starts_line=None, is_jump_target=False),
1070 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=114, starts_line=22, is_jump_target=True),
1071 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=116, starts_line=None, is_jump_target=False),
1072 Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=138, argval=138, argrepr='', offset=118, starts_line=None, is_jump_target=False),
1073 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=120, starts_line=None, is_jump_target=False),
1074 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 +00001075 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 +00001076 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=126, starts_line=23, is_jump_target=False),
1077 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),
1078 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=130, starts_line=None, is_jump_target=False),
1079 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=132, starts_line=None, is_jump_target=False),
1080 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=134, starts_line=None, is_jump_target=False),
1081 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=184, argrepr='to 184', offset=136, starts_line=None, is_jump_target=False),
1082 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=True),
1083 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=140, starts_line=25, is_jump_target=True),
1084 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=168, argrepr='to 168', offset=142, starts_line=None, is_jump_target=False),
1085 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=144, starts_line=None, is_jump_target=False),
1086 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=146, starts_line=26, is_jump_target=False),
1087 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),
1088 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=150, starts_line=None, is_jump_target=False),
1089 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=152, starts_line=None, is_jump_target=False),
1090 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=154, starts_line=None, is_jump_target=False),
1091 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=156, starts_line=None, is_jump_target=False),
1092 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1093 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False),
1094 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=162, starts_line=None, is_jump_target=False),
1095 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
1096 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=184, argrepr='to 184', offset=166, starts_line=None, is_jump_target=False),
1097 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=True),
1098 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=174, argval=174, argrepr='', offset=170, starts_line=None, is_jump_target=False),
1099 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
1100 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=174, starts_line=None, is_jump_target=True),
1101 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1102 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=False),
1103 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 +00001104 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 +00001105 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=True),
1106 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=186, starts_line=28, is_jump_target=False),
1107 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),
1108 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=190, starts_line=None, is_jump_target=False),
1109 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=192, starts_line=None, is_jump_target=False),
1110 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=194, starts_line=None, is_jump_target=False),
1111 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1112 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=198, starts_line=None, is_jump_target=True),
1113 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),
1114 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=202, starts_line=None, is_jump_target=False),
1115 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=204, starts_line=None, is_jump_target=False),
1116 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 +10001117]
1118
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001119# One last piece of inspect fodder to check the default line number handling
1120def simple(): pass
1121expected_opinfo_simple = [
1122 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 +03001123 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 +10001124]
1125
1126
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001127class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001128
Mark Shannonfee55262019-11-21 09:11:43 +00001129 def __init__(self, *args):
1130 super().__init__(*args)
1131 self.maxDiff = None
1132
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001133 def test_default_first_line(self):
1134 actual = dis.get_instructions(simple)
1135 self.assertEqual(list(actual), expected_opinfo_simple)
1136
1137 def test_first_line_set_to_None(self):
1138 actual = dis.get_instructions(simple, first_line=None)
1139 self.assertEqual(list(actual), expected_opinfo_simple)
1140
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001141 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001142 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1143 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001144
1145 def test_nested(self):
1146 with captured_stdout():
1147 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001148 actual = dis.get_instructions(f, first_line=expected_f_line)
1149 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001150
1151 def test_doubly_nested(self):
1152 with captured_stdout():
1153 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001154 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1155 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001156
1157 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001158 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1159 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001160
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001161# get_instructions has its own tests above, so can rely on it to validate
1162# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001163class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001164
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001165 def test_instantiation(self):
1166 # Test with function, method, code string and code object
1167 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001168 with self.subTest(obj=obj):
1169 b = dis.Bytecode(obj)
1170 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001171
1172 self.assertRaises(TypeError, dis.Bytecode, object())
1173
1174 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001175 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1176 with self.subTest(obj=obj):
1177 via_object = list(dis.Bytecode(obj))
1178 via_generator = list(dis.get_instructions(obj))
1179 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001180
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001181 def test_explicit_first_line(self):
1182 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1183 self.assertEqual(list(actual), expected_opinfo_outer)
1184
1185 def test_source_line_in_disassembly(self):
1186 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001187 actual = dis.Bytecode(simple).dis()
1188 actual = actual.strip().partition(" ")[0] # extract the line no
1189 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001190 self.assertEqual(actual, expected)
1191 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001192 actual = dis.Bytecode(simple, first_line=350).dis()
1193 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001194 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001195
1196 def test_info(self):
1197 self.maxDiff = 1000
1198 for x, expected in CodeInfoTests.test_pairs:
1199 b = dis.Bytecode(x)
1200 self.assertRegex(b.info(), expected)
1201
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001202 def test_disassembled(self):
1203 actual = dis.Bytecode(_f).dis()
1204 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001205
Nick Coghlan50c48b82013-11-23 00:57:00 +10001206 def test_from_traceback(self):
1207 tb = get_tb()
1208 b = dis.Bytecode.from_traceback(tb)
1209 while tb.tb_next: tb = tb.tb_next
1210
1211 self.assertEqual(b.current_offset, tb.tb_lasti)
1212
1213 def test_from_traceback_dis(self):
1214 tb = get_tb()
1215 b = dis.Bytecode.from_traceback(tb)
1216 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001217
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001218if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001219 unittest.main()