[3.12] gh-108654: restore comprehension locals before handling exception (GH-108659) (#108700)

gh-108654: restore comprehension locals before handling exception (GH-108659)
(cherry picked from commit d52c4482a8)

Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-08-31 06:32:17 -07:00 committed by GitHub
parent 320d398262
commit 1a15d20b75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 14 deletions

View file

@ -561,6 +561,41 @@ class ListComprehensionTest(unittest.TestCase):
}
)
def test_comp_in_try_except(self):
template = """
value = ["a"]
try:
[{func}(value) for value in value]
except:
pass
"""
for func in ["str", "int"]:
code = template.format(func=func)
raises = func != "str"
with self.subTest(raises=raises):
self._check_in_scopes(code, {"value": ["a"]})
def test_comp_in_try_finally(self):
code = """
def f(value):
try:
[{func}(value) for value in value]
finally:
return value
ret = f(["a"])
"""
self._check_in_scopes(code, {"ret": ["a"]})
def test_exception_in_post_comp_call(self):
code = """
value = [1, None]
try:
[v for v in value].sort()
except:
pass
"""
self._check_in_scopes(code, {"value": [1, None]})
__test__ = {'doctests' : doctests}