diff options
| author | Mark Shannon <mark@hotpy.org> | 2024-06-18 11:17:46 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-18 11:17:46 (GMT) |
| commit | 9cefcc0ee781a1bef9e0685c2271237005fb488b (patch) | |
| tree | 7c02067f4e021d90ec4fd850d9a06315b8fc39ab /Python/compile.c | |
| parent | 73dc1c678eb720c2ced94d2f435a908bb6d18566 (diff) | |
| download | cpython-9cefcc0ee781a1bef9e0685c2271237005fb488b.zip cpython-9cefcc0ee781a1bef9e0685c2271237005fb488b.tar.gz cpython-9cefcc0ee781a1bef9e0685c2271237005fb488b.tar.bz2 | |
GH-120507: Lower the `BEFORE_WITH` and `BEFORE_ASYNC_WITH` instructions. (#120640)
* Remove BEFORE_WITH and BEFORE_ASYNC_WITH instructions.
* Add LOAD_SPECIAL instruction
* Reimplement `with` and `async with` statements using LOAD_SPECIAL
Diffstat (limited to 'Python/compile.c')
| -rw-r--r-- | Python/compile.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Python/compile.c b/Python/compile.c index 749b69f..53fe6db 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1260,7 +1260,7 @@ compiler_call_exit_with_nones(struct compiler *c, location loc) ADDOP_LOAD_CONST(c, loc, Py_None); ADDOP_LOAD_CONST(c, loc, Py_None); ADDOP_LOAD_CONST(c, loc, Py_None); - ADDOP_I(c, loc, CALL, 2); + ADDOP_I(c, loc, CALL, 3); return SUCCESS; } @@ -1369,6 +1369,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, *ploc = LOC((stmt_ty)info->fb_datum); ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { + ADDOP_I(c, *ploc, SWAP, 3); ADDOP_I(c, *ploc, SWAP, 2); } RETURN_IF_ERROR(compiler_call_exit_with_nones(c, *ploc)); @@ -5897,6 +5898,7 @@ compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) { ADDOP(c, NO_LOCATION, POP_EXCEPT); ADDOP(c, NO_LOCATION, POP_TOP); ADDOP(c, NO_LOCATION, POP_TOP); + ADDOP(c, NO_LOCATION, POP_TOP); NEW_JUMP_TARGET_LABEL(c, exit); ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, exit); @@ -5952,7 +5954,12 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) /* Evaluate EXPR */ VISIT(c, expr, item->context_expr); loc = LOC(item->context_expr); - ADDOP(c, loc, BEFORE_ASYNC_WITH); + ADDOP_I(c, loc, COPY, 1); + ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AEXIT__); + ADDOP_I(c, loc, SWAP, 2); + ADDOP_I(c, loc, SWAP, 3); + ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___AENTER__); + ADDOP_I(c, loc, CALL, 0); ADDOP_I(c, loc, GET_AWAITABLE, 1); ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); @@ -6050,7 +6057,12 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) VISIT(c, expr, item->context_expr); /* Will push bound __exit__ */ location loc = LOC(item->context_expr); - ADDOP(c, loc, BEFORE_WITH); + ADDOP_I(c, loc, COPY, 1); + ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___EXIT__); + ADDOP_I(c, loc, SWAP, 2); + ADDOP_I(c, loc, SWAP, 3); + ADDOP_I(c, loc, LOAD_SPECIAL, SPECIAL___ENTER__); + ADDOP_I(c, loc, CALL, 0); ADDOP_JUMP(c, loc, SETUP_WITH, final); /* SETUP_WITH pushes a finally block. */ |
