blob: bbaddd57d2918937094a191fcbf0fce1bcf9a957 [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Zachary Ware38c707e2015-04-13 15:00:43 -05003from test.support import captured_stdout
Joannah Nanjekye92777d52019-09-12 10:02:59 +01004from test.support.bytecode_helper import BytecodeTestCase
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import unittest
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00006import sys
7import dis
Guido van Rossum34d19282007-08-09 01:03:29 +00008import io
Zachary Waree80e8062013-12-26 09:53:49 -06009import re
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100010import types
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100011import contextlib
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000012
Nick Coghlan50c48b82013-11-23 00:57:00 +100013def get_tb():
14 def _error():
15 try:
16 1 / 0
17 except Exception as e:
18 tb = e.__traceback__
19 return tb
20
21 tb = _error()
22 while tb.tb_next:
23 tb = tb.tb_next
24 return tb
25
26TRACEBACK_CODE = get_tb().tb_frame.f_code
27
Benjamin Petersond6afe722011-03-15 14:44:52 -050028class _C:
29 def __init__(self, x):
30 self.x = x == 1
31
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030032 @staticmethod
33 def sm(x):
34 x = x == 1
35
36 @classmethod
37 def cm(cls, x):
38 cls.x = x == 1
39
Benjamin Petersond6afe722011-03-15 14:44:52 -050040dis_c_instance_method = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030041%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030042 2 LOAD_CONST 1 (1)
43 4 COMPARE_OP 2 (==)
44 6 LOAD_FAST 0 (self)
45 8 STORE_ATTR 0 (x)
46 10 LOAD_CONST 0 (None)
47 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050048""" % (_C.__init__.__code__.co_firstlineno + 1,)
49
50dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100051 0 LOAD_FAST 1 (1)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030052 2 LOAD_CONST 1 (1)
53 4 COMPARE_OP 2 (==)
54 6 LOAD_FAST 0 (0)
55 8 STORE_ATTR 0 (0)
56 10 LOAD_CONST 0 (0)
57 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050058"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000059
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030060dis_c_class_method = """\
61%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030062 2 LOAD_CONST 1 (1)
63 4 COMPARE_OP 2 (==)
64 6 LOAD_FAST 0 (cls)
65 8 STORE_ATTR 0 (x)
66 10 LOAD_CONST 0 (None)
67 12 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030068""" % (_C.cm.__code__.co_firstlineno + 2,)
69
70dis_c_static_method = """\
71%3d 0 LOAD_FAST 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030072 2 LOAD_CONST 1 (1)
73 4 COMPARE_OP 2 (==)
74 6 STORE_FAST 0 (x)
75 8 LOAD_CONST 0 (None)
76 10 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030077""" % (_C.sm.__code__.co_firstlineno + 2,)
78
79# Class disassembling info has an extra newline at end.
80dis_c = """\
81Disassembly of %s:
82%s
83Disassembly of %s:
84%s
85Disassembly of %s:
86%s
87""" % (_C.__init__.__name__, dis_c_instance_method,
88 _C.cm.__name__, dis_c_class_method,
89 _C.sm.__name__, dis_c_static_method)
90
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000091def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000093 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000094
95dis_f = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030096%3d 0 LOAD_GLOBAL 0 (print)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030097 2 LOAD_FAST 0 (a)
Victor Stinnerf9b760f2016-09-09 10:17:08 -070098 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030099 6 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000100
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300101%3d 8 LOAD_CONST 1 (1)
102 10 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000103""" % (_f.__code__.co_firstlineno + 1,
104 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000105
106
Benjamin Petersond6afe722011-03-15 14:44:52 -0500107dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000108 0 LOAD_GLOBAL 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300109 2 LOAD_FAST 0 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700110 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300111 6 POP_TOP
112 8 LOAD_CONST 1 (1)
113 10 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -0500114"""
115
116
Michael W. Hudson26848a32003-04-29 17:07:36 +0000117def bug708901():
118 for res in range(1,
119 10):
120 pass
121
122dis_bug708901 = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200123%3d 0 LOAD_GLOBAL 0 (range)
124 2 LOAD_CONST 1 (1)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000125
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200126%3d 4 LOAD_CONST 2 (10)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300127
128%3d 6 CALL_FUNCTION 2
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200129 8 GET_ITER
130 >> 10 FOR_ITER 4 (to 16)
131 12 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000132
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200133%3d 14 JUMP_ABSOLUTE 10
134 >> 16 LOAD_CONST 0 (None)
135 18 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000136""" % (bug708901.__code__.co_firstlineno + 1,
137 bug708901.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300138 bug708901.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000139 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000140
Neal Norwitz51abbc72005-12-18 07:06:23 +0000141
142def bug1333982(x=[]):
143 assert 0, ([s for s in x] +
144 1)
145 pass
146
147dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600148%3d 0 LOAD_CONST 1 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300149 2 POP_JUMP_IF_TRUE 26
Zackery Spytzce6a0702019-08-25 03:44:09 -0600150 4 LOAD_ASSERTION_ERROR
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300151 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153 10 MAKE_FUNCTION 0
154 12 LOAD_FAST 0 (x)
155 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700156 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300157
158%3d 18 LOAD_CONST 4 (1)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300159
160%3d 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700161 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300162 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000163
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300164%3d >> 26 LOAD_CONST 0 (None)
165 28 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600167 __file__,
168 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000169 bug1333982.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300170 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000171 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000172
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173_BIG_LINENO_FORMAT = """\
174%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300175 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300177 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178"""
179
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300180_BIG_LINENO_FORMAT2 = """\
181%4d 0 LOAD_GLOBAL 0 (spam)
182 2 POP_TOP
183 4 LOAD_CONST 0 (None)
184 6 RETURN_VALUE
185"""
186
Guido van Rossume7ba4952007-06-06 23:52:48 +0000187dis_module_expected_results = """\
188Disassembly of f:
189 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300190 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000191
192Disassembly of g:
193 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300194 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000195
196"""
197
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000198expr_str = "x + 1"
199
200dis_expr_str = """\
201 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300202 2 LOAD_CONST 0 (1)
203 4 BINARY_ADD
204 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000205"""
206
207simple_stmt_str = "x = x + 1"
208
209dis_simple_stmt_str = """\
210 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300211 2 LOAD_CONST 0 (1)
212 4 BINARY_ADD
213 6 STORE_NAME 0 (x)
214 8 LOAD_CONST 1 (None)
215 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000216"""
217
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700218annot_stmt_str = """\
219
220x: int = 1
221y: fun(1)
222lst[fun(0)]: int = 1
223"""
224# leading newline is for a reason (tests lineno)
225
226dis_annot_stmt_str = """\
227 2 0 SETUP_ANNOTATIONS
228 2 LOAD_CONST 0 (1)
229 4 STORE_NAME 0 (x)
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300230 6 LOAD_CONST 1 ('int')
231 8 LOAD_NAME 1 (__annotations__)
232 10 LOAD_CONST 2 ('x')
Mark Shannon332cd5e2018-01-30 00:41:04 +0000233 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700234
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300235 3 14 LOAD_CONST 3 ('fun(1)')
236 16 LOAD_NAME 1 (__annotations__)
237 18 LOAD_CONST 4 ('y')
238 20 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700239
Batuhan Taskaya044a1042020-10-06 23:03:02 +0300240 4 22 LOAD_CONST 0 (1)
241 24 LOAD_NAME 2 (lst)
242 26 LOAD_NAME 3 (fun)
243 28 LOAD_CONST 5 (0)
244 30 CALL_FUNCTION 1
245 32 STORE_SUBSCR
246 34 LOAD_NAME 4 (int)
247 36 POP_TOP
248 38 LOAD_CONST 6 (None)
249 40 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700250"""
251
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000252compound_stmt_str = """\
253x = 0
254while 1:
255 x += 1"""
256# Trailing newline has been deliberately omitted
257
258dis_compound_stmt_str = """\
259 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300260 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000261
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200262 3 >> 4 LOAD_NAME 0 (x)
263 6 LOAD_CONST 1 (1)
264 8 INPLACE_ADD
265 10 STORE_NAME 0 (x)
266 12 JUMP_ABSOLUTE 4
267 14 LOAD_CONST 2 (None)
268 16 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000269"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000270
Nick Coghlan50c48b82013-11-23 00:57:00 +1000271dis_traceback = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200272%3d 0 SETUP_FINALLY 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000273
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300274%3d 2 LOAD_CONST 1 (1)
275 4 LOAD_CONST 2 (0)
276 --> 6 BINARY_TRUE_DIVIDE
277 8 POP_TOP
278 10 POP_BLOCK
Mark Shannon9af0e472020-01-14 10:12:45 +0000279 12 JUMP_FORWARD 42 (to 56)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000280
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300281%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282 16 LOAD_GLOBAL 0 (Exception)
Mark Shannon9af0e472020-01-14 10:12:45 +0000283 18 JUMP_IF_NOT_EXC_MATCH 54
284 20 POP_TOP
285 22 STORE_FAST 0 (e)
286 24 POP_TOP
287 26 SETUP_FINALLY 18 (to 46)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000288
Mark Shannon9af0e472020-01-14 10:12:45 +0000289%3d 28 LOAD_FAST 0 (e)
290 30 LOAD_ATTR 1 (__traceback__)
291 32 STORE_FAST 1 (tb)
292 34 POP_BLOCK
293 36 POP_EXCEPT
294 38 LOAD_CONST 0 (None)
295 40 STORE_FAST 0 (e)
296 42 DELETE_FAST 0 (e)
297 44 JUMP_FORWARD 10 (to 56)
298 >> 46 LOAD_CONST 0 (None)
299 48 STORE_FAST 0 (e)
300 50 DELETE_FAST 0 (e)
301 52 RERAISE
302 >> 54 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000303
Mark Shannon9af0e472020-01-14 10:12:45 +0000304%3d >> 56 LOAD_FAST 1 (tb)
305 58 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000306""" % (TRACEBACK_CODE.co_firstlineno + 1,
307 TRACEBACK_CODE.co_firstlineno + 2,
308 TRACEBACK_CODE.co_firstlineno + 3,
309 TRACEBACK_CODE.co_firstlineno + 4,
310 TRACEBACK_CODE.co_firstlineno + 5)
311
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300312def _fstring(a, b, c, d):
313 return f'{a} {b:4} {c!r} {d!r:4}'
314
315dis_fstring = """\
316%3d 0 LOAD_FAST 0 (a)
317 2 FORMAT_VALUE 0
318 4 LOAD_CONST 1 (' ')
319 6 LOAD_FAST 1 (b)
320 8 LOAD_CONST 2 ('4')
321 10 FORMAT_VALUE 4 (with format)
322 12 LOAD_CONST 1 (' ')
323 14 LOAD_FAST 2 (c)
324 16 FORMAT_VALUE 2 (repr)
325 18 LOAD_CONST 1 (' ')
326 20 LOAD_FAST 3 (d)
327 22 LOAD_CONST 2 ('4')
328 24 FORMAT_VALUE 6 (repr, with format)
329 26 BUILD_STRING 7
330 28 RETURN_VALUE
331""" % (_fstring.__code__.co_firstlineno + 1,)
332
Mark Shannon88dce262019-12-30 09:53:36 +0000333def _tryfinally(a, b):
334 try:
335 return a
336 finally:
337 b()
338
339def _tryfinallyconst(b):
340 try:
341 return 1
342 finally:
343 b()
344
345dis_tryfinally = """\
346%3d 0 SETUP_FINALLY 12 (to 14)
347
348%3d 2 LOAD_FAST 0 (a)
349 4 POP_BLOCK
350
351%3d 6 LOAD_FAST 1 (b)
352 8 CALL_FUNCTION 0
353 10 POP_TOP
354
355%3d 12 RETURN_VALUE
356
357%3d >> 14 LOAD_FAST 1 (b)
358 16 CALL_FUNCTION 0
359 18 POP_TOP
360 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000361""" % (_tryfinally.__code__.co_firstlineno + 1,
362 _tryfinally.__code__.co_firstlineno + 2,
363 _tryfinally.__code__.co_firstlineno + 4,
364 _tryfinally.__code__.co_firstlineno + 2,
365 _tryfinally.__code__.co_firstlineno + 4,
366 )
367
368dis_tryfinallyconst = """\
369%3d 0 SETUP_FINALLY 12 (to 14)
370
371%3d 2 POP_BLOCK
372
373%3d 4 LOAD_FAST 0 (b)
374 6 CALL_FUNCTION 0
375 8 POP_TOP
376
377%3d 10 LOAD_CONST 1 (1)
378 12 RETURN_VALUE
379
380%3d >> 14 LOAD_FAST 0 (b)
381 16 CALL_FUNCTION 0
382 18 POP_TOP
383 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000384""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
385 _tryfinallyconst.__code__.co_firstlineno + 2,
386 _tryfinallyconst.__code__.co_firstlineno + 4,
387 _tryfinallyconst.__code__.co_firstlineno + 2,
388 _tryfinallyconst.__code__.co_firstlineno + 4,
389 )
390
Nick Coghlanefd5df92014-07-25 23:02:56 +1000391def _g(x):
392 yield x
393
syncosmicfe2b56a2017-08-17 19:29:21 -0700394async def _ag(x):
395 yield x
396
397async def _co(x):
398 async for item in _ag(x):
399 pass
400
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300401def _h(y):
402 def foo(x):
403 '''funcdoc'''
404 return [x + z for z in y]
405 return foo
406
407dis_nested_0 = """\
408%3d 0 LOAD_CLOSURE 0 (y)
409 2 BUILD_TUPLE 1
410 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
411 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200412 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300413 10 STORE_FAST 1 (foo)
414
415%3d 12 LOAD_FAST 1 (foo)
416 14 RETURN_VALUE
417""" % (_h.__code__.co_firstlineno + 1,
418 __file__,
419 _h.__code__.co_firstlineno + 1,
420 _h.__code__.co_firstlineno + 4,
421)
422
423dis_nested_1 = """%s
424Disassembly of <code object foo at 0x..., file "%s", line %d>:
425%3d 0 LOAD_CLOSURE 0 (x)
426 2 BUILD_TUPLE 1
427 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
428 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200429 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300430 10 LOAD_DEREF 1 (y)
431 12 GET_ITER
432 14 CALL_FUNCTION 1
433 16 RETURN_VALUE
434""" % (dis_nested_0,
435 __file__,
436 _h.__code__.co_firstlineno + 1,
437 _h.__code__.co_firstlineno + 3,
438 __file__,
439 _h.__code__.co_firstlineno + 3,
440)
441
442dis_nested_2 = """%s
443Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
444%3d 0 BUILD_LIST 0
445 2 LOAD_FAST 0 (.0)
446 >> 4 FOR_ITER 12 (to 18)
447 6 STORE_FAST 1 (z)
448 8 LOAD_DEREF 0 (x)
449 10 LOAD_FAST 1 (z)
450 12 BINARY_ADD
451 14 LIST_APPEND 2
452 16 JUMP_ABSOLUTE 4
453 >> 18 RETURN_VALUE
454""" % (dis_nested_1,
455 __file__,
456 _h.__code__.co_firstlineno + 3,
457 _h.__code__.co_firstlineno + 3,
458)
459
syncosmicfe2b56a2017-08-17 19:29:21 -0700460
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000461class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500462
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300463 maxDiff = None
464
465 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000466 # We want to test the default printing behaviour, not the file arg
467 output = io.StringIO()
468 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500469 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300470 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500471 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300472 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000473 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500474
475 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000476 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500477
Zachary Warebb4b7c12013-12-26 09:55:24 -0600478 def strip_addresses(self, text):
479 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500480
481 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300482 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600483 if got != expected:
484 got = self.strip_addresses(got)
485 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000486
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000487 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500488 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000489 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
490 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000491
492 def test_opname(self):
493 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
494
495 def test_boundaries(self):
496 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
497 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
498
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300499 def test_widths(self):
500 for opcode, opname in enumerate(dis.opname):
501 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
Mark Shannon9af0e472020-01-14 10:12:45 +0000502 'BUILD_TUPLE_UNPACK_WITH_CALL',
503 'JUMP_IF_NOT_EXC_MATCH'):
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300504 continue
505 with self.subTest(opname=opname):
506 width = dis._OPNAME_WIDTH
507 if opcode < dis.HAVE_ARGUMENT:
508 width += 1 + dis._OPARG_WIDTH
509 self.assertLessEqual(len(opname), width)
510
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000511 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000512 self.do_disassembly_test(_f, dis_f)
513
514 def test_bug_708901(self):
515 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000516
Neal Norwitz51abbc72005-12-18 07:06:23 +0000517 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000518 # This one is checking bytecodes generated for an `assert` statement,
519 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600520 if not __debug__:
521 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600522
Zachary Waree80e8062013-12-26 09:53:49 -0600523 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000524
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 def test_big_linenos(self):
526 def func(count):
527 namespace = {}
528 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000529 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 return namespace['foo']
531
532 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000533 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 expected = _BIG_LINENO_FORMAT % (i + 2)
535 self.do_disassembly_test(func(i), expected)
536
537 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300538 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000539 expected = _BIG_LINENO_FORMAT % (i + 2)
540 self.do_disassembly_test(func(i), expected)
541
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300542 for i in range(1000, 5000, 10):
543 expected = _BIG_LINENO_FORMAT2 % (i + 2)
544 self.do_disassembly_test(func(i), expected)
545
Guido van Rossume7ba4952007-06-06 23:52:48 +0000546 from test import dis_module
547 self.do_disassembly_test(dis_module, dis_module_expected_results)
548
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300549 def test_big_offsets(self):
550 def func(count):
551 namespace = {}
552 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
553 exec(func, namespace)
554 return namespace['foo']
555
556 def expected(count, w):
557 s = ['''\
558 %*d LOAD_FAST 0 (x)
559 %*d LOAD_CONST 1 (1)
560 %*d BINARY_ADD
561 %*d STORE_FAST 0 (x)
562''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
563 for i in range(count)]
564 s += ['''\
565
566 3 %*d LOAD_FAST 0 (x)
567 %*d RETURN_VALUE
568''' % (w, 8*count, w, 8*count + 2)]
569 s[0] = ' 2' + s[0][3:]
570 return ''.join(s)
571
572 for i in range(1, 5):
573 self.do_disassembly_test(func(i), expected(i, 4))
574 self.do_disassembly_test(func(1249), expected(1249, 4))
575 self.do_disassembly_test(func(1250), expected(1250, 5))
576
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000577 def test_disassemble_str(self):
578 self.do_disassembly_test(expr_str, dis_expr_str)
579 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700580 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000581 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
582
Benjamin Petersond6afe722011-03-15 14:44:52 -0500583 def test_disassemble_bytes(self):
584 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
585
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300586 def test_disassemble_class(self):
587 self.do_disassembly_test(_C, dis_c)
588
589 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500590 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
591
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300592 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500593 method_bytecode = _C(1).__init__.__code__.co_code
594 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
595
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300596 def test_disassemble_static_method(self):
597 self.do_disassembly_test(_C.sm, dis_c_static_method)
598
599 def test_disassemble_class_method(self):
600 self.do_disassembly_test(_C.cm, dis_c_class_method)
601
Nick Coghlanefd5df92014-07-25 23:02:56 +1000602 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700603 gen_func_disas = self.get_disassembly(_g) # Generator function
604 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000605 self.assertEqual(gen_disas, gen_func_disas)
606
syncosmicfe2b56a2017-08-17 19:29:21 -0700607 def test_disassemble_async_generator(self):
608 agen_func_disas = self.get_disassembly(_ag) # Async generator function
609 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
610 self.assertEqual(agen_disas, agen_func_disas)
611
612 def test_disassemble_coroutine(self):
613 coro_func_disas = self.get_disassembly(_co) # Coroutine function
614 coro = _co(1) # Coroutine object
615 coro.close() # Avoid a RuntimeWarning (never awaited)
616 coro_disas = self.get_disassembly(coro)
617 self.assertEqual(coro_disas, coro_func_disas)
618
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300619 def test_disassemble_fstring(self):
620 self.do_disassembly_test(_fstring, dis_fstring)
621
Mark Shannon88dce262019-12-30 09:53:36 +0000622 def test_disassemble_try_finally(self):
623 self.do_disassembly_test(_tryfinally, dis_tryfinally)
624 self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
625
Benjamin Petersond6afe722011-03-15 14:44:52 -0500626 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500627 try:
628 del sys.last_traceback
629 except AttributeError:
630 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500631 self.assertRaises(RuntimeError, dis.dis, None)
632
Benjamin Petersond6afe722011-03-15 14:44:52 -0500633 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500634 try:
635 del sys.last_traceback
636 except AttributeError:
637 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500638
639 try:
640 1/0
641 except Exception as e:
642 tb = e.__traceback__
643 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500644
645 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
646 self.do_disassembly_test(None, tb_dis)
647
648 def test_dis_object(self):
649 self.assertRaises(TypeError, dis.dis, object())
650
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300651 def test_disassemble_recursive(self):
652 def check(expected, **kwargs):
653 dis = self.get_disassembly(_h, **kwargs)
654 dis = self.strip_addresses(dis)
655 self.assertEqual(dis, expected)
656
657 check(dis_nested_0, depth=0)
658 check(dis_nested_1, depth=1)
659 check(dis_nested_2, depth=2)
660 check(dis_nested_2, depth=3)
661 check(dis_nested_2, depth=None)
662 check(dis_nested_2)
663
664
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000665class DisWithFileTests(DisTests):
666
667 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300668 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000669 output = io.StringIO()
670 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300671 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000672 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300673 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000674 return output.getvalue()
675
676
677
Nick Coghlaneae2da12010-08-17 08:03:36 +0000678code_info_code_info = """\
679Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000680Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000681Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100682Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000683Kw-only arguments: 0
684Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000685Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000686Flags: OPTIMIZED, NEWLOCALS, NOFREE
687Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000688 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000689Names:
690 0: _format_code_info
691 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000692Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000693 0: x""" % (('Formatted details of methods, functions, or code.',)
694 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000695
696@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100697def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000698 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100699 print(a, b, x, y, z, c, d, e, f)
700 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000701
Nick Coghlaneae2da12010-08-17 08:03:36 +0000702code_info_tricky = """\
703Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000704Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100705Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100706Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000707Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100708Number of locals: 10
709Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000710Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
711Constants:
712 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000713 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100714 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000715Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100716 0: a
717 1: b
718 2: x
719 3: y
720 4: z
721 5: c
722 6: d
723 7: e
724 8: args
725 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000726Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100727 0: [abedfxyz]
728 1: [abedfxyz]
729 2: [abedfxyz]
730 3: [abedfxyz]
731 4: [abedfxyz]
732 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100733# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000734
735co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000736
737code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000738Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000739Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100740Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000741Kw-only arguments: 0
742Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100743Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000744Flags: OPTIMIZED, NEWLOCALS, NESTED
745Constants:
746 0: None
747Names:
748 0: print
749Variable names:
750 0: c
751Free 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]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000758
759code_info_expr_str = """\
760Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000761Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000762Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100763Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000764Kw-only arguments: 0
765Number of locals: 0
766Stack size: 2
767Flags: NOFREE
768Constants:
769 0: 1
770Names:
771 0: x"""
772
773code_info_simple_stmt_str = """\
774Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000775Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000776Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100777Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000778Kw-only arguments: 0
779Number of locals: 0
780Stack size: 2
781Flags: NOFREE
782Constants:
783 0: 1
784 1: None
785Names:
786 0: x"""
787
788code_info_compound_stmt_str = """\
789Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000790Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000791Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100792Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000793Kw-only arguments: 0
794Number of locals: 0
795Stack size: 2
796Flags: NOFREE
797Constants:
798 0: 0
799 1: 1
800 2: None
801Names:
802 0: x"""
803
Yury Selivanov75445082015-05-11 22:57:16 -0400804
805async def async_def():
806 await 1
807 async for a in b: pass
808 async with c as d: pass
809
810code_info_async_def = """\
811Name: async_def
812Filename: (.*)
813Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100814Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400815Kw-only arguments: 0
816Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000817Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700818Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400819Constants:
820 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200821 1: 1
822Names:
823 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200824 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200825Variable names:
826 0: a
827 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400828
Nick Coghlaneae2da12010-08-17 08:03:36 +0000829class CodeInfoTests(unittest.TestCase):
830 test_pairs = [
831 (dis.code_info, code_info_code_info),
832 (tricky, code_info_tricky),
833 (co_tricky_nested_f, code_info_tricky_nested_f),
834 (expr_str, code_info_expr_str),
835 (simple_stmt_str, code_info_simple_stmt_str),
836 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400837 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000838 ]
839
840 def test_code_info(self):
841 self.maxDiff = 1000
842 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000843 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000844
845 def test_show_code(self):
846 self.maxDiff = 1000
847 for x, expected in self.test_pairs:
848 with captured_stdout() as output:
849 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000850 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000851 output = io.StringIO()
852 dis.show_code(x, file=output)
853 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000854
Benjamin Petersond6afe722011-03-15 14:44:52 -0500855 def test_code_info_object(self):
856 self.assertRaises(TypeError, dis.code_info, object())
857
858 def test_pretty_flags_no_flags(self):
859 self.assertEqual(dis.pretty_flags(0), '0x0')
860
861
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000862# Fodder for instruction introspection tests
863# Editing any of these may require recalculating the expected output
864def outer(a=1, b=2):
865 def f(c=3, d=4):
866 def inner(e=5, f=6):
867 print(a, b, c, d, e, f)
868 print(a, b, c, d)
869 return inner
870 print(a, b, '', 1, [], {}, "Hello world!")
871 return f
872
873def jumpy():
874 # This won't actually run (but that's OK, we only disassemble it)
875 for i in range(10):
876 print(i)
877 if i < 4:
878 continue
879 if i > 6:
880 break
881 else:
882 print("I can haz else clause?")
883 while i:
884 print(i)
885 i -= 1
886 if i > 6:
887 continue
888 if i < 4:
889 break
890 else:
891 print("Who let lolcatz into this test suite?")
892 try:
893 1 / 0
894 except ZeroDivisionError:
895 print("Here we go, here we go, here we go...")
896 else:
897 with i as dodgy:
898 print("Never reach this")
899 finally:
900 print("OK, now we're done")
901
902# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000903expected_outer_line = 1
904_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000905code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000906expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000907code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000908expected_inner_line = code_object_inner.co_firstlineno - _line_offset
909expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000910
911# The following lines are useful to regenerate the expected results after
912# either the fodder is modified or the bytecode generation changes
913# After regeneration, update the references to code_object_f and
914# code_object_inner before rerunning the tests
915
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000916#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000917#print('expected_opinfo_outer = [\n ',
918 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200919#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000920#print('expected_opinfo_f = [\n ',
921 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200922#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000923#print('expected_opinfo_inner = [\n ',
924 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000925#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000926#print('expected_opinfo_jumpy = [\n ',
927 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
928
929
930Instruction = dis.Instruction
931expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300932 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
933 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
934 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
935 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
936 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),
937 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 +0200938 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 +0300939 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
940 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
941 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
942 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
943 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
944 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
945 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
946 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
947 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 -0700948 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 +0300949 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
950 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
951 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 +1000952]
953
954expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300955 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
956 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
957 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
958 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
959 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
960 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
961 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),
962 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 +0200963 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 +0300964 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
965 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
966 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
967 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
968 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
969 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 -0700970 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 +0300971 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
972 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
973 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 +1000974]
975
976expected_opinfo_inner = [
977 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 +0300978 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
979 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
980 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
981 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
982 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
983 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 -0700984 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 +0300985 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
986 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
987 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 +1000988]
989
990expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200991 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
992 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
993 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
994 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
995 Instruction(opname='FOR_ITER', opcode=93, arg=34, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True),
996 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
997 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
998 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
999 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1000 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
1001 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
1002 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
1003 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
1004 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
1005 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
1006 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
1007 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
1008 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
1009 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1010 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
1011 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=40, starts_line=None, is_jump_target=False),
1012 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=42, starts_line=None, is_jump_target=False),
1013 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
1014 Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False),
1015 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False),
1016 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False),
1017 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True),
1018 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=94, argval=94, argrepr='', offset=54, starts_line=None, is_jump_target=False),
1019 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=False),
1020 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False),
1021 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False),
1022 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False),
1023 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False),
1024 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False),
1025 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False),
1026 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False),
1027 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False),
1028 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False),
1029 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False),
1030 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=82, argval=82, argrepr='', offset=78, starts_line=None, is_jump_target=False),
1031 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=80, starts_line=15, is_jump_target=False),
1032 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True),
1033 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
1034 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
1035 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=52, argval=52, argrepr='', offset=88, starts_line=None, is_jump_target=False),
1036 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=102, argval=102, argrepr='', offset=90, starts_line=17, is_jump_target=False),
1037 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=92, starts_line=None, is_jump_target=False),
1038 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=94, starts_line=19, is_jump_target=True),
1039 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),
1040 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=98, starts_line=None, is_jump_target=False),
1041 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=100, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001042 Instruction(opname='SETUP_FINALLY', opcode=122, arg=96, argval=200, argrepr='to 200', offset=102, starts_line=20, is_jump_target=True),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001043 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=118, argrepr='to 118', offset=104, starts_line=None, is_jump_target=False),
1044 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001045 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001046 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
1047 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
1048 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=114, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001049 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=144, argrepr='to 144', offset=116, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001050 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=True),
1051 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001052 Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=142, argval=142, argrepr='', offset=122, starts_line=None, is_jump_target=False),
1053 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=124, starts_line=None, is_jump_target=False),
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001054 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
1055 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001056 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=130, starts_line=23, is_jump_target=False),
1057 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),
1058 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False),
1059 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
1060 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
1061 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=188, argrepr='to 188', offset=140, starts_line=None, is_jump_target=False),
1062 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=True),
1063 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True),
1064 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=172, argrepr='to 172', offset=146, starts_line=None, is_jump_target=False),
1065 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=148, starts_line=None, is_jump_target=False),
1066 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=150, starts_line=26, is_jump_target=False),
1067 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),
1068 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
1069 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
1070 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1071 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=None, is_jump_target=False),
1072 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
Mark Shannonfee55262019-11-21 09:11:43 +00001073 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001074 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False),
1075 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
1076 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=188, argrepr='to 188', offset=170, starts_line=None, is_jump_target=False),
1077 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=True),
1078 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=178, argval=178, argrepr='', offset=174, starts_line=None, is_jump_target=False),
1079 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1080 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=True),
1081 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 +00001082 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
Mark Shannon9af0e472020-01-14 10:12:45 +00001083 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
1084 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
1085 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=True),
1086 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=190, starts_line=28, is_jump_target=False),
1087 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),
1088 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=194, starts_line=None, is_jump_target=False),
1089 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1090 Instruction(opname='JUMP_FORWARD', opcode=110, arg=10, argval=210, argrepr='to 210', offset=198, starts_line=None, is_jump_target=False),
1091 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=200, starts_line=None, is_jump_target=True),
1092 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=202, starts_line=None, is_jump_target=False),
1093 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=204, starts_line=None, is_jump_target=False),
1094 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
1095 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=208, starts_line=None, is_jump_target=False),
1096 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=210, starts_line=None, is_jump_target=True),
1097 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=212, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001098]
1099
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001100# One last piece of inspect fodder to check the default line number handling
1101def simple(): pass
1102expected_opinfo_simple = [
1103 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 +03001104 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 +10001105]
1106
1107
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001108class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001109
Mark Shannonfee55262019-11-21 09:11:43 +00001110 def __init__(self, *args):
1111 super().__init__(*args)
1112 self.maxDiff = None
1113
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001114 def test_default_first_line(self):
1115 actual = dis.get_instructions(simple)
1116 self.assertEqual(list(actual), expected_opinfo_simple)
1117
1118 def test_first_line_set_to_None(self):
1119 actual = dis.get_instructions(simple, first_line=None)
1120 self.assertEqual(list(actual), expected_opinfo_simple)
1121
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001122 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001123 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1124 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001125
1126 def test_nested(self):
1127 with captured_stdout():
1128 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001129 actual = dis.get_instructions(f, first_line=expected_f_line)
1130 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001131
1132 def test_doubly_nested(self):
1133 with captured_stdout():
1134 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001135 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1136 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001137
1138 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001139 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1140 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001141
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001142# get_instructions has its own tests above, so can rely on it to validate
1143# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001144class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001145
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001146 def test_instantiation(self):
1147 # Test with function, method, code string and code object
1148 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001149 with self.subTest(obj=obj):
1150 b = dis.Bytecode(obj)
1151 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001152
1153 self.assertRaises(TypeError, dis.Bytecode, object())
1154
1155 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001156 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1157 with self.subTest(obj=obj):
1158 via_object = list(dis.Bytecode(obj))
1159 via_generator = list(dis.get_instructions(obj))
1160 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001161
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001162 def test_explicit_first_line(self):
1163 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1164 self.assertEqual(list(actual), expected_opinfo_outer)
1165
1166 def test_source_line_in_disassembly(self):
1167 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001168 actual = dis.Bytecode(simple).dis()
1169 actual = actual.strip().partition(" ")[0] # extract the line no
1170 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001171 self.assertEqual(actual, expected)
1172 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001173 actual = dis.Bytecode(simple, first_line=350).dis()
1174 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001175 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001176
1177 def test_info(self):
1178 self.maxDiff = 1000
1179 for x, expected in CodeInfoTests.test_pairs:
1180 b = dis.Bytecode(x)
1181 self.assertRegex(b.info(), expected)
1182
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001183 def test_disassembled(self):
1184 actual = dis.Bytecode(_f).dis()
1185 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001186
Nick Coghlan50c48b82013-11-23 00:57:00 +10001187 def test_from_traceback(self):
1188 tb = get_tb()
1189 b = dis.Bytecode.from_traceback(tb)
1190 while tb.tb_next: tb = tb.tb_next
1191
1192 self.assertEqual(b.current_offset, tb.tb_lasti)
1193
1194 def test_from_traceback_dis(self):
1195 tb = get_tb()
1196 b = dis.Bytecode.from_traceback(tb)
1197 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001198
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001199if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001200 unittest.main()