2009-07-24 08:35:21 +00:00
|
|
|
#include <cstdio>
|
2010-08-31 23:17:38 -07:00
|
|
|
#include <cstring>
|
2009-07-24 08:35:21 +00:00
|
|
|
#include <cstdarg>
|
2022-06-15 11:33:11 +01:00
|
|
|
#include <string>
|
2011-10-23 17:48:10 -07:00
|
|
|
#include "pyc_module.h"
|
|
|
|
#include "pyc_numeric.h"
|
2009-07-24 08:35:21 +00:00
|
|
|
#include "bytecode.h"
|
|
|
|
|
2010-09-03 21:50:35 -07:00
|
|
|
#ifdef WIN32
|
|
|
|
# define PATHSEP '\\'
|
|
|
|
#else
|
|
|
|
# define PATHSEP '/'
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char* flag_names[] = {
|
|
|
|
"CO_OPTIMIZED", "CO_NEWLOCALS", "CO_VARARGS", "CO_VARKEYWORDS",
|
2015-10-01 16:06:09 -07:00
|
|
|
"CO_NESTED", "CO_GENERATOR", "CO_NOFREE", "CO_COROUTINE",
|
|
|
|
"CO_ITERABLE_COROUTINE", "<0x200>", "<0x400>", "<0x800>",
|
|
|
|
"CO_GENERATOR_ALLOWED", "CO_FUTURE_DIVISION",
|
2010-09-03 21:50:35 -07:00
|
|
|
"CO_FUTURE_ABSOLUTE_IMPORT", "CO_FUTURE_WITH_STATEMENT",
|
|
|
|
"CO_FUTURE_PRINT_FUNCTION", "CO_FUTURE_UNICODE_LITERALS",
|
2015-10-01 16:06:09 -07:00
|
|
|
"CO_FUTURE_BARRY_AS_BDFL", "CO_FUTURE_GENERATOR_STOP",
|
|
|
|
"<0x100000>", "<0x200000>", "<0x400000>", "<0x800000>",
|
|
|
|
"<0x1000000>", "<0x2000000>", "<0x4000000>", "<0x8000000>",
|
|
|
|
"<0x10000000>", "<0x20000000>", "<0x40000000>", "<0x80000000>"
|
2010-09-03 21:50:35 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void print_coflags(unsigned long flags)
|
|
|
|
{
|
|
|
|
if (flags == 0) {
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("\n", pyc_output);
|
2010-09-03 21:50:35 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs(" (", pyc_output);
|
2010-09-03 21:50:35 -07:00
|
|
|
unsigned long f = 1;
|
|
|
|
int k = 0;
|
|
|
|
while (k < 32) {
|
|
|
|
if ((flags & f) != 0) {
|
|
|
|
flags &= ~f;
|
|
|
|
if (flags == 0)
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs(flag_names[k], pyc_output);
|
2010-09-03 21:50:35 -07:00
|
|
|
else
|
2011-10-23 19:04:06 -07:00
|
|
|
fprintf(pyc_output, "%s | ", flag_names[k]);
|
2010-09-03 21:50:35 -07:00
|
|
|
}
|
|
|
|
++k;
|
|
|
|
f <<= 1;
|
|
|
|
}
|
2018-01-28 10:32:44 -08:00
|
|
|
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);
|
2010-09-03 21:50:35 -07:00
|
|
|
}
|
|
|
|
|
2009-07-24 08:35:21 +00:00
|
|
|
static void ivprintf(int indent, const char* fmt, va_list varargs)
|
|
|
|
{
|
|
|
|
for (int i=0; i<indent; i++)
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs(" ", pyc_output);
|
2022-04-26 14:44:10 -07:00
|
|
|
vfprintf(pyc_output, fmt, varargs);
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iprintf(int indent, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list varargs;
|
|
|
|
va_start(varargs, fmt);
|
|
|
|
ivprintf(indent, fmt, varargs);
|
|
|
|
va_end(varargs);
|
|
|
|
}
|
|
|
|
|
2023-01-19 14:50:55 -08:00
|
|
|
void output_object(PycRef<PycObject> obj, PycModule* mod, int indent,
|
|
|
|
unsigned flags)
|
2009-07-24 08:35:21 +00:00
|
|
|
{
|
2017-07-05 16:36:04 -07:00
|
|
|
if (obj == NULL) {
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "<NULL>");
|
2017-07-05 16:36:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-24 08:35:21 +00:00
|
|
|
switch (obj->type()) {
|
|
|
|
case PycObject::TYPE_CODE:
|
2009-07-25 00:43:46 +00:00
|
|
|
case PycObject::TYPE_CODE2:
|
2009-07-24 08:35:21 +00:00
|
|
|
{
|
|
|
|
PycRef<PycCode> codeObj = obj.cast<PycCode>();
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "[Code]\n");
|
2009-07-24 08:35:21 +00:00
|
|
|
iprintf(indent + 1, "File Name: %s\n", codeObj->fileName()->value());
|
|
|
|
iprintf(indent + 1, "Object Name: %s\n", codeObj->name()->value());
|
2022-12-05 09:12:39 -08:00
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
iprintf(indent + 1, "Qualified Name: %s\n", codeObj->qualName()->value());
|
2009-07-24 08:35:21 +00:00
|
|
|
iprintf(indent + 1, "Arg Count: %d\n", codeObj->argCount());
|
2019-02-08 16:06:55 -08:00
|
|
|
if (mod->verCompare(3, 8) >= 0)
|
|
|
|
iprintf(indent + 1, "Pos Only Arg Count: %d\n", codeObj->posOnlyArgCount());
|
2016-09-03 11:05:03 -07:00
|
|
|
if (mod->majorVer() >= 3)
|
|
|
|
iprintf(indent + 1, "KW Only Arg Count: %d\n", codeObj->kwOnlyArgCount());
|
2022-12-05 09:12:39 -08:00
|
|
|
if (mod->verCompare(3, 11) < 0)
|
|
|
|
iprintf(indent + 1, "Locals: %d\n", codeObj->numLocals());
|
|
|
|
if (mod->verCompare(1, 5) >= 0)
|
|
|
|
iprintf(indent + 1, "Stack Size: %d\n", codeObj->stackSize());
|
|
|
|
if (mod->verCompare(1, 3) >= 0) {
|
|
|
|
iprintf(indent + 1, "Flags: 0x%08X", codeObj->flags());
|
|
|
|
print_coflags(codeObj->flags());
|
2009-07-24 23:21:56 +00:00
|
|
|
}
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-05 09:12:39 -08:00
|
|
|
iputs(indent + 1, "[Names]\n");
|
|
|
|
for (int i=0; i<codeObj->names()->size(); i++)
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->names()->get(i), mod, indent + 2, flags);
|
2022-12-05 09:12:39 -08:00
|
|
|
|
|
|
|
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(3, 11) < 0) {
|
2023-01-19 11:20:06 -08:00
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
iputs(indent + 1, "[Locals+Names]\n");
|
|
|
|
else
|
|
|
|
iputs(indent + 1, "[Var Names]\n");
|
|
|
|
for (int i=0; i<codeObj->localNames()->size(); i++)
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->localNames()->get(i), mod, indent + 2, flags);
|
2023-01-19 11:20:06 -08:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:50:55 -08:00
|
|
|
if (mod->verCompare(3, 11) >= 0 && (flags & Pyc::DISASM_PYCODE_VERBOSE) != 0) {
|
2023-01-19 11:20:06 -08:00
|
|
|
iputs(indent + 1, "[Locals+Kinds]\n");
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->localKinds().cast<PycObject>(), mod, indent + 2, flags);
|
2009-07-24 23:21:56 +00:00
|
|
|
}
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-05 09:12:39 -08:00
|
|
|
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0) {
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent + 1, "[Free Vars]\n");
|
2009-07-24 23:21:56 +00:00
|
|
|
for (int i=0; i<codeObj->freeVars()->size(); i++)
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->freeVars()->get(i), mod, indent + 2, flags);
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent + 1, "[Cell Vars]\n");
|
2009-07-24 23:21:56 +00:00
|
|
|
for (int i=0; i<codeObj->cellVars()->size(); i++)
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->cellVars()->get(i), mod, indent + 2, flags);
|
2009-07-24 23:21:56 +00:00
|
|
|
}
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-05 09:12:39 -08:00
|
|
|
iputs(indent + 1, "[Constants]\n");
|
|
|
|
for (int i=0; i<codeObj->consts()->size(); i++)
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->consts()->get(i), mod, indent + 2, flags);
|
2022-12-05 09:12:39 -08:00
|
|
|
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent + 1, "[Disassembly]\n");
|
2023-01-19 14:50:55 -08:00
|
|
|
bc_disasm(codeObj, mod, indent + 2, flags);
|
2022-12-05 09:12:39 -08:00
|
|
|
|
2023-01-19 14:50:55 -08:00
|
|
|
if (mod->verCompare(1, 5) >= 0 && (flags & Pyc::DISASM_PYCODE_VERBOSE) != 0) {
|
2022-12-05 09:12:39 -08:00
|
|
|
iputs(indent + 1, "[Line Number Table]\n");
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->lnTable().cast<PycObject>(), mod, indent + 2, flags);
|
2022-12-05 09:12:39 -08:00
|
|
|
}
|
|
|
|
|
2023-01-19 14:50:55 -08:00
|
|
|
if (mod->verCompare(3, 11) >= 0 && (flags & Pyc::DISASM_PYCODE_VERBOSE) != 0) {
|
2022-12-05 09:12:39 -08:00
|
|
|
iputs(indent + 1, "[Exception Table]\n");
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(codeObj->exceptTable().cast<PycObject>(), mod, indent + 2, flags);
|
2022-12-05 09:12:39 -08:00
|
|
|
}
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PycObject::TYPE_STRING:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "");
|
2019-10-06 14:34:24 -07:00
|
|
|
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0);
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("\n", pyc_output);
|
2009-07-24 08:35:21 +00:00
|
|
|
break;
|
2009-07-25 00:02:31 +00:00
|
|
|
case PycObject::TYPE_UNICODE:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "");
|
2019-10-06 14:34:24 -07:00
|
|
|
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 0 : 'u');
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("\n", pyc_output);
|
2009-07-25 00:02:31 +00:00
|
|
|
break;
|
2014-01-21 00:47:11 -08:00
|
|
|
case PycObject::TYPE_INTERNED:
|
|
|
|
case PycObject::TYPE_ASCII:
|
|
|
|
case PycObject::TYPE_ASCII_INTERNED:
|
|
|
|
case PycObject::TYPE_SHORT_ASCII:
|
|
|
|
case PycObject::TYPE_SHORT_ASCII_INTERNED:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "");
|
2019-10-06 14:34:24 -07:00
|
|
|
if (mod->majorVer() >= 3)
|
|
|
|
OutputString(obj.cast<PycString>(), 0);
|
|
|
|
else
|
|
|
|
OutputString(obj.cast<PycString>(), mod->strIsUnicode() ? 'b' : 0);
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("\n", pyc_output);
|
2014-01-21 00:47:11 -08:00
|
|
|
break;
|
2009-07-24 08:35:21 +00:00
|
|
|
case PycObject::TYPE_TUPLE:
|
2014-01-21 00:07:34 -08:00
|
|
|
case PycObject::TYPE_SMALL_TUPLE:
|
2009-07-24 08:35:21 +00:00
|
|
|
{
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "(\n");
|
2019-10-04 15:56:24 -07:00
|
|
|
for (const auto& val : obj.cast<PycTuple>()->values())
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(val, mod, indent + 1, flags);
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, ")\n");
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PycObject::TYPE_LIST:
|
|
|
|
{
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "[\n");
|
2019-10-04 15:56:24 -07:00
|
|
|
for (const auto& val : obj.cast<PycList>()->values())
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(val, mod, indent + 1, flags);
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "]\n");
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-07-24 21:39:51 +00:00
|
|
|
case PycObject::TYPE_DICT:
|
|
|
|
{
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "{\n");
|
2009-07-24 21:39:51 +00:00
|
|
|
PycDict::key_t keys = obj.cast<PycDict>()->keys();
|
|
|
|
PycDict::value_t values = obj.cast<PycDict>()->values();
|
2009-07-27 00:23:49 +00:00
|
|
|
PycDict::key_t::const_iterator ki = keys.begin();
|
|
|
|
PycDict::value_t::const_iterator vi = values.begin();
|
2009-07-24 21:39:51 +00:00
|
|
|
while (ki != keys.end()) {
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(*ki, mod, indent + 1, flags);
|
|
|
|
output_object(*vi, mod, indent + 2, flags);
|
2009-07-25 00:02:31 +00:00
|
|
|
++ki, ++vi;
|
2009-07-24 21:39:51 +00:00
|
|
|
}
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "}\n");
|
2009-07-24 21:39:51 +00:00
|
|
|
}
|
|
|
|
break;
|
2009-07-25 02:41:15 +00:00
|
|
|
case PycObject::TYPE_SET:
|
|
|
|
{
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "{\n");
|
2019-10-04 15:56:24 -07:00
|
|
|
for (const auto& val : obj.cast<PycSet>()->values())
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(val, mod, indent + 1, flags);
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "}\n");
|
2009-07-25 02:41:15 +00:00
|
|
|
}
|
|
|
|
break;
|
2023-02-13 19:26:30 -08:00
|
|
|
case PycObject::TYPE_FROZENSET:
|
|
|
|
{
|
|
|
|
iputs(indent, "frozenset({\n");
|
|
|
|
for (const auto& val : obj.cast<PycSet>()->values())
|
|
|
|
output_object(val, mod, indent + 1, flags);
|
|
|
|
iputs(indent, "})\n");
|
|
|
|
}
|
|
|
|
break;
|
2009-07-24 08:35:21 +00:00
|
|
|
case PycObject::TYPE_NONE:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "None\n");
|
2009-07-24 08:35:21 +00:00
|
|
|
break;
|
2009-07-25 00:02:31 +00:00
|
|
|
case PycObject::TYPE_FALSE:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "False\n");
|
2009-07-25 00:02:31 +00:00
|
|
|
break;
|
|
|
|
case PycObject::TYPE_TRUE:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "True\n");
|
2009-07-25 00:02:31 +00:00
|
|
|
break;
|
2016-08-30 13:32:53 -07:00
|
|
|
case PycObject::TYPE_ELLIPSIS:
|
2018-01-28 10:32:44 -08:00
|
|
|
iputs(indent, "...\n");
|
2016-08-30 13:32:53 -07:00
|
|
|
break;
|
2009-07-24 08:35:21 +00:00
|
|
|
case PycObject::TYPE_INT:
|
|
|
|
iprintf(indent, "%d\n", obj.cast<PycInt>()->value());
|
|
|
|
break;
|
2011-09-23 21:46:05 -07:00
|
|
|
case PycObject::TYPE_LONG:
|
|
|
|
iprintf(indent, "%s\n", obj.cast<PycLong>()->repr().c_str());
|
|
|
|
break;
|
2009-07-24 08:35:21 +00:00
|
|
|
case PycObject::TYPE_FLOAT:
|
|
|
|
iprintf(indent, "%s\n", obj.cast<PycFloat>()->value());
|
|
|
|
break;
|
2009-07-25 02:41:15 +00:00
|
|
|
case PycObject::TYPE_COMPLEX:
|
|
|
|
iprintf(indent, "(%s+%sj)\n", obj.cast<PycComplex>()->value(),
|
|
|
|
obj.cast<PycComplex>()->imag());
|
|
|
|
break;
|
|
|
|
case PycObject::TYPE_BINARY_FLOAT:
|
|
|
|
iprintf(indent, "%g\n", obj.cast<PycCFloat>()->value());
|
|
|
|
break;
|
|
|
|
case PycObject::TYPE_BINARY_COMPLEX:
|
|
|
|
iprintf(indent, "(%g+%gj)\n", obj.cast<PycCComplex>()->value(),
|
|
|
|
obj.cast<PycCComplex>()->imag());
|
|
|
|
break;
|
2009-07-24 08:35:21 +00:00
|
|
|
default:
|
|
|
|
iprintf(indent, "<TYPE: %d>\n", obj->type());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-04-26 14:44:10 -07:00
|
|
|
const char* infile = nullptr;
|
2022-06-15 11:33:11 +01:00
|
|
|
bool marshalled = false;
|
|
|
|
const char* version = nullptr;
|
2023-01-19 14:50:55 -08:00
|
|
|
unsigned disasm_flags = 0;
|
|
|
|
|
2022-04-26 14:44:10 -07:00
|
|
|
for (int arg = 1; arg < argc; ++arg) {
|
|
|
|
if (strcmp(argv[arg], "-o") == 0) {
|
|
|
|
if (arg + 1 < argc) {
|
|
|
|
const char* filename = argv[++arg];
|
|
|
|
FILE* outfile = fopen(filename, "w");
|
|
|
|
if (!outfile) {
|
|
|
|
fprintf(stderr, "Error opening file '%s' for writing\n",
|
|
|
|
argv[arg]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pyc_output = outfile;
|
|
|
|
} else {
|
|
|
|
fputs("Option '-o' requires a filename\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2022-06-15 11:33:11 +01:00
|
|
|
} else if (strcmp(argv[arg], "-c") == 0) {
|
|
|
|
marshalled = true;
|
|
|
|
} else if (strcmp(argv[arg], "-v") == 0) {
|
|
|
|
if (arg + 1 < argc) {
|
|
|
|
version = argv[++arg];
|
|
|
|
} else {
|
|
|
|
fputs("Option '-v' requires a version\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-01-19 14:50:55 -08:00
|
|
|
} else if (strcmp(argv[arg], "--pycode-extra") == 0) {
|
|
|
|
disasm_flags |= Pyc::DISASM_PYCODE_VERBOSE;
|
|
|
|
} else if (strcmp(argv[arg], "--show-caches") == 0) {
|
|
|
|
disasm_flags |= Pyc::DISASM_SHOW_CACHES;
|
2022-04-26 14:44:10 -07:00
|
|
|
} else if (strcmp(argv[arg], "--help") == 0 || strcmp(argv[arg], "-h") == 0) {
|
|
|
|
fprintf(stderr, "Usage: %s [options] input.pyc\n\n", argv[0]);
|
|
|
|
fputs("Options:\n", stderr);
|
2022-06-15 11:33:11 +01:00
|
|
|
fputs(" -o <filename> Write output to <filename> (default: stdout)\n", stderr);
|
|
|
|
fputs(" -c Specify loading a compiled code object. Requires the version to be set\n", stderr);
|
2022-06-16 12:49:04 +01:00
|
|
|
fputs(" -v <x.y> Specify a Python version for loading a compiled code object\n", stderr);
|
2023-01-19 14:50:55 -08:00
|
|
|
fputs(" --pycode-extra Show extra fields in PyCode object dumps\n", stderr);
|
|
|
|
fputs(" --show-caches Don't suprress CACHE instructions in Python 3.11+ disassembly\n", stderr);
|
2022-06-15 11:33:11 +01:00
|
|
|
fputs(" --help Show this help text and then exit\n", stderr);
|
2022-04-26 14:44:10 -07:00
|
|
|
return 0;
|
2023-01-19 14:50:55 -08:00
|
|
|
} else if (argv[arg][0] == '-') {
|
|
|
|
fprintf(stderr, "Error: Unrecognized argument %s\n", argv[arg]);
|
|
|
|
return 1;
|
2022-04-26 14:44:10 -07:00
|
|
|
} else {
|
|
|
|
infile = argv[arg];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!infile) {
|
2018-01-28 10:32:44 -08:00
|
|
|
fputs("No input file specified\n", stderr);
|
2009-07-24 08:35:21 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PycModule mod;
|
2022-06-15 11:33:11 +01:00
|
|
|
if (!marshalled) {
|
|
|
|
try {
|
|
|
|
mod.loadFromFile(infile);
|
|
|
|
} catch (std::exception &ex) {
|
|
|
|
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!version) {
|
|
|
|
fputs("Opening raw code objects requires a version to be specified\n", stderr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::string s(version);
|
|
|
|
auto dot = s.find('.');
|
|
|
|
if (dot == std::string::npos || dot == s.size()-1) {
|
2022-06-16 12:49:04 +01:00
|
|
|
fputs("Unable to parse version string (use the format x.y)\n", stderr);
|
2022-06-15 11:33:11 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int major = std::stoi(s.substr(0, dot));
|
|
|
|
int minor = std::stoi(s.substr(dot+1, s.size()));
|
|
|
|
mod.loadFromMarshalledFile(infile, major, minor);
|
2018-01-28 14:33:26 -08:00
|
|
|
}
|
2022-04-26 14:44:10 -07:00
|
|
|
const char* dispname = strrchr(infile, PATHSEP);
|
|
|
|
dispname = (dispname == NULL) ? infile : dispname + 1;
|
2011-10-23 19:04:06 -07:00
|
|
|
fprintf(pyc_output, "%s (Python %d.%d%s)\n", dispname, mod.majorVer(), mod.minorVer(),
|
2009-07-25 02:41:15 +00:00
|
|
|
(mod.majorVer() < 3 && mod.isUnicode()) ? " -U" : "");
|
2018-01-28 14:33:26 -08:00
|
|
|
try {
|
2023-01-19 14:50:55 -08:00
|
|
|
output_object(mod.code().try_cast<PycObject>(), &mod, 0, disasm_flags);
|
2018-01-28 14:33:26 -08:00
|
|
|
} catch (std::exception& ex) {
|
2022-04-26 14:44:10 -07:00
|
|
|
fprintf(stderr, "Error disassembling %s: %s\n", infile, ex.what());
|
2018-01-28 14:33:26 -08:00
|
|
|
return 1;
|
|
|
|
}
|
2009-07-24 08:35:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|