-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpyc_code.cpp
More file actions
193 lines (159 loc) · 6.53 KB
/
pyc_code.cpp
File metadata and controls
193 lines (159 loc) · 6.53 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
#include "pyc_code.h"
#include "pyc_module.h"
#include "data.h"
/* == Marshal structure for Code object ==
1.0 1.3 1.5 2.1 2.3 3.0 3.8 3.11
argcount short short short long long long long
posonlyargc long long
kwonlyargc long long long
nlocals short short short long long long
stacksize short short long long long long
flags short short short long long long long
code Obj Obj Obj Obj Obj Obj Obj Obj
consts Obj Obj Obj Obj Obj Obj Obj Obj
names Obj Obj Obj Obj Obj Obj Obj Obj
varnames Obj Obj Obj Obj Obj Obj
freevars Obj Obj Obj Obj
cellvars Obj Obj Obj Obj
locals+names Obj
locals+kinds Obj
filename Obj Obj Obj Obj Obj Obj Obj Obj
name Obj Obj Obj Obj Obj Obj Obj Obj
qualname Obj
firstline short short long long long long
lntable Obj Obj Obj Obj Obj Obj
exceptiontable Obj
*/
void PycCode::load(PycData* stream, PycModule* mod)
{
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
m_argCount = stream->get16();
else if (mod->verCompare(2, 3) >= 0)
m_argCount = stream->get32();
if (mod->verCompare(3, 8) >= 0)
m_posOnlyArgCount = stream->get32();
else
m_posOnlyArgCount = 0;
if (mod->majorVer() >= 3)
m_kwOnlyArgCount = stream->get32();
else
m_kwOnlyArgCount = 0;
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
m_numLocals = stream->get16();
else if (mod->verCompare(2, 3) >= 0 && mod->verCompare(3, 11) < 0)
m_numLocals = stream->get32();
else
m_numLocals = 0;
if (mod->verCompare(1, 5) >= 0 && mod->verCompare(2, 3) < 0)
m_stackSize = stream->get16();
else if (mod->verCompare(2, 3) >= 0)
m_stackSize = stream->get32();
else
m_stackSize = 0;
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
m_flags = stream->get16();
else if (mod->verCompare(2, 3) >= 0)
m_flags = stream->get32();
else
m_flags = 0;
if (mod->verCompare(3, 8) < 0) {
// Remap flags to new values introduced in 3.8
if (m_flags & 0xF0000000)
throw std::runtime_error("Cannot remap unexpected flags");
m_flags = (m_flags & 0xFFFF) | ((m_flags & 0xFFF0000) << 4);
}
m_code = LoadObject(stream, mod).cast<PycString>();
m_consts = LoadObject(stream, mod).cast<PycSequence>();
m_names = LoadObject(stream, mod).cast<PycSequence>();
if (mod->verCompare(1, 3) >= 0)
m_localNames = LoadObject(stream, mod).cast<PycSequence>();
else
m_localNames = new PycTuple;
if (mod->verCompare(3, 11) >= 0)
m_localKinds = LoadObject(stream, mod).cast<PycString>();
else
m_localKinds = new PycString;
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0)
m_freeVars = LoadObject(stream, mod).cast<PycSequence>();
else
m_freeVars = new PycTuple;
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0)
m_cellVars = LoadObject(stream, mod).cast<PycSequence>();
else
m_cellVars = new PycTuple;
m_fileName = LoadObject(stream, mod).cast<PycString>();
m_name = LoadObject(stream, mod).cast<PycString>();
if (mod->verCompare(3, 11) >= 0)
m_qualName = LoadObject(stream, mod).cast<PycString>();
else
m_qualName = new PycString;
if (mod->verCompare(1, 5) >= 0 && mod->verCompare(2, 3) < 0)
m_firstLine = stream->get16();
else if (mod->verCompare(2, 3) >= 0)
m_firstLine = stream->get32();
if (mod->verCompare(1, 5) >= 0)
m_lnTable = LoadObject(stream, mod).cast<PycString>();
else
m_lnTable = new PycString;
if (mod->verCompare(3, 11) >= 0)
m_exceptTable = LoadObject(stream, mod).cast<PycString>();
else
m_exceptTable = new PycString;
}
PycRef<PycString> PycCode::getCellVar(PycModule* mod, int idx) const
{
if (mod->verCompare(3, 11) >= 0)
return getLocal(idx);
return (idx >= m_cellVars->size())
? m_freeVars->get(idx - m_cellVars->size()).cast<PycString>()
: m_cellVars->get(idx).cast<PycString>();
}
// Helper for varint parsing, compatible with both implementations.
int _parse_varint(PycBuffer& data, int& pos) {
int b = data.getByte();
pos += 1;
int val = b & 0x3F;
while (b & 0x40) {
val <<= 6;
b = data.getByte();
pos += 1;
val |= (b & 0x3F);
}
return val;
}
// Tuple-style exception table entries
std::vector<PycCode::exception_table_entry_t> PycCode::exceptTableEntries() const
{
PycBuffer data(m_exceptTable->value(), m_exceptTable->length());
std::vector<exception_table_entry_t> entries;
int pos = 0;
while (!data.atEof()) {
int start = _parse_varint(data, pos) * 2;
int length = _parse_varint(data, pos) * 2;
int end = start + length;
int target = _parse_varint(data, pos) * 2;
int dl = _parse_varint(data, pos);
int depth = dl >> 1;
bool lasti = bool(dl & 1);
entries.emplace_back(start, end, target, depth, lasti);
}
return entries;
}
// Legacy class-style exception table entries
std::vector<PycExceptionTableEntry> PycCode::exceptionTableEntries() const
{
PycBuffer data(m_exceptTable->value(), m_exceptTable->length());
std::vector<PycExceptionTableEntry> entries;
int pos = 0;
while (!data.atEof()) {
int start = _parse_varint(data, pos) * 2;
int length = _parse_varint(data, pos) * 2;
int end = start + length;
int target = _parse_varint(data, pos) * 2;
int dl = _parse_varint(data, pos);
int depth = dl >> 1;
bool lasti = bool(dl & 1);
entries.push_back(PycExceptionTableEntry(start, end, target, depth, lasti));
}
return entries;
}