summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2024-05-01 17:23:29 (GMT)
committerGitHub <noreply@github.com>2024-05-01 17:23:29 (GMT)
commitebef3c5ba48aa4d5bdf8c02aba6c6356aef9d56b (patch)
tree7d4a2979efa2dbd1d33b4c1d33fb1a533579e1e5 /Python
parentf5406ef454662b98df107775d18ff71ae6849618 (diff)
downloadcpython-ebef3c5ba48aa4d5bdf8c02aba6c6356aef9d56b.zip
cpython-ebef3c5ba48aa4d5bdf8c02aba6c6356aef9d56b.tar.gz
cpython-ebef3c5ba48aa4d5bdf8c02aba6c6356aef9d56b.tar.bz2
[3.12] gh-116767: fix crash on 'async with' with many context managers (GH-118348) (#118477)
gh-116767: fix crash on 'async with' with many context managers (GH-118348) Account for `add_stopiteration_handler` pushing a block for `async with`. To allow generator functions that previously almost hit the `CO_MAXBLOCKS` limit by nesting non-async blocks, the limit is increased by 1. This increase allows one more block in non-generator functions. (cherry picked from commit c1bf4874c1e9db2beda1d62c8c241229783c789b)
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/Python/compile.c b/Python/compile.c
index a871e9c..c5d7d38 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -129,7 +129,8 @@ compiler IR.
enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
- EXCEPTION_GROUP_HANDLER, ASYNC_COMPREHENSION_GENERATOR };
+ EXCEPTION_GROUP_HANDLER, ASYNC_COMPREHENSION_GENERATOR,
+ STOP_ITERATION };
struct fblockinfo {
enum fblocktype fb_type;
@@ -1546,6 +1547,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc,
case EXCEPTION_HANDLER:
case EXCEPTION_GROUP_HANDLER:
case ASYNC_COMPREHENSION_GENERATOR:
+ case STOP_ITERATION:
return SUCCESS;
case FOR_LOOP:
@@ -2235,14 +2237,26 @@ compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t f
c->u->u_metadata.u_argcount = asdl_seq_LEN(args->args);
c->u->u_metadata.u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
c->u->u_metadata.u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
+
+ NEW_JUMP_TARGET_LABEL(c, start);
+ USE_LABEL(c, start);
+ bool add_stopiteration_handler = c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator;
+ if (add_stopiteration_handler) {
+ /* wrap_in_stopiteration_handler will push a block, so we need to account for that */
+ RETURN_IF_ERROR(
+ compiler_push_fblock(c, NO_LOCATION, STOP_ITERATION,
+ start, NO_LABEL, NULL));
+ }
+
for (Py_ssize_t i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
}
- if (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator) {
+ if (add_stopiteration_handler) {
if (wrap_in_stopiteration_handler(c) < 0) {
compiler_exit_scope(c);
return ERROR;
}
+ compiler_pop_fblock(c, STOP_ITERATION, start);
}
PyCodeObject *co = optimize_and_assemble(c, 1);
compiler_exit_scope(c);