Merge <3.11 VarNames and >=3.11 Locals+Names into a single variable for simplicity.

Also cleans up some extra verbose output by making extra PyCode fields optional
This commit is contained in:
Michael Hansen
2023-01-19 11:20:06 -08:00
parent dfb0a976c5
commit 379c0c94d0
6 changed files with 46 additions and 47 deletions

View File

@@ -704,7 +704,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (mod->verCompare(1, 3) < 0)
name = new ASTName(code->getName(operand));
else
name = new ASTName(code->getVarName(operand));
name = new ASTName(code->getLocal(operand));
if (name.cast<ASTName>()->name()->value()[0] == '_'
&& name.cast<ASTName>()->name()->value()[1] == '[') {
@@ -1612,7 +1612,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (mod->verCompare(1, 3) < 0)
stack.push(new ASTName(code->getName(operand)));
else
stack.push(new ASTName(code->getVarName(operand)));
stack.push(new ASTName(code->getLocal(operand)));
break;
case Pyc::LOAD_GLOBAL_A:
stack.push(new ASTName(code->getName(operand)));
@@ -2140,7 +2140,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (mod->verCompare(1, 3) < 0)
name = new ASTName(code->getName(operand));
else
name = new ASTName(code->getVarName(operand));
name = new ASTName(code->getLocal(operand));
PycRef<ASTNode> tup = stack.top();
if (tup.type() == ASTNode::NODE_TUPLE)
@@ -2173,7 +2173,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (mod->verCompare(1, 3) < 0)
name = new ASTName(code->getName(operand));
else
name = new ASTName(code->getVarName(operand));
name = new ASTName(code->getLocal(operand));
if (name.cast<ASTName>()->name()->value()[0] == '_'
&& name.cast<ASTName>()->name()->value()[1] == '[') {
@@ -3088,7 +3088,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
for (int i=0; i<code_src->argCount(); i++) {
if (narg)
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "%s", code_src->getLocal(narg++)->value());
if ((code_src->argCount() - i) <= (int)defargs.size()) {
fputs(" = ", pyc_output);
print_src(*da++, mod);
@@ -3099,7 +3099,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fputs(narg == 0 ? "*" : ", *", pyc_output);
for (int i = 0; i < code_src->argCount(); i++) {
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "%s", code_src->getLocal(narg++)->value());
if ((code_src->kwOnlyArgCount() - i) <= (int)kwdefargs.size()) {
fputs(" = ", pyc_output);
print_src(*da++, mod);
@@ -3147,7 +3147,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
for (int i = 0; i < code_src->argCount(); ++i) {
if (narg)
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "%s", code_src->getLocal(narg++)->value());
if ((code_src->argCount() - i) <= (int)defargs.size()) {
fputs(" = ", pyc_output);
print_src(*da++, mod);
@@ -3158,7 +3158,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
fputs(narg == 0 ? "*" : ", *", pyc_output);
for (int i = 0; i < code_src->kwOnlyArgCount(); ++i) {
fputs(", ", pyc_output);
fprintf(pyc_output, "%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "%s", code_src->getLocal(narg++)->value());
if ((code_src->kwOnlyArgCount() - i) <= (int)kwdefargs.size()) {
fputs(" = ", pyc_output);
print_src(*da++, mod);
@@ -3168,12 +3168,12 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
if (code_src->flags() & PycCode::CO_VARARGS) {
if (narg)
fputs(", ", pyc_output);
fprintf(pyc_output, "*%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "*%s", code_src->getLocal(narg++)->value());
}
if (code_src->flags() & PycCode::CO_VARKEYWORDS) {
if (narg)
fputs(", ", pyc_output);
fprintf(pyc_output, "**%s", code_src->getVarName(narg++)->value());
fprintf(pyc_output, "**%s", code_src->getLocal(narg++)->value());
}
if (isLambda) {

View File

@@ -11,6 +11,7 @@ Python MAGIC Python MAGIC Python MAGIC
3.8 0x0A0D0D55
3.9 0x0A0D0D61
3.10 0x0A0D0D6F
3.11 0x0A0D0DA7
1.0 1.1 1.2 1.3 1.4 1.5 1.6

View File

@@ -318,7 +318,7 @@ void print_const(PycRef<PycObject> obj, PycModule* mod, const char* parent_f_str
void bc_next(PycBuffer& source, PycModule* mod, int& opcode, int& operand, int& pos)
{
opcode = Pyc::ByteToOpcode(mod->majorVer(), mod->minorVer(), source.getByte());
bool py36_opcode = (mod->majorVer() > 3 || (mod->majorVer() == 3 && mod->minorVer() >= 6));
bool py36_opcode = (mod->verCompare(3, 6) >= 0);
if (py36_opcode) {
operand = source.getByte();
pos += 2;
@@ -382,7 +382,7 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
}
} else if (Pyc::IsVarNameArg(opcode)) {
try {
fprintf(pyc_output, "%d: %s", operand, code->getVarName(operand)->value());
fprintf(pyc_output, "%d: %s", operand, code->getLocal(operand)->value());
} catch (const std::out_of_range &) {
fprintf(pyc_output, "%d <INVALID>", operand);
}
@@ -397,8 +397,7 @@ void bc_disasm(PycRef<PycCode> code, PycModule* mod, int indent)
if (mod->verCompare(3, 10) >= 0)
offs *= sizeof(uint16_t); // BPO-27129
fprintf(pyc_output, "%d (to %d)", operand, pos+offs);
}
else if (Pyc::IsJumpArg(opcode)) {
} else if (Pyc::IsJumpArg(opcode)) {
if (mod->verCompare(3, 10) >= 0) // BPO-27129
fprintf(pyc_output, "%d (to %d)", operand, int(operand * sizeof(uint16_t)));
else

View File

@@ -44,10 +44,15 @@ void PycCode::load(PycData* stream, PycModule* mod)
m_consts = LoadObject(stream, mod).cast<PycSequence>();
m_names = LoadObject(stream, mod).cast<PycSequence>();
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(3, 11) < 0)
m_varNames = LoadObject(stream, mod).cast<PycSequence>();
if (mod->verCompare(1, 3) >= 0)
m_localNames = LoadObject(stream, mod).cast<PycSequence>();
else
m_varNames = new PycTuple;
m_localNames = new PycTuple;
if (mod->verCompare(3, 11) >= 0)
m_localKinds = LoadObject(stream, mod).cast<PycString>();
else
m_localKinds = new PycString;
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0)
m_freeVars = LoadObject(stream, mod).cast<PycSequence>();
@@ -59,16 +64,6 @@ void PycCode::load(PycData* stream, PycModule* mod)
else
m_cellVars = new PycTuple;
if (mod->verCompare(3, 11) >= 0)
m_localsAndNames = LoadObject(stream, mod).cast<PycSequence>();
else
m_localsAndNames = new PycTuple;
if (mod->verCompare(3, 11) >= 0)
m_localsAndKinds = LoadObject(stream, mod).cast<PycString>();
else
m_localsAndKinds = new PycString;
m_fileName = LoadObject(stream, mod).cast<PycString>();
m_name = LoadObject(stream, mod).cast<PycString>();

View File

@@ -43,11 +43,10 @@ public:
PycRef<PycString> code() const { return m_code; }
PycRef<PycSequence> consts() const { return m_consts; }
PycRef<PycSequence> names() const { return m_names; }
PycRef<PycSequence> varNames() const { return m_varNames; }
PycRef<PycSequence> localNames() const { return m_localNames; }
PycRef<PycString> localKinds() const { return m_localKinds; }
PycRef<PycSequence> freeVars() const { return m_freeVars; }
PycRef<PycSequence> cellVars() const { return m_cellVars; }
PycRef<PycSequence> localsAndNames() const { return m_localsAndNames; }
PycRef<PycString> localsAndKinds() const { return m_localsAndKinds; }
PycRef<PycString> fileName() const { return m_fileName; }
PycRef<PycString> name() const { return m_name; }
PycRef<PycString> qualName() const { return m_qualName; }
@@ -65,9 +64,9 @@ public:
return m_names->get(idx).cast<PycString>();
}
PycRef<PycString> getVarName(int idx) const
PycRef<PycString> getLocal(int idx) const
{
return m_varNames->get(idx).cast<PycString>();
return m_localNames->get(idx).cast<PycString>();
}
PycRef<PycString> getCellVar(int idx) const
@@ -90,11 +89,10 @@ private:
PycRef<PycString> m_code;
PycRef<PycSequence> m_consts;
PycRef<PycSequence> m_names;
PycRef<PycSequence> m_varNames;
PycRef<PycSequence> m_localNames;
PycRef<PycString> m_localKinds;
PycRef<PycSequence> m_freeVars;
PycRef<PycSequence> m_cellVars;
PycRef<PycSequence> m_localsAndNames;
PycRef<PycString> m_localsAndKinds;
PycRef<PycString> m_fileName;
PycRef<PycString> m_name;
PycRef<PycString> m_qualName;

View File

@@ -12,6 +12,9 @@
# define PATHSEP '/'
#endif
// Set this to 1 to print extra details on PyCode objects
#define PRINT_EXTRA_PYCODE_FIELDS 0
static const char* flag_names[] = {
"CO_OPTIMIZED", "CO_NEWLOCALS", "CO_VARARGS", "CO_VARKEYWORDS",
"CO_NESTED", "CO_GENERATOR", "CO_NOFREE", "CO_COROUTINE",
@@ -107,11 +110,21 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
output_object(codeObj->names()->get(i), mod, indent + 2);
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(3, 11) < 0) {
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 (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++)
output_object(codeObj->localNames()->get(i), mod, indent + 2);
}
#if PRINT_EXTRA_PYCODE_FIELDS
if (mod->verCompare(3, 11) >= 0) {
iputs(indent + 1, "[Locals+Kinds]\n");
output_object(codeObj->localKinds().cast<PycObject>(), mod, indent + 2);
}
#endif
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0) {
iputs(indent + 1, "[Free Vars]\n");
for (int i=0; i<codeObj->freeVars()->size(); i++)
@@ -122,15 +135,6 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
output_object(codeObj->cellVars()->get(i), mod, indent + 2);
}
if (mod->verCompare(3, 11) >= 0) {
iputs(indent + 1, "[Locals+Names]\n");
for (int i=0; i<codeObj->localsAndNames()->size(); i++)
output_object(codeObj->localsAndNames()->get(i), mod, indent + 2);
iputs(indent + 1, "[Locals+Kinds]\n");
output_object(codeObj->localsAndKinds().cast<PycObject>(), mod, indent + 2);
}
iputs(indent + 1, "[Constants]\n");
for (int i=0; i<codeObj->consts()->size(); i++)
output_object(codeObj->consts()->get(i), mod, indent + 2);
@@ -138,6 +142,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
iputs(indent + 1, "[Disassembly]\n");
bc_disasm(codeObj, mod, indent + 2);
#if PRINT_EXTRA_PYCODE_FIELDS
if (mod->verCompare(1, 5) >= 0) {
iputs(indent + 1, "[Line Number Table]\n");
output_object(codeObj->lnTable().cast<PycObject>(), mod, indent + 2);
@@ -147,6 +152,7 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
iputs(indent + 1, "[Exception Table]\n");
output_object(codeObj->exceptTable().cast<PycObject>(), mod, indent + 2);
}
#endif
}
break;
case PycObject::TYPE_STRING: