blob: eb931703d51ed1eccbe210987e042f3b6950bc87 [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
Mark Shannon8473cf82020-12-15 11:07:50 +0000272 2 4 NOP
273
274 3 >> 6 LOAD_NAME 0 (x)
275 8 LOAD_CONST 1 (1)
276 10 INPLACE_ADD
277 12 STORE_NAME 0 (x)
278
279 2 14 JUMP_ABSOLUTE 6
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000280"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000281
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282dis_traceback = """\
Mark Shannoncc75ab72020-11-12 19:49:33 +0000283%3d 0 SETUP_FINALLY 14 (to 16)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000284
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300285%3d 2 LOAD_CONST 1 (1)
286 4 LOAD_CONST 2 (0)
287 --> 6 BINARY_TRUE_DIVIDE
288 8 POP_TOP
289 10 POP_BLOCK
Nick Coghlan50c48b82013-11-23 00:57:00 +1000290
Mark Shannoncc75ab72020-11-12 19:49:33 +0000291%3d 12 LOAD_FAST 1 (tb)
292 14 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000293
Mark Shannoncc75ab72020-11-12 19:49:33 +0000294%3d >> 16 DUP_TOP
295 18 LOAD_GLOBAL 0 (Exception)
296 20 JUMP_IF_NOT_EXC_MATCH 58
297 22 POP_TOP
298 24 STORE_FAST 0 (e)
299 26 POP_TOP
300 28 SETUP_FINALLY 20 (to 50)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000301
Mark Shannoncc75ab72020-11-12 19:49:33 +0000302%3d 30 LOAD_FAST 0 (e)
303 32 LOAD_ATTR 1 (__traceback__)
304 34 STORE_FAST 1 (tb)
305 36 POP_BLOCK
306 38 POP_EXCEPT
307 40 LOAD_CONST 0 (None)
308 42 STORE_FAST 0 (e)
309 44 DELETE_FAST 0 (e)
310
311%3d 46 LOAD_FAST 1 (tb)
312 48 RETURN_VALUE
313 >> 50 LOAD_CONST 0 (None)
314 52 STORE_FAST 0 (e)
315 54 DELETE_FAST 0 (e)
316 56 RERAISE
Mark Shannon5977a792020-12-02 13:31:40 +0000317
318%3d >> 58 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000319""" % (TRACEBACK_CODE.co_firstlineno + 1,
320 TRACEBACK_CODE.co_firstlineno + 2,
Mark Shannoncc75ab72020-11-12 19:49:33 +0000321 TRACEBACK_CODE.co_firstlineno + 5,
Nick Coghlan50c48b82013-11-23 00:57:00 +1000322 TRACEBACK_CODE.co_firstlineno + 3,
323 TRACEBACK_CODE.co_firstlineno + 4,
Mark Shannon5977a792020-12-02 13:31:40 +0000324 TRACEBACK_CODE.co_firstlineno + 5,
325 TRACEBACK_CODE.co_firstlineno + 3)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000326
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300327def _fstring(a, b, c, d):
328 return f'{a} {b:4} {c!r} {d!r:4}'
329
330dis_fstring = """\
331%3d 0 LOAD_FAST 0 (a)
332 2 FORMAT_VALUE 0
333 4 LOAD_CONST 1 (' ')
334 6 LOAD_FAST 1 (b)
335 8 LOAD_CONST 2 ('4')
336 10 FORMAT_VALUE 4 (with format)
337 12 LOAD_CONST 1 (' ')
338 14 LOAD_FAST 2 (c)
339 16 FORMAT_VALUE 2 (repr)
340 18 LOAD_CONST 1 (' ')
341 20 LOAD_FAST 3 (d)
342 22 LOAD_CONST 2 ('4')
343 24 FORMAT_VALUE 6 (repr, with format)
344 26 BUILD_STRING 7
345 28 RETURN_VALUE
346""" % (_fstring.__code__.co_firstlineno + 1,)
347
Mark Shannon88dce262019-12-30 09:53:36 +0000348def _tryfinally(a, b):
349 try:
350 return a
351 finally:
352 b()
353
354def _tryfinallyconst(b):
355 try:
356 return 1
357 finally:
358 b()
359
360dis_tryfinally = """\
361%3d 0 SETUP_FINALLY 12 (to 14)
362
363%3d 2 LOAD_FAST 0 (a)
364 4 POP_BLOCK
365
366%3d 6 LOAD_FAST 1 (b)
367 8 CALL_FUNCTION 0
368 10 POP_TOP
369
370%3d 12 RETURN_VALUE
371
372%3d >> 14 LOAD_FAST 1 (b)
373 16 CALL_FUNCTION 0
374 18 POP_TOP
375 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000376""" % (_tryfinally.__code__.co_firstlineno + 1,
377 _tryfinally.__code__.co_firstlineno + 2,
378 _tryfinally.__code__.co_firstlineno + 4,
379 _tryfinally.__code__.co_firstlineno + 2,
380 _tryfinally.__code__.co_firstlineno + 4,
381 )
382
383dis_tryfinallyconst = """\
384%3d 0 SETUP_FINALLY 12 (to 14)
385
386%3d 2 POP_BLOCK
387
388%3d 4 LOAD_FAST 0 (b)
389 6 CALL_FUNCTION 0
390 8 POP_TOP
391
392%3d 10 LOAD_CONST 1 (1)
393 12 RETURN_VALUE
394
395%3d >> 14 LOAD_FAST 0 (b)
396 16 CALL_FUNCTION 0
397 18 POP_TOP
398 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000399""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
400 _tryfinallyconst.__code__.co_firstlineno + 2,
401 _tryfinallyconst.__code__.co_firstlineno + 4,
402 _tryfinallyconst.__code__.co_firstlineno + 2,
403 _tryfinallyconst.__code__.co_firstlineno + 4,
404 )
405
Nick Coghlanefd5df92014-07-25 23:02:56 +1000406def _g(x):
407 yield x
408
syncosmicfe2b56a2017-08-17 19:29:21 -0700409async def _ag(x):
410 yield x
411
412async def _co(x):
413 async for item in _ag(x):
414 pass
415
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300416def _h(y):
417 def foo(x):
418 '''funcdoc'''
419 return [x + z for z in y]
420 return foo
421
422dis_nested_0 = """\
423%3d 0 LOAD_CLOSURE 0 (y)
424 2 BUILD_TUPLE 1
425 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
426 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200427 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300428 10 STORE_FAST 1 (foo)
429
430%3d 12 LOAD_FAST 1 (foo)
431 14 RETURN_VALUE
432""" % (_h.__code__.co_firstlineno + 1,
433 __file__,
434 _h.__code__.co_firstlineno + 1,
435 _h.__code__.co_firstlineno + 4,
436)
437
438dis_nested_1 = """%s
439Disassembly of <code object foo at 0x..., file "%s", line %d>:
440%3d 0 LOAD_CLOSURE 0 (x)
441 2 BUILD_TUPLE 1
442 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
443 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200444 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300445 10 LOAD_DEREF 1 (y)
446 12 GET_ITER
447 14 CALL_FUNCTION 1
448 16 RETURN_VALUE
449""" % (dis_nested_0,
450 __file__,
451 _h.__code__.co_firstlineno + 1,
452 _h.__code__.co_firstlineno + 3,
453 __file__,
454 _h.__code__.co_firstlineno + 3,
455)
456
457dis_nested_2 = """%s
458Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
459%3d 0 BUILD_LIST 0
460 2 LOAD_FAST 0 (.0)
461 >> 4 FOR_ITER 12 (to 18)
462 6 STORE_FAST 1 (z)
463 8 LOAD_DEREF 0 (x)
464 10 LOAD_FAST 1 (z)
465 12 BINARY_ADD
466 14 LIST_APPEND 2
467 16 JUMP_ABSOLUTE 4
468 >> 18 RETURN_VALUE
469""" % (dis_nested_1,
470 __file__,
471 _h.__code__.co_firstlineno + 3,
472 _h.__code__.co_firstlineno + 3,
473)
474
syncosmicfe2b56a2017-08-17 19:29:21 -0700475
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000476class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500477
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300478 maxDiff = None
479
480 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000481 # We want to test the default printing behaviour, not the file arg
482 output = io.StringIO()
483 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500484 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300485 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500486 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300487 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000488 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500489
490 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000491 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500492
Zachary Warebb4b7c12013-12-26 09:55:24 -0600493 def strip_addresses(self, text):
494 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500495
496 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300497 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600498 if got != expected:
499 got = self.strip_addresses(got)
500 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000501
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000502 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500503 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000504 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
505 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000506
507 def test_opname(self):
508 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
509
510 def test_boundaries(self):
511 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
512 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
513
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300514 def test_widths(self):
515 for opcode, opname in enumerate(dis.opname):
516 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
Mark Shannon9af0e472020-01-14 10:12:45 +0000517 'BUILD_TUPLE_UNPACK_WITH_CALL',
518 'JUMP_IF_NOT_EXC_MATCH'):
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300519 continue
520 with self.subTest(opname=opname):
521 width = dis._OPNAME_WIDTH
522 if opcode < dis.HAVE_ARGUMENT:
523 width += 1 + dis._OPARG_WIDTH
524 self.assertLessEqual(len(opname), width)
525
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000526 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000527 self.do_disassembly_test(_f, dis_f)
528
529 def test_bug_708901(self):
530 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000531
Neal Norwitz51abbc72005-12-18 07:06:23 +0000532 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000533 # This one is checking bytecodes generated for an `assert` statement,
534 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600535 if not __debug__:
536 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600537
Zachary Waree80e8062013-12-26 09:53:49 -0600538 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000539
Yurii Karabasf24b8102020-12-04 17:20:53 +0200540 def test_bug_42562(self):
541 self.do_disassembly_test(bug42562, dis_bug42562)
542
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000543 def test_big_linenos(self):
544 def func(count):
545 namespace = {}
546 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000547 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000548 return namespace['foo']
549
550 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000551 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000552 expected = _BIG_LINENO_FORMAT % (i + 2)
553 self.do_disassembly_test(func(i), expected)
554
555 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300556 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557 expected = _BIG_LINENO_FORMAT % (i + 2)
558 self.do_disassembly_test(func(i), expected)
559
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300560 for i in range(1000, 5000, 10):
561 expected = _BIG_LINENO_FORMAT2 % (i + 2)
562 self.do_disassembly_test(func(i), expected)
563
Guido van Rossume7ba4952007-06-06 23:52:48 +0000564 from test import dis_module
565 self.do_disassembly_test(dis_module, dis_module_expected_results)
566
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300567 def test_big_offsets(self):
568 def func(count):
569 namespace = {}
570 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
571 exec(func, namespace)
572 return namespace['foo']
573
574 def expected(count, w):
575 s = ['''\
576 %*d LOAD_FAST 0 (x)
577 %*d LOAD_CONST 1 (1)
578 %*d BINARY_ADD
579 %*d STORE_FAST 0 (x)
580''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
581 for i in range(count)]
582 s += ['''\
583
584 3 %*d LOAD_FAST 0 (x)
585 %*d RETURN_VALUE
586''' % (w, 8*count, w, 8*count + 2)]
587 s[0] = ' 2' + s[0][3:]
588 return ''.join(s)
589
590 for i in range(1, 5):
591 self.do_disassembly_test(func(i), expected(i, 4))
592 self.do_disassembly_test(func(1249), expected(1249, 4))
593 self.do_disassembly_test(func(1250), expected(1250, 5))
594
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000595 def test_disassemble_str(self):
596 self.do_disassembly_test(expr_str, dis_expr_str)
597 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700598 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000599 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
600
Benjamin Petersond6afe722011-03-15 14:44:52 -0500601 def test_disassemble_bytes(self):
602 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
603
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300604 def test_disassemble_class(self):
605 self.do_disassembly_test(_C, dis_c)
606
607 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500608 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
609
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300610 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500611 method_bytecode = _C(1).__init__.__code__.co_code
612 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
613
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300614 def test_disassemble_static_method(self):
615 self.do_disassembly_test(_C.sm, dis_c_static_method)
616
617 def test_disassemble_class_method(self):
618 self.do_disassembly_test(_C.cm, dis_c_class_method)
619
Nick Coghlanefd5df92014-07-25 23:02:56 +1000620 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700621 gen_func_disas = self.get_disassembly(_g) # Generator function
622 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000623 self.assertEqual(gen_disas, gen_func_disas)
624
syncosmicfe2b56a2017-08-17 19:29:21 -0700625 def test_disassemble_async_generator(self):
626 agen_func_disas = self.get_disassembly(_ag) # Async generator function
627 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
628 self.assertEqual(agen_disas, agen_func_disas)
629
630 def test_disassemble_coroutine(self):
631 coro_func_disas = self.get_disassembly(_co) # Coroutine function
632 coro = _co(1) # Coroutine object
633 coro.close() # Avoid a RuntimeWarning (never awaited)
634 coro_disas = self.get_disassembly(coro)
635 self.assertEqual(coro_disas, coro_func_disas)
636
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300637 def test_disassemble_fstring(self):
638 self.do_disassembly_test(_fstring, dis_fstring)
639
Mark Shannon88dce262019-12-30 09:53:36 +0000640 def test_disassemble_try_finally(self):
641 self.do_disassembly_test(_tryfinally, dis_tryfinally)
642 self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
643
Benjamin Petersond6afe722011-03-15 14:44:52 -0500644 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500645 try:
646 del sys.last_traceback
647 except AttributeError:
648 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500649 self.assertRaises(RuntimeError, dis.dis, None)
650
Benjamin Petersond6afe722011-03-15 14:44:52 -0500651 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500652 try:
653 del sys.last_traceback
654 except AttributeError:
655 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500656
657 try:
658 1/0
659 except Exception as e:
660 tb = e.__traceback__
661 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500662
663 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
664 self.do_disassembly_test(None, tb_dis)
665
666 def test_dis_object(self):
667 self.assertRaises(TypeError, dis.dis, object())
668
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300669 def test_disassemble_recursive(self):
670 def check(expected, **kwargs):
671 dis = self.get_disassembly(_h, **kwargs)
672 dis = self.strip_addresses(dis)
673 self.assertEqual(dis, expected)
674
675 check(dis_nested_0, depth=0)
676 check(dis_nested_1, depth=1)
677 check(dis_nested_2, depth=2)
678 check(dis_nested_2, depth=3)
679 check(dis_nested_2, depth=None)
680 check(dis_nested_2)
681
682
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000683class DisWithFileTests(DisTests):
684
685 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300686 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000687 output = io.StringIO()
688 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300689 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000690 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300691 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000692 return output.getvalue()
693
694
Mark Shannon266b4622020-11-17 19:30:14 +0000695if sys.flags.optimize:
696 code_info_consts = "0: None"
697else:
698 code_info_consts = (
699 """0: 'Formatted details of methods, functions, or code.'
700 1: None"""
701)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000702
Mark Shannon266b4622020-11-17 19:30:14 +0000703code_info_code_info = f"""\
Nick Coghlaneae2da12010-08-17 08:03:36 +0000704Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000705Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000706Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100707Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000708Kw-only arguments: 0
709Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000710Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000711Flags: OPTIMIZED, NEWLOCALS, NOFREE
712Constants:
Mark Shannon266b4622020-11-17 19:30:14 +0000713 {code_info_consts}
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000714Names:
715 0: _format_code_info
716 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000717Variable names:
Mark Shannon266b4622020-11-17 19:30:14 +0000718 0: x"""
719
Nick Coghlaneae2da12010-08-17 08:03:36 +0000720
721@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100722def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000723 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100724 print(a, b, x, y, z, c, d, e, f)
725 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000726
Nick Coghlaneae2da12010-08-17 08:03:36 +0000727code_info_tricky = """\
728Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000729Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100730Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100731Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000732Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100733Number of locals: 10
734Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000735Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
736Constants:
737 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000738 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100739 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000740Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100741 0: a
742 1: b
743 2: x
744 3: y
745 4: z
746 5: c
747 6: d
748 7: e
749 8: args
750 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000751Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100752 0: [abedfxyz]
753 1: [abedfxyz]
754 2: [abedfxyz]
755 3: [abedfxyz]
756 4: [abedfxyz]
757 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100758# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000759
760co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000761
762code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000763Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000764Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100765Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000766Kw-only arguments: 0
767Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100768Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000769Flags: OPTIMIZED, NEWLOCALS, NESTED
770Constants:
771 0: None
772Names:
773 0: print
774Variable names:
775 0: c
776Free variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100777 0: [abedfxyz]
778 1: [abedfxyz]
779 2: [abedfxyz]
780 3: [abedfxyz]
781 4: [abedfxyz]
782 5: [abedfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000783
784code_info_expr_str = """\
785Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000786Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000787Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100788Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000789Kw-only arguments: 0
790Number of locals: 0
791Stack size: 2
792Flags: NOFREE
793Constants:
794 0: 1
795Names:
796 0: x"""
797
798code_info_simple_stmt_str = """\
799Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000800Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000801Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100802Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000803Kw-only arguments: 0
804Number of locals: 0
805Stack size: 2
806Flags: NOFREE
807Constants:
808 0: 1
809 1: None
810Names:
811 0: x"""
812
813code_info_compound_stmt_str = """\
814Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000815Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000816Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100817Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000818Kw-only arguments: 0
819Number of locals: 0
820Stack size: 2
821Flags: NOFREE
822Constants:
823 0: 0
824 1: 1
825 2: None
826Names:
827 0: x"""
828
Yury Selivanov75445082015-05-11 22:57:16 -0400829
830async def async_def():
831 await 1
832 async for a in b: pass
833 async with c as d: pass
834
835code_info_async_def = """\
836Name: async_def
837Filename: (.*)
838Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100839Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400840Kw-only arguments: 0
841Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000842Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700843Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400844Constants:
845 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200846 1: 1
847Names:
848 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200849 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200850Variable names:
851 0: a
852 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400853
Nick Coghlaneae2da12010-08-17 08:03:36 +0000854class CodeInfoTests(unittest.TestCase):
855 test_pairs = [
856 (dis.code_info, code_info_code_info),
857 (tricky, code_info_tricky),
858 (co_tricky_nested_f, code_info_tricky_nested_f),
859 (expr_str, code_info_expr_str),
860 (simple_stmt_str, code_info_simple_stmt_str),
861 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400862 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000863 ]
864
865 def test_code_info(self):
866 self.maxDiff = 1000
867 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000868 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000869
870 def test_show_code(self):
871 self.maxDiff = 1000
872 for x, expected in self.test_pairs:
873 with captured_stdout() as output:
874 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000875 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000876 output = io.StringIO()
877 dis.show_code(x, file=output)
878 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000879
Benjamin Petersond6afe722011-03-15 14:44:52 -0500880 def test_code_info_object(self):
881 self.assertRaises(TypeError, dis.code_info, object())
882
883 def test_pretty_flags_no_flags(self):
884 self.assertEqual(dis.pretty_flags(0), '0x0')
885
886
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000887# Fodder for instruction introspection tests
888# Editing any of these may require recalculating the expected output
889def outer(a=1, b=2):
890 def f(c=3, d=4):
891 def inner(e=5, f=6):
892 print(a, b, c, d, e, f)
893 print(a, b, c, d)
894 return inner
895 print(a, b, '', 1, [], {}, "Hello world!")
896 return f
897
898def jumpy():
899 # This won't actually run (but that's OK, we only disassemble it)
900 for i in range(10):
901 print(i)
902 if i < 4:
903 continue
904 if i > 6:
905 break
906 else:
907 print("I can haz else clause?")
908 while i:
909 print(i)
910 i -= 1
911 if i > 6:
912 continue
913 if i < 4:
914 break
915 else:
916 print("Who let lolcatz into this test suite?")
917 try:
918 1 / 0
919 except ZeroDivisionError:
920 print("Here we go, here we go, here we go...")
921 else:
922 with i as dodgy:
923 print("Never reach this")
924 finally:
925 print("OK, now we're done")
926
927# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000928expected_outer_line = 1
929_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000930code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000931expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000932code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000933expected_inner_line = code_object_inner.co_firstlineno - _line_offset
934expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000935
936# The following lines are useful to regenerate the expected results after
937# either the fodder is modified or the bytecode generation changes
938# After regeneration, update the references to code_object_f and
939# code_object_inner before rerunning the tests
940
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000941#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000942#print('expected_opinfo_outer = [\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_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000945#print('expected_opinfo_f = [\n ',
946 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200947#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000948#print('expected_opinfo_inner = [\n ',
949 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000950#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000951#print('expected_opinfo_jumpy = [\n ',
952 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
953
954
955Instruction = dis.Instruction
956expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300957 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
958 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
959 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
960 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
961 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),
962 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 +0200963 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 +0300964 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
965 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
966 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
967 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
968 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
969 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
970 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
971 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
972 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 -0700973 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 +0300974 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
975 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
976 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 +1000977]
978
979expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300980 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
981 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
982 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
983 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
984 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
985 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
986 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),
987 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 +0200988 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 +0300989 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
990 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
991 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
992 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
993 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
994 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 -0700995 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 +0300996 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
997 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
998 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 +1000999]
1000
1001expected_opinfo_inner = [
1002 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 +03001003 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
1004 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
1005 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
1006 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
1007 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
1008 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 -07001009 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 +03001010 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1011 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
1012 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 +10001013]
1014
1015expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001016 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
1017 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
1018 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
1019 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 +00001020 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 +02001021 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
1022 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
1023 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
1024 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1025 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
1026 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
1027 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
1028 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
1029 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
1030 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
1031 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
1032 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
1033 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
1034 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1035 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 +00001036 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=40, starts_line=None, is_jump_target=False),
1037 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=42, starts_line=10, is_jump_target=True),
1038 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),
1039 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=46, starts_line=None, is_jump_target=False),
1040 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=48, starts_line=None, is_jump_target=False),
1041 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=50, starts_line=11, is_jump_target=True),
Mark Shannon8473cf82020-12-15 11:07:50 +00001042 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=94, argval=94, argrepr='', offset=52, starts_line=None, is_jump_target=False),
1043 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=54, starts_line=12, is_jump_target=True),
Mark Shannoncc75ab72020-11-12 19:49:33 +00001044 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=56, starts_line=None, is_jump_target=False),
1045 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=58, starts_line=None, is_jump_target=False),
1046 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=60, starts_line=None, is_jump_target=False),
1047 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=62, starts_line=13, is_jump_target=False),
1048 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=64, starts_line=None, is_jump_target=False),
1049 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=66, starts_line=None, is_jump_target=False),
1050 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=68, starts_line=None, is_jump_target=False),
1051 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=70, starts_line=14, is_jump_target=False),
1052 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=72, starts_line=None, is_jump_target=False),
1053 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=74, starts_line=None, is_jump_target=False),
1054 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=80, argval=80, argrepr='', offset=76, starts_line=None, is_jump_target=False),
1055 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=50, argval=50, argrepr='', offset=78, starts_line=15, is_jump_target=False),
1056 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=80, starts_line=16, is_jump_target=True),
1057 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=82, starts_line=None, is_jump_target=False),
1058 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=84, starts_line=None, is_jump_target=False),
Mark Shannon8473cf82020-12-15 11:07:50 +00001059 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=90, argval=90, argrepr='', offset=86, starts_line=None, is_jump_target=False),
1060 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=102, argval=102, argrepr='', offset=88, starts_line=17, is_jump_target=False),
1061 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=90, starts_line=11, is_jump_target=True),
1062 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=54, argval=54, argrepr='', offset=92, starts_line=None, is_jump_target=False),
1063 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=94, starts_line=19, is_jump_target=True),
1064 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval='Who let lolcatz into this test suite?', argrepr="'Who let lolcatz into this test suite?'", offset=96, starts_line=None, is_jump_target=False),
1065 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=98, starts_line=None, is_jump_target=False),
1066 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=100, starts_line=None, is_jump_target=False),
1067 Instruction(opname='SETUP_FINALLY', opcode=122, arg=98, argval=202, argrepr='to 202', offset=102, starts_line=20, is_jump_target=True),
1068 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=118, argrepr='to 118', offset=104, starts_line=None, is_jump_target=False),
1069 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False),
1070 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False),
1071 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
1072 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
1073 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=114, starts_line=None, is_jump_target=False),
1074 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=144, argrepr='to 144', offset=116, starts_line=None, is_jump_target=False),
1075 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=True),
1076 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False),
1077 Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=142, argval=142, argrepr='', offset=122, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001078 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False),
Mark Shannon8473cf82020-12-15 11:07:50 +00001079 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
1080 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False),
1081 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=130, starts_line=23, is_jump_target=False),
1082 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=132, starts_line=None, is_jump_target=False),
1083 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False),
1084 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
1085 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
1086 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=188, argrepr='to 188', offset=140, starts_line=None, is_jump_target=False),
1087 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=True),
1088 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True),
1089 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=172, argrepr='to 172', offset=146, starts_line=None, is_jump_target=False),
1090 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=148, starts_line=None, is_jump_target=False),
1091 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=150, starts_line=26, is_jump_target=False),
1092 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=152, starts_line=None, is_jump_target=False),
1093 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
1094 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
1095 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1096 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=None, is_jump_target=False),
1097 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
1098 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
1099 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False),
1100 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
1101 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=188, argrepr='to 188', offset=170, starts_line=None, is_jump_target=False),
1102 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=True),
1103 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=178, argval=178, argrepr='', offset=174, starts_line=None, is_jump_target=False),
1104 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1105 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=True),
1106 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001107 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
Mark Shannon8473cf82020-12-15 11:07:50 +00001108 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
1109 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
1110 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=True),
1111 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=190, starts_line=28, is_jump_target=False),
1112 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=192, starts_line=None, is_jump_target=False),
1113 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=194, starts_line=None, is_jump_target=False),
1114 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1115 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=198, starts_line=None, is_jump_target=False),
1116 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=200, starts_line=None, is_jump_target=False),
1117 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=202, starts_line=None, is_jump_target=True),
1118 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=204, starts_line=None, is_jump_target=False),
1119 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=206, starts_line=None, is_jump_target=False),
1120 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=208, starts_line=None, is_jump_target=False),
1121 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=210, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001122]
1123
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001124# One last piece of inspect fodder to check the default line number handling
1125def simple(): pass
1126expected_opinfo_simple = [
1127 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 +03001128 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 +10001129]
1130
1131
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001132class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001133
Mark Shannonfee55262019-11-21 09:11:43 +00001134 def __init__(self, *args):
1135 super().__init__(*args)
1136 self.maxDiff = None
1137
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001138 def test_default_first_line(self):
1139 actual = dis.get_instructions(simple)
1140 self.assertEqual(list(actual), expected_opinfo_simple)
1141
1142 def test_first_line_set_to_None(self):
1143 actual = dis.get_instructions(simple, first_line=None)
1144 self.assertEqual(list(actual), expected_opinfo_simple)
1145
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001146 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001147 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1148 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001149
1150 def test_nested(self):
1151 with captured_stdout():
1152 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001153 actual = dis.get_instructions(f, first_line=expected_f_line)
1154 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001155
1156 def test_doubly_nested(self):
1157 with captured_stdout():
1158 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001159 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1160 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001161
1162 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001163 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1164 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001165
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001166# get_instructions has its own tests above, so can rely on it to validate
1167# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001168class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001169
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001170 def test_instantiation(self):
1171 # Test with function, method, code string and code object
1172 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001173 with self.subTest(obj=obj):
1174 b = dis.Bytecode(obj)
1175 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001176
1177 self.assertRaises(TypeError, dis.Bytecode, object())
1178
1179 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001180 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1181 with self.subTest(obj=obj):
1182 via_object = list(dis.Bytecode(obj))
1183 via_generator = list(dis.get_instructions(obj))
1184 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001185
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001186 def test_explicit_first_line(self):
1187 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1188 self.assertEqual(list(actual), expected_opinfo_outer)
1189
1190 def test_source_line_in_disassembly(self):
1191 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001192 actual = dis.Bytecode(simple).dis()
1193 actual = actual.strip().partition(" ")[0] # extract the line no
1194 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001195 self.assertEqual(actual, expected)
1196 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001197 actual = dis.Bytecode(simple, first_line=350).dis()
1198 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001199 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001200
1201 def test_info(self):
1202 self.maxDiff = 1000
1203 for x, expected in CodeInfoTests.test_pairs:
1204 b = dis.Bytecode(x)
1205 self.assertRegex(b.info(), expected)
1206
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001207 def test_disassembled(self):
1208 actual = dis.Bytecode(_f).dis()
1209 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001210
Nick Coghlan50c48b82013-11-23 00:57:00 +10001211 def test_from_traceback(self):
1212 tb = get_tb()
1213 b = dis.Bytecode.from_traceback(tb)
1214 while tb.tb_next: tb = tb.tb_next
1215
1216 self.assertEqual(b.current_offset, tb.tb_lasti)
1217
1218 def test_from_traceback_dis(self):
1219 tb = get_tb()
1220 b = dis.Bytecode.from_traceback(tb)
1221 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001222
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001223if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001224 unittest.main()