[3.15] gh-150700: Fix class-scope inline comprehensions when nested scopes reference __class__ and friends (GH-150735)#151211
Merged
Conversation
…opes reference `__class__` and friends (pythonGH-150735) * Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends In `inline_comprehension()`, when `__class__` / `__classdict__` / `__conditional_annotations__` appears as `FREE` in a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded from `comp_free` unconditionally. This prevents `drop_class_free()` from seeing it, so the appropriate `ste_needs_(...)` flag is never set on the enclosing class. That leads to `codegen_make_closure()` throwing `SystemError` when it couldn't find `__class__` / `__classdict__` / `__conditional_annotations__` in the class's cellvars. From now on we just discard from `comp_free` when no child scope (e.g. a lambda) still needs the name as `FREE`. When a child scope does need it, keep it in `comp_free` so `drop_class_free()` can set the appropriate flag and the class creates the implicit cell. * Fix tests * Fix typo * Fix formatting * Add test checking validity of `__class__` returned * Prefer 'used' to 'deferred' (cherry picked from commit ce916dc) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
This was referenced Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
__class__and friendsIn
inline_comprehension(), when__class__/__classdict__/__conditional_annotations__appears asFREEin a comprehension'ssymbol table because a nested scope captured it (e.g. nested lambdas),
this name is still discarded from
comp_freeunconditionally.This prevents
drop_class_free()from seeing it, so the appropriateste_needs_(...)flag is never set on the enclosing class.That leads to
codegen_make_closure()throwingSystemErrorwhen itcouldn't find
__class__/__classdict__/__conditional_annotations__in the class's cellvars.From now on we just discard from
comp_freewhen no child scope(e.g. a lambda) still needs the name as
FREE. When a child scope doesneed it, keep it in
comp_freesodrop_class_free()can set theappropriate flag and the class creates the implicit cell.
Fix tests
Fix typo
Fix formatting
Add test checking validity of
__class__returnedPrefer 'used' to 'deferred'
(cherry picked from commit ce916dc)
Co-authored-by: Bartosz Sławecki bartosz@ilikepython.com