gh-150700: Fix class-scope inline comprehensions when nested scopes reference __class__ and friends#150735
Conversation
…_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.
…th-lambda-raises-syste
|
Updating branch to see if it fixes doc build issue. |
carljm
left a comment
There was a problem hiding this comment.
This looks good to me. Might be nice to have some tests that validate more than "compilation doesn't fail" but actually check we are getting the right value out of __class__.
Excellent, added. |
|
Thanks for the fix! |
|
Thanks @johnslavik for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-151211 is a backport of this pull request to the 3.15 branch. |
|
GH-151212 is a backport of this pull request to the 3.14 branch. |
…copes reference `__class__` and friends (GH-150735) (#151212) gh-150700: Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends (GH-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>
|
…copes reference `__class__` and friends (GH-150735) (#151211) gh-150700: Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends (GH-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>
Follow up to GH-120295.
In
inline_comprehension(), when__class__/__classdict__/__conditional_annotations__appears asFREEin a comprehension's symbol table because a nested scope captured it (e.g. nested lambdas), this name is still discarded fromcomp_freeunconditionally. This preventsdrop_class_free()from seeing it, so the appropriateste_needs_(...)flag is never set on the enclosing class. That leads tocodegen_make_closure()throwingSystemErrorwhen it couldn'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 asFREE. When a child scope does need it, keep it incomp_freesodrop_class_free()can set the appropriate flag and the class creates the implicit cell.