aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/compile.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2022-10-27 03:55:03 -0700
committerGitHub <noreply@github.com>2022-10-27 11:55:03 +0100
commit22863df7ca5f9cd01a40ab3dce3d067ec5666081 (patch)
tree73c460d80f41b45a331b7d886defe39127e12472 /Python/compile.c
parente60892f9db1316dbabf7a652d7648e4f968b745d (diff)
downloadcpython-22863df7ca5f9cd01a40ab3dce3d067ec5666081.tar.gz
cpython-22863df7ca5f9cd01a40ab3dce3d067ec5666081.zip
GH-96793: Change `FOR_ITER` to not pop the iterator on exhaustion. (GH-96801)
Change FOR_ITER to have the same stack effect regardless of whether it branches or not. Performance is unchanged as FOR_ITER (and specialized forms jump over the cleanup code).
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 67ccd223938..779729119e2 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1042,6 +1042,8 @@ stack_effect(int opcode, int oparg, int jump)
return -1;
case SWAP:
return 0;
+ case END_FOR:
+ return -2;
/* Unary operators */
case UNARY_POSITIVE:
@@ -1098,8 +1100,7 @@ stack_effect(int opcode, int oparg, int jump)
case UNPACK_EX:
return (oparg&0xFF) + (oparg>>8);
case FOR_ITER:
- /* -1 at end of iterator, 1 if continue iterating. */
- return jump > 0 ? -1 : 1;
+ return 1;
case SEND:
return jump > 0 ? -1 : 0;
case STORE_ATTR:
@@ -3103,6 +3104,7 @@ compiler_for(struct compiler *c, stmt_ty s)
ADDOP_JUMP(c, NO_LOCATION, JUMP, start);
USE_LABEL(c, cleanup);
+ ADDOP(c, NO_LOCATION, END_FOR);
compiler_pop_fblock(c, FOR_LOOP, start);
@@ -5315,6 +5317,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc,
ADDOP_JUMP(c, elt_loc, JUMP, start);
USE_LABEL(c, anchor);
+ ADDOP(c, NO_LOCATION, END_FOR);
}
return 1;