Use fputs instead of fprintf when no formatting is required

This commit is contained in:
Michael Hansen
2018-01-28 10:32:44 -08:00
parent 98ad031109
commit a9a362254e
6 changed files with 183 additions and 176 deletions

View File

@@ -1703,7 +1703,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -1739,7 +1739,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -1777,7 +1777,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -1836,7 +1836,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -1876,7 +1876,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -1991,7 +1991,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.push(tuple.cast<ASTNode>());
} else {
fprintf(stderr, "Something TERRIBLE happened!\n");
fputs("Something TERRIBLE happened!\n", stderr);
}
if (--unpack <= 0) {
@@ -2096,7 +2096,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
if (stack_hist.size()) {
fprintf(stderr, "Warning: Stack history is not empty!\n");
fputs("Warning: Stack history is not empty!\n", stderr);
while (stack_hist.size()) {
stack_hist.pop();
@@ -2104,7 +2104,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
if (blocks.size() > 1) {
fprintf(stderr, "Warning: block stack is not empty!\n");
fputs("Warning: block stack is not empty!\n", stderr);
while (blocks.size() > 1) {
PycRef<ASTBlock> tmp = blocks.top();
@@ -2175,17 +2175,17 @@ static void print_ordered(PycRef<ASTNode> parent, PycRef<ASTNode> child,
if (child.type() == ASTNode::NODE_BINARY ||
child.type() == ASTNode::NODE_COMPARE) {
if (cmp_prec(parent, child) > 0) {
fprintf(pyc_output, "(");
fputs("(", pyc_output);
print_src(child, mod);
fprintf(pyc_output, ")");
fputs(")", pyc_output);
} else {
print_src(child, mod);
}
} else if (child.type() == ASTNode::NODE_UNARY) {
if (cmp_prec(parent, child) > 0) {
fprintf(pyc_output, "(");
fputs("(", pyc_output);
print_src(child, mod);
fprintf(pyc_output, ")");
fputs(")", pyc_output);
} else {
print_src(child, mod);
}
@@ -2199,14 +2199,14 @@ static void start_line(int indent)
if (inPrint || inLambda)
return;
for (int i=0; i<indent; i++)
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
}
static void end_line()
{
if (inPrint || inLambda)
return;
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
}
int cur_indent = -1;
@@ -2233,7 +2233,7 @@ static void print_block(PycRef<ASTBlock> blk, PycModule* mod) {
void print_src(PycRef<ASTNode> node, PycModule* mod)
{
if (node == NULL) {
fprintf(pyc_output, "None");
fputs("None", pyc_output);
cleanBuild = true;
return;
}
@@ -2259,57 +2259,57 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
{
PycRef<ASTCall> call = node.cast<ASTCall>();
print_src(call->func(), mod);
fprintf(pyc_output, "(");
fputs("(", pyc_output);
bool first = true;
for (ASTCall::pparam_t::const_iterator p = call->pparams().begin(); p != call->pparams().end(); ++p) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(*p, mod);
first = false;
}
for (ASTCall::kwparam_t::const_iterator p = call->kwparams().begin(); p != call->kwparams().end(); ++p) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "%s = ", p->first.cast<ASTName>()->name()->value());
print_src(p->second, mod);
first = false;
}
if (call->hasVar()) {
if (!first)
fprintf(pyc_output, ", ");
fprintf(pyc_output, "*");
fputs(", ", pyc_output);
fputs("*", pyc_output);
print_src(call->var(), mod);
first = false;
}
if (call->hasKW()) {
if (!first)
fprintf(pyc_output, ", ");
fprintf(pyc_output, "**");
fputs(", ", pyc_output);
fputs("**", pyc_output);
print_src(call->var(), mod);
first = false;
}
fprintf(pyc_output, ")");
fputs(")", pyc_output);
}
break;
case ASTNode::NODE_DELETE:
{
fprintf(pyc_output, "del ");
fputs("del ", pyc_output);
print_src(node.cast<ASTDelete>()->value(), mod);
}
break;
case ASTNode::NODE_EXEC:
{
PycRef<ASTExec> exec = node.cast<ASTExec>();
fprintf(pyc_output, "exec ");
fputs("exec ", pyc_output);
print_src(exec->statement(), mod);
if (exec->globals() != NULL) {
fprintf(pyc_output, " in ");
fputs(" in ", pyc_output);
print_src(exec->globals(), mod);
if (exec->locals() != NULL
&& exec->globals() != exec->locals()) {
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(exec->locals(), mod);
}
}
@@ -2321,20 +2321,20 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
case ASTNode::NODE_LIST:
{
ASTList::value_t values = node.cast<ASTList>()->values();
fprintf(pyc_output, "[");
fputs("[", pyc_output);
bool first = true;
cur_indent++;
for (ASTList::value_t::const_iterator b = values.begin(); b != values.end(); ++b) {
if (first)
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
else
fprintf(pyc_output, ",\n");
fputs(",\n", pyc_output);
start_line(cur_indent);
print_src(*b, mod);
first = false;
}
cur_indent--;
fprintf(pyc_output, "]");
fputs("]", pyc_output);
}
break;
case ASTNode::NODE_COMPREHENSION:
@@ -2342,37 +2342,37 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
PycRef<ASTComprehension> comp = node.cast<ASTComprehension>();
ASTComprehension::generator_t values = comp->generators();
fprintf(pyc_output, "[ ");
fputs("[ ", pyc_output);
print_src(comp->result(), mod);
for (ASTComprehension::generator_t::const_iterator it = values.begin(); it != values.end(); ++it) {
fprintf(pyc_output, " for ");
fputs(" for ", pyc_output);
print_src((*it)->index(), mod);
fprintf(pyc_output, " in ");
fputs(" in ", pyc_output);
print_src((*it)->iter(), mod);
}
fprintf(pyc_output, " ]");
fputs(" ]", pyc_output);
}
break;
case ASTNode::NODE_MAP:
{
ASTMap::map_t values = node.cast<ASTMap>()->values();
fprintf(pyc_output, "{");
fputs("{", pyc_output);
bool first = true;
cur_indent++;
for (ASTMap::map_t::const_iterator b = values.begin(); b != values.end(); ++b) {
if (first)
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
else
fprintf(pyc_output, ",\n");
fputs(",\n", pyc_output);
start_line(cur_indent);
print_src(b->first, mod);
fprintf(pyc_output, ": ");
fputs(": ", pyc_output);
print_src(b->second, mod);
first = false;
}
cur_indent--;
fprintf(pyc_output, " }");
fputs(" }", pyc_output);
}
break;
case ASTNode::NODE_NAME:
@@ -2413,35 +2413,35 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
|| blk->blktype() == ASTBlock::BLK_ELIF
|| blk->blktype() == ASTBlock::BLK_WHILE) {
if (blk.cast<ASTCondBlock>()->negative())
fprintf(pyc_output, " not ");
fputs(" not ", pyc_output);
else
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
print_src(blk.cast<ASTCondBlock>()->cond(), mod);
} else if (blk->blktype() == ASTBlock::BLK_FOR) {
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
print_src(blk.cast<ASTIterBlock>()->index(), mod);
fprintf(pyc_output, " in ");
fputs(" in ", pyc_output);
print_src(blk.cast<ASTIterBlock>()->iter(), mod);
} else if (blk->blktype() == ASTBlock::BLK_EXCEPT &&
blk.cast<ASTCondBlock>()->cond() != NULL) {
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
print_src(blk.cast<ASTCondBlock>()->cond(), mod);
} else if (blk->blktype() == ASTBlock::BLK_WITH) {
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
print_src(blk.cast<ASTWithBlock>()->expr(), mod);
PycRef<ASTNode> var = blk.cast<ASTWithBlock>()->var();
if (var != NULL) {
fprintf(pyc_output, " as ");
fputs(" as ", pyc_output);
print_src(var, mod);
}
}
fprintf(pyc_output, ":\n");
fputs(":\n", pyc_output);
cur_indent++;
print_block(blk, mod);
if (inPrint) {
fprintf(pyc_output, ",");
fputs(",", pyc_output);
}
cur_indent--;
inPrint = false;
@@ -2459,40 +2459,40 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
}
break;
case ASTNode::NODE_PASS:
fprintf(pyc_output, "pass");
fputs("pass", pyc_output);
break;
case ASTNode::NODE_PRINT:
if (node.cast<ASTPrint>()->value() == NULL) {
if (!inPrint) {
fprintf(pyc_output, "print ");
fputs("print ", pyc_output);
if (node.cast<ASTPrint>()->stream() != NULL) {
fprintf(pyc_output, ">>");
fputs(">>", pyc_output);
print_src(node.cast<ASTPrint>()->stream(), mod);
}
}
inPrint = false;
} else if (!inPrint) {
fprintf(pyc_output, "print ");
fputs("print ", pyc_output);
if (node.cast<ASTPrint>()->stream() != NULL) {
fprintf(pyc_output, ">>");
fputs(">>", pyc_output);
print_src(node.cast<ASTPrint>()->stream(), mod);
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
}
print_src(node.cast<ASTPrint>()->value(), mod);
inPrint = true;
} else {
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(node.cast<ASTPrint>()->value(), mod);
}
break;
case ASTNode::NODE_RAISE:
{
PycRef<ASTRaise> raise = node.cast<ASTRaise>();
fprintf(pyc_output, "raise ");
fputs("raise ", pyc_output);
bool first = true;
for (ASTRaise::param_t::const_iterator p = raise->params().begin(); p != raise->params().end(); ++p) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(*p, mod);
first = false;
}
@@ -2504,10 +2504,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
if (!inLambda) {
switch (ret->rettype()) {
case ASTReturn::RETURN:
fprintf(pyc_output, "return ");
fputs("return ", pyc_output);
break;
case ASTReturn::YIELD:
fprintf(pyc_output, "yield ");
fputs("yield ", pyc_output);
break;
}
}
@@ -2521,7 +2521,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
if (slice->op() & ASTSlice::SLICE1) {
print_src(slice->left(), mod);
}
fprintf(pyc_output, ":");
fputs(":", pyc_output);
if (slice->op() & ASTSlice::SLICE2) {
print_src(slice->right(), mod);
}
@@ -2533,37 +2533,37 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
if (import->stores().size()) {
ASTImport::list_t stores = import->stores();
fprintf(pyc_output, "from ");
fputs("from ", pyc_output);
if (import->name().type() == ASTNode::NODE_IMPORT)
print_src(import->name().cast<ASTImport>()->name(), mod);
else
print_src(import->name(), mod);
fprintf(pyc_output, " import ");
fputs(" import ", pyc_output);
ASTImport::list_t::const_iterator ii = stores.begin();
if (stores.size() == 1) {
print_src((*ii)->src(), mod);
if ((*ii)->src().cast<ASTName>()->name()->value() != (*ii)->dest().cast<ASTName>()->name()->value()) {
fprintf(pyc_output, " as ");
fputs(" as ", pyc_output);
print_src((*ii)->dest(), mod);
}
} else {
bool first = true;
for (; ii != stores.end(); ++ii) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src((*ii)->src(), mod);
first = false;
if ((*ii)->src().cast<ASTName>()->name()->value() != (*ii)->dest().cast<ASTName>()->name()->value()) {
fprintf(pyc_output, " as ");
fputs(" as ", pyc_output);
print_src((*ii)->dest(), mod);
}
}
}
} else {
fprintf(pyc_output, "import ");
fputs("import ", pyc_output);
print_src(import->name(), mod);
}
}
@@ -2571,27 +2571,27 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
case ASTNode::NODE_FUNCTION:
{
/* Actual named functions are NODE_STORE with a name */
fprintf(pyc_output, "(lambda ");
fputs("(lambda ", pyc_output);
PycRef<ASTNode> code = node.cast<ASTFunction>()->code();
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
ASTFunction::defarg_t defargs = node.cast<ASTFunction>()->defargs();
ASTFunction::defarg_t::iterator da = defargs.begin();
for (int i=0; i<code_src->argCount(); i++) {
if (i > 0)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(i)->value());
if ((code_src->argCount() - i) <= (int)defargs.size()) {
fprintf(pyc_output, " = ");
fputs(" = ", pyc_output);
print_src(*da++, mod);
}
}
fprintf(pyc_output, ": ");
fputs(": ", pyc_output);
inLambda = true;
print_src(code, mod);
inLambda = false;
fprintf(pyc_output, ")");
fputs(")", pyc_output);
}
break;
case ASTNode::NODE_STORE:
@@ -2604,19 +2604,19 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
bool isLambda = false;
if (strcmp(code_src->name()->value(), "<lambda>") == 0) {
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
start_line(cur_indent);
print_src(dest, mod);
fprintf(pyc_output, " = lambda ");
fputs(" = lambda ", pyc_output);
isLambda = true;
} else {
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
start_line(cur_indent);
if (code_src->flags() & PycCode::CO_COROUTINE)
fprintf(pyc_output, "async ");
fprintf(pyc_output, "def ");
fputs("async ", pyc_output);
fputs("def ", pyc_output);
print_src(dest, mod);
fprintf(pyc_output, "(");
fputs("(", pyc_output);
}
ASTFunction::defarg_t defargs = src.cast<ASTFunction>()->defargs();
@@ -2624,23 +2624,23 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
bool first = true;
for (int i=0; i<code_src->argCount(); i++) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(i)->value());
if ((code_src->argCount() - i) <= (int)defargs.size()) {
fprintf(pyc_output, " = ");
fputs(" = ", pyc_output);
print_src(*da++, mod);
}
first = false;
}
if (code_src->flags() & PycCode::CO_VARARGS) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "*%s", code_src->getVarName(code_src->argCount())->value());
first = false;
}
if (code_src->flags() & PycCode::CO_VARKEYWORDS) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
int idx = code_src->argCount();
if (code_src->flags() & PycCode::CO_VARARGS) {
@@ -2651,9 +2651,9 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
}
if (isLambda) {
fprintf(pyc_output, ": ");
fputs(": ", pyc_output);
} else {
fprintf(pyc_output, "):\n");
fputs("):\n", pyc_output);
printDocstringAndGlobals = true;
}
@@ -2664,24 +2664,24 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
inLambda = preLambda;
} else if (src.type() == ASTNode::NODE_CLASS) {
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
start_line(cur_indent);
fprintf(pyc_output, "class ");
fputs("class ", pyc_output);
print_src(dest, mod);
PycRef<ASTTuple> bases = src.cast<ASTClass>()->bases().cast<ASTTuple>();
if (bases->values().size() > 0) {
fprintf(pyc_output, "(");
fputs("(", pyc_output);
bool first = true;
for (ASTTuple::value_t::const_iterator b = bases->values().begin(); b != bases->values().end(); ++b) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(*b, mod);
first = false;
}
fprintf(pyc_output, "):\n");
fputs("):\n", pyc_output);
} else {
// Don't put parens if there are no base classes
fprintf(pyc_output, ":\n");
fputs(":\n", pyc_output);
}
printClassDocstring = true;
PycRef<ASTNode> code = src.cast<ASTClass>()->code().cast<ASTCall>()
@@ -2692,19 +2692,19 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
if (import->fromlist() != NULL) {
PycRef<PycObject> fromlist = import->fromlist().cast<ASTObject>()->object();
if (fromlist != Pyc_None) {
fprintf(pyc_output, "from ");
fputs("from ", pyc_output);
if (import->name().type() == ASTNode::NODE_IMPORT)
print_src(import->name().cast<ASTImport>()->name(), mod);
else
print_src(import->name(), mod);
fprintf(pyc_output, " import ");
fputs(" import ", pyc_output);
if (fromlist.type() == PycObject::TYPE_TUPLE ||
fromlist.type() == PycObject::TYPE_SMALL_TUPLE) {
bool first = true;
PycTuple::value_t::const_iterator ii = fromlist.cast<PycTuple>()->values().begin();
for (; ii != fromlist.cast<PycTuple>()->values().end(); ++ii) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", ii->cast<PycString>()->value());
first = false;
}
@@ -2712,15 +2712,15 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fprintf(pyc_output, "%s", fromlist.cast<PycString>()->value());
}
} else {
fprintf(pyc_output, "import ");
fputs("import ", pyc_output);
print_src(import->name(), mod);
}
} else {
fprintf(pyc_output, "import ");
fputs("import ", pyc_output);
PycRef<ASTNode> import_name = import->name();
print_src(import_name, mod);
if (!dest.cast<ASTName>()->name()->isEqual(import_name.cast<ASTName>()->name().cast<PycObject>())) {
fprintf(pyc_output, " as ");
fputs(" as ", pyc_output);
print_src(dest, mod);
}
}
@@ -2729,7 +2729,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
print_src(src, mod);
} else {
print_src(dest, mod);
fprintf(pyc_output, " = ");
fputs(" = ", pyc_output);
print_src(src, mod);
}
}
@@ -2737,33 +2737,33 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
case ASTNode::NODE_SUBSCR:
{
print_src(node.cast<ASTSubscr>()->name(), mod);
fprintf(pyc_output, "[");
fputs("[", pyc_output);
print_src(node.cast<ASTSubscr>()->key(), mod);
fprintf(pyc_output, "]");
fputs("]", pyc_output);
}
break;
case ASTNode::NODE_CONVERT:
{
fprintf(pyc_output, "`");
fputs("`", pyc_output);
print_src(node.cast<ASTConvert>()->name(), mod);
fprintf(pyc_output, "`");
fputs("`", pyc_output);
}
break;
case ASTNode::NODE_TUPLE:
{
ASTTuple::value_t values = node.cast<ASTTuple>()->values();
fprintf(pyc_output, "(");
fputs("(", pyc_output);
bool first = true;
for (ASTTuple::value_t::const_iterator b = values.begin(); b != values.end(); ++b) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_src(*b, mod);
first = false;
}
if (values.size() == 1)
fprintf(pyc_output, ",)");
fputs(",)", pyc_output);
else
fprintf(pyc_output, ")");
fputs(")", pyc_output);
}
break;
default:
@@ -2794,7 +2794,7 @@ bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod)
if (prefix != -1) {
start_line(indent);
OutputString(obj.cast<PycString>(), prefix, true);
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
return true;
} else
return false;
@@ -2860,16 +2860,16 @@ void decompyle(PycRef<PycCode> code, PycModule* mod)
PycCode::globals_t globs = code->getGlobals();
if (globs.size()) {
start_line(cur_indent + 1);
fprintf(pyc_output, "global ");
fputs("global ", pyc_output);
bool first = true;
PycCode::globals_t::iterator it;
for (it = globs.begin(); it != globs.end(); ++it) {
if (!first)
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", (*it)->value());
first = false;
}
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
}
printDocstringAndGlobals = false;
}
@@ -2878,6 +2878,6 @@ void decompyle(PycRef<PycCode> code, PycModule* mod)
if (!cleanBuild || !part1clean) {
start_line(cur_indent);
fprintf(pyc_output, "# WARNING: Decompyle incomplete\n");
fputs("# WARNING: Decompyle incomplete\n", pyc_output);
}
}

