Skip to content

gh-156466: ensure in codegen that a pushed COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR is always popped - #157338

Open
iritkatriel wants to merge 1 commit into
python:mainfrom
iritkatriel:codegen_async_comprehension_generator
Open

gh-156466: ensure in codegen that a pushed COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR is always popped#157338
iritkatriel wants to merge 1 commit into
python:mainfrom
iritkatriel:codegen_async_comprehension_generator

Conversation

@iritkatriel

@iritkatriel iritkatriel commented Sep 11, 2026

Copy link
Copy Markdown
Member

In case of error after COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR is pushed, it was not always popped. This hasn't caused a problem in main AFAIK, but it was exposed during work on gh-156819 when compiling the invalid syntax {a async for b in d for *(b,) in e}.

Comment thread Python/codegen.c
int ret = codegen_async_comprehension_generator_body(
c, loc, generators, gen_index, depth, elt, val, type, avoid_creation,
gen, start);
_PyCompile_PopFBlock(c, COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR, start);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can pop the wrong block after a codegen error in a nested inlined comprehension. For example, compiling this source aborts a debug build:

async def f(it):
    return [[f(a=1, a=2) for y in z] async for x in it]

I verified that the parent commit raises SyntaxError: keyword argument repeated: a, while this PR aborts on the block-type assertion in _PyCompile_PopFBlock.

The inner comprehension leaves its COMPILE_FBLOCK_INLINED_COMPREHENSION pushed when codegen_comprehension takes its error path, so it is still above the async block when we get here. Could we make sure nested inlined-comprehension blocks are cleaned up on error before making this pop unconditional, and add a regression test for this case?

@bedevere-app

bedevere-app Bot commented Sep 11, 2026

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants