]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-150700: Fix class-scope inline comprehensions when nested scopes reference...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 9 Jun 2026 22:48:25 +0000 (00:48 +0200)
committerGitHub <noreply@github.com>
Tue, 9 Jun 2026 22:48:25 +0000 (22:48 +0000)
commit51e83ca706d04878e0f6f0bf8030e0dd2f7585a6
treea2a9a54175f29dc61c8f66063ddca4f1f4272b70
parent24cb16ee804791295b442eefc2dd30f4926efab9
[3.14] gh-150700: Fix class-scope inline comprehensions when nested scopes 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 ce916dc50644bb1de940f5fb580bd9907cceb959)

Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Lib/test/test_listcomps.py
Misc/NEWS.d/next/Core_and_Builtins/2026-06-01-19-00-00.gh-issue-150700.W8CzVR.rst [new file with mode: 0644]
Python/symtable.c