mirror of
https://github.com/python/cpython.git
synced 2025-08-27 20:25:18 +00:00
gh-104602: ensure all cellvars are known up front (#104603)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
3fadd7d585
commit
86e6f16ccb
4 changed files with 50 additions and 22 deletions
|
@ -381,6 +381,32 @@ class ListComprehensionTest(unittest.TestCase):
|
|||
with self.assertRaises(UnboundLocalError):
|
||||
f()
|
||||
|
||||
def test_global_outside_cellvar_inside_plus_freevar(self):
|
||||
code = """
|
||||
a = 1
|
||||
def f():
|
||||
func, = [(lambda: b) for b in [a]]
|
||||
return b, func()
|
||||
x = f()
|
||||
"""
|
||||
self._check_in_scopes(
|
||||
code, {"x": (2, 1)}, ns={"b": 2}, scopes=["function", "module"])
|
||||
# inside a class, the `a = 1` assignment is not visible
|
||||
self._check_in_scopes(code, raises=NameError, scopes=["class"])
|
||||
|
||||
def test_cell_in_nested_comprehension(self):
|
||||
code = """
|
||||
a = 1
|
||||
def f():
|
||||
(func, inner_b), = [[lambda: b for b in c] + [b] for c in [[a]]]
|
||||
return b, inner_b, func()
|
||||
x = f()
|
||||
"""
|
||||
self._check_in_scopes(
|
||||
code, {"x": (2, 2, 1)}, ns={"b": 2}, scopes=["function", "module"])
|
||||
# inside a class, the `a = 1` assignment is not visible
|
||||
self._check_in_scopes(code, raises=NameError, scopes=["class"])
|
||||
|
||||
def test_name_error_in_class_scope(self):
|
||||
code = """
|
||||
y = 1
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue