1
# Minimal tests for dis module
3
from test.support import run_unittest
15
%-4d 0 LOAD_GLOBAL 0 (print)
20
%-4d 10 LOAD_CONST 1 (1)
22
"""%(_f.__code__.co_firstlineno + 1,
23
_f.__code__.co_firstlineno + 2)
32
%-4d 0 SETUP_LOOP 23 (to 26)
33
3 LOAD_GLOBAL 0 (range)
36
%-4d 9 LOAD_CONST 2 (10)
39
>> 16 FOR_ITER 6 (to 25)
42
%-4d 22 JUMP_ABSOLUTE 16
44
>> 26 LOAD_CONST 0 (None)
46
"""%(bug708901.__code__.co_firstlineno + 1,
47
bug708901.__code__.co_firstlineno + 2,
48
bug708901.__code__.co_firstlineno + 3)
52
assert 0, ([s for s in x] +
57
%-4d 0 LOAD_CONST 1 (0)
58
3 JUMP_IF_TRUE 33 (to 39)
60
7 LOAD_GLOBAL 0 (AssertionError)
64
>> 17 FOR_ITER 12 (to 32)
70
%-4d >> 32 LOAD_CONST 2 (1)
75
%-4d 40 LOAD_CONST 0 (None)
77
"""%(bug1333982.__code__.co_firstlineno + 1,
78
bug1333982.__code__.co_firstlineno + 2,
79
bug1333982.__code__.co_firstlineno + 3)
81
_BIG_LINENO_FORMAT = """\
82
%3d 0 LOAD_GLOBAL 0 (spam)
88
dis_module_expected_results = """\
90
4 0 LOAD_CONST 0 (None)
94
5 0 LOAD_CONST 0 (None)
100
class DisTests(unittest.TestCase):
101
def do_disassembly_test(self, func, expected):
103
save_stdout = sys.stdout
106
sys.stdout = save_stdout
108
# Trim trailing blanks (if any).
109
lines = got.split('\n')
110
lines = [line.rstrip() for line in lines]
111
expected = expected.split("\n")
113
if expected != lines:
115
"events did not match expectation:\n" +
116
"\n".join(difflib.ndiff(expected,
119
def test_opmap(self):
120
self.assertEqual(dis.opmap["STOP_CODE"], 0)
121
self.assertEqual(dis.opmap["LOAD_CONST"] in dis.hasconst, True)
122
self.assertEqual(dis.opmap["STORE_NAME"] in dis.hasname, True)
124
def test_opname(self):
125
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
127
def test_boundaries(self):
128
self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
129
self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
132
self.do_disassembly_test(_f, dis_f)
134
def test_bug_708901(self):
135
self.do_disassembly_test(bug708901, dis_bug708901)
137
def test_bug_1333982(self):
138
# XXX: re-enable this test!
139
# This one is checking bytecodes generated for an `assert` statement,
140
# so fails if the tests are run with -O. Skip this test then.
141
pass # Test has been disabled due to change in the way
142
# list comps are handled. The byte code now includes
143
# a memory address and a file location, so they change from
146
# self.do_disassembly_test(bug1333982, dis_bug1333982)
148
def test_big_linenos(self):
151
func = "def foo():\n " + "".join(["\n "] * count + ["spam\n"])
152
exec(func, namespace)
153
return namespace['foo']
155
# Test all small ranges
156
for i in range(1, 300):
157
expected = _BIG_LINENO_FORMAT % (i + 2)
158
self.do_disassembly_test(func(i), expected)
160
# Test some larger ranges too
161
for i in range(300, 5000, 10):
162
expected = _BIG_LINENO_FORMAT % (i + 2)
163
self.do_disassembly_test(func(i), expected)
165
def test_big_linenos(self):
166
from test import dis_module
167
self.do_disassembly_test(dis_module, dis_module_expected_results)
170
run_unittest(DisTests)
172
if __name__ == "__main__":