gh-150700: Fix class-scope inline comprehensions when nested scopes reference `__class__` and friends by johnslavik · Pull Request #150735 · python/cpython · GitHub
Skip to content

gh-150700: Fix class-scope inline comprehensions when nested scopes reference __class__ and friends - #150735

Merged
carljm merged 7 commits into
python:mainfrom
johnslavik:gh-150700-class-scope-comprehension-with-lambda-raises-syste
Jun 9, 2026
Merged

carljm merged 7 commits into
python:mainfrom
johnslavik:gh-150700-class-scope-comprehension-with-lambda-raises-syste

Conversation

@johnslavik

@johnslavik johnslavik commented Jun 1, 2026

Copy link
Copy Markdown
Member

Follow up to GH-120295.

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.

…_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.
@johnslavik johnslavik added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jun 1, 2026
@johnslavik

Copy link
Copy Markdown
Member Author

Comment thread Lib/test/test_listcomps.py Outdated

@carljm carljm left a comment

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 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__.

@johnslavik

Copy link
Copy Markdown
Member Author

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.

@carljm

carljm commented Jun 9, 2026

Copy link
Copy Markdown
Member

Thanks for the fix!

@carljm
carljm merged commit ce916dc into python:main Jun 9, 2026
54 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @johnslavik for the PR, and @carljm for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151211 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 9, 2026
@bedevere-app

bedevere-app Bot commented Jun 9, 2026

Copy link
Copy Markdown

GH-151212 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 9, 2026
carljm pushed a commit that referenced this pull request Jun 9, 2026
…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>
@bedevere-bot

Copy link
Copy Markdown

carljm pushed a commit that referenced this pull request Jun 9, 2026
…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>
philthompson10 pushed a commit to philthompson10/cpython that referenced this pull request Jun 17, 2026
…opes reference `__class__` and friends (python#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'
@johnslavik
johnslavik deleted the gh-150700-class-scope-comprehension-with-lambda-raises-syste branch July 21, 2026 01:55
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.

5 participants