bpo-46202: Remove opcode POP_EXCEPT_AND_RERAISE (GH-30302)

* bpo-46202: remove opcode POP_EXCEPT_AND_RERAISE

* do not assume that an exception group is truthy
This commit is contained in:
Irit Katriel 2022-01-04 10:37:12 +00:00 committed by GitHub
parent a09062c267
commit a94461d718
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 138 additions and 146 deletions

View file

@ -2725,31 +2725,6 @@ check_eval_breaker:
DISPATCH();
}
TARGET(POP_EXCEPT_AND_RERAISE) {
PyObject *lasti = PEEK(2);
if (PyLong_Check(lasti)) {
frame->f_lasti = PyLong_AsLong(lasti);
assert(!_PyErr_Occurred(tstate));
}
else {
_PyErr_SetString(tstate, PyExc_SystemError, "lasti is not an int");
goto error;
}
PyObject *value = POP();
assert(value);
assert(PyExceptionInstance_Check(value));
PyObject *type = Py_NewRef(PyExceptionInstance_Class(value));
PyObject *traceback = PyException_GetTraceback(value);
Py_DECREF(POP()); /* lasti */
_PyErr_Restore(tstate, type, value, traceback);
_PyErr_StackItem *exc_info = tstate->exc_info;
value = exc_info->exc_value;
exc_info->exc_value = POP();
Py_XDECREF(value);
goto exception_unwind;
}
TARGET(RERAISE) {
if (oparg) {
PyObject *lasti = PEEK(oparg + 1);

View file

@ -1049,8 +1049,6 @@ stack_effect(int opcode, int oparg, int jump)
return 0;
case POP_EXCEPT:
return -1;
case POP_EXCEPT_AND_RERAISE:
return -3;
case STORE_NAME:
return -1;
@ -1669,6 +1667,9 @@ compiler_addop_j_noline(struct compiler *c, int opcode, basicblock *b)
#define ADD_YIELD_FROM(C) \
RETURN_IF_FALSE(compiler_add_yield_from((C)))
#define POP_EXCEPT_AND_RERAISE(C) \
RETURN_IF_FALSE(compiler_pop_except_and_reraise((C)))
#define VISIT(C, TYPE, V) {\
if (!compiler_visit_ ## TYPE((C), (V))) \
return 0; \
@ -1839,6 +1840,22 @@ compiler_add_yield_from(struct compiler *c)
return 1;
}
static int
compiler_pop_except_and_reraise(struct compiler *c)
{
/* Stack contents
* [exc_info, lasti, exc] COPY 3
* [exc_info, lasti, exc, exc_info] POP_EXCEPT
* [exc_info, lasti, exc] RERAISE 1
* (exception_unwind clears the stack)
*/
ADDOP_I(c, COPY, 3);
ADDOP(c, POP_EXCEPT);
ADDOP_I(c, RERAISE, 1);
return 1;
}
/* Unwind a frame block. If preserve_tos is true, the TOS before
* popping the blocks will be restored afterwards, unless another
* return, break or continue is found. In which case, the TOS will
@ -3235,7 +3252,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
compiler_pop_fblock(c, FINALLY_END, end);
ADDOP_I(c, RERAISE, 0);
compiler_use_next_block(c, cleanup);
ADDOP(c, POP_EXCEPT_AND_RERAISE);
POP_EXCEPT_AND_RERAISE(c);
compiler_use_next_block(c, exit);
return 1;
}
@ -3290,7 +3307,7 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s)
compiler_pop_fblock(c, FINALLY_END, end);
ADDOP_I(c, RERAISE, 0);
compiler_use_next_block(c, cleanup);
ADDOP(c, POP_EXCEPT_AND_RERAISE);
POP_EXCEPT_AND_RERAISE(c);
compiler_use_next_block(c, exit);
return 1;
}
@ -3446,7 +3463,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
ADDOP_I(c, RERAISE, 0);
compiler_use_next_block(c, cleanup);
ADDOP(c, POP_EXCEPT_AND_RERAISE);
POP_EXCEPT_AND_RERAISE(c);
compiler_use_next_block(c, orelse);
VISIT_SEQ(c, stmt, s->v.Try.orelse);
ADDOP_JUMP(c, JUMP_FORWARD, end);
@ -3497,7 +3514,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
[exc] RER: ROT_TWO
[exc, prev_exc_info] POP_EXCEPT
[exc] RERAISE 0
[exc] RERAISE 0
[] L0: <next statement>
*/
@ -3677,7 +3694,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s)
ADDOP(c, POP_EXCEPT);
ADDOP_I(c, RERAISE, 0);
compiler_use_next_block(c, cleanup);
ADDOP(c, POP_EXCEPT_AND_RERAISE);
POP_EXCEPT_AND_RERAISE(c);
compiler_use_next_block(c, orelse);
VISIT_SEQ(c, stmt, s->v.TryStar.orelse);
ADDOP_JUMP(c, JUMP_FORWARD, end);
@ -5429,7 +5446,7 @@ compiler_with_except_finish(struct compiler *c, basicblock * cleanup) {
NEXT_BLOCK(c);
ADDOP_I(c, RERAISE, 2);
compiler_use_next_block(c, cleanup);
ADDOP(c, POP_EXCEPT_AND_RERAISE);
POP_EXCEPT_AND_RERAISE(c);
compiler_use_next_block(c, exit);
ADDOP(c, POP_TOP); /* exc_value */
ADDOP(c, POP_BLOCK);
@ -7032,8 +7049,7 @@ stackdepth(struct compiler *c)
instr->i_opcode == JUMP_FORWARD ||
instr->i_opcode == RETURN_VALUE ||
instr->i_opcode == RAISE_VARARGS ||
instr->i_opcode == RERAISE ||
instr->i_opcode == POP_EXCEPT_AND_RERAISE)
instr->i_opcode == RERAISE)
{
/* remaining code is dead */
next = NULL;
@ -8756,7 +8772,6 @@ normalize_basic_block(basicblock *bb) {
case RETURN_VALUE:
case RAISE_VARARGS:
case RERAISE:
case POP_EXCEPT_AND_RERAISE:
bb->b_exit = 1;
bb->b_nofallthrough = 1;
break;

View file

@ -36,7 +36,6 @@ static void *opcode_targets[256] = {
&&TARGET_BINARY_SUBSCR_DICT,
&&TARGET_PUSH_EXC_INFO,
&&TARGET_STORE_SUBSCR_ADAPTIVE,
&&TARGET_POP_EXCEPT_AND_RERAISE,
&&TARGET_STORE_SUBSCR_LIST_INT,
&&TARGET_STORE_SUBSCR_DICT,
&&TARGET_CALL_NO_KW_ADAPTIVE,
@ -48,45 +47,46 @@ static void *opcode_targets[256] = {
&&TARGET_CALL_NO_KW_LIST_APPEND,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_O,
&&TARGET_CALL_NO_KW_TYPE_1,
&&TARGET_CALL_NO_KW_BUILTIN_CLASS_1,
&&TARGET_WITH_EXCEPT_START,
&&TARGET_GET_AITER,
&&TARGET_GET_ANEXT,
&&TARGET_BEFORE_ASYNC_WITH,
&&TARGET_BEFORE_WITH,
&&TARGET_END_ASYNC_FOR,
&&TARGET_CALL_NO_KW_BUILTIN_CLASS_1,
&&TARGET_CALL_NO_KW_METHOD_DESCRIPTOR_FAST,
&&TARGET_JUMP_ABSOLUTE_QUICK,
&&TARGET_LOAD_ATTR_ADAPTIVE,
&&TARGET_LOAD_ATTR_INSTANCE_VALUE,
&&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_STORE_SUBSCR,
&&TARGET_DELETE_SUBSCR,
&&TARGET_LOAD_ATTR_WITH_HINT,
&&TARGET_LOAD_ATTR_SLOT,
&&TARGET_LOAD_ATTR_MODULE,
&&TARGET_LOAD_GLOBAL_ADAPTIVE,
&&TARGET_LOAD_GLOBAL_MODULE,
&&TARGET_LOAD_GLOBAL_BUILTIN,
&&TARGET_LOAD_METHOD_ADAPTIVE,
&&TARGET_GET_ITER,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_PRINT_EXPR,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_METHOD_ADAPTIVE,
&&TARGET_LOAD_METHOD_CACHED,
&&TARGET_GET_AWAITABLE,
&&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_LOAD_METHOD_CACHED,
&&TARGET_LOAD_METHOD_CLASS,
&&TARGET_LOAD_METHOD_MODULE,
&&TARGET_LOAD_METHOD_NO_DICT,
&&TARGET_STORE_ATTR_ADAPTIVE,
&&TARGET_STORE_ATTR_INSTANCE_VALUE,
&&TARGET_STORE_ATTR_SLOT,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_LIST_TO_TUPLE,
&&TARGET_RETURN_VALUE,
&&TARGET_IMPORT_STAR,
&&TARGET_SETUP_ANNOTATIONS,
&&TARGET_YIELD_VALUE,
&&TARGET_STORE_ATTR_WITH_HINT,
&&TARGET_LOAD_FAST__LOAD_FAST,
&&TARGET_PREP_RERAISE_STAR,
&&TARGET_POP_EXCEPT,
&&TARGET_STORE_NAME,
@ -127,20 +127,20 @@ static void *opcode_targets[256] = {
&&TARGET_STORE_FAST,
&&TARGET_DELETE_FAST,
&&TARGET_JUMP_IF_NOT_EG_MATCH,
&&TARGET_LOAD_FAST__LOAD_FAST,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_GEN_START,
&&TARGET_RAISE_VARARGS,
&&TARGET_STORE_FAST__LOAD_FAST,
&&TARGET_LOAD_FAST__LOAD_CONST,
&&TARGET_MAKE_FUNCTION,
&&TARGET_BUILD_SLICE,
&&TARGET_LOAD_FAST__LOAD_CONST,
&&TARGET_LOAD_CONST__LOAD_FAST,
&&TARGET_MAKE_CELL,
&&TARGET_LOAD_CLOSURE,
&&TARGET_LOAD_DEREF,
&&TARGET_STORE_DEREF,
&&TARGET_DELETE_DEREF,
&&TARGET_LOAD_CONST__LOAD_FAST,
&&TARGET_STORE_FAST__STORE_FAST,
&&_unknown_opcode,
&&TARGET_CALL_FUNCTION_EX,
&&_unknown_opcode,
&&TARGET_EXTENDED_ARG,