mirror of
https://github.com/python/cpython.git
synced 2025-08-10 11:58:39 +00:00
[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:
parent
320d398262
commit
1a15d20b75
3 changed files with 90 additions and 14 deletions
|
@ -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}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue