Repository URL to install this package:
# Minimal tests for dis module
from test.support import captured_stdout
from test.bytecode_helper import BytecodeTestCase
import unittest
import sys
import dis
import io
import re
import types
import contextlib
def get_tb():
def _error():
try:
1 / 0
except Exception as e:
tb = e.__traceback__
return tb
tb = _error()
while tb.tb_next:
tb = tb.tb_next
return tb
TRACEBACK_CODE = get_tb().tb_frame.f_code
class _C:
def __init__(self, x):
self.x = x == 1
@staticmethod
def sm(x):
x = x == 1
@classmethod
def cm(cls, x):
cls.x = x == 1
dis_c_instance_method = """\
%3d 0 LOAD_FAST 1 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (self)
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
""" % (_C.__init__.__code__.co_firstlineno + 1,)
dis_c_instance_method_bytes = """\
0 LOAD_FAST 1 (1)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (0)
8 STORE_ATTR 0 (0)
10 LOAD_CONST 0 (0)
12 RETURN_VALUE
"""
dis_c_class_method = """\
%3d 0 LOAD_FAST 1 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 LOAD_FAST 0 (cls)
8 STORE_ATTR 0 (x)
10 LOAD_CONST 0 (None)
12 RETURN_VALUE
""" % (_C.cm.__code__.co_firstlineno + 2,)
dis_c_static_method = """\
%3d 0 LOAD_FAST 0 (x)
2 LOAD_CONST 1 (1)
4 COMPARE_OP 2 (==)
6 STORE_FAST 0 (x)
8 LOAD_CONST 0 (None)
10 RETURN_VALUE
""" % (_C.sm.__code__.co_firstlineno + 2,)
# Class disassembling info has an extra newline at end.
dis_c = """\
Disassembly of %s:
%s
Disassembly of %s:
%s
Disassembly of %s:
%s
""" % (_C.__init__.__name__, dis_c_instance_method,
_C.cm.__name__, dis_c_class_method,
_C.sm.__name__, dis_c_static_method)
def _f(a):
print(a)
return 1
dis_f = """\
%3d 0 LOAD_GLOBAL 0 (print)
2 LOAD_FAST 0 (a)
4 CALL_FUNCTION 1
6 POP_TOP
%3d 8 LOAD_CONST 1 (1)
10 RETURN_VALUE
""" % (_f.__code__.co_firstlineno + 1,
_f.__code__.co_firstlineno + 2)
dis_f_co_code = """\
0 LOAD_GLOBAL 0 (0)
2 LOAD_FAST 0 (0)
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 1 (1)
10 RETURN_VALUE
"""
def bug708901():
for res in range(1,
10):
pass
dis_bug708901 = """\
%3d 0 LOAD_GLOBAL 0 (range)
2 LOAD_CONST 1 (1)
%3d 4 LOAD_CONST 2 (10)
%3d 6 CALL_FUNCTION 2
8 GET_ITER
>> 10 FOR_ITER 4 (to 16)
12 STORE_FAST 0 (res)
%3d 14 JUMP_ABSOLUTE 10
>> 16 LOAD_CONST 0 (None)
18 RETURN_VALUE
""" % (bug708901.__code__.co_firstlineno + 1,
bug708901.__code__.co_firstlineno + 2,
bug708901.__code__.co_firstlineno + 1,
bug708901.__code__.co_firstlineno + 3)
def bug1333982(x=[]):
assert 0, ([s for s in x] +
1)
pass
dis_bug1333982 = """\
%3d 0 LOAD_CONST 1 (0)
2 POP_JUMP_IF_TRUE 26
4 LOAD_GLOBAL 0 (AssertionError)
6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
10 MAKE_FUNCTION 0
12 LOAD_FAST 0 (x)
14 GET_ITER
16 CALL_FUNCTION 1
%3d 18 LOAD_CONST 4 (1)
%3d 20 BINARY_ADD
22 CALL_FUNCTION 1
24 RAISE_VARARGS 1
%3d >> 26 LOAD_CONST 0 (None)
28 RETURN_VALUE
""" % (bug1333982.__code__.co_firstlineno + 1,
__file__,
bug1333982.__code__.co_firstlineno + 1,
bug1333982.__code__.co_firstlineno + 2,
bug1333982.__code__.co_firstlineno + 1,
bug1333982.__code__.co_firstlineno + 3)
_BIG_LINENO_FORMAT = """\
%3d 0 LOAD_GLOBAL 0 (spam)
2 POP_TOP
4 LOAD_CONST 0 (None)
6 RETURN_VALUE
"""
_BIG_LINENO_FORMAT2 = """\
%4d 0 LOAD_GLOBAL 0 (spam)
2 POP_TOP
4 LOAD_CONST 0 (None)
6 RETURN_VALUE
"""
dis_module_expected_results = """\
Disassembly of f:
4 0 LOAD_CONST 0 (None)
2 RETURN_VALUE
Disassembly of g:
5 0 LOAD_CONST 0 (None)
2 RETURN_VALUE
"""
expr_str = "x + 1"
dis_expr_str = """\
1 0 LOAD_NAME 0 (x)
2 LOAD_CONST 0 (1)
4 BINARY_ADD
6 RETURN_VALUE
"""
simple_stmt_str = "x = x + 1"
dis_simple_stmt_str = """\
1 0 LOAD_NAME 0 (x)
2 LOAD_CONST 0 (1)
4 BINARY_ADD
6 STORE_NAME 0 (x)
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
"""
annot_stmt_str = """\
x: int = 1
y: fun(1)
lst[fun(0)]: int = 1
"""
# leading newline is for a reason (tests lineno)
dis_annot_stmt_str = """\
2 0 SETUP_ANNOTATIONS
2 LOAD_CONST 0 (1)
4 STORE_NAME 0 (x)
6 LOAD_NAME 1 (int)
8 LOAD_NAME 2 (__annotations__)
10 LOAD_CONST 1 ('x')
12 STORE_SUBSCR
3 14 LOAD_NAME 3 (fun)
16 LOAD_CONST 0 (1)
18 CALL_FUNCTION 1
20 LOAD_NAME 2 (__annotations__)
22 LOAD_CONST 2 ('y')
24 STORE_SUBSCR
4 26 LOAD_CONST 0 (1)
28 LOAD_NAME 4 (lst)
30 LOAD_NAME 3 (fun)
32 LOAD_CONST 3 (0)
34 CALL_FUNCTION 1
36 STORE_SUBSCR
38 LOAD_NAME 1 (int)
40 POP_TOP
42 LOAD_CONST 4 (None)
44 RETURN_VALUE
"""
compound_stmt_str = """\
x = 0
while 1:
x += 1"""
# Trailing newline has been deliberately omitted
dis_compound_stmt_str = """\
1 0 LOAD_CONST 0 (0)
2 STORE_NAME 0 (x)
3 >> 4 LOAD_NAME 0 (x)
6 LOAD_CONST 1 (1)
8 INPLACE_ADD
10 STORE_NAME 0 (x)
12 JUMP_ABSOLUTE 4
14 LOAD_CONST 2 (None)
16 RETURN_VALUE
"""
dis_traceback = """\
%3d 0 SETUP_FINALLY 12 (to 14)
%3d 2 LOAD_CONST 1 (1)
4 LOAD_CONST 2 (0)
--> 6 BINARY_TRUE_DIVIDE
8 POP_TOP
10 POP_BLOCK
12 JUMP_FORWARD 40 (to 54)
%3d >> 14 DUP_TOP
16 LOAD_GLOBAL 0 (Exception)
18 COMPARE_OP 10 (exception match)
20 POP_JUMP_IF_FALSE 52
22 POP_TOP
24 STORE_FAST 0 (e)
26 POP_TOP
28 SETUP_FINALLY 10 (to 40)
%3d 30 LOAD_FAST 0 (e)
32 LOAD_ATTR 1 (__traceback__)
34 STORE_FAST 1 (tb)
36 POP_BLOCK
38 BEGIN_FINALLY
>> 40 LOAD_CONST 0 (None)
42 STORE_FAST 0 (e)
44 DELETE_FAST 0 (e)
46 END_FINALLY
48 POP_EXCEPT
50 JUMP_FORWARD 2 (to 54)
>> 52 END_FINALLY
%3d >> 54 LOAD_FAST 1 (tb)
56 RETURN_VALUE
""" % (TRACEBACK_CODE.co_firstlineno + 1,
TRACEBACK_CODE.co_firstlineno + 2,
TRACEBACK_CODE.co_firstlineno + 3,
TRACEBACK_CODE.co_firstlineno + 4,
TRACEBACK_CODE.co_firstlineno + 5)
def _fstring(a, b, c, d):
return f'{a} {b:4} {c!r} {d!r:4}'
dis_fstring = """\
%3d 0 LOAD_FAST 0 (a)
2 FORMAT_VALUE 0
4 LOAD_CONST 1 (' ')
6 LOAD_FAST 1 (b)
8 LOAD_CONST 2 ('4')
10 FORMAT_VALUE 4 (with format)
12 LOAD_CONST 1 (' ')
14 LOAD_FAST 2 (c)
16 FORMAT_VALUE 2 (repr)
18 LOAD_CONST 1 (' ')
20 LOAD_FAST 3 (d)
22 LOAD_CONST 2 ('4')
24 FORMAT_VALUE 6 (repr, with format)
26 BUILD_STRING 7
28 RETURN_VALUE
""" % (_fstring.__code__.co_firstlineno + 1,)
def _g(x):
yield x
async def _ag(x):
yield x
async def _co(x):
async for item in _ag(x):
pass
def _h(y):
def foo(x):
Loading ...