View File

@@ -145,7 +145,7 @@ bool Pyc::IsCompareArg(int opcode)
void print_const(PycRef<PycObject> obj, PycModule* mod)
{
if (obj == NULL) {
fprintf(pyc_output, "<NULL>");
fputs("<NULL>", pyc_output);
return;
}
@@ -167,85 +167,85 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
case PycObject::TYPE_TUPLE:
case PycObject::TYPE_SMALL_TUPLE:
{
fprintf(pyc_output, "(");
fputs("(", pyc_output);
PycTuple::value_t values = obj.cast<PycTuple>()->values();
PycTuple::value_t::const_iterator it = values.begin();
if (it != values.end()) {
print_const(*it, mod);
while (++it != values.end()) {
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_const(*it, mod);
}
}
if (values.size() == 1)
fprintf(pyc_output, ",)");
fputs(",)", pyc_output);
else
fprintf(pyc_output, ")");
fputs(")", pyc_output);
}
break;
case PycObject::TYPE_LIST:
{
fprintf(pyc_output, "[");
fputs("[", pyc_output);
PycList::value_t values = obj.cast<PycList>()->values();
PycList::value_t::const_iterator it = values.begin();
if (it != values.end()) {
print_const(*it, mod);
while (++it != values.end()) {
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_const(*it, mod);
}
}
fprintf(pyc_output, "]");
fputs("]", pyc_output);
}
break;
case PycObject::TYPE_DICT:
{
fprintf(pyc_output, "{");
fputs("{", pyc_output);
PycDict::key_t keys = obj.cast<PycDict>()->keys();
PycDict::value_t values = obj.cast<PycDict>()->values();
PycDict::key_t::const_iterator ki = keys.begin();
PycDict::value_t::const_iterator vi = values.begin();
if (ki != keys.end()) {
print_const(*ki, mod);
fprintf(pyc_output, ": ");
fputs(": ", pyc_output);
print_const(*vi, mod);
while (++ki != keys.end()) {
++vi;
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_const(*ki, mod);
fprintf(pyc_output, ": ");
fputs(": ", pyc_output);
print_const(*vi, mod);
}
}
fprintf(pyc_output, "}");
fputs("}", pyc_output);
}
break;
case PycObject::TYPE_SET:
{
fprintf(pyc_output, "{");
fputs("{", pyc_output);
PycSet::value_t values = obj.cast<PycSet>()->values();
PycSet::value_t::const_iterator it = values.begin();
if (it != values.end()) {
print_const(*it, mod);
while (++it != values.end()) {
fprintf(pyc_output, ", ");
fputs(", ", pyc_output);
print_const(*it, mod);
}
}
fprintf(pyc_output, "}");
fputs("}", pyc_output);
}
break;
case PycObject::TYPE_NONE:
fprintf(pyc_output, "None");
fputs("None", pyc_output);
break;
case PycObject::TYPE_TRUE:
fprintf(pyc_output, "True");
fputs("True", pyc_output);
break;
case PycObject::TYPE_FALSE:
fprintf(pyc_output, "False");
fputs("False", pyc_output);
break;
case PycObject::TYPE_ELLIPSIS:
fprintf(pyc_output, "...");
fputs("...", pyc_output);
break;
case PycObject::TYPE_INT:
fprintf(pyc_output, "%d", obj.cast<PycInt>()->value());
@@ -318,7 +318,7 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
int pos = 0;
while (!source.atEof()) {
for (int i=0; i<indent; i++)
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
fprintf(pyc_output, "%-7d ", pos); // Current bytecode position
bc_next(source, mod, opcode, operand, pos);
@@ -345,6 +345,6 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
fprintf(pyc_output, "%d", operand);
}
}
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
}
}

