diff options
author | Irit Katriel <1055913+iritkatriel@users.noreply.github.com> | 2022-01-04 10:37:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-04 10:37:12 (GMT) |
commit | a94461d7189d7f1147ab304a332c8684263dc17e (patch) | |
tree | bda2df4f74cc58205d548dce76337260984962fd /Python/compile.c | |
parent | a09062c267a94200ad299f779429fea1b571ee35 (diff) | |
download | cpython-a94461d7189d7f1147ab304a332c8684263dc17e.zip cpython-a94461d7189d7f1147ab304a332c8684263dc17e.tar.gz cpython-a94461d7189d7f1147ab304a332c8684263dc17e.tar.bz2 |
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
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/Python/compile.c b/Python/compile.c index 48250b5..9d37529 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; |