Skip to content

Commit

Permalink
Some more Py1k fixes, and added Py3k support
Browse files Browse the repository at this point in the history
  • Loading branch information
zrax committed Jul 25, 2009
1 parent b11b69c commit 47b3a24
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 26 deletions.
39 changes: 21 additions & 18 deletions bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ bool Py1k::IsVarNameArg(int opcode)
(opcode == Py1k::STORE_FAST);
}

bool Py1k::IsCellArg(int opcode)
{
return false;
}

bool Py2k::IsConstArg(int opcode)
{
return (opcode == Py2k::LOAD_CONST);
Expand Down Expand Up @@ -231,7 +226,7 @@ bool Py3k::IsCellArg(int opcode)
}


static void print_const(PycRef<PycObject> obj)
static void print_const(PycRef<PycObject> obj, PycModule* mod)
{
switch (obj->type()) {
case PycObject::TYPE_STRING:
Expand All @@ -241,16 +236,24 @@ static void print_const(PycRef<PycObject> obj)
OutputString(obj.cast<PycString>(), QS_Double);
printf("\"");
break;
case PycObject::TYPE_UNICODE:
if (mod->majorVer() == 3)
printf("\"");
else
printf("u\"");
OutputString(obj.cast<PycString>(), QS_Double);
printf("\"");
break;
case PycObject::TYPE_TUPLE:
{
printf("(");
PycTuple::value_t values = obj.cast<PycTuple>()->values();
PycTuple::value_t::iterator it = values.begin();
if (it != values.end()) {
print_const(*it);
print_const(*it, mod);
while (++it != values.end()) {
printf(", ");
print_const(*it);
print_const(*it, mod);
}
}
printf(")");
Expand All @@ -262,10 +265,10 @@ static void print_const(PycRef<PycObject> obj)
PycList::value_t values = obj.cast<PycList>()->values();
PycList::value_t::iterator it = values.begin();
if (it != values.end()) {
print_const(*it);
print_const(*it, mod);
while (++it != values.end()) {
printf(", ");
print_const(*it);
print_const(*it, mod);
}
}
printf("]");
Expand All @@ -279,15 +282,15 @@ static void print_const(PycRef<PycObject> obj)
PycDict::key_t::iterator ki = keys.begin();
PycDict::value_t::iterator vi = values.begin();
if (ki != keys.end()) {
print_const(*ki);
print_const(*ki, mod);
printf(": ");
print_const(*vi);
print_const(*vi, mod);
while (++ki != keys.end()) {
++vi;
printf(", ");
print_const(*ki);
print_const(*ki, mod);
printf(": ");
print_const(*vi);
print_const(*vi, mod);
}
}
printf("}");
Expand Down Expand Up @@ -353,20 +356,20 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
(mod->majorVer() == 2 && Py2k::IsConstArg(opcode)) ||
(mod->majorVer() == 3 && Py3k::IsConstArg(opcode))) {
printf("%d: ", operand);
print_const(code->getConst(operand));
print_const(code->getConst(operand), mod);
} else if ((mod->majorVer() == 1 && Py1k::IsNameArg(opcode)) ||
(mod->majorVer() == 1 && mod->minorVer() < 4 && Py1k::IsVarNameArg(opcode)) ||
(mod->majorVer() == 2 && Py2k::IsNameArg(opcode)) ||
(mod->majorVer() == 3 && Py3k::IsNameArg(opcode))) {
printf("%d: %s", operand, code->getName(operand)->value());
} else if ((mod->majorVer() == 1 && Py1k::IsVarNameArg(opcode)) ||
(mod->majorVer() == 2 && Py2k::IsVarNameArg(opcode)) ||
(mod->majorVer() == 3 && Py3k::IsVarNameArg(opcode))) {
printf("%d: %s", operand, code->getVarName(operand)->value());
} else if ((mod->majorVer() == 1 && Py1k::IsCellArg(opcode)) ||
(mod->majorVer() == 2 && Py2k::IsCellArg(opcode)) ||
} else if ((mod->majorVer() == 2 && Py2k::IsCellArg(opcode)) ||
(mod->majorVer() == 3 && Py3k::IsCellArg(opcode))) {
printf("%d: ", operand);
print_const(code->getConst(operand));
print_const(code->getConst(operand), mod);
} else {
printf("%d", operand);
}
Expand Down
1 change: 0 additions & 1 deletion bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ extern const char* OpcodeNames[256];
bool IsConstArg(int opcode);
bool IsNameArg(int opcode);
bool IsVarNameArg(int opcode);
bool IsCellArg(int opcode);

}

Expand Down
4 changes: 2 additions & 2 deletions object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ PycRef<PycObject> CreateObject(int type)
case PycObject::TYPE_CODE:
case PycObject::TYPE_CODE2:
return new PycCode();
//case PycObject::TYPE_UNICODE:
// ...
case PycObject::TYPE_UNICODE:
return new PycUnicode();
//case PycObject::TYPE_SET:
// ...
//case PycObject::TYPE_FROZENSET:
Expand Down
1 change: 0 additions & 1 deletion object.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _PYC_OBJECT_H
#define _PYC_OBJECT_H


template <class _Obj>
class PycRef {
public:
Expand Down
15 changes: 15 additions & 0 deletions pycdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
OutputString(obj.cast<PycString>(), QS_Double);
printf("\"\n");
break;
case PycObject::TYPE_UNICODE:
if (mod->majorVer() == 3)
iprintf(indent, "\"");
else
iprintf(indent, "u\"");
OutputString(obj.cast<PycString>(), QS_Double);
printf("\"\n");
break;
case PycObject::TYPE_TUPLE:
{
iprintf(indent, "(\n");
Expand Down Expand Up @@ -102,13 +110,20 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
while (ki != keys.end()) {
output_object(*ki, mod, indent + 1);
output_object(*vi, mod, indent + 2);
++ki, ++vi;
}
iprintf(indent, "}\n");
}
break;
case PycObject::TYPE_NONE:
iprintf(indent, "None\n");
break;
case PycObject::TYPE_FALSE:
iprintf(indent, "False\n");
break;
case PycObject::TYPE_TRUE:
iprintf(indent, "True\n");
break;
case PycObject::TYPE_INT:
iprintf(indent, "%d\n", obj.cast<PycInt>()->value());
break;
Expand Down
14 changes: 10 additions & 4 deletions string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ bool PycString::isEqual(PycRef<PycObject> obj) const
void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
{
const char* ch = str->value();
int len = str->length();
if (ch == 0)
return;
while (*ch != 0) {
if (*ch < 0x20) {
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
if (*ch == '\r') {
fprintf(F, "\\r");
} else if (*ch == '\n') {
Expand All @@ -61,8 +62,13 @@ void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F)
} else {
fprintf(F, "\\x%x", *ch);
}
} else if (*ch >= 0x7F) {
fprintf(F, "\\x%x", *ch);
} else if (*ch >= 0x80) {
if (str->type() == PycObject::TYPE_UNICODE) {
// Unicode stored as UTF-8... Let the stream interpret it
fputc(*ch, F);
} else {
fprintf(F, "\\x%x", *ch);
}
} else {
if (style == QS_Single && *ch == '\'')
fprintf(F, "\\'");
Expand Down
10 changes: 10 additions & 0 deletions string.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class PycString : public PycObject {
int m_length;
};

class PycUnicode : public PycString {
public:
PycUnicode(int type = TYPE_UNICODE) : PycString(type) { }

bool isType(int type) const
{
return (type == TYPE_UNICODE) || PycString::isType(type);
}
};

void OutputString(PycRef<PycString> str, QuoteStyle style, FILE* F = stdout);

#endif

0 comments on commit 47b3a24

Please sign in to comment.