Skip to content

Commit

Permalink
Fix for Unsupported opcode: WITH_EXCEPT_START zrax#303
Browse files Browse the repository at this point in the history
  • Loading branch information
mycroftjr committed Oct 23, 2024
1 parent 9c085c5 commit 8f20304
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,28 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
bool variable_annotations = false;

while (!source.atEof()) {

curpos = pos;
bc_next(source, mod, opcode, operand, pos);

#if defined(BLOCK_DEBUG) || defined(STACK_DEBUG)
fprintf(stderr, "%-7d", pos);
// DEBUG Positon in source file
fprintf(stderr, "%-7d %-3d %-16s_%-2d", pos, opcode, Pyc::OpcodeName(opcode & 0xFF), operand);
#ifdef STACK_DEBUG
fprintf(stderr, "%-5d", (unsigned int)stack_hist.size() + 1);
#endif
#ifdef BLOCK_DEBUG
// DEBUG Indept
for (unsigned int i = 0; i < blocks.size(); i++)
fprintf(stderr, " ");

// DEBUG Type String & Length
fprintf(stderr, "%s (%d)", curblock->type_str(), curblock->end());
#endif
fprintf(stderr, "\n");
#endif

curpos = pos;
bc_next(source, mod, opcode, operand, pos);


if (need_try && opcode != Pyc::SETUP_EXCEPT_A) {
need_try = false;
Expand Down Expand Up @@ -1675,9 +1682,59 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
}
break;

case Pyc::POP_EXCEPT:
/* Do nothing. */
/* Do nothing.
Pops a value from the stack, which is used to restore the exception state.
Changed in version 3.11: Exception representation on the stack now consist of one, not three, items.
*/
break;

case Pyc::WITH_EXCEPT_START:
/*
Calls the function in position 4 on the stack with arguments (type, val, tb) representing the exception at the top of the stack. Used to implement the call context_manager.__exit__(*exc_info()) when an exception has occurred in a with statement.
New in version 3.9.
The __exit__ function is in position 4 of the stack rather than 7. Exception representation on the stack now consist of one, not three, items.
Changed in version 3.11:
*/
{
//curblock = ASTBlock::BLK_EXCEPT;
PycRef<PycString> msg = new PycString();
msg->setValue(
"# Decompile 'WITH_EXCEPT_START' is not implemented yet.\n"
);
//fputs( msg->strValue, pyc_output);
OutputString(msg);
/*
// TODO: Get that message into the AST (Abtract Syntax Tree) to be shown at the right spot
// However that f*** Types in CPP gimme a crisis
//
// ... and again I know why I prefer Python when coding.
PycRef<PycString> name = new ASTName( msg );
curblock->append(
name.cast<PycString>()
);
*/
}
break;

case Pyc::RERAISE:
// Re-raises the exception currently on top of the stack.
// If oparg is non-zero, pops an additional value from the stack which is used to
// set f_lasti of the current frame.
// New in version 3.9.
// Changed in version 3.11: Exception representation on the stack now consist of one, not three, items.
//https://docs.python.org/3/library/dis.html#opcode-RERAISE

{
const char* msg = "# Decompile 'RERAISE' is not implemented yet.\n";
fputs(msg, pyc_output);
}
break;

case Pyc::POP_TOP:
{
PycRef<ASTNode> value = stack.top();
Expand Down Expand Up @@ -1716,6 +1773,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTPrint> printNode;
if (curblock->size() > 0 && curblock->nodes().back().type() == ASTNode::NODE_PRINT)
printNode = curblock->nodes().back().try_cast<ASTPrint>();

if (printNode && printNode->stream() == nullptr && !printNode->eol())
printNode->add(stack.top());
else
Expand Down Expand Up @@ -2102,6 +2160,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
else
name = new ASTName(code->getLocal(operand));


if (name.cast<ASTName>()->name()->value()[0] == '_'
&& name.cast<ASTName>()->name()->value()[1] == '[') {
/* Don't show stores of list comp append objects. */
Expand Down

0 comments on commit 8f20304

Please sign in to comment.