- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathbytecode_helper.py
158 lines (131 loc) · 5.63 KB
/
bytecode_helper.py
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
"""bytecode_helper - support tools for testing correct bytecode generation"""
importunittest
importdis
importio
importopcode
try:
import_testinternalcapi
exceptImportError:
_testinternalcapi=None
_UNSPECIFIED=object()
definstructions_with_positions(instrs, co_positions):
# Return (instr, positions) pairs from the instrs list and co_positions
# iterator. The latter contains items for cache lines and the former
# doesn't, so those need to be skipped.
co_positions=co_positionsoriter(())
forinstrininstrs:
yieldinstr, next(co_positions, ())
for_, size, _in (instr.cache_infoor ()):
foriinrange(size):
next(co_positions, ())
classBytecodeTestCase(unittest.TestCase):
"""Custom assertion methods for inspecting bytecode."""
defget_disassembly_as_string(self, co):
s=io.StringIO()
dis.dis(co, file=s)
returns.getvalue()
defassertInBytecode(self, x, opname, argval=_UNSPECIFIED):
"""Returns instr if opname is found, otherwise throws AssertionError"""
self.assertIn(opname, dis.opmap)
forinstrindis.get_instructions(x):
ifinstr.opname==opname:
ifargvalis_UNSPECIFIEDorinstr.argval==argval:
returninstr
disassembly=self.get_disassembly_as_string(x)
ifargvalis_UNSPECIFIED:
msg='%s not found in bytecode:\n%s'% (opname, disassembly)
else:
msg='(%s,%r) not found in bytecode:\n%s'
msg=msg% (opname, argval, disassembly)
self.fail(msg)
defassertNotInBytecode(self, x, opname, argval=_UNSPECIFIED):
"""Throws AssertionError if opname is found"""
self.assertIn(opname, dis.opmap)
forinstrindis.get_instructions(x):
ifinstr.opname==opname:
disassembly=self.get_disassembly_as_string(x)
ifargvalis_UNSPECIFIED:
msg='%s occurs in bytecode:\n%s'% (opname, disassembly)
self.fail(msg)
elifinstr.argval==argval:
msg='(%s,%r) occurs in bytecode:\n%s'
msg=msg% (opname, argval, disassembly)
self.fail(msg)
classCompilationStepTestCase(unittest.TestCase):
HAS_ARG=set(dis.hasarg)
HAS_TARGET=set(dis.hasjrel+dis.hasjabs+dis.hasexc)
HAS_ARG_OR_TARGET=HAS_ARG.union(HAS_TARGET)
classLabel:
pass
defassertInstructionsMatch(self, actual_seq, expected):
# get an InstructionSequence and an expected list, where each
# entry is a label or an instruction tuple. Construct an expcted
# instruction sequence and compare with the one given.
self.assertIsInstance(expected, list)
actual=actual_seq.get_instructions()
expected=self.seq_from_insts(expected).get_instructions()
self.assertEqual(len(actual), len(expected))
# compare instructions
foract, expinzip(actual, expected):
ifisinstance(act, int):
self.assertEqual(exp, act)
continue
self.assertIsInstance(exp, tuple)
self.assertIsInstance(act, tuple)
idx=max([p[0] forpinenumerate(exp) ifp[1] !=-1])
self.assertEqual(exp[:idx], act[:idx])
defresolveAndRemoveLabels(self, insts):
idx=0
res= []
foritemininsts:
assertisinstance(item, (self.Label, tuple))
ifisinstance(item, self.Label):
item.value=idx
else:
idx+=1
res.append(item)
returnres
defseq_from_insts(self, insts):
labels= {itemforitemininstsifisinstance(item, self.Label)}
fori, lblinenumerate(labels):
lbl.value=i
seq=_testinternalcapi.new_instruction_sequence()
foritemininsts:
ifisinstance(item, self.Label):
seq.use_label(item.value)
else:
op=item[0]
ifisinstance(op, str):
op=opcode.opmap[op]
arg, *loc=item[1:]
ifisinstance(arg, self.Label):
arg=arg.value
loc=loc+ [-1] * (4-len(loc))
seq.addop(op, argor0, *loc)
returnseq
defcheck_instructions(self, insts):
forinstininsts:
ifisinstance(inst, self.Label):
continue
op, arg, *loc=inst
ifisinstance(op, str):
op=opcode.opmap[op]
self.assertEqual(opinopcode.hasarg,
argisnotNone,
f"{opcode.opname[op]=}{arg=}")
self.assertTrue(all(isinstance(l, int) forlinloc))
@unittest.skipIf(_testinternalcapiisNone, "requires _testinternalcapi")
classCodegenTestCase(CompilationStepTestCase):
defgenerate_code(self, ast):
insts, _=_testinternalcapi.compiler_codegen(ast, "my_file.py", 0)
returninsts
@unittest.skipIf(_testinternalcapiisNone, "requires _testinternalcapi")
classCfgOptimizationTestCase(CompilationStepTestCase):
defget_optimized(self, seq, consts, nlocals=0):
insts=_testinternalcapi.optimize_cfg(seq, consts, nlocals)
returninsts, consts
@unittest.skipIf(_testinternalcapiisNone, "requires _testinternalcapi")
classAssemblerTestCase(CompilationStepTestCase):
defget_code_object(self, filename, insts, metadata):
co=_testinternalcapi.assemble_code_object(filename, insts, metadata)
returnco