Skip to content

[mypyc] Fix segfault in zip() with an unfinished native generator - #22144

Merged
JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1234
Oct 9, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
rheard:fix-mypyc-1234

Conversation

@rheard

@rheard rheard commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1234.

A for loop over a call to a compiled generator function calls the generator's helper method directly (ForNativeGenerator, added in #19415). The helper sets the generator's return value only when the generator returns, so if the helper returns NULL without setting it, the generator raised. This was checked in gen_cleanup(), which runs when the loop exits. That works for a plain loop, but in zip() another iterator can end the loop while the generator is suspended. The return value was NULL then too, so the loop took its error path without an exception set, and crashed.

The check now happens in gen_condition(), in a block that's only reached when the generator stops, so ForNativeGenerator no longer needs a cleanup block. For other loops, the generated code is the same, just in a different order. No IR tests cover these loops, so none change.

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix!

@JukkaL
JukkaL merged commit 7907393 into python:master Oct 9, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

for loop over zip() segfaults when another argument runs out before a compiled generator

2 participants