Skip to content

Commit 47b3a24

Browse files
committed
Some more Py1k fixes, and added Py3k support
1 parent b11b69c commit 47b3a24

7 files changed

+58
-26
lines changed

bytecode.cpp

+21-18
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ bool Py1k::IsVarNameArg(int opcode)
171171
(opcode == Py1k::STORE_FAST);
172172
}
173173

174-
bool Py1k::IsCellArg(int opcode)
175-
{
176-
return false;
177-
}
178-
179174
bool Py2k::IsConstArg(int opcode)
180175
{
181176
return (opcode == Py2k::LOAD_CONST);
@@ -231,7 +226,7 @@ bool Py3k::IsCellArg(int opcode)
231226
}
232227

233228

234-
static void print_const(PycRef<PycObject> obj)
229+
static void print_const(PycRef<PycObject> obj, PycModule* mod)
235230
{
236231
switch (obj->type()) {
237232
case PycObject::TYPE_STRING:
@@ -241,16 +236,24 @@ static void print_const(PycRef<PycObject> obj)
241236
OutputString(obj.cast<PycString>(), QS_Double);
242237
printf("\"");
243238
break;
239+
case PycObject::TYPE_UNICODE:
240+
if (mod->majorVer() == 3)
241+
printf("\"");
242+
else
243+
printf("u\"");
244+
OutputString(obj.cast<PycString>(), QS_Double);
245+
printf("\"");
246+
break;
244247
case PycObject::TYPE_TUPLE:
245248
{
246249
printf("(");
247250
PycTuple::value_t values = obj.cast<PycTuple>()->values();
248251
PycTuple::value_t::iterator it = values.begin();
249252
if (it != values.end()) {
250-
print_const(*it);
253+
print_const(*it, mod);
251254
while (++it != values.end()) {
252255
printf(", ");
253-
print_const(*it);
256+
print_const(*it, mod);
254257
}
255258
}
256259
printf(")");
@@ -262,10 +265,10 @@ static void print_const(PycRef<PycObject> obj)
262265
PycList::value_t values = obj.cast<PycList>()->values();
263266
PycList::value_t::iterator it = values.begin();
264267
if (it != values.end()) {
265-
print_const(*it);
268+
print_const(*it, mod);
266269
while (++it != values.end()) {
267270
printf(", ");
268-
print_const(*it);
271+
print_const(*it, mod);
269272
}
270273
}
271274
printf("]");
@@ -279,15 +282,15 @@ static void print_const(PycRef<PycObject> obj)
279282
PycDict::key_t::iterator ki = keys.begin();
280283
PycDict::value_t::iterator vi = values.begin();
281284
if (ki != keys.end()) {
282-
print_const(*ki);
285+
print_const(*ki, mod);
283286
printf(": ");
284-
print_const(*vi);
287+
print_const(*vi, mod);
285288
while (++ki != keys.end()) {
286289
++vi;
287290
printf(", ");
288-
print_const(*ki);
291+
print_const(*ki, mod);
289292
printf(": ");
290-
print_const(*vi);
293+
print_const(*vi, mod);
291294
}
292295
}
293296
printf("}");
@@ -353,20 +356,20 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
353356
(mod->majorVer() == 2 && Py2k::IsConstArg(opcode)) ||
354357
(mod->majorVer() == 3 && Py3k::IsConstArg(opcode))) {
355358
printf("%d: ", operand);
356-
print_const(code->getConst(operand));
359+
print_const(code->getConst(operand), mod);
357360
} else if ((mod->majorVer() == 1 && Py1k::IsNameArg(opcode)) ||
361+
(mod->majorVer() == 1 && mod->minorVer() < 4 && Py1k::IsVarNameArg(opcode)) ||
358362
(mod->majorVer() == 2 && Py2k::IsNameArg(opcode)) ||
359363
(mod->majorVer() == 3 && Py3k::IsNameArg(opcode))) {
360364
printf("%d: %s", operand, code->getName(operand)->value());
361365
} else if ((mod->majorVer() == 1 && Py1k::IsVarNameArg(opcode)) ||
362366
(mod->majorVer() == 2 && Py2k::IsVarNameArg(opcode)) ||
363367
(mod->majorVer() == 3 && Py3k::IsVarNameArg(opcode))) {
364368
printf("%d: %s", operand, code->getVarName(operand)->value());
365-
} else if ((mod->majorVer() == 1 && Py1k::IsCellArg(opcode)) ||
366-
(mod->majorVer() == 2 && Py2k::IsCellArg(opcode)) ||
369+
} else if ((mod->majorVer() == 2 && Py2k::IsCellArg(opcode)) ||
367370
(mod->majorVer() == 3 && Py3k::IsCellArg(opcode))) {
368371
printf("%d: ", operand);
369-
print_const(code->getConst(operand));
372+
print_const(code->getConst(operand), mod);
370373
} else {
371374
printf("%d", operand);
372375
}

bytecode.h

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ extern const char* OpcodeNames[256];
3838
bool IsConstArg(int opcode);
3939
bool IsNameArg(int opcode);
4040
bool IsVarNameArg(int opcode);
41-
bool IsCellArg(int opcode);
4241

4342
}
4443

object.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ PycRef<PycObject> CreateObject(int type)
5757
case PycObject::TYPE_CODE:
5858
case PycObject::TYPE_CODE2:
5959
return new PycCode();
60-
//case PycObject::TYPE_UNICODE:
61-
// ...
60+
case PycObject::TYPE_UNICODE:
61+
return new PycUnicode();
6262
//case PycObject::TYPE_SET:
6363
// ...
6464
//case PycObject::TYPE_FROZENSET:

object.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef _PYC_OBJECT_H
22
#define _PYC_OBJECT_H
33

4-
54
template <class _Obj>
65
class PycRef {
76
public:

pycdas.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
7474
OutputString(obj.cast<PycString>(), QS_Double);
7575
printf("\"\n");
7676
break;
77+
case PycObject::TYPE_UNICODE:
78+
if (mod->majorVer() == 3)
79+
iprintf(indent, "\"");
80+
else
81+
iprintf(indent, "u\"");
82+
OutputString(obj.cast<PycString>(), QS_Double);
83+
printf("\"\n");
84+
break;
7785
case PycObject::TYPE_TUPLE:
7886
{
7987
iprintf(indent, "(\n");
@@ -102,13 +110,20 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
102110
while (ki != keys.end()) {
103111
output_object(*ki, mod, indent + 1);
104112
output_object(*vi, mod, indent + 2);
113+
++ki, ++vi;
105114
}
106115
iprintf(indent, "}\n");
107116
}
108117
break;
109118
case PycObject::TYPE_NONE:
110119
iprintf(indent, "None\n");
111120
break;
121+
case PycObject::TYPE_FALSE:
122+
iprintf(indent, "False\n");
123+
break;
124+
case PycObject::TYPE_TRUE:
125+
iprintf(indent, "True\n");
126+
break;
112127
case PycObject::TYPE_INT:
113128
iprintf(indent, "%d\n", obj.cast<PycInt>()->value());
114129
break;

string.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ bool PycString::isEqual(PycRef<PycObject> obj) const
4545
void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
4646
{
4747
const char* ch = str->value();
48+
int len = str->length();
4849
if (ch == 0)
4950
return;
50-
while (*ch != 0) {
51-
if (*ch < 0x20) {
51+
while (len--) {
52+
if (*ch < 0x20 || *ch == 0x7F) {
5253
if (*ch == '\r') {
5354
fprintf(F, "\\r");
5455
} else if (*ch == '\n') {
@@ -61,8 +62,13 @@ void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
6162
} else {
6263
fprintf(F, "\\x%x", *ch);
6364
}
64-
} else if (*ch >= 0x7F) {
65-
fprintf(F, "\\x%x", *ch);
65+
} else if (*ch >= 0x80) {
66+
if (str->type() == PycObject::TYPE_UNICODE) {
67+
// Unicode stored as UTF-8... Let the stream interpret it
68+
fputc(*ch, F);
69+
} else {
70+
fprintf(F, "\\x%x", *ch);
71+
}
6672
} else {
6773
if (style == QS_Single && *ch == '\'')
6874
fprintf(F, "\\'");

string.h

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class PycString : public PycObject {
3333
int m_length;
3434
};
3535

36+
class PycUnicode : public PycString {
37+
public:
38+
PycUnicode(int type = TYPE_UNICODE) : PycString(type) { }
39+
40+
bool isType(int type) const
41+
{
42+
return (type == TYPE_UNICODE) || PycString::isType(type);
43+
}
44+
};
45+
3646
void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F = stdout);
3747

3848
#endif

0 commit comments

Comments
 (0)