forked from MatthieuDartiailh/bytecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_misc.py
More file actions
235 lines (196 loc) · 5.98 KB
/
test_misc.py
File metadata and controls
235 lines (196 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
import contextlib
import io
import textwrap
import unittest
import bytecode
from bytecode import Label, Instr, Bytecode, BasicBlock, ControlFlowGraph
from bytecode.tests import disassemble
class DumpCodeTests(unittest.TestCase):
maxDiff = 80 * 100
def check_dump_bytecode(self, code, expected, lineno=None):
with contextlib.redirect_stdout(io.StringIO()) as stderr:
if lineno is not None:
bytecode.dump_bytecode(code, lineno=True)
else:
bytecode.dump_bytecode(code)
output = stderr.getvalue()
self.assertEqual(output, expected)
def test_bytecode(self):
source = """
def func(test):
if test == 1:
return 1
elif test == 2:
return 2
return 3
"""
code = disassemble(source, function=True)
# without line numbers
expected = """
LOAD_FAST 'test'
LOAD_CONST 1
COMPARE_OP <Compare.EQ: 2>
POP_JUMP_IF_FALSE <label_instr6>
LOAD_CONST 1
RETURN_VALUE
label_instr6:
LOAD_FAST 'test'
LOAD_CONST 2
COMPARE_OP <Compare.EQ: 2>
POP_JUMP_IF_FALSE <label_instr13>
LOAD_CONST 2
RETURN_VALUE
label_instr13:
LOAD_CONST 3
RETURN_VALUE
"""[1:].rstrip(" ")
self.check_dump_bytecode(code, expected)
# with line numbers
expected = """
L. 2 0: LOAD_FAST 'test'
1: LOAD_CONST 1
2: COMPARE_OP <Compare.EQ: 2>
3: POP_JUMP_IF_FALSE <label_instr6>
L. 3 4: LOAD_CONST 1
5: RETURN_VALUE
label_instr6:
L. 4 7: LOAD_FAST 'test'
8: LOAD_CONST 2
9: COMPARE_OP <Compare.EQ: 2>
10: POP_JUMP_IF_FALSE <label_instr13>
L. 5 11: LOAD_CONST 2
12: RETURN_VALUE
label_instr13:
L. 6 14: LOAD_CONST 3
15: RETURN_VALUE
"""[1:].rstrip(" ")
self.check_dump_bytecode(code, expected, lineno=True)
def test_bytecode_broken_label(self):
label = Label()
code = Bytecode([Instr('JUMP_ABSOLUTE', label)])
expected = " JUMP_ABSOLUTE <error: unknown label>\n\n"
self.check_dump_bytecode(code, expected)
def test_blocks_broken_jump(self):
block = BasicBlock()
code = ControlFlowGraph()
code[0].append(Instr('JUMP_ABSOLUTE', block))
expected = textwrap.dedent("""
block1:
JUMP_ABSOLUTE <error: unknown block>
""").lstrip("\n")
self.check_dump_bytecode(code, expected)
def test_bytecode_blocks(self):
source = """
def func(test):
if test == 1:
return 1
elif test == 2:
return 2
return 3
"""
code = disassemble(source, function=True)
code = ControlFlowGraph.from_bytecode(code)
# without line numbers
expected = textwrap.dedent("""
block1:
LOAD_FAST 'test'
LOAD_CONST 1
COMPARE_OP <Compare.EQ: 2>
POP_JUMP_IF_FALSE <block3>
-> block2
block2:
LOAD_CONST 1
RETURN_VALUE
block3:
LOAD_FAST 'test'
LOAD_CONST 2
COMPARE_OP <Compare.EQ: 2>
POP_JUMP_IF_FALSE <block5>
-> block4
block4:
LOAD_CONST 2
RETURN_VALUE
block5:
LOAD_CONST 3
RETURN_VALUE
""").lstrip()
self.check_dump_bytecode(code, expected)
# with line numbers
expected = textwrap.dedent("""
block1:
L. 2 0: LOAD_FAST 'test'
1: LOAD_CONST 1
2: COMPARE_OP <Compare.EQ: 2>
3: POP_JUMP_IF_FALSE <block3>
-> block2
block2:
L. 3 0: LOAD_CONST 1
1: RETURN_VALUE
block3:
L. 4 0: LOAD_FAST 'test'
1: LOAD_CONST 2
2: COMPARE_OP <Compare.EQ: 2>
3: POP_JUMP_IF_FALSE <block5>
-> block4
block4:
L. 5 0: LOAD_CONST 2
1: RETURN_VALUE
block5:
L. 6 0: LOAD_CONST 3
1: RETURN_VALUE
""").lstrip()
self.check_dump_bytecode(code, expected, lineno=True)
def test_concrete_bytecode(self):
source = """
def func(test):
if test == 1:
return 1
elif test == 2:
return 2
return 3
"""
code = disassemble(source, function=True)
code = code.to_concrete_bytecode()
# without line numbers
expected = """
0 LOAD_FAST 0
3 LOAD_CONST 1
6 COMPARE_OP 2
9 POP_JUMP_IF_FALSE 16
12 LOAD_CONST 1
15 RETURN_VALUE
16 LOAD_FAST 0
19 LOAD_CONST 2
22 COMPARE_OP 2
25 POP_JUMP_IF_FALSE 32
28 LOAD_CONST 2
31 RETURN_VALUE
32 LOAD_CONST 3
35 RETURN_VALUE
""".lstrip("\n")
self.check_dump_bytecode(code, expected)
# with line numbers
expected = """
L. 2 0: LOAD_FAST 0
3: LOAD_CONST 1
6: COMPARE_OP 2
9: POP_JUMP_IF_FALSE 16
L. 3 12: LOAD_CONST 1
15: RETURN_VALUE
L. 4 16: LOAD_FAST 0
19: LOAD_CONST 2
22: COMPARE_OP 2
25: POP_JUMP_IF_FALSE 32
L. 5 28: LOAD_CONST 2
31: RETURN_VALUE
L. 6 32: LOAD_CONST 3
35: RETURN_VALUE
""".lstrip("\n")
self.check_dump_bytecode(code, expected, lineno=True)
class MiscTests(unittest.TestCase):
def test_version(self):
import setup
self.assertEqual(bytecode.__version__, setup.VERSION)
if __name__ == "__main__":
unittest.main()