forked from zrax/pycdc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyc_module.cpp
189 lines (172 loc) · 3.75 KB
/
pyc_module.cpp
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
#include "pyc_module.h"
#include "data.h"
void PycModule::setVersion(unsigned int magic)
{
// Default for versions that don't support unicode selection
m_unicode = false;
switch (magic) {
case MAGIC_1_0:
m_maj = 1;
m_min = 0;
break;
case MAGIC_1_1:
m_maj = 1;
m_min = 1;
break;
case MAGIC_1_3:
m_maj = 1;
m_min = 3;
break;
case MAGIC_1_4:
m_maj = 1;
m_min = 4;
break;
case MAGIC_1_5:
m_maj = 1;
m_min = 5;
break;
/* Starting with 1.6, Python adds +1 for unicode mode (-U) */
case MAGIC_1_6+1:
m_unicode = true;
/* Fall through */
case MAGIC_1_6:
m_maj = 1;
m_min = 6;
break;
case MAGIC_2_0+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_0:
m_maj = 2;
m_min = 0;
break;
case MAGIC_2_1+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_1:
m_maj = 2;
m_min = 1;
break;
case MAGIC_2_2+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_2:
m_maj = 2;
m_min = 2;
break;
case MAGIC_2_3+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_3:
m_maj = 2;
m_min = 3;
break;
case MAGIC_2_4+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_4:
m_maj = 2;
m_min = 4;
break;
case MAGIC_2_5+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_5:
m_maj = 2;
m_min = 5;
break;
case MAGIC_2_6+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_6:
m_maj = 2;
m_min = 6;
break;
case MAGIC_2_7+1:
m_unicode = true;
/* Fall through */
case MAGIC_2_7:
m_maj = 2;
m_min = 7;
break;
/* 3.0 and above are always unicode */
case MAGIC_3_0+1:
m_maj = 3;
m_min = 0;
m_unicode = true;
break;
case MAGIC_3_1+1:
m_maj = 3;
m_min = 1;
m_unicode = true;
break;
/* 3.2 stops using the unicode increment */
case MAGIC_3_2:
m_maj = 3;
m_min = 2;
m_unicode = true;
break;
case MAGIC_3_3:
m_maj = 3;
m_min = 3;
m_unicode = true;
break;
case MAGIC_3_4:
m_maj = 3;
m_min = 4;
m_unicode = true;
break;
case MAGIC_3_5:
/* fall through */
case MAGIC_3_5_3:
m_maj = 3;
m_min = 5;
m_unicode = true;
break;
case MAGIC_3_6:
m_maj = 3;
m_min = 6;
m_unicode = true;
break;
case MAGIC_3_7:
m_maj = 3;
m_min = 7;
m_unicode = true;
break;
/* Bad Magic detected */
default:
m_maj = -1;
m_min = -1;
}
}
void PycModule::loadFromFile(const char* filename)
{
PycFile in(filename);
if (!in.isOpen()) {
fprintf(stderr, "Error opening file %s\n", filename);
return;
}
setVersion(in.get32());
if (!isValid()) {
fprintf(stderr, "Bad MAGIC!\n");
return;
}
in.get32(); // Timestamp -- who cares?
if (verCompare(3, 3) >= 0)
in.get32(); // Size parameter added in Python 3.3
m_code = LoadObject(&in, this).cast<PycCode>();
}
PycRef<PycString> PycModule::getIntern(int ref) const
{
std::list<PycRef<PycString> >::const_iterator it = m_interns.begin();
while (ref--)
++it;
return *it;
}
PycRef<PycObject> PycModule::getRef(int ref) const
{
std::list<PycRef<PycObject> >::const_iterator it = m_refs.begin();
while (ref--)
++it;
return *it;
}