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

bpo-17611. Move unwinding of stack for "pseudo exceptions" from interpreter to compiler. #5071

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove WITH_EXCEPT_FINISH bytecode.
  • Loading branch information
markshannon committed Jan 2, 2018
commit 8e874795781b928b1b24d49585aa0c3a75db44e6
1 change: 0 additions & 1 deletion Include/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ extern "C" {
#define INPLACE_FLOOR_DIVIDE 28
#define INPLACE_TRUE_DIVIDE 29
#define WITH_EXCEPT_START 42
#define WITH_EXCEPT_FINISH 43
#define GET_AITER 50
#define GET_ANEXT 51
#define BEFORE_ASYNC_WITH 52
Expand Down
1 change: 0 additions & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def jabs_op(name, op):
def_op('INPLACE_TRUE_DIVIDE', 29)

def_op('WITH_EXCEPT_START', 42)
def_op('WITH_EXCEPT_FINISH', 43)

def_op('GET_AITER', 50)
def_op('GET_ANEXT', 51)
Expand Down
46 changes: 0 additions & 46 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2944,53 +2944,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
if (res == NULL)
goto error;

Py_INCREF(exc); /* Duplicating the exception on the stack (XXX: required?) */
PUSH(exc);
PUSH(res);
PREDICT(WITH_EXCEPT_FINISH);
DISPATCH();
}

PREDICTED(WITH_EXCEPT_FINISH);
TARGET(WITH_EXCEPT_FINISH) {
PyObject *res = POP();
PyObject *exc = POP();
PyObject *val, *tb;
int err;

if (exc != Py_None)
err = PyObject_IsTrue(res);
else
err = 0;

Py_DECREF(res);
Py_DECREF(exc);

if (err < 0)
goto error;
if (exc != Py_None) {
if (err > 0) {
/* There was an exception and a True return.
* We must manually unwind the EXCEPT_HANDLER block
* which was created when the exception was caught,
* otherwise the stack will be in an inconsisten state.
*/
PyTryBlock *b = PyFrame_BlockPop(f);
assert(b->b_type == EXCEPT_HANDLER);
UNWIND_EXCEPT_HANDLER(b);
/* The bound __exit__ method is still on top */
Py_DECREF(POP());
DISPATCH();
}
else {
assert(TOP() == exc);
STACKADJ(-1);
val = POP();
tb = POP();
PyErr_Restore(exc, val, tb);
goto error;
}
}
DISPATCH();
}

Expand Down
24 changes: 19 additions & 5 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,6 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
return -1;
case WITH_EXCEPT_START:
return 2;
case WITH_EXCEPT_FINISH:
return -2; /* XXX Sometimes more */
case RETURN_VALUE:
return -1;
case IMPORT_STAR:
Expand Down Expand Up @@ -4389,7 +4387,6 @@ expr_constant(expr_ty e)
return -1;
}


static int
compiler_call_exit_with_nones(struct compiler *c) {
PyObject *three_nones = PyTuple_Pack(3, Py_None, Py_None, Py_None);
Expand All @@ -4402,6 +4399,23 @@ compiler_call_exit_with_nones(struct compiler *c) {
return 1;
}

static int
compiler_with_except_finish(struct compiler *c) {
basicblock *exit;
exit = compiler_new_block(c);
if (exit == NULL)
return 0;
ADDOP_JABS(c, POP_JUMP_IF_TRUE, exit);
ADDOP(c, RERAISE);
compiler_use_next_block(c, exit);
ADDOP(c, POP_TOP);
ADDOP(c, POP_TOP);
ADDOP(c, POP_TOP);
ADDOP(c, POP_EXCEPT);
ADDOP(c, POP_TOP);
return 1;
}

/*
Implements the async with statement.

Expand Down Expand Up @@ -4500,7 +4514,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos)
ADDOP(c, GET_AWAITABLE);
ADDOP_O(c, LOAD_CONST, Py_None, consts);
ADDOP(c, YIELD_FROM);
ADDOP(c, WITH_EXCEPT_FINISH);
compiler_with_except_finish(c);

compiler_pop_fblock(c, FINALLY_END, final2);

Expand Down Expand Up @@ -4595,7 +4609,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
return 0;

ADDOP(c, WITH_EXCEPT_START);
ADDOP(c, WITH_EXCEPT_FINISH);
compiler_with_except_finish(c);
compiler_pop_fblock(c, FINALLY_END, final2);

compiler_use_next_block(c, exit);
Expand Down
Loading