gh-104619: never leak comprehension locals to outer locals() (#104637)

This commit is contained in:
Carl Meyer 2023-05-18 19:50:24 -06:00 committed by GitHub
parent 86e6f16ccb
commit 70c7796477
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 21 deletions

View file

@ -516,6 +516,19 @@ class ListComprehensionTest(unittest.TestCase):
"""
self._check_in_scopes(code, {"a": [1]}, scopes=["function"])
def test_no_leakage_to_locals(self):
code = """
def b():
[a for b in [1] for _ in []]
return b, locals()
r, s = b()
x = r is b
y = list(s.keys())
"""
self._check_in_scopes(code, {"x": True, "y": []}, scopes=["module"])
self._check_in_scopes(code, {"x": True, "y": ["b"]}, scopes=["function"])
self._check_in_scopes(code, raises=NameError, scopes=["class"])
__test__ = {'doctests' : doctests}