blob: 4533a016a2fab51979dcfcf60fe433f7b7d6e8ea [file] [log] [blame]
Guido van Rossumd8faa362007-04-27 19:54:29 +00001# Minimal tests for dis module
2
Zachary Ware38c707e2015-04-13 15:00:43 -05003from test.support import captured_stdout
Joannah Nanjekye92777d52019-09-12 10:02:59 +01004from test.support.bytecode_helper import BytecodeTestCase
Guido van Rossumd8faa362007-04-27 19:54:29 +00005import unittest
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00006import sys
7import dis
Guido van Rossum34d19282007-08-09 01:03:29 +00008import io
Zachary Waree80e8062013-12-26 09:53:49 -06009import re
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100010import types
Nick Coghlan90b8e7d2013-11-06 22:08:36 +100011import contextlib
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000012
Nick Coghlan50c48b82013-11-23 00:57:00 +100013def get_tb():
14 def _error():
15 try:
16 1 / 0
17 except Exception as e:
18 tb = e.__traceback__
19 return tb
20
21 tb = _error()
22 while tb.tb_next:
23 tb = tb.tb_next
24 return tb
25
26TRACEBACK_CODE = get_tb().tb_frame.f_code
27
Benjamin Petersond6afe722011-03-15 14:44:52 -050028class _C:
29 def __init__(self, x):
30 self.x = x == 1
31
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030032 @staticmethod
33 def sm(x):
34 x = x == 1
35
36 @classmethod
37 def cm(cls, x):
38 cls.x = x == 1
39
Benjamin Petersond6afe722011-03-15 14:44:52 -050040dis_c_instance_method = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030041%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030042 2 LOAD_CONST 1 (1)
43 4 COMPARE_OP 2 (==)
44 6 LOAD_FAST 0 (self)
45 8 STORE_ATTR 0 (x)
46 10 LOAD_CONST 0 (None)
47 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050048""" % (_C.__init__.__code__.co_firstlineno + 1,)
49
50dis_c_instance_method_bytes = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +100051 0 LOAD_FAST 1 (1)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030052 2 LOAD_CONST 1 (1)
53 4 COMPARE_OP 2 (==)
54 6 LOAD_FAST 0 (0)
55 8 STORE_ATTR 0 (0)
56 10 LOAD_CONST 0 (0)
57 12 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -050058"""
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000059
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030060dis_c_class_method = """\
61%3d 0 LOAD_FAST 1 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030062 2 LOAD_CONST 1 (1)
63 4 COMPARE_OP 2 (==)
64 6 LOAD_FAST 0 (cls)
65 8 STORE_ATTR 0 (x)
66 10 LOAD_CONST 0 (None)
67 12 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030068""" % (_C.cm.__code__.co_firstlineno + 2,)
69
70dis_c_static_method = """\
71%3d 0 LOAD_FAST 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030072 2 LOAD_CONST 1 (1)
73 4 COMPARE_OP 2 (==)
74 6 STORE_FAST 0 (x)
75 8 LOAD_CONST 0 (None)
76 10 RETURN_VALUE
Serhiy Storchaka585c93d2016-04-23 09:23:52 +030077""" % (_C.sm.__code__.co_firstlineno + 2,)
78
79# Class disassembling info has an extra newline at end.
80dis_c = """\
81Disassembly of %s:
82%s
83Disassembly of %s:
84%s
85Disassembly of %s:
86%s
87""" % (_C.__init__.__name__, dis_c_instance_method,
88 _C.cm.__name__, dis_c_class_method,
89 _C.sm.__name__, dis_c_static_method)
90
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000091def _f(a):
Guido van Rossumbe19ed72007-02-09 05:37:30 +000092 print(a)
Tim Peterseabafeb2003-03-07 15:55:36 +000093 return 1
Skip Montanaroadd0ccc2003-02-27 21:27:07 +000094
95dis_f = """\
Serhiy Storchaka247763d2016-04-12 08:46:28 +030096%3d 0 LOAD_GLOBAL 0 (print)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030097 2 LOAD_FAST 0 (a)
Victor Stinnerf9b760f2016-09-09 10:17:08 -070098 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030099 6 POP_TOP
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000100
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300101%3d 8 LOAD_CONST 1 (1)
102 10 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000103""" % (_f.__code__.co_firstlineno + 1,
104 _f.__code__.co_firstlineno + 2)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000105
106
Benjamin Petersond6afe722011-03-15 14:44:52 -0500107dis_f_co_code = """\
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000108 0 LOAD_GLOBAL 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300109 2 LOAD_FAST 0 (0)
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700110 4 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300111 6 POP_TOP
112 8 LOAD_CONST 1 (1)
113 10 RETURN_VALUE
Benjamin Petersond6afe722011-03-15 14:44:52 -0500114"""
115
116
Michael W. Hudson26848a32003-04-29 17:07:36 +0000117def bug708901():
118 for res in range(1,
119 10):
120 pass
121
122dis_bug708901 = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200123%3d 0 LOAD_GLOBAL 0 (range)
124 2 LOAD_CONST 1 (1)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000125
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200126%3d 4 LOAD_CONST 2 (10)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300127
128%3d 6 CALL_FUNCTION 2
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200129 8 GET_ITER
130 >> 10 FOR_ITER 4 (to 16)
131 12 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000132
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200133%3d 14 JUMP_ABSOLUTE 10
134 >> 16 LOAD_CONST 0 (None)
135 18 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000136""" % (bug708901.__code__.co_firstlineno + 1,
137 bug708901.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300138 bug708901.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000139 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000140
Neal Norwitz51abbc72005-12-18 07:06:23 +0000141
142def bug1333982(x=[]):
143 assert 0, ([s for s in x] +
144 1)
145 pass
146
147dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600148%3d 0 LOAD_CONST 1 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300149 2 POP_JUMP_IF_TRUE 26
Zackery Spytzce6a0702019-08-25 03:44:09 -0600150 4 LOAD_ASSERTION_ERROR
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300151 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
152 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
153 10 MAKE_FUNCTION 0
154 12 LOAD_FAST 0 (x)
155 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700156 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300157
158%3d 18 LOAD_CONST 4 (1)
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300159
160%3d 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700161 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300162 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000163
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300164%3d >> 26 LOAD_CONST 0 (None)
165 28 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600167 __file__,
168 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000169 bug1333982.__code__.co_firstlineno + 2,
Serhiy Storchakada8d72c2018-09-17 15:17:29 +0300170 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000171 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000172
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000173_BIG_LINENO_FORMAT = """\
174%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300175 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300177 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000178"""
179
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300180_BIG_LINENO_FORMAT2 = """\
181%4d 0 LOAD_GLOBAL 0 (spam)
182 2 POP_TOP
183 4 LOAD_CONST 0 (None)
184 6 RETURN_VALUE
185"""
186
Guido van Rossume7ba4952007-06-06 23:52:48 +0000187dis_module_expected_results = """\
188Disassembly of f:
189 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300190 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000191
192Disassembly of g:
193 5 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300194 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000195
196"""
197
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000198expr_str = "x + 1"
199
200dis_expr_str = """\
201 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300202 2 LOAD_CONST 0 (1)
203 4 BINARY_ADD
204 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000205"""
206
207simple_stmt_str = "x = x + 1"
208
209dis_simple_stmt_str = """\
210 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300211 2 LOAD_CONST 0 (1)
212 4 BINARY_ADD
213 6 STORE_NAME 0 (x)
214 8 LOAD_CONST 1 (None)
215 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000216"""
217
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700218annot_stmt_str = """\
219
220x: int = 1
221y: fun(1)
222lst[fun(0)]: int = 1
223"""
224# leading newline is for a reason (tests lineno)
225
226dis_annot_stmt_str = """\
227 2 0 SETUP_ANNOTATIONS
228 2 LOAD_CONST 0 (1)
229 4 STORE_NAME 0 (x)
230 6 LOAD_NAME 1 (int)
Mark Shannon332cd5e2018-01-30 00:41:04 +0000231 8 LOAD_NAME 2 (__annotations__)
232 10 LOAD_CONST 1 ('x')
233 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700234
Mark Shannon332cd5e2018-01-30 00:41:04 +0000235 3 14 LOAD_NAME 3 (fun)
236 16 LOAD_CONST 0 (1)
237 18 CALL_FUNCTION 1
238 20 LOAD_NAME 2 (__annotations__)
239 22 LOAD_CONST 2 ('y')
240 24 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700241
Mark Shannon332cd5e2018-01-30 00:41:04 +0000242 4 26 LOAD_CONST 0 (1)
243 28 LOAD_NAME 4 (lst)
244 30 LOAD_NAME 3 (fun)
245 32 LOAD_CONST 3 (0)
246 34 CALL_FUNCTION 1
247 36 STORE_SUBSCR
248 38 LOAD_NAME 1 (int)
249 40 POP_TOP
250 42 LOAD_CONST 4 (None)
251 44 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700252"""
253
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000254compound_stmt_str = """\
255x = 0
256while 1:
257 x += 1"""
258# Trailing newline has been deliberately omitted
259
260dis_compound_stmt_str = """\
261 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300262 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000263
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200264 3 >> 4 LOAD_NAME 0 (x)
265 6 LOAD_CONST 1 (1)
266 8 INPLACE_ADD
267 10 STORE_NAME 0 (x)
268 12 JUMP_ABSOLUTE 4
269 14 LOAD_CONST 2 (None)
270 16 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000271"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000272
Nick Coghlan50c48b82013-11-23 00:57:00 +1000273dis_traceback = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200274%3d 0 SETUP_FINALLY 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000275
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300276%3d 2 LOAD_CONST 1 (1)
277 4 LOAD_CONST 2 (0)
278 --> 6 BINARY_TRUE_DIVIDE
279 8 POP_TOP
280 10 POP_BLOCK
Mark Shannon9af0e472020-01-14 10:12:45 +0000281 12 JUMP_FORWARD 42 (to 56)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000282
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300283%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000284 16 LOAD_GLOBAL 0 (Exception)
Mark Shannon9af0e472020-01-14 10:12:45 +0000285 18 JUMP_IF_NOT_EXC_MATCH 54
286 20 POP_TOP
287 22 STORE_FAST 0 (e)
288 24 POP_TOP
289 26 SETUP_FINALLY 18 (to 46)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000290
Mark Shannon9af0e472020-01-14 10:12:45 +0000291%3d 28 LOAD_FAST 0 (e)
292 30 LOAD_ATTR 1 (__traceback__)
293 32 STORE_FAST 1 (tb)
294 34 POP_BLOCK
295 36 POP_EXCEPT
296 38 LOAD_CONST 0 (None)
297 40 STORE_FAST 0 (e)
298 42 DELETE_FAST 0 (e)
299 44 JUMP_FORWARD 10 (to 56)
300 >> 46 LOAD_CONST 0 (None)
301 48 STORE_FAST 0 (e)
302 50 DELETE_FAST 0 (e)
303 52 RERAISE
304 >> 54 RERAISE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000305
Mark Shannon9af0e472020-01-14 10:12:45 +0000306%3d >> 56 LOAD_FAST 1 (tb)
307 58 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000308""" % (TRACEBACK_CODE.co_firstlineno + 1,
309 TRACEBACK_CODE.co_firstlineno + 2,
310 TRACEBACK_CODE.co_firstlineno + 3,
311 TRACEBACK_CODE.co_firstlineno + 4,
312 TRACEBACK_CODE.co_firstlineno + 5)
313
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300314def _fstring(a, b, c, d):
315 return f'{a} {b:4} {c!r} {d!r:4}'
316
317dis_fstring = """\
318%3d 0 LOAD_FAST 0 (a)
319 2 FORMAT_VALUE 0
320 4 LOAD_CONST 1 (' ')
321 6 LOAD_FAST 1 (b)
322 8 LOAD_CONST 2 ('4')
323 10 FORMAT_VALUE 4 (with format)
324 12 LOAD_CONST 1 (' ')
325 14 LOAD_FAST 2 (c)
326 16 FORMAT_VALUE 2 (repr)
327 18 LOAD_CONST 1 (' ')
328 20 LOAD_FAST 3 (d)
329 22 LOAD_CONST 2 ('4')
330 24 FORMAT_VALUE 6 (repr, with format)
331 26 BUILD_STRING 7
332 28 RETURN_VALUE
333""" % (_fstring.__code__.co_firstlineno + 1,)
334
Mark Shannon88dce262019-12-30 09:53:36 +0000335def _tryfinally(a, b):
336 try:
337 return a
338 finally:
339 b()
340
341def _tryfinallyconst(b):
342 try:
343 return 1
344 finally:
345 b()
346
347dis_tryfinally = """\
348%3d 0 SETUP_FINALLY 12 (to 14)
349
350%3d 2 LOAD_FAST 0 (a)
351 4 POP_BLOCK
352
353%3d 6 LOAD_FAST 1 (b)
354 8 CALL_FUNCTION 0
355 10 POP_TOP
356
357%3d 12 RETURN_VALUE
358
359%3d >> 14 LOAD_FAST 1 (b)
360 16 CALL_FUNCTION 0
361 18 POP_TOP
362 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000363""" % (_tryfinally.__code__.co_firstlineno + 1,
364 _tryfinally.__code__.co_firstlineno + 2,
365 _tryfinally.__code__.co_firstlineno + 4,
366 _tryfinally.__code__.co_firstlineno + 2,
367 _tryfinally.__code__.co_firstlineno + 4,
368 )
369
370dis_tryfinallyconst = """\
371%3d 0 SETUP_FINALLY 12 (to 14)
372
373%3d 2 POP_BLOCK
374
375%3d 4 LOAD_FAST 0 (b)
376 6 CALL_FUNCTION 0
377 8 POP_TOP
378
379%3d 10 LOAD_CONST 1 (1)
380 12 RETURN_VALUE
381
382%3d >> 14 LOAD_FAST 0 (b)
383 16 CALL_FUNCTION 0
384 18 POP_TOP
385 20 RERAISE
Mark Shannon88dce262019-12-30 09:53:36 +0000386""" % (_tryfinallyconst.__code__.co_firstlineno + 1,
387 _tryfinallyconst.__code__.co_firstlineno + 2,
388 _tryfinallyconst.__code__.co_firstlineno + 4,
389 _tryfinallyconst.__code__.co_firstlineno + 2,
390 _tryfinallyconst.__code__.co_firstlineno + 4,
391 )
392
Nick Coghlanefd5df92014-07-25 23:02:56 +1000393def _g(x):
394 yield x
395
syncosmicfe2b56a2017-08-17 19:29:21 -0700396async def _ag(x):
397 yield x
398
399async def _co(x):
400 async for item in _ag(x):
401 pass
402
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300403def _h(y):
404 def foo(x):
405 '''funcdoc'''
406 return [x + z for z in y]
407 return foo
408
409dis_nested_0 = """\
410%3d 0 LOAD_CLOSURE 0 (y)
411 2 BUILD_TUPLE 1
412 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
413 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200414 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300415 10 STORE_FAST 1 (foo)
416
417%3d 12 LOAD_FAST 1 (foo)
418 14 RETURN_VALUE
419""" % (_h.__code__.co_firstlineno + 1,
420 __file__,
421 _h.__code__.co_firstlineno + 1,
422 _h.__code__.co_firstlineno + 4,
423)
424
425dis_nested_1 = """%s
426Disassembly of <code object foo at 0x..., file "%s", line %d>:
427%3d 0 LOAD_CLOSURE 0 (x)
428 2 BUILD_TUPLE 1
429 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
430 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200431 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300432 10 LOAD_DEREF 1 (y)
433 12 GET_ITER
434 14 CALL_FUNCTION 1
435 16 RETURN_VALUE
436""" % (dis_nested_0,
437 __file__,
438 _h.__code__.co_firstlineno + 1,
439 _h.__code__.co_firstlineno + 3,
440 __file__,
441 _h.__code__.co_firstlineno + 3,
442)
443
444dis_nested_2 = """%s
445Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
446%3d 0 BUILD_LIST 0
447 2 LOAD_FAST 0 (.0)
448 >> 4 FOR_ITER 12 (to 18)
449 6 STORE_FAST 1 (z)
450 8 LOAD_DEREF 0 (x)
451 10 LOAD_FAST 1 (z)
452 12 BINARY_ADD
453 14 LIST_APPEND 2
454 16 JUMP_ABSOLUTE 4
455 >> 18 RETURN_VALUE
456""" % (dis_nested_1,
457 __file__,
458 _h.__code__.co_firstlineno + 3,
459 _h.__code__.co_firstlineno + 3,
460)
461
syncosmicfe2b56a2017-08-17 19:29:21 -0700462
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000463class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500464
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300465 maxDiff = None
466
467 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000468 # We want to test the default printing behaviour, not the file arg
469 output = io.StringIO()
470 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500471 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300472 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500473 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300474 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000475 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500476
477 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000478 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500479
Zachary Warebb4b7c12013-12-26 09:55:24 -0600480 def strip_addresses(self, text):
481 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500482
483 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300484 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600485 if got != expected:
486 got = self.strip_addresses(got)
487 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000488
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000489 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500490 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000491 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
492 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000493
494 def test_opname(self):
495 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
496
497 def test_boundaries(self):
498 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
499 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
500
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300501 def test_widths(self):
502 for opcode, opname in enumerate(dis.opname):
503 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
Mark Shannon9af0e472020-01-14 10:12:45 +0000504 'BUILD_TUPLE_UNPACK_WITH_CALL',
505 'JUMP_IF_NOT_EXC_MATCH'):
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300506 continue
507 with self.subTest(opname=opname):
508 width = dis._OPNAME_WIDTH
509 if opcode < dis.HAVE_ARGUMENT:
510 width += 1 + dis._OPARG_WIDTH
511 self.assertLessEqual(len(opname), width)
512
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000513 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000514 self.do_disassembly_test(_f, dis_f)
515
516 def test_bug_708901(self):
517 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000518
Neal Norwitz51abbc72005-12-18 07:06:23 +0000519 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000520 # This one is checking bytecodes generated for an `assert` statement,
521 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600522 if not __debug__:
523 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600524
Zachary Waree80e8062013-12-26 09:53:49 -0600525 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000526
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000527 def test_big_linenos(self):
528 def func(count):
529 namespace = {}
530 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000531 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000532 return namespace['foo']
533
534 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000535 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536 expected = _BIG_LINENO_FORMAT % (i + 2)
537 self.do_disassembly_test(func(i), expected)
538
539 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300540 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541 expected = _BIG_LINENO_FORMAT % (i + 2)
542 self.do_disassembly_test(func(i), expected)
543
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300544 for i in range(1000, 5000, 10):
545 expected = _BIG_LINENO_FORMAT2 % (i + 2)
546 self.do_disassembly_test(func(i), expected)
547
Guido van Rossume7ba4952007-06-06 23:52:48 +0000548 from test import dis_module
549 self.do_disassembly_test(dis_module, dis_module_expected_results)
550
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300551 def test_big_offsets(self):
552 def func(count):
553 namespace = {}
554 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
555 exec(func, namespace)
556 return namespace['foo']
557
558 def expected(count, w):
559 s = ['''\
560 %*d LOAD_FAST 0 (x)
561 %*d LOAD_CONST 1 (1)
562 %*d BINARY_ADD
563 %*d STORE_FAST 0 (x)
564''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
565 for i in range(count)]
566 s += ['''\
567
568 3 %*d LOAD_FAST 0 (x)
569 %*d RETURN_VALUE
570''' % (w, 8*count, w, 8*count + 2)]
571 s[0] = ' 2' + s[0][3:]
572 return ''.join(s)
573
574 for i in range(1, 5):
575 self.do_disassembly_test(func(i), expected(i, 4))
576 self.do_disassembly_test(func(1249), expected(1249, 4))
577 self.do_disassembly_test(func(1250), expected(1250, 5))
578
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000579 def test_disassemble_str(self):
580 self.do_disassembly_test(expr_str, dis_expr_str)
581 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700582 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000583 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
584
Benjamin Petersond6afe722011-03-15 14:44:52 -0500585 def test_disassemble_bytes(self):
586 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
587
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300588 def test_disassemble_class(self):
589 self.do_disassembly_test(_C, dis_c)
590
591 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500592 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
593
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300594 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500595 method_bytecode = _C(1).__init__.__code__.co_code
596 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
597
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300598 def test_disassemble_static_method(self):
599 self.do_disassembly_test(_C.sm, dis_c_static_method)
600
601 def test_disassemble_class_method(self):
602 self.do_disassembly_test(_C.cm, dis_c_class_method)
603
Nick Coghlanefd5df92014-07-25 23:02:56 +1000604 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700605 gen_func_disas = self.get_disassembly(_g) # Generator function
606 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000607 self.assertEqual(gen_disas, gen_func_disas)
608
syncosmicfe2b56a2017-08-17 19:29:21 -0700609 def test_disassemble_async_generator(self):
610 agen_func_disas = self.get_disassembly(_ag) # Async generator function
611 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
612 self.assertEqual(agen_disas, agen_func_disas)
613
614 def test_disassemble_coroutine(self):
615 coro_func_disas = self.get_disassembly(_co) # Coroutine function
616 coro = _co(1) # Coroutine object
617 coro.close() # Avoid a RuntimeWarning (never awaited)
618 coro_disas = self.get_disassembly(coro)
619 self.assertEqual(coro_disas, coro_func_disas)
620
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300621 def test_disassemble_fstring(self):
622 self.do_disassembly_test(_fstring, dis_fstring)
623
Mark Shannon88dce262019-12-30 09:53:36 +0000624 def test_disassemble_try_finally(self):
625 self.do_disassembly_test(_tryfinally, dis_tryfinally)
626 self.do_disassembly_test(_tryfinallyconst, dis_tryfinallyconst)
627
Benjamin Petersond6afe722011-03-15 14:44:52 -0500628 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500629 try:
630 del sys.last_traceback
631 except AttributeError:
632 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500633 self.assertRaises(RuntimeError, dis.dis, None)
634
Benjamin Petersond6afe722011-03-15 14:44:52 -0500635 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500636 try:
637 del sys.last_traceback
638 except AttributeError:
639 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500640
641 try:
642 1/0
643 except Exception as e:
644 tb = e.__traceback__
645 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500646
647 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
648 self.do_disassembly_test(None, tb_dis)
649
650 def test_dis_object(self):
651 self.assertRaises(TypeError, dis.dis, object())
652
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300653 def test_disassemble_recursive(self):
654 def check(expected, **kwargs):
655 dis = self.get_disassembly(_h, **kwargs)
656 dis = self.strip_addresses(dis)
657 self.assertEqual(dis, expected)
658
659 check(dis_nested_0, depth=0)
660 check(dis_nested_1, depth=1)
661 check(dis_nested_2, depth=2)
662 check(dis_nested_2, depth=3)
663 check(dis_nested_2, depth=None)
664 check(dis_nested_2)
665
666
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000667class DisWithFileTests(DisTests):
668
669 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300670 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000671 output = io.StringIO()
672 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300673 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000674 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300675 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000676 return output.getvalue()
677
678
679
Nick Coghlaneae2da12010-08-17 08:03:36 +0000680code_info_code_info = """\
681Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000682Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000683Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100684Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000685Kw-only arguments: 0
686Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000687Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000688Flags: OPTIMIZED, NEWLOCALS, NOFREE
689Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000690 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000691Names:
692 0: _format_code_info
693 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000694Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000695 0: x""" % (('Formatted details of methods, functions, or code.',)
696 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000697
698@staticmethod
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100699def tricky(a, b, /, x, y, z=True, *args, c, d, e=[], **kwds):
Nick Coghlaneae2da12010-08-17 08:03:36 +0000700 def f(c=c):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100701 print(a, b, x, y, z, c, d, e, f)
702 yield a, b, x, y, z, c, d, e, f
Nick Coghlaneae2da12010-08-17 08:03:36 +0000703
Nick Coghlaneae2da12010-08-17 08:03:36 +0000704code_info_tricky = """\
705Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000706Filename: (.*)
Pablo Galindocd74e662019-06-01 18:08:04 +0100707Argument count: 5
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100708Positional-only arguments: 2
Nick Coghlaneae2da12010-08-17 08:03:36 +0000709Kw-only arguments: 3
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100710Number of locals: 10
711Stack size: 9
Nick Coghlaneae2da12010-08-17 08:03:36 +0000712Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
713Constants:
714 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000715 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100716 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000717Variable names:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100718 0: a
719 1: b
720 2: x
721 3: y
722 4: z
723 5: c
724 6: d
725 7: e
726 8: args
727 9: kwds
Nick Coghlaneae2da12010-08-17 08:03:36 +0000728Cell variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100729 0: [abedfxyz]
730 1: [abedfxyz]
731 2: [abedfxyz]
732 3: [abedfxyz]
733 4: [abedfxyz]
734 5: [abedfxyz]"""
Georg Brandla1082272012-02-20 21:41:03 +0100735# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000736
737co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000738
739code_info_tricky_nested_f = """\
Nick Coghlan46e63802010-08-17 11:28:07 +0000740Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000741Argument count: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100742Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000743Kw-only arguments: 0
744Number of locals: 1
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100745Stack size: 10
Nick Coghlaneae2da12010-08-17 08:03:36 +0000746Flags: OPTIMIZED, NEWLOCALS, NESTED
747Constants:
748 0: None
749Names:
750 0: print
751Variable names:
752 0: c
753Free variables:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100754 0: [abedfxyz]
755 1: [abedfxyz]
756 2: [abedfxyz]
757 3: [abedfxyz]
758 4: [abedfxyz]
759 5: [abedfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000760
761code_info_expr_str = """\
762Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000763Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000764Argument count: 0
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: 0
768Stack size: 2
769Flags: NOFREE
770Constants:
771 0: 1
772Names:
773 0: x"""
774
775code_info_simple_stmt_str = """\
776Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000777Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000778Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100779Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000780Kw-only arguments: 0
781Number of locals: 0
782Stack size: 2
783Flags: NOFREE
784Constants:
785 0: 1
786 1: None
787Names:
788 0: x"""
789
790code_info_compound_stmt_str = """\
791Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000792Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000793Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100794Positional-only arguments: 0
Nick Coghlaneae2da12010-08-17 08:03:36 +0000795Kw-only arguments: 0
796Number of locals: 0
797Stack size: 2
798Flags: NOFREE
799Constants:
800 0: 0
801 1: 1
802 2: None
803Names:
804 0: x"""
805
Yury Selivanov75445082015-05-11 22:57:16 -0400806
807async def async_def():
808 await 1
809 async for a in b: pass
810 async with c as d: pass
811
812code_info_async_def = """\
813Name: async_def
814Filename: (.*)
815Argument count: 0
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100816Positional-only arguments: 0
Yury Selivanov75445082015-05-11 22:57:16 -0400817Kw-only arguments: 0
818Number of locals: 2
Mark Shannonfee55262019-11-21 09:11:43 +0000819Stack size: 9
Yury Selivanoveb636452016-09-08 22:01:51 -0700820Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400821Constants:
822 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200823 1: 1
824Names:
825 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200826 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200827Variable names:
828 0: a
829 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400830
Nick Coghlaneae2da12010-08-17 08:03:36 +0000831class CodeInfoTests(unittest.TestCase):
832 test_pairs = [
833 (dis.code_info, code_info_code_info),
834 (tricky, code_info_tricky),
835 (co_tricky_nested_f, code_info_tricky_nested_f),
836 (expr_str, code_info_expr_str),
837 (simple_stmt_str, code_info_simple_stmt_str),
838 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400839 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000840 ]
841
842 def test_code_info(self):
843 self.maxDiff = 1000
844 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000845 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000846
847 def test_show_code(self):
848 self.maxDiff = 1000
849 for x, expected in self.test_pairs:
850 with captured_stdout() as output:
851 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000852 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000853 output = io.StringIO()
854 dis.show_code(x, file=output)
855 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000856
Benjamin Petersond6afe722011-03-15 14:44:52 -0500857 def test_code_info_object(self):
858 self.assertRaises(TypeError, dis.code_info, object())
859
860 def test_pretty_flags_no_flags(self):
861 self.assertEqual(dis.pretty_flags(0), '0x0')
862
863
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000864# Fodder for instruction introspection tests
865# Editing any of these may require recalculating the expected output
866def outer(a=1, b=2):
867 def f(c=3, d=4):
868 def inner(e=5, f=6):
869 print(a, b, c, d, e, f)
870 print(a, b, c, d)
871 return inner
872 print(a, b, '', 1, [], {}, "Hello world!")
873 return f
874
875def jumpy():
876 # This won't actually run (but that's OK, we only disassemble it)
877 for i in range(10):
878 print(i)
879 if i < 4:
880 continue
881 if i > 6:
882 break
883 else:
884 print("I can haz else clause?")
885 while i:
886 print(i)
887 i -= 1
888 if i > 6:
889 continue
890 if i < 4:
891 break
892 else:
893 print("Who let lolcatz into this test suite?")
894 try:
895 1 / 0
896 except ZeroDivisionError:
897 print("Here we go, here we go, here we go...")
898 else:
899 with i as dodgy:
900 print("Never reach this")
901 finally:
902 print("OK, now we're done")
903
904# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000905expected_outer_line = 1
906_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000907code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000908expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000909code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000910expected_inner_line = code_object_inner.co_firstlineno - _line_offset
911expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000912
913# The following lines are useful to regenerate the expected results after
914# either the fodder is modified or the bytecode generation changes
915# After regeneration, update the references to code_object_f and
916# code_object_inner before rerunning the tests
917
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000918#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000919#print('expected_opinfo_outer = [\n ',
920 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200921#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000922#print('expected_opinfo_f = [\n ',
923 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200924#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000925#print('expected_opinfo_inner = [\n ',
926 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000927#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000928#print('expected_opinfo_jumpy = [\n ',
929 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
930
931
932Instruction = dis.Instruction
933expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300934 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
935 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
936 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
937 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
938 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),
939 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 +0200940 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 +0300941 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
942 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
943 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
944 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
945 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
946 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
947 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
948 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
949 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 -0700950 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 +0300951 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
952 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
953 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 +1000954]
955
956expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300957 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
958 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
959 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
960 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
961 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
962 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
963 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),
964 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 +0200965 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 +0300966 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
967 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
968 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
969 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
970 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
971 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 -0700972 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 +0300973 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
974 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
975 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 +1000976]
977
978expected_opinfo_inner = [
979 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 +0300980 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
981 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
982 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
983 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
984 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
985 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 -0700986 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 +0300987 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
988 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
989 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 +1000990]
991
992expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200993 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
994 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
995 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
996 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
997 Instruction(opname='FOR_ITER', opcode=93, arg=34, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True),
998 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
999 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
1000 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
1001 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
1002 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
1003 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
1004 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
1005 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
1006 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
1007 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
1008 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
1009 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
1010 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
1011 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
1012 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
1013 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=40, starts_line=None, is_jump_target=False),
1014 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=42, starts_line=None, is_jump_target=False),
1015 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
1016 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),
1017 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False),
1018 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False),
1019 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True),
1020 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=94, argval=94, argrepr='', offset=54, starts_line=None, is_jump_target=False),
1021 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=False),
1022 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False),
1023 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False),
1024 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False),
1025 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False),
1026 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False),
1027 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False),
1028 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False),
1029 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False),
1030 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False),
1031 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False),
1032 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=82, argval=82, argrepr='', offset=78, starts_line=None, is_jump_target=False),
1033 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=80, starts_line=15, is_jump_target=False),
1034 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True),
1035 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
1036 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
1037 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=52, argval=52, argrepr='', offset=88, starts_line=None, is_jump_target=False),
1038 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=102, argval=102, argrepr='', offset=90, starts_line=17, is_jump_target=False),
1039 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=92, starts_line=None, is_jump_target=False),
1040 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=94, starts_line=19, is_jump_target=True),
1041 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),
1042 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=98, starts_line=None, is_jump_target=False),
1043 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 +00001044 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 +02001045 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=118, argrepr='to 118', offset=104, starts_line=None, is_jump_target=False),
1046 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 +00001047 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 +02001048 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
1049 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
1050 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 +00001051 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 +02001052 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=True),
1053 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 +00001054 Instruction(opname='JUMP_IF_NOT_EXC_MATCH', opcode=121, arg=142, argval=142, argrepr='', offset=122, starts_line=None, is_jump_target=False),
1055 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 +02001056 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
1057 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 +00001058 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=130, starts_line=23, is_jump_target=False),
1059 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),
1060 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=134, starts_line=None, is_jump_target=False),
1061 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=136, starts_line=None, is_jump_target=False),
1062 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
1063 Instruction(opname='JUMP_FORWARD', opcode=110, arg=46, argval=188, argrepr='to 188', offset=140, starts_line=None, is_jump_target=False),
1064 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=142, starts_line=None, is_jump_target=True),
1065 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=144, starts_line=25, is_jump_target=True),
1066 Instruction(opname='SETUP_WITH', opcode=143, arg=24, argval=172, argrepr='to 172', offset=146, starts_line=None, is_jump_target=False),
1067 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=148, starts_line=None, is_jump_target=False),
1068 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=150, starts_line=26, is_jump_target=False),
1069 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),
1070 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=154, starts_line=None, is_jump_target=False),
1071 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=156, starts_line=None, is_jump_target=False),
1072 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
1073 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=160, starts_line=None, is_jump_target=False),
1074 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 +00001075 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 +00001076 Instruction(opname='CALL_FUNCTION', opcode=131, arg=3, argval=3, argrepr='', offset=166, starts_line=None, is_jump_target=False),
1077 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
1078 Instruction(opname='JUMP_FORWARD', opcode=110, arg=16, argval=188, argrepr='to 188', offset=170, starts_line=None, is_jump_target=False),
1079 Instruction(opname='WITH_EXCEPT_START', opcode=49, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=True),
1080 Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=178, argval=178, argrepr='', offset=174, starts_line=None, is_jump_target=False),
1081 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=176, starts_line=None, is_jump_target=False),
1082 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=178, starts_line=None, is_jump_target=True),
1083 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 +00001084 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 +00001085 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=184, starts_line=None, is_jump_target=False),
1086 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
1087 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=188, starts_line=None, is_jump_target=True),
1088 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=190, starts_line=28, is_jump_target=False),
1089 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),
1090 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=194, starts_line=None, is_jump_target=False),
1091 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=196, starts_line=None, is_jump_target=False),
1092 Instruction(opname='JUMP_FORWARD', opcode=110, arg=10, argval=210, argrepr='to 210', offset=198, starts_line=None, is_jump_target=False),
1093 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=200, starts_line=None, is_jump_target=True),
1094 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),
1095 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=204, starts_line=None, is_jump_target=False),
1096 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=206, starts_line=None, is_jump_target=False),
1097 Instruction(opname='RERAISE', opcode=48, arg=None, argval=None, argrepr='', offset=208, starts_line=None, is_jump_target=False),
1098 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=210, starts_line=None, is_jump_target=True),
1099 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 +10001100]
1101
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001102# One last piece of inspect fodder to check the default line number handling
1103def simple(): pass
1104expected_opinfo_simple = [
1105 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 +03001106 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 +10001107]
1108
1109
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001110class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001111
Mark Shannonfee55262019-11-21 09:11:43 +00001112 def __init__(self, *args):
1113 super().__init__(*args)
1114 self.maxDiff = None
1115
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001116 def test_default_first_line(self):
1117 actual = dis.get_instructions(simple)
1118 self.assertEqual(list(actual), expected_opinfo_simple)
1119
1120 def test_first_line_set_to_None(self):
1121 actual = dis.get_instructions(simple, first_line=None)
1122 self.assertEqual(list(actual), expected_opinfo_simple)
1123
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001124 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001125 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1126 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001127
1128 def test_nested(self):
1129 with captured_stdout():
1130 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001131 actual = dis.get_instructions(f, first_line=expected_f_line)
1132 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001133
1134 def test_doubly_nested(self):
1135 with captured_stdout():
1136 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001137 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1138 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001139
1140 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001141 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1142 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001143
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001144# get_instructions has its own tests above, so can rely on it to validate
1145# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001146class BytecodeTests(unittest.TestCase):
Mark Shannonfee55262019-11-21 09:11:43 +00001147
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001148 def test_instantiation(self):
1149 # Test with function, method, code string and code object
1150 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001151 with self.subTest(obj=obj):
1152 b = dis.Bytecode(obj)
1153 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001154
1155 self.assertRaises(TypeError, dis.Bytecode, object())
1156
1157 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001158 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1159 with self.subTest(obj=obj):
1160 via_object = list(dis.Bytecode(obj))
1161 via_generator = list(dis.get_instructions(obj))
1162 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001163
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001164 def test_explicit_first_line(self):
1165 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1166 self.assertEqual(list(actual), expected_opinfo_outer)
1167
1168 def test_source_line_in_disassembly(self):
1169 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001170 actual = dis.Bytecode(simple).dis()
1171 actual = actual.strip().partition(" ")[0] # extract the line no
1172 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001173 self.assertEqual(actual, expected)
1174 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001175 actual = dis.Bytecode(simple, first_line=350).dis()
1176 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001177 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001178
1179 def test_info(self):
1180 self.maxDiff = 1000
1181 for x, expected in CodeInfoTests.test_pairs:
1182 b = dis.Bytecode(x)
1183 self.assertRegex(b.info(), expected)
1184
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001185 def test_disassembled(self):
1186 actual = dis.Bytecode(_f).dis()
1187 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001188
Nick Coghlan50c48b82013-11-23 00:57:00 +10001189 def test_from_traceback(self):
1190 tb = get_tb()
1191 b = dis.Bytecode.from_traceback(tb)
1192 while tb.tb_next: tb = tb.tb_next
1193
1194 self.assertEqual(b.current_offset, tb.tb_lasti)
1195
1196 def test_from_traceback_dis(self):
1197 tb = get_tb()
1198 b = dis.Bytecode.from_traceback(tb)
1199 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001200
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001201if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001202 unittest.main()