Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Unsupported opcode: WITH_EXCEPT_START #303

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,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 @@ -1758,9 +1765,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 @@ -1799,6 +1856,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 @@ -1979,6 +2037,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
}
break;

case Pyc::SETUP_EXCEPT_A:
{
if (curblock->blktype() == ASTBlock::BLK_CONTAINER) {
Expand Down Expand Up @@ -2172,6 +2231,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
else
name = new ASTName(code->getVarName(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