gh-150700: Fix class-scope inline comprehensions when nested scopes r… · python/cpython@ce916dc · GitHub
Skip to content

Commit ce916dc

Browse files
authored
gh-150700: Fix class-scope inline comprehensions when nested scopes reference __class__ and friends (#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'
1 parent 5804991 commit ce916dc

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

Lib/test/test_listcomps.py

Lines changed: 31 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a :exc:`SystemError` when compiling a class-scope comprehension containing
2+
a ``lambda`` that references ``__class__``, ``__classdict__``, or
3+
``__conditional_annotations__``. Patch by Bartosz Sławecki.

Python/symtable.c

Lines changed: 9 additions & 4 deletions

0 commit comments

Comments
 (0)