From 7ffa2562c3a61565cc5ae2a39d815148669f8f0d Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Fri, 3 Sep 2010 21:50:35 -0700 Subject: [PATCH] Merge uncommitted changes from old SVN copy --- ASTree.cpp | 62 +++++++++++++++++++++++++++++++++++++++++------------- pycdas.cpp | 51 ++++++++++++++++++++++++++++++++++++++------ pycdc.cpp | 11 +++++++++- string.cpp | 10 +++++++-- 4 files changed, 109 insertions(+), 25 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 49b0b1c..c0a476c 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -152,11 +152,11 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) case (PY_2000 | Py2k::BINARY_SUBSCR): case (PY_3000 | Py3k::BINARY_SUBSCR): { - PycRef right = stack.top(); + PycRef subscr = stack.top(); stack.pop(); - PycRef left = stack.top(); + PycRef src = stack.top(); stack.pop(); - stack.push(new ASTBinary(left, right, ASTBinary::BIN_SUBSCR)); + stack.push(new ASTSubscr(src, subscr)); } break; case (PY_1000 | Py1k::BINARY_SUBTRACT): @@ -524,6 +524,13 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } } break; + case (PY_1000 | Py1k::UNARY_CALL): + { + PycRef func = stack.top(); + stack.pop(); + stack.push(new ASTCall(func, ASTCall::pparam_t(), ASTCall::kwparam_t())); + } + break; case (PY_1000 | Py1k::UNARY_INVERT): case (PY_2000 | Py2k::UNARY_INVERT): case (PY_3000 | Py3k::UNARY_INVERT): @@ -830,17 +837,25 @@ void print_src(PycRef node, PycModule* mod, int indent) } else if (src->type() == ASTNode::NODE_IMPORT) { PycRef import = src.cast(); if (import->fromlist() != Node_NULL) { - PycRef fromlist = import->fromlist().cast()->object().cast(); - if (fromlist != Pyc_None && fromlist->size() != 0) { + PycRef fromlist = import->fromlist().cast()->object(); + if (fromlist != Pyc_None) { printf("from "); - print_src(import->name(), mod, indent); + if (import->name()->type() == ASTObject::NODE_IMPORT) + print_src(import->name().cast()->name(), mod, indent); + else + print_src(import->name(), mod, indent); printf(" import "); - bool first = true; - PycTuple::value_t::const_iterator ii = fromlist->values().begin(); - for (; ii != fromlist->values().end(); ++ii) { - if (!first) printf(", "); - printf("%s", ii->cast()->value()); - first = false; + if (fromlist->type() == ASTObject::NODE_TUPLE) { + bool first = true; + PycTuple::value_t::const_iterator ii = fromlist.cast()->values().begin(); + for (; ii != fromlist.cast()->values().end(); ++ii) { + if (!first) + printf(", "); + printf("%s", ii->cast()->value()); + first = false; + } + } else { + printf("%s", fromlist.cast()->value()); } } else { printf("import "); @@ -851,9 +866,26 @@ void print_src(PycRef node, PycModule* mod, int indent) print_src(import->name(), mod, indent); } } else { - print_src(dest, mod, indent); - printf(" = "); - print_src(src, mod, indent); + if (dest->type() == ASTNode::NODE_NAME && + dest.cast()->name()->isEqual("__doc__")) { + if (src->type() == ASTNode::NODE_OBJECT) { + PycRef obj = src.cast()->object(); + if (obj->type() == PycObject::TYPE_STRING || + obj->type() == PycObject::TYPE_INTERNED || + obj->type() == PycObject::TYPE_STRINGREF) + OutputString(obj.cast(), (mod->majorVer() == 3) ? 'b' : 0, true); + else if (obj->type() == PycObject::TYPE_UNICODE) + OutputString(obj.cast(), (mod->majorVer() == 3) ? 0 : 'u', true); + } else { + print_src(dest, mod, indent); + printf(" = "); + print_src(src, mod, indent); + } + } else { + print_src(dest, mod, indent); + printf(" = "); + print_src(src, mod, indent); + } } } break; diff --git a/pycdas.cpp b/pycdas.cpp index cec2f9f..3550315 100644 --- a/pycdas.cpp +++ b/pycdas.cpp @@ -5,6 +5,48 @@ #include "bytecode.h" #include "numeric.h" +#ifdef WIN32 +# define PATHSEP '\\' +#else +# define PATHSEP '/' +#endif + +static const char* flag_names[] = { + "CO_OPTIMIZED", "CO_NEWLOCALS", "CO_VARARGS", "CO_VARKEYWORDS", + "CO_NESTED", "CO_GENERATOR", "CO_NOFREE", "<0x80>", "<0x100>", "<0x200>", + "<0x400>", "<0x800>", "CO_GENERATOR_ALLOWED", "CO_FUTURE_DIVISION", + "CO_FUTURE_ABSOLUTE_IMPORT", "CO_FUTURE_WITH_STATEMENT", + "CO_FUTURE_PRINT_FUNCTION", "CO_FUTURE_UNICODE_LITERALS", + "CO_FUTURE_BARRY_AS_BDFL", "<0x80000>", "<0x100000>", "<0x200000>", + "<0x400000>", "<0x800000>", "<0x1000000>", "<0x2000000>", "<0x4000000>", + "<0x8000000>", "<0x10000000>", "<0x20000000>", "<0x40000000>", + "<0x80000000>" +}; + +static void print_coflags(unsigned long flags) +{ + if (flags == 0) { + printf("\n"); + return; + } + + printf(" ("); + unsigned long f = 1; + int k = 0; + while (k < 32) { + if ((flags & f) != 0) { + flags &= ~f; + if (flags == 0) + printf("%s", flag_names[k]); + else + printf("%s | ", flag_names[k]); + } + ++k; + f <<= 1; + } + printf(")\n"); +} + static void ivprintf(int indent, const char* fmt, va_list varargs) { for (int i=0; i obj, PycModule* mod, int indent) iprintf(indent + 1, "Arg Count: %d\n", codeObj->argCount()); iprintf(indent + 1, "Locals: %d\n", codeObj->numLocals()); iprintf(indent + 1, "Stack Size: %d\n", codeObj->stackSize()); - iprintf(indent + 1, "Flags: 0x%08X\n", codeObj->flags()); + iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags()); + print_coflags(codeObj->flags()); if (codeObj->names() != Pyc_NULL) { iprintf(indent + 1, "[Names]\n"); @@ -154,12 +197,6 @@ void output_object(PycRef obj, PycModule* mod, int indent) } } -#ifdef WIN32 -# define PATHSEP '\\' -#else -# define PATHSEP '/' -#endif - int main(int argc, char* argv[]) { if (argc < 2) { diff --git a/pycdc.cpp b/pycdc.cpp index 3fa321d..0f35b32 100644 --- a/pycdc.cpp +++ b/pycdc.cpp @@ -1,5 +1,12 @@ +#include #include "ASTree.h" +#ifdef WIN32 +# define PATHSEP '\\' +#else +# define PATHSEP '/' +#endif + int main(int argc, char* argv[]) { if (argc < 2) { @@ -13,8 +20,10 @@ int main(int argc, char* argv[]) fprintf(stderr, "Could not load file %s\n", argv[1]); return 1; } + const char* dispname = strrchr(argv[1], PATHSEP); + dispname = (dispname == NULL) ? argv[1] : dispname + 1; printf("# Source Generated with Decompyle++\n"); - printf("# File: %s (Python %d.%d%s)\n", argv[1], mod.majorVer(), mod.minorVer(), + printf("# File: %s (Python %d.%d%s)\n", dispname, mod.majorVer(), mod.minorVer(), (mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : ""); decompyle(mod.code(), &mod); diff --git a/string.cpp b/string.cpp index 037b6fb..6fcdbb7 100644 --- a/string.cpp +++ b/string.cpp @@ -76,7 +76,10 @@ void OutputString(PycRef str, char prefix, bool triple, FILE* F) len = str->length(); // Output the string - fputc(useQuotes ? '"' : '\'', F); + if (triple) + fprintf(F, useQuotes ? "\"\"\"" : "'''"); + else + fputc(useQuotes ? '"' : '\'', F); while (len--) { if (*ch < 0x20 || *ch == 0x7F) { if (*ch == '\r') { @@ -108,5 +111,8 @@ void OutputString(PycRef str, char prefix, bool triple, FILE* F) } ch++; } - fputc(useQuotes ? '"' : '\'', F); + if (triple) + fprintf(F, useQuotes ? "\"\"\"" : "'''"); + else + fputc(useQuotes ? '"' : '\'', F); }