View File

@@ -161,7 +161,7 @@ void PycModule::loadFromFile(const char* filename)
}
setVersion(in.get32());
if (!isValid()) {
fprintf(stderr, "Bad MAGIC!\n");
fputs("Bad MAGIC!\n", stderr);
return;
}
in.get32(); // Timestamp -- who cares?

View File

@@ -106,7 +106,7 @@ void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F)
const char* ch = str->value();
int len = str->length();
if (ch == 0) {
fprintf(F, "''");
fputs("''", F);
return;
}
@@ -126,20 +126,20 @@ void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F)
// Output the string
if (triple)
fprintf(F, useQuotes ? "\"\"\"" : "'''");
fputs(useQuotes ? "\"\"\"" : "'''", F);
else
fputc(useQuotes ? '"' : '\'', F);
while (len--) {
if (*ch < 0x20 || *ch == 0x7F) {
if (*ch == '\r') {
fprintf(F, "\\r");
fputs("\\r", F);
} else if (*ch == '\n') {
if (triple)
fputc('\n', F);
else
fprintf(F, "\\n");
fputs("\\n", F);
} else if (*ch == '\t') {
fprintf(F, "\\t");
fputs("\\t", F);
} else {
fprintf(F, "\\x%02x", (*ch & 0xFF));
}
@@ -152,18 +152,18 @@ void OutputString(PycRef<PycString> str, char prefix, bool triple, FILE* F)
}
} else {
if (!useQuotes && *ch == '\'')
fprintf(F, "\\'");
fputs("\\'", F);
else if (useQuotes && *ch == '"')
fprintf(F, "\\\"");
fputs("\\\"", F);
else if (*ch == '\\')
fprintf(F, "\\\\");
fputs("\\\\", F);
else
fputc(*ch, F);
}
ch++;
}
if (triple)
fprintf(F, useQuotes ? "\"\"\"" : "'''");
fputs(useQuotes ? "\"\"\"" : "'''", F);
else
fputc(useQuotes ? '"' : '\'', F);
}

View File

@@ -27,31 +27,38 @@ static const char* flag_names[] = {
static void print_coflags(unsigned long flags)
{
if (flags == 0) {
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
return;
}
fprintf(pyc_output, " (");
fputs(" (", pyc_output);
unsigned long f = 1;
int k = 0;
while (k < 32) {
if ((flags & f) != 0) {
flags &= ~f;
if (flags == 0)
fprintf(pyc_output, "%s", flag_names[k]);
fputs(flag_names[k], pyc_output);
else
fprintf(pyc_output, "%s | ", flag_names[k]);
}
++k;
f <<= 1;
}
fprintf(pyc_output, ")\n");
fputs(")\n", pyc_output);
}
static void iputs(int indent, const char* text)
{
for (int i=0; i<indent; i++)
fputs(" ", pyc_output);
fputs(text, pyc_output);
}
static void ivprintf(int indent, const char* fmt, va_list varargs)
{
for (int i=0; i<indent; i++)
fprintf(pyc_output, " ");
fputs(" ", pyc_output);
vprintf(fmt, varargs);
}
@@ -66,7 +73,7 @@ static void iprintf(int indent, const char* fmt, ...)
void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
{
if (obj == NULL) {
iprintf(indent, "<NULL>");
iputs(indent, "<NULL>");
return;
}
@@ -75,7 +82,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
case PycObject::TYPE_CODE2:
{
PycRef<PycCode> codeObj = obj.cast<PycCode>();
iprintf(indent, "[Code]\n");
iputs(indent, "[Code]\n");
iprintf(indent + 1, "File Name: %s\n", codeObj->fileName()->value());
iprintf(indent + 1, "Object Name: %s\n", codeObj->name()->value());
iprintf(indent + 1, "Arg Count: %d\n", codeObj->argCount());
@@ -87,48 +94,48 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
print_coflags(codeObj->flags());
if (codeObj->names() != NULL) {
iprintf(indent + 1, "[Names]\n");
iputs(indent + 1, "[Names]\n");
for (int i=0; i<codeObj->names()->size(); i++)
output_object(codeObj->names()->get(i), mod, indent + 2);
}
if (codeObj->varNames() != NULL) {
iprintf(indent + 1, "[Var Names]\n");
iputs(indent + 1, "[Var Names]\n");
for (int i=0; i<codeObj->varNames()->size(); i++)
output_object(codeObj->varNames()->get(i), mod, indent + 2);
}
if (codeObj->freeVars() != NULL) {
iprintf(indent + 1, "[Free Vars]\n");
iputs(indent + 1, "[Free Vars]\n");
for (int i=0; i<codeObj->freeVars()->size(); i++)
output_object(codeObj->freeVars()->get(i), mod, indent + 2);
}
if (codeObj->cellVars() != NULL) {
iprintf(indent + 1, "[Cell Vars]\n");
iputs(indent + 1, "[Cell Vars]\n");
for (int i=0; i<codeObj->cellVars()->size(); i++)
output_object(codeObj->cellVars()->get(i), mod, indent + 2);
}
if (codeObj->consts() != NULL) {
iprintf(indent + 1, "[Constants]\n");
iputs(indent + 1, "[Constants]\n");
for (int i=0; i<codeObj->consts()->size(); i++)
output_object(codeObj->consts()->get(i), mod, indent + 2);
}
iprintf(indent + 1, "[Disassembly]\n");
iputs(indent + 1, "[Disassembly]\n");
bc_disasm(codeObj, mod, indent + 2);
}
break;
case PycObject::TYPE_STRING:
iprintf(indent, "");
iputs(indent, "");
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 'b' : 0);
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
break;
case PycObject::TYPE_UNICODE:
iprintf(indent, "");
iputs(indent, "");
OutputString(obj.cast<PycString>(), (mod->majorVer() == 3) ? 0 : 'u');
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
break;
case PycObject::TYPE_STRINGREF:
case PycObject::TYPE_INTERNED:
@@ -136,32 +143,32 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
case PycObject::TYPE_ASCII_INTERNED:
case PycObject::TYPE_SHORT_ASCII:
case PycObject::TYPE_SHORT_ASCII_INTERNED:
iprintf(indent, "");
iputs(indent, "");
OutputString(obj.cast<PycString>(), 0);
fprintf(pyc_output, "\n");
fputs("\n", pyc_output);
break;
case PycObject::TYPE_TUPLE:
case PycObject::TYPE_SMALL_TUPLE:
{
iprintf(indent, "(\n");
iputs(indent, "(\n");
PycTuple::value_t values = obj.cast<PycTuple>()->values();
for (PycTuple::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, ")\n");
iputs(indent, ")\n");
}
break;
case PycObject::TYPE_LIST:
{
iprintf(indent, "[\n");
iputs(indent, "[\n");
PycList::value_t values = obj.cast<PycList>()->values();
for (PycList::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, "]\n");
iputs(indent, "]\n");
}
break;
case PycObject::TYPE_DICT:
{
iprintf(indent, "{\n");
iputs(indent, "{\n");
PycDict::key_t keys = obj.cast<PycDict>()->keys();
PycDict::value_t values = obj.cast<PycDict>()->values();
PycDict::key_t::const_iterator ki = keys.begin();
@@ -171,29 +178,29 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
output_object(*vi, mod, indent + 2);
++ki, ++vi;
}
iprintf(indent, "}\n");
iputs(indent, "}\n");
}
break;
case PycObject::TYPE_SET:
{
iprintf(indent, "{\n");
iputs(indent, "{\n");
PycSet::value_t values = obj.cast<PycSet>()->values();
for (PycSet::value_t::const_iterator i = values.begin(); i != values.end(); i++)
output_object(*i, mod, indent + 1);
iprintf(indent, "}\n");
iputs(indent, "}\n");
}
break;
case PycObject::TYPE_NONE:
iprintf(indent, "None\n");
iputs(indent, "None\n");
break;
case PycObject::TYPE_FALSE:
iprintf(indent, "False\n");
iputs(indent, "False\n");
break;
case PycObject::TYPE_TRUE:
iprintf(indent, "True\n");
iputs(indent, "True\n");
break;
case PycObject::TYPE_ELLIPSIS:
iprintf(indent, "...\n");
iputs(indent, "...\n");
break;
case PycObject::TYPE_INT:
iprintf(indent, "%d\n", obj.cast<PycInt>()->value());
@@ -223,7 +230,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
int main(int argc, char* argv[])
{
if (argc < 2) {
fprintf(stderr, "No input file specified\n");
fputs("No input file specified\n", stderr);
return 1;
}

View File

@@ -10,7 +10,7 @@
int main(int argc, char* argv[])
{
if (argc < 2) {
fprintf(stderr, "No input file specified\n");
fputs("No input file specified\n", stderr);
return 1;
}
@@ -22,7 +22,7 @@ int main(int argc, char* argv[])
}
const char* dispname = strrchr(argv[1], PATHSEP);
dispname = (dispname == NULL) ? argv[1] : dispname + 1;
fprintf(pyc_output, "# Source Generated with Decompyle++\n");
fputs("# Source Generated with Decompyle++\n", pyc_output);
fprintf(pyc_output, "# File: %s (Python %d.%d%s)\n\n", dispname, mod.majorVer(), mod.minorVer(),
(mod.majorVer() < 3 && mod.isUnicode()) ? " Unicode" : "");
decompyle(mod.code(), &mod);