blob: c86f61f236bd0e6c65bcc495d1e4b04d903eb553 [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
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10004from test.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)
127 6 CALL_FUNCTION 2
128 8 GET_ITER
129 >> 10 FOR_ITER 4 (to 16)
130 12 STORE_FAST 0 (res)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000131
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200132%3d 14 JUMP_ABSOLUTE 10
133 >> 16 LOAD_CONST 0 (None)
134 18 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000135""" % (bug708901.__code__.co_firstlineno + 1,
136 bug708901.__code__.co_firstlineno + 2,
137 bug708901.__code__.co_firstlineno + 3)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000138
Neal Norwitz51abbc72005-12-18 07:06:23 +0000139
140def bug1333982(x=[]):
141 assert 0, ([s for s in x] +
142 1)
143 pass
144
145dis_bug1333982 = """\
Zachary Warebb4b7c12013-12-26 09:55:24 -0600146%3d 0 LOAD_CONST 1 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300147 2 POP_JUMP_IF_TRUE 26
148 4 LOAD_GLOBAL 0 (AssertionError)
149 6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
150 8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
151 10 MAKE_FUNCTION 0
152 12 LOAD_FAST 0 (x)
153 14 GET_ITER
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700154 16 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300155
156%3d 18 LOAD_CONST 4 (1)
157 20 BINARY_ADD
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700158 22 CALL_FUNCTION 1
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300159 24 RAISE_VARARGS 1
Neal Norwitz51abbc72005-12-18 07:06:23 +0000160
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300161%3d >> 26 LOAD_CONST 0 (None)
162 28 RETURN_VALUE
Georg Brandlebbf63b2010-10-14 07:23:01 +0000163""" % (bug1333982.__code__.co_firstlineno + 1,
Zachary Waree80e8062013-12-26 09:53:49 -0600164 __file__,
165 bug1333982.__code__.co_firstlineno + 1,
Georg Brandlebbf63b2010-10-14 07:23:01 +0000166 bug1333982.__code__.co_firstlineno + 2,
167 bug1333982.__code__.co_firstlineno + 3)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000168
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000169_BIG_LINENO_FORMAT = """\
170%3d 0 LOAD_GLOBAL 0 (spam)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300171 2 POP_TOP
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000172 4 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300173 6 RETURN_VALUE
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000174"""
175
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300176_BIG_LINENO_FORMAT2 = """\
177%4d 0 LOAD_GLOBAL 0 (spam)
178 2 POP_TOP
179 4 LOAD_CONST 0 (None)
180 6 RETURN_VALUE
181"""
182
Guido van Rossume7ba4952007-06-06 23:52:48 +0000183dis_module_expected_results = """\
184Disassembly of f:
185 4 0 LOAD_CONST 0 (None)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300186 2 RETURN_VALUE
Guido van Rossume7ba4952007-06-06 23:52:48 +0000187
188Disassembly of g:
189 5 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
192"""
193
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000194expr_str = "x + 1"
195
196dis_expr_str = """\
197 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300198 2 LOAD_CONST 0 (1)
199 4 BINARY_ADD
200 6 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000201"""
202
203simple_stmt_str = "x = x + 1"
204
205dis_simple_stmt_str = """\
206 1 0 LOAD_NAME 0 (x)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300207 2 LOAD_CONST 0 (1)
208 4 BINARY_ADD
209 6 STORE_NAME 0 (x)
210 8 LOAD_CONST 1 (None)
211 10 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000212"""
213
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700214annot_stmt_str = """\
215
216x: int = 1
217y: fun(1)
218lst[fun(0)]: int = 1
219"""
220# leading newline is for a reason (tests lineno)
221
222dis_annot_stmt_str = """\
223 2 0 SETUP_ANNOTATIONS
224 2 LOAD_CONST 0 (1)
225 4 STORE_NAME 0 (x)
226 6 LOAD_NAME 1 (int)
Mark Shannon332cd5e2018-01-30 00:41:04 +0000227 8 LOAD_NAME 2 (__annotations__)
228 10 LOAD_CONST 1 ('x')
229 12 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700230
Mark Shannon332cd5e2018-01-30 00:41:04 +0000231 3 14 LOAD_NAME 3 (fun)
232 16 LOAD_CONST 0 (1)
233 18 CALL_FUNCTION 1
234 20 LOAD_NAME 2 (__annotations__)
235 22 LOAD_CONST 2 ('y')
236 24 STORE_SUBSCR
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700237
Mark Shannon332cd5e2018-01-30 00:41:04 +0000238 4 26 LOAD_CONST 0 (1)
239 28 LOAD_NAME 4 (lst)
240 30 LOAD_NAME 3 (fun)
241 32 LOAD_CONST 3 (0)
242 34 CALL_FUNCTION 1
243 36 STORE_SUBSCR
244 38 LOAD_NAME 1 (int)
245 40 POP_TOP
246 42 LOAD_CONST 4 (None)
247 44 RETURN_VALUE
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700248"""
249
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000250compound_stmt_str = """\
251x = 0
252while 1:
253 x += 1"""
254# Trailing newline has been deliberately omitted
255
256dis_compound_stmt_str = """\
257 1 0 LOAD_CONST 0 (0)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300258 2 STORE_NAME 0 (x)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000259
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200260 3 >> 4 LOAD_NAME 0 (x)
261 6 LOAD_CONST 1 (1)
262 8 INPLACE_ADD
263 10 STORE_NAME 0 (x)
264 12 JUMP_ABSOLUTE 4
265 14 LOAD_CONST 2 (None)
266 16 RETURN_VALUE
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000267"""
Guido van Rossume7ba4952007-06-06 23:52:48 +0000268
Nick Coghlan50c48b82013-11-23 00:57:00 +1000269dis_traceback = """\
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200270%3d 0 SETUP_FINALLY 12 (to 14)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000271
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300272%3d 2 LOAD_CONST 1 (1)
273 4 LOAD_CONST 2 (0)
274 --> 6 BINARY_TRUE_DIVIDE
275 8 POP_TOP
276 10 POP_BLOCK
277 12 JUMP_FORWARD 40 (to 54)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000278
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300279%3d >> 14 DUP_TOP
Nick Coghlan50c48b82013-11-23 00:57:00 +1000280 16 LOAD_GLOBAL 0 (Exception)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300281 18 COMPARE_OP 10 (exception match)
282 20 POP_JUMP_IF_FALSE 52
283 22 POP_TOP
284 24 STORE_FAST 0 (e)
285 26 POP_TOP
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200286 28 SETUP_FINALLY 10 (to 40)
Nick Coghlan50c48b82013-11-23 00:57:00 +1000287
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300288%3d 30 LOAD_FAST 0 (e)
289 32 LOAD_ATTR 1 (__traceback__)
290 34 STORE_FAST 1 (tb)
291 36 POP_BLOCK
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200292 38 BEGIN_FINALLY
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200293 >> 40 LOAD_CONST 0 (None)
294 42 STORE_FAST 0 (e)
295 44 DELETE_FAST 0 (e)
296 46 END_FINALLY
297 48 POP_EXCEPT
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300298 50 JUMP_FORWARD 2 (to 54)
299 >> 52 END_FINALLY
Nick Coghlan50c48b82013-11-23 00:57:00 +1000300
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300301%3d >> 54 LOAD_FAST 1 (tb)
302 56 RETURN_VALUE
Nick Coghlan50c48b82013-11-23 00:57:00 +1000303""" % (TRACEBACK_CODE.co_firstlineno + 1,
304 TRACEBACK_CODE.co_firstlineno + 2,
305 TRACEBACK_CODE.co_firstlineno + 3,
306 TRACEBACK_CODE.co_firstlineno + 4,
307 TRACEBACK_CODE.co_firstlineno + 5)
308
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300309def _fstring(a, b, c, d):
310 return f'{a} {b:4} {c!r} {d!r:4}'
311
312dis_fstring = """\
313%3d 0 LOAD_FAST 0 (a)
314 2 FORMAT_VALUE 0
315 4 LOAD_CONST 1 (' ')
316 6 LOAD_FAST 1 (b)
317 8 LOAD_CONST 2 ('4')
318 10 FORMAT_VALUE 4 (with format)
319 12 LOAD_CONST 1 (' ')
320 14 LOAD_FAST 2 (c)
321 16 FORMAT_VALUE 2 (repr)
322 18 LOAD_CONST 1 (' ')
323 20 LOAD_FAST 3 (d)
324 22 LOAD_CONST 2 ('4')
325 24 FORMAT_VALUE 6 (repr, with format)
326 26 BUILD_STRING 7
327 28 RETURN_VALUE
328""" % (_fstring.__code__.co_firstlineno + 1,)
329
Nick Coghlanefd5df92014-07-25 23:02:56 +1000330def _g(x):
331 yield x
332
syncosmicfe2b56a2017-08-17 19:29:21 -0700333async def _ag(x):
334 yield x
335
336async def _co(x):
337 async for item in _ag(x):
338 pass
339
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300340def _h(y):
341 def foo(x):
342 '''funcdoc'''
343 return [x + z for z in y]
344 return foo
345
346dis_nested_0 = """\
347%3d 0 LOAD_CLOSURE 0 (y)
348 2 BUILD_TUPLE 1
349 4 LOAD_CONST 1 (<code object foo at 0x..., file "%s", line %d>)
350 6 LOAD_CONST 2 ('_h.<locals>.foo')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200351 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300352 10 STORE_FAST 1 (foo)
353
354%3d 12 LOAD_FAST 1 (foo)
355 14 RETURN_VALUE
356""" % (_h.__code__.co_firstlineno + 1,
357 __file__,
358 _h.__code__.co_firstlineno + 1,
359 _h.__code__.co_firstlineno + 4,
360)
361
362dis_nested_1 = """%s
363Disassembly of <code object foo at 0x..., file "%s", line %d>:
364%3d 0 LOAD_CLOSURE 0 (x)
365 2 BUILD_TUPLE 1
366 4 LOAD_CONST 1 (<code object <listcomp> at 0x..., file "%s", line %d>)
367 6 LOAD_CONST 2 ('_h.<locals>.foo.<locals>.<listcomp>')
Serhiy Storchakae2732d32018-03-11 11:07:06 +0200368 8 MAKE_FUNCTION 8 (closure)
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300369 10 LOAD_DEREF 1 (y)
370 12 GET_ITER
371 14 CALL_FUNCTION 1
372 16 RETURN_VALUE
373""" % (dis_nested_0,
374 __file__,
375 _h.__code__.co_firstlineno + 1,
376 _h.__code__.co_firstlineno + 3,
377 __file__,
378 _h.__code__.co_firstlineno + 3,
379)
380
381dis_nested_2 = """%s
382Disassembly of <code object <listcomp> at 0x..., file "%s", line %d>:
383%3d 0 BUILD_LIST 0
384 2 LOAD_FAST 0 (.0)
385 >> 4 FOR_ITER 12 (to 18)
386 6 STORE_FAST 1 (z)
387 8 LOAD_DEREF 0 (x)
388 10 LOAD_FAST 1 (z)
389 12 BINARY_ADD
390 14 LIST_APPEND 2
391 16 JUMP_ABSOLUTE 4
392 >> 18 RETURN_VALUE
393""" % (dis_nested_1,
394 __file__,
395 _h.__code__.co_firstlineno + 3,
396 _h.__code__.co_firstlineno + 3,
397)
398
syncosmicfe2b56a2017-08-17 19:29:21 -0700399
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000400class DisTests(unittest.TestCase):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500401
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300402 maxDiff = None
403
404 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000405 # We want to test the default printing behaviour, not the file arg
406 output = io.StringIO()
407 with contextlib.redirect_stdout(output):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500408 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300409 dis.dis(func, **kwargs)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500410 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300411 dis.disassemble(func, lasti, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000412 return output.getvalue()
Benjamin Petersond6afe722011-03-15 14:44:52 -0500413
414 def get_disassemble_as_string(self, func, lasti=-1):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000415 return self.get_disassembly(func, lasti, False)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500416
Zachary Warebb4b7c12013-12-26 09:55:24 -0600417 def strip_addresses(self, text):
418 return re.sub(r'\b0x[0-9A-Fa-f]+\b', '0x...', text)
Benjamin Petersond6afe722011-03-15 14:44:52 -0500419
420 def do_disassembly_test(self, func, expected):
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300421 got = self.get_disassembly(func, depth=0)
Zachary Warebb4b7c12013-12-26 09:55:24 -0600422 if got != expected:
423 got = self.strip_addresses(got)
424 self.assertEqual(got, expected)
Michael W. Hudson26848a32003-04-29 17:07:36 +0000425
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000426 def test_opmap(self):
Benjamin Peterson76f7f4d2011-07-17 22:49:50 -0500427 self.assertEqual(dis.opmap["NOP"], 9)
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000428 self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
429 self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000430
431 def test_opname(self):
432 self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
433
434 def test_boundaries(self):
435 self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
436 self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
437
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300438 def test_widths(self):
439 for opcode, opname in enumerate(dis.opname):
440 if opname in ('BUILD_MAP_UNPACK_WITH_CALL',
441 'BUILD_TUPLE_UNPACK_WITH_CALL'):
442 continue
443 with self.subTest(opname=opname):
444 width = dis._OPNAME_WIDTH
445 if opcode < dis.HAVE_ARGUMENT:
446 width += 1 + dis._OPARG_WIDTH
447 self.assertLessEqual(len(opname), width)
448
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000449 def test_dis(self):
Michael W. Hudson26848a32003-04-29 17:07:36 +0000450 self.do_disassembly_test(_f, dis_f)
451
452 def test_bug_708901(self):
453 self.do_disassembly_test(bug708901, dis_bug708901)
Skip Montanaroadd0ccc2003-02-27 21:27:07 +0000454
Neal Norwitz51abbc72005-12-18 07:06:23 +0000455 def test_bug_1333982(self):
Tim Peters83a8c392005-12-25 22:52:32 +0000456 # This one is checking bytecodes generated for an `assert` statement,
457 # so fails if the tests are run with -O. Skip this test then.
Zachary Waree80e8062013-12-26 09:53:49 -0600458 if not __debug__:
459 self.skipTest('need asserts, run without -O')
Zachary Ware9fe6d862013-12-08 00:20:35 -0600460
Zachary Waree80e8062013-12-26 09:53:49 -0600461 self.do_disassembly_test(bug1333982, dis_bug1333982)
Neal Norwitz51abbc72005-12-18 07:06:23 +0000462
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000463 def test_big_linenos(self):
464 def func(count):
465 namespace = {}
466 func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
Georg Brandl7cae87c2006-09-06 06:51:57 +0000467 exec(func, namespace)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000468 return namespace['foo']
469
470 # Test all small ranges
Guido van Rossum805365e2007-05-07 22:24:25 +0000471 for i in range(1, 300):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000472 expected = _BIG_LINENO_FORMAT % (i + 2)
473 self.do_disassembly_test(func(i), expected)
474
475 # Test some larger ranges too
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300476 for i in range(300, 1000, 10):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000477 expected = _BIG_LINENO_FORMAT % (i + 2)
478 self.do_disassembly_test(func(i), expected)
479
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300480 for i in range(1000, 5000, 10):
481 expected = _BIG_LINENO_FORMAT2 % (i + 2)
482 self.do_disassembly_test(func(i), expected)
483
Guido van Rossume7ba4952007-06-06 23:52:48 +0000484 from test import dis_module
485 self.do_disassembly_test(dis_module, dis_module_expected_results)
486
Serhiy Storchakad90045f2017-04-19 20:36:31 +0300487 def test_big_offsets(self):
488 def func(count):
489 namespace = {}
490 func = "def foo(x):\n " + ";".join(["x = x + 1"] * count) + "\n return x"
491 exec(func, namespace)
492 return namespace['foo']
493
494 def expected(count, w):
495 s = ['''\
496 %*d LOAD_FAST 0 (x)
497 %*d LOAD_CONST 1 (1)
498 %*d BINARY_ADD
499 %*d STORE_FAST 0 (x)
500''' % (w, 8*i, w, 8*i + 2, w, 8*i + 4, w, 8*i + 6)
501 for i in range(count)]
502 s += ['''\
503
504 3 %*d LOAD_FAST 0 (x)
505 %*d RETURN_VALUE
506''' % (w, 8*count, w, 8*count + 2)]
507 s[0] = ' 2' + s[0][3:]
508 return ''.join(s)
509
510 for i in range(1, 5):
511 self.do_disassembly_test(func(i), expected(i, 4))
512 self.do_disassembly_test(func(1249), expected(1249, 4))
513 self.do_disassembly_test(func(1250), expected(1250, 5))
514
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000515 def test_disassemble_str(self):
516 self.do_disassembly_test(expr_str, dis_expr_str)
517 self.do_disassembly_test(simple_stmt_str, dis_simple_stmt_str)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700518 self.do_disassembly_test(annot_stmt_str, dis_annot_stmt_str)
Nick Coghlan5c8b54e2010-07-03 07:36:51 +0000519 self.do_disassembly_test(compound_stmt_str, dis_compound_stmt_str)
520
Benjamin Petersond6afe722011-03-15 14:44:52 -0500521 def test_disassemble_bytes(self):
522 self.do_disassembly_test(_f.__code__.co_code, dis_f_co_code)
523
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300524 def test_disassemble_class(self):
525 self.do_disassembly_test(_C, dis_c)
526
527 def test_disassemble_instance_method(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500528 self.do_disassembly_test(_C(1).__init__, dis_c_instance_method)
529
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300530 def test_disassemble_instance_method_bytes(self):
Benjamin Petersond6afe722011-03-15 14:44:52 -0500531 method_bytecode = _C(1).__init__.__code__.co_code
532 self.do_disassembly_test(method_bytecode, dis_c_instance_method_bytes)
533
Serhiy Storchaka585c93d2016-04-23 09:23:52 +0300534 def test_disassemble_static_method(self):
535 self.do_disassembly_test(_C.sm, dis_c_static_method)
536
537 def test_disassemble_class_method(self):
538 self.do_disassembly_test(_C.cm, dis_c_class_method)
539
Nick Coghlanefd5df92014-07-25 23:02:56 +1000540 def test_disassemble_generator(self):
syncosmicfe2b56a2017-08-17 19:29:21 -0700541 gen_func_disas = self.get_disassembly(_g) # Generator function
542 gen_disas = self.get_disassembly(_g(1)) # Generator iterator
Nick Coghlanefd5df92014-07-25 23:02:56 +1000543 self.assertEqual(gen_disas, gen_func_disas)
544
syncosmicfe2b56a2017-08-17 19:29:21 -0700545 def test_disassemble_async_generator(self):
546 agen_func_disas = self.get_disassembly(_ag) # Async generator function
547 agen_disas = self.get_disassembly(_ag(1)) # Async generator iterator
548 self.assertEqual(agen_disas, agen_func_disas)
549
550 def test_disassemble_coroutine(self):
551 coro_func_disas = self.get_disassembly(_co) # Coroutine function
552 coro = _co(1) # Coroutine object
553 coro.close() # Avoid a RuntimeWarning (never awaited)
554 coro_disas = self.get_disassembly(coro)
555 self.assertEqual(coro_disas, coro_func_disas)
556
Serhiy Storchakadd102f72016-10-08 12:34:25 +0300557 def test_disassemble_fstring(self):
558 self.do_disassembly_test(_fstring, dis_fstring)
559
Benjamin Petersond6afe722011-03-15 14:44:52 -0500560 def test_dis_none(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500561 try:
562 del sys.last_traceback
563 except AttributeError:
564 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500565 self.assertRaises(RuntimeError, dis.dis, None)
566
Benjamin Petersond6afe722011-03-15 14:44:52 -0500567 def test_dis_traceback(self):
Benjamin Peterson47afc2a2011-03-15 15:54:50 -0500568 try:
569 del sys.last_traceback
570 except AttributeError:
571 pass
Benjamin Petersond6afe722011-03-15 14:44:52 -0500572
573 try:
574 1/0
575 except Exception as e:
576 tb = e.__traceback__
577 sys.last_traceback = tb
Benjamin Petersond6afe722011-03-15 14:44:52 -0500578
579 tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
580 self.do_disassembly_test(None, tb_dis)
581
582 def test_dis_object(self):
583 self.assertRaises(TypeError, dis.dis, object())
584
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300585 def test_disassemble_recursive(self):
586 def check(expected, **kwargs):
587 dis = self.get_disassembly(_h, **kwargs)
588 dis = self.strip_addresses(dis)
589 self.assertEqual(dis, expected)
590
591 check(dis_nested_0, depth=0)
592 check(dis_nested_1, depth=1)
593 check(dis_nested_2, depth=2)
594 check(dis_nested_2, depth=3)
595 check(dis_nested_2, depth=None)
596 check(dis_nested_2)
597
598
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000599class DisWithFileTests(DisTests):
600
601 # Run the tests again, using the file arg instead of print
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300602 def get_disassembly(self, func, lasti=-1, wrapper=True, **kwargs):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000603 output = io.StringIO()
604 if wrapper:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300605 dis.dis(func, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000606 else:
Serhiy Storchaka1efbf922017-06-11 14:09:39 +0300607 dis.disassemble(func, lasti, file=output, **kwargs)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000608 return output.getvalue()
609
610
611
Nick Coghlaneae2da12010-08-17 08:03:36 +0000612code_info_code_info = """\
613Name: code_info
Nick Coghlan46e63802010-08-17 11:28:07 +0000614Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000615Argument count: 1
616Kw-only arguments: 0
617Number of locals: 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000618Stack size: 3
Nick Coghlaneae2da12010-08-17 08:03:36 +0000619Flags: OPTIMIZED, NEWLOCALS, NOFREE
620Constants:
Georg Brandlebbf63b2010-10-14 07:23:01 +0000621 0: %r
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000622Names:
623 0: _format_code_info
624 1: _get_code_object
Nick Coghlaneae2da12010-08-17 08:03:36 +0000625Variable names:
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000626 0: x""" % (('Formatted details of methods, functions, or code.',)
627 if sys.flags.optimize < 2 else (None,))
Nick Coghlaneae2da12010-08-17 08:03:36 +0000628
629@staticmethod
630def tricky(x, y, z=True, *args, c, d, e=[], **kwds):
631 def f(c=c):
632 print(x, y, z, c, d, e, f)
633 yield x, y, z, c, d, e, f
634
Nick Coghlaneae2da12010-08-17 08:03:36 +0000635code_info_tricky = """\
636Name: tricky
Nick Coghlan46e63802010-08-17 11:28:07 +0000637Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000638Argument count: 3
639Kw-only arguments: 3
640Number of locals: 8
641Stack size: 7
642Flags: OPTIMIZED, NEWLOCALS, VARARGS, VARKEYWORDS, GENERATOR
643Constants:
644 0: None
Nick Coghlan46e63802010-08-17 11:28:07 +0000645 1: <code object f at (.*), file "(.*)", line (.*)>
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100646 2: 'tricky.<locals>.f'
Nick Coghlaneae2da12010-08-17 08:03:36 +0000647Variable names:
648 0: x
649 1: y
650 2: z
651 3: c
652 4: d
653 5: e
654 6: args
655 7: kwds
656Cell variables:
Georg Brandla1082272012-02-20 21:41:03 +0100657 0: [edfxyz]
658 1: [edfxyz]
659 2: [edfxyz]
660 3: [edfxyz]
661 4: [edfxyz]
662 5: [edfxyz]"""
663# NOTE: the order of the cell variables above depends on dictionary order!
Nick Coghlan46e63802010-08-17 11:28:07 +0000664
665co_tricky_nested_f = tricky.__func__.__code__.co_consts[1]
Nick Coghlaneae2da12010-08-17 08:03:36 +0000666
667code_info_tricky_nested_f = """\
668Name: f
Nick Coghlan46e63802010-08-17 11:28:07 +0000669Filename: (.*)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000670Argument count: 1
671Kw-only arguments: 0
672Number of locals: 1
673Stack size: 8
674Flags: OPTIMIZED, NEWLOCALS, NESTED
675Constants:
676 0: None
677Names:
678 0: print
679Variable names:
680 0: c
681Free variables:
Georg Brandl27fe2262012-02-20 22:03:28 +0100682 0: [edfxyz]
683 1: [edfxyz]
684 2: [edfxyz]
685 3: [edfxyz]
686 4: [edfxyz]
687 5: [edfxyz]"""
Nick Coghlaneae2da12010-08-17 08:03:36 +0000688
689code_info_expr_str = """\
690Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000691Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000692Argument count: 0
693Kw-only arguments: 0
694Number of locals: 0
695Stack size: 2
696Flags: NOFREE
697Constants:
698 0: 1
699Names:
700 0: x"""
701
702code_info_simple_stmt_str = """\
703Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000704Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000705Argument count: 0
706Kw-only arguments: 0
707Number of locals: 0
708Stack size: 2
709Flags: NOFREE
710Constants:
711 0: 1
712 1: None
713Names:
714 0: x"""
715
716code_info_compound_stmt_str = """\
717Name: <module>
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000718Filename: <disassembly>
Nick Coghlaneae2da12010-08-17 08:03:36 +0000719Argument count: 0
720Kw-only arguments: 0
721Number of locals: 0
722Stack size: 2
723Flags: NOFREE
724Constants:
725 0: 0
726 1: 1
727 2: None
728Names:
729 0: x"""
730
Yury Selivanov75445082015-05-11 22:57:16 -0400731
732async def async_def():
733 await 1
734 async for a in b: pass
735 async with c as d: pass
736
737code_info_async_def = """\
738Name: async_def
739Filename: (.*)
740Argument count: 0
741Kw-only arguments: 0
742Number of locals: 2
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200743Stack size: 10
Yury Selivanoveb636452016-09-08 22:01:51 -0700744Flags: OPTIMIZED, NEWLOCALS, NOFREE, COROUTINE
Yury Selivanov75445082015-05-11 22:57:16 -0400745Constants:
746 0: None
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200747 1: 1
748Names:
749 0: b
Serhiy Storchaka702f8f32018-03-23 14:34:35 +0200750 1: c
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200751Variable names:
752 0: a
753 1: d"""
Yury Selivanov75445082015-05-11 22:57:16 -0400754
Nick Coghlaneae2da12010-08-17 08:03:36 +0000755class CodeInfoTests(unittest.TestCase):
756 test_pairs = [
757 (dis.code_info, code_info_code_info),
758 (tricky, code_info_tricky),
759 (co_tricky_nested_f, code_info_tricky_nested_f),
760 (expr_str, code_info_expr_str),
761 (simple_stmt_str, code_info_simple_stmt_str),
762 (compound_stmt_str, code_info_compound_stmt_str),
Yury Selivanov75445082015-05-11 22:57:16 -0400763 (async_def, code_info_async_def)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000764 ]
765
766 def test_code_info(self):
767 self.maxDiff = 1000
768 for x, expected in self.test_pairs:
Ezio Melottied3a7d22010-12-01 02:32:32 +0000769 self.assertRegex(dis.code_info(x), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000770
771 def test_show_code(self):
772 self.maxDiff = 1000
773 for x, expected in self.test_pairs:
774 with captured_stdout() as output:
775 dis.show_code(x)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000776 self.assertRegex(output.getvalue(), expected+"\n")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000777 output = io.StringIO()
778 dis.show_code(x, file=output)
779 self.assertRegex(output.getvalue(), expected)
Nick Coghlaneae2da12010-08-17 08:03:36 +0000780
Benjamin Petersond6afe722011-03-15 14:44:52 -0500781 def test_code_info_object(self):
782 self.assertRaises(TypeError, dis.code_info, object())
783
784 def test_pretty_flags_no_flags(self):
785 self.assertEqual(dis.pretty_flags(0), '0x0')
786
787
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000788# Fodder for instruction introspection tests
789# Editing any of these may require recalculating the expected output
790def outer(a=1, b=2):
791 def f(c=3, d=4):
792 def inner(e=5, f=6):
793 print(a, b, c, d, e, f)
794 print(a, b, c, d)
795 return inner
796 print(a, b, '', 1, [], {}, "Hello world!")
797 return f
798
799def jumpy():
800 # This won't actually run (but that's OK, we only disassemble it)
801 for i in range(10):
802 print(i)
803 if i < 4:
804 continue
805 if i > 6:
806 break
807 else:
808 print("I can haz else clause?")
809 while i:
810 print(i)
811 i -= 1
812 if i > 6:
813 continue
814 if i < 4:
815 break
816 else:
817 print("Who let lolcatz into this test suite?")
818 try:
819 1 / 0
820 except ZeroDivisionError:
821 print("Here we go, here we go, here we go...")
822 else:
823 with i as dodgy:
824 print("Never reach this")
825 finally:
826 print("OK, now we're done")
827
828# End fodder for opinfo generation tests
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000829expected_outer_line = 1
830_line_offset = outer.__code__.co_firstlineno - 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000831code_object_f = outer.__code__.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000832expected_f_line = code_object_f.co_firstlineno - _line_offset
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000833code_object_inner = code_object_f.co_consts[3]
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000834expected_inner_line = code_object_inner.co_firstlineno - _line_offset
835expected_jumpy_line = 1
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000836
837# The following lines are useful to regenerate the expected results after
838# either the fodder is modified or the bytecode generation changes
839# After regeneration, update the references to code_object_f and
840# code_object_inner before rerunning the tests
841
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000842#_instructions = dis.get_instructions(outer, first_line=expected_outer_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000843#print('expected_opinfo_outer = [\n ',
844 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200845#_instructions = dis.get_instructions(outer(), first_line=expected_f_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000846#print('expected_opinfo_f = [\n ',
847 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Antoine Pitroue7811fc2014-09-18 03:06:50 +0200848#_instructions = dis.get_instructions(outer()(), first_line=expected_inner_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000849#print('expected_opinfo_inner = [\n ',
850 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
Nick Coghlan90b8e7d2013-11-06 22:08:36 +1000851#_instructions = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +1000852#print('expected_opinfo_jumpy = [\n ',
853 #',\n '.join(map(str, _instructions)), ',\n]', sep='')
854
855
856Instruction = dis.Instruction
857expected_opinfo_outer = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300858 Instruction(opname='LOAD_CONST', opcode=100, arg=8, argval=(3, 4), argrepr='(3, 4)', offset=0, starts_line=2, is_jump_target=False),
859 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
860 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
861 Instruction(opname='BUILD_TUPLE', opcode=102, arg=2, argval=2, argrepr='', offset=6, starts_line=None, is_jump_target=False),
862 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),
863 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 +0200864 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 +0300865 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='f', argrepr='f', offset=14, starts_line=None, is_jump_target=False),
866 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=16, starts_line=7, is_jump_target=False),
867 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=18, starts_line=None, is_jump_target=False),
868 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=20, starts_line=None, is_jump_target=False),
869 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval='', argrepr="''", offset=22, starts_line=None, is_jump_target=False),
870 Instruction(opname='LOAD_CONST', opcode=100, arg=6, argval=1, argrepr='1', offset=24, starts_line=None, is_jump_target=False),
871 Instruction(opname='BUILD_LIST', opcode=103, arg=0, argval=0, argrepr='', offset=26, starts_line=None, is_jump_target=False),
872 Instruction(opname='BUILD_MAP', opcode=105, arg=0, argval=0, argrepr='', offset=28, starts_line=None, is_jump_target=False),
873 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 -0700874 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 +0300875 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=34, starts_line=None, is_jump_target=False),
876 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='f', argrepr='f', offset=36, starts_line=8, is_jump_target=False),
877 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 +1000878]
879
880expected_opinfo_f = [
Serhiy Storchaka64204de2016-06-12 17:36:24 +0300881 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=(5, 6), argrepr='(5, 6)', offset=0, starts_line=3, is_jump_target=False),
882 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=2, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
883 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=3, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
884 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=0, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
885 Instruction(opname='LOAD_CLOSURE', opcode=135, arg=1, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
886 Instruction(opname='BUILD_TUPLE', opcode=102, arg=4, argval=4, argrepr='', offset=10, starts_line=None, is_jump_target=False),
887 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),
888 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 +0200889 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 +0300890 Instruction(opname='STORE_FAST', opcode=125, arg=2, argval='inner', argrepr='inner', offset=18, starts_line=None, is_jump_target=False),
891 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='print', argrepr='print', offset=20, starts_line=5, is_jump_target=False),
892 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='a', argrepr='a', offset=22, starts_line=None, is_jump_target=False),
893 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='b', argrepr='b', offset=24, starts_line=None, is_jump_target=False),
894 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='c', argrepr='c', offset=26, starts_line=None, is_jump_target=False),
895 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 -0700896 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 +0300897 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=32, starts_line=None, is_jump_target=False),
898 Instruction(opname='LOAD_FAST', opcode=124, arg=2, argval='inner', argrepr='inner', offset=34, starts_line=6, is_jump_target=False),
899 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 +1000900]
901
902expected_opinfo_inner = [
903 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 +0300904 Instruction(opname='LOAD_DEREF', opcode=136, arg=0, argval='a', argrepr='a', offset=2, starts_line=None, is_jump_target=False),
905 Instruction(opname='LOAD_DEREF', opcode=136, arg=1, argval='b', argrepr='b', offset=4, starts_line=None, is_jump_target=False),
906 Instruction(opname='LOAD_DEREF', opcode=136, arg=2, argval='c', argrepr='c', offset=6, starts_line=None, is_jump_target=False),
907 Instruction(opname='LOAD_DEREF', opcode=136, arg=3, argval='d', argrepr='d', offset=8, starts_line=None, is_jump_target=False),
908 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='e', argrepr='e', offset=10, starts_line=None, is_jump_target=False),
909 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 -0700910 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 +0300911 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=16, starts_line=None, is_jump_target=False),
912 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=18, starts_line=None, is_jump_target=False),
913 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 +1000914]
915
916expected_opinfo_jumpy = [
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200917 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=0, argval='range', argrepr='range', offset=0, starts_line=3, is_jump_target=False),
918 Instruction(opname='LOAD_CONST', opcode=100, arg=1, argval=10, argrepr='10', offset=2, starts_line=None, is_jump_target=False),
919 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=4, starts_line=None, is_jump_target=False),
920 Instruction(opname='GET_ITER', opcode=68, arg=None, argval=None, argrepr='', offset=6, starts_line=None, is_jump_target=False),
921 Instruction(opname='FOR_ITER', opcode=93, arg=34, argval=44, argrepr='to 44', offset=8, starts_line=None, is_jump_target=True),
922 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=10, starts_line=None, is_jump_target=False),
923 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=12, starts_line=4, is_jump_target=False),
924 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=14, starts_line=None, is_jump_target=False),
925 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=16, starts_line=None, is_jump_target=False),
926 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=18, starts_line=None, is_jump_target=False),
927 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=20, starts_line=5, is_jump_target=False),
928 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=22, starts_line=None, is_jump_target=False),
929 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=24, starts_line=None, is_jump_target=False),
930 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=30, argval=30, argrepr='', offset=26, starts_line=None, is_jump_target=False),
931 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=28, starts_line=6, is_jump_target=False),
932 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=30, starts_line=7, is_jump_target=True),
933 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=32, starts_line=None, is_jump_target=False),
934 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
935 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=8, argval=8, argrepr='', offset=36, starts_line=None, is_jump_target=False),
936 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
937 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=40, starts_line=None, is_jump_target=False),
938 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=8, argval=8, argrepr='', offset=42, starts_line=None, is_jump_target=False),
939 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
940 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),
941 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=48, starts_line=None, is_jump_target=False),
942 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=50, starts_line=None, is_jump_target=False),
943 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=52, starts_line=11, is_jump_target=True),
944 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=94, argval=94, argrepr='', offset=54, starts_line=None, is_jump_target=False),
945 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=56, starts_line=12, is_jump_target=False),
946 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=58, starts_line=None, is_jump_target=False),
947 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=60, starts_line=None, is_jump_target=False),
948 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=62, starts_line=None, is_jump_target=False),
949 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=64, starts_line=13, is_jump_target=False),
950 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=66, starts_line=None, is_jump_target=False),
951 Instruction(opname='INPLACE_SUBTRACT', opcode=56, arg=None, argval=None, argrepr='', offset=68, starts_line=None, is_jump_target=False),
952 Instruction(opname='STORE_FAST', opcode=125, arg=0, argval='i', argrepr='i', offset=70, starts_line=None, is_jump_target=False),
953 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=72, starts_line=14, is_jump_target=False),
954 Instruction(opname='LOAD_CONST', opcode=100, arg=3, argval=6, argrepr='6', offset=74, starts_line=None, is_jump_target=False),
955 Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=76, starts_line=None, is_jump_target=False),
956 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=82, argval=82, argrepr='', offset=78, starts_line=None, is_jump_target=False),
957 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=80, starts_line=15, is_jump_target=False),
958 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=82, starts_line=16, is_jump_target=True),
959 Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
960 Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
961 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=52, argval=52, argrepr='', offset=88, starts_line=None, is_jump_target=False),
962 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=102, argval=102, argrepr='', offset=90, starts_line=17, is_jump_target=False),
963 Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=52, argrepr='', offset=92, starts_line=None, is_jump_target=False),
964 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=94, starts_line=19, is_jump_target=True),
965 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),
966 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=98, starts_line=None, is_jump_target=False),
967 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=100, starts_line=None, is_jump_target=False),
968 Instruction(opname='SETUP_FINALLY', opcode=122, arg=70, argval=174, argrepr='to 174', offset=102, starts_line=20, is_jump_target=True),
969 Instruction(opname='SETUP_FINALLY', opcode=122, arg=12, argval=118, argrepr='to 118', offset=104, starts_line=None, is_jump_target=False),
970 Instruction(opname='LOAD_CONST', opcode=100, arg=5, argval=1, argrepr='1', offset=106, starts_line=21, is_jump_target=False),
971 Instruction(opname='LOAD_CONST', opcode=100, arg=7, argval=0, argrepr='0', offset=108, starts_line=None, is_jump_target=False),
972 Instruction(opname='BINARY_TRUE_DIVIDE', opcode=27, arg=None, argval=None, argrepr='', offset=110, starts_line=None, is_jump_target=False),
973 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False),
974 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=114, starts_line=None, is_jump_target=False),
975 Instruction(opname='JUMP_FORWARD', opcode=110, arg=28, argval=146, argrepr='to 146', offset=116, starts_line=None, is_jump_target=False),
976 Instruction(opname='DUP_TOP', opcode=4, arg=None, argval=None, argrepr='', offset=118, starts_line=22, is_jump_target=True),
977 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=2, argval='ZeroDivisionError', argrepr='ZeroDivisionError', offset=120, starts_line=None, is_jump_target=False),
978 Instruction(opname='COMPARE_OP', opcode=107, arg=10, argval='exception match', argrepr='exception match', offset=122, starts_line=None, is_jump_target=False),
979 Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=144, argval=144, argrepr='', offset=124, starts_line=None, is_jump_target=False),
980 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=126, starts_line=None, is_jump_target=False),
981 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=128, starts_line=None, is_jump_target=False),
982 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=130, starts_line=None, is_jump_target=False),
983 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=132, starts_line=23, is_jump_target=False),
984 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=134, starts_line=None, is_jump_target=False),
985 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=136, starts_line=None, is_jump_target=False),
986 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=138, starts_line=None, is_jump_target=False),
987 Instruction(opname='POP_EXCEPT', opcode=89, arg=None, argval=None, argrepr='', offset=140, starts_line=None, is_jump_target=False),
988 Instruction(opname='JUMP_FORWARD', opcode=110, arg=26, argval=170, argrepr='to 170', offset=142, starts_line=None, is_jump_target=False),
989 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=144, starts_line=None, is_jump_target=True),
990 Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=146, starts_line=25, is_jump_target=True),
991 Instruction(opname='SETUP_WITH', opcode=143, arg=14, argval=164, argrepr='to 164', offset=148, starts_line=None, is_jump_target=False),
992 Instruction(opname='STORE_FAST', opcode=125, arg=1, argval='dodgy', argrepr='dodgy', offset=150, starts_line=None, is_jump_target=False),
993 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=152, starts_line=26, is_jump_target=False),
994 Instruction(opname='LOAD_CONST', opcode=100, arg=9, argval='Never reach this', argrepr="'Never reach this'", offset=154, starts_line=None, is_jump_target=False),
995 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=156, starts_line=None, is_jump_target=False),
996 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=158, starts_line=None, is_jump_target=False),
997 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=160, starts_line=None, is_jump_target=False),
998 Instruction(opname='BEGIN_FINALLY', opcode=53, arg=None, argval=None, argrepr='', offset=162, starts_line=None, is_jump_target=False),
999 Instruction(opname='WITH_CLEANUP_START', opcode=81, arg=None, argval=None, argrepr='', offset=164, starts_line=None, is_jump_target=True),
1000 Instruction(opname='WITH_CLEANUP_FINISH', opcode=82, arg=None, argval=None, argrepr='', offset=166, starts_line=None, is_jump_target=False),
1001 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=168, starts_line=None, is_jump_target=False),
1002 Instruction(opname='POP_BLOCK', opcode=87, arg=None, argval=None, argrepr='', offset=170, starts_line=None, is_jump_target=True),
1003 Instruction(opname='BEGIN_FINALLY', opcode=53, arg=None, argval=None, argrepr='', offset=172, starts_line=None, is_jump_target=False),
1004 Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=174, starts_line=28, is_jump_target=True),
1005 Instruction(opname='LOAD_CONST', opcode=100, arg=10, argval="OK, now we're done", argrepr='"OK, now we\'re done"', offset=176, starts_line=None, is_jump_target=False),
1006 Instruction(opname='CALL_FUNCTION', opcode=131, arg=1, argval=1, argrepr='', offset=178, starts_line=None, is_jump_target=False),
1007 Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=180, starts_line=None, is_jump_target=False),
1008 Instruction(opname='END_FINALLY', opcode=88, arg=None, argval=None, argrepr='', offset=182, starts_line=None, is_jump_target=False),
1009 Instruction(opname='LOAD_CONST', opcode=100, arg=0, argval=None, argrepr='None', offset=184, starts_line=None, is_jump_target=False),
1010 Instruction(opname='RETURN_VALUE', opcode=83, arg=None, argval=None, argrepr='', offset=186, starts_line=None, is_jump_target=False),
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001011]
1012
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001013# One last piece of inspect fodder to check the default line number handling
1014def simple(): pass
1015expected_opinfo_simple = [
1016 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 +03001017 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 +10001018]
1019
1020
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001021class InstructionTests(BytecodeTestCase):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001022
1023 def test_default_first_line(self):
1024 actual = dis.get_instructions(simple)
1025 self.assertEqual(list(actual), expected_opinfo_simple)
1026
1027 def test_first_line_set_to_None(self):
1028 actual = dis.get_instructions(simple, first_line=None)
1029 self.assertEqual(list(actual), expected_opinfo_simple)
1030
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001031 def test_outer(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001032 actual = dis.get_instructions(outer, first_line=expected_outer_line)
1033 self.assertEqual(list(actual), expected_opinfo_outer)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001034
1035 def test_nested(self):
1036 with captured_stdout():
1037 f = outer()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001038 actual = dis.get_instructions(f, first_line=expected_f_line)
1039 self.assertEqual(list(actual), expected_opinfo_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001040
1041 def test_doubly_nested(self):
1042 with captured_stdout():
1043 inner = outer()()
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001044 actual = dis.get_instructions(inner, first_line=expected_inner_line)
1045 self.assertEqual(list(actual), expected_opinfo_inner)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001046
1047 def test_jumpy(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001048 actual = dis.get_instructions(jumpy, first_line=expected_jumpy_line)
1049 self.assertEqual(list(actual), expected_opinfo_jumpy)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001050
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001051# get_instructions has its own tests above, so can rely on it to validate
1052# the object oriented API
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001053class BytecodeTests(unittest.TestCase):
1054 def test_instantiation(self):
1055 # Test with function, method, code string and code object
1056 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001057 with self.subTest(obj=obj):
1058 b = dis.Bytecode(obj)
1059 self.assertIsInstance(b.codeobj, types.CodeType)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001060
1061 self.assertRaises(TypeError, dis.Bytecode, object())
1062
1063 def test_iteration(self):
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001064 for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
1065 with self.subTest(obj=obj):
1066 via_object = list(dis.Bytecode(obj))
1067 via_generator = list(dis.get_instructions(obj))
1068 self.assertEqual(via_object, via_generator)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001069
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001070 def test_explicit_first_line(self):
1071 actual = dis.Bytecode(outer, first_line=expected_outer_line)
1072 self.assertEqual(list(actual), expected_opinfo_outer)
1073
1074 def test_source_line_in_disassembly(self):
1075 # Use the line in the source code
syncosmicfe2b56a2017-08-17 19:29:21 -07001076 actual = dis.Bytecode(simple).dis()
1077 actual = actual.strip().partition(" ")[0] # extract the line no
1078 expected = str(simple.__code__.co_firstlineno)
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001079 self.assertEqual(actual, expected)
1080 # Use an explicit first line number
syncosmicfe2b56a2017-08-17 19:29:21 -07001081 actual = dis.Bytecode(simple, first_line=350).dis()
1082 actual = actual.strip().partition(" ")[0] # extract the line no
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001083 self.assertEqual(actual, "350")
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001084
1085 def test_info(self):
1086 self.maxDiff = 1000
1087 for x, expected in CodeInfoTests.test_pairs:
1088 b = dis.Bytecode(x)
1089 self.assertRegex(b.info(), expected)
1090
Nick Coghlan90b8e7d2013-11-06 22:08:36 +10001091 def test_disassembled(self):
1092 actual = dis.Bytecode(_f).dis()
1093 self.assertEqual(actual, dis_f)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001094
Nick Coghlan50c48b82013-11-23 00:57:00 +10001095 def test_from_traceback(self):
1096 tb = get_tb()
1097 b = dis.Bytecode.from_traceback(tb)
1098 while tb.tb_next: tb = tb.tb_next
1099
1100 self.assertEqual(b.current_offset, tb.tb_lasti)
1101
1102 def test_from_traceback_dis(self):
1103 tb = get_tb()
1104 b = dis.Bytecode.from_traceback(tb)
1105 self.assertEqual(b.dis(), dis_traceback)
Nick Coghlanb39fd0c2013-05-06 23:59:20 +10001106
Skip Montanaroadd0ccc2003-02-27 21:27:07 +00001107if __name__ == "__main__":
Zachary Waree80e8062013-12-26 09:53:49 -06001108 unittest.main()