Skip to content

Commit 5305cdc

Browse files
[3.12] gh-109182: Fix and improve tests for gh-108654 (GH-109189) (#109271)
gh-109182: Fix and improve tests for gh-108654 (GH-109189) (cherry picked from commit c0f488b) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3efe7bc commit 5305cdc

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

Lib/test/test_listcomps.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def get_output(moddict, name):
125125
self.assertIs(type(e), raises)
126126
else:
127127
for k, v in (outputs or {}).items():
128-
self.assertEqual(get_output(newns, k), v)
128+
self.assertEqual(get_output(newns, k), v, k)
129129

130130
def test_lambdas_with_iteration_var_as_default(self):
131131
code = """
@@ -563,28 +563,38 @@ def test_iter_var_available_in_locals(self):
563563

564564
def test_comp_in_try_except(self):
565565
template = """
566-
value = ["a"]
566+
value = ["ab"]
567+
result = snapshot = None
567568
try:
568-
[{func}(value) for value in value]
569+
result = [{func}(value) for value in value]
569570
except:
570-
pass
571+
snapshot = value
572+
raise
571573
"""
572-
for func in ["str", "int"]:
573-
code = template.format(func=func)
574-
raises = func != "str"
575-
with self.subTest(raises=raises):
576-
self._check_in_scopes(code, {"value": ["a"]})
574+
# No exception.
575+
code = template.format(func='len')
576+
self._check_in_scopes(code, {"value": ["ab"], "result": [2], "snapshot": None})
577+
# Handles exception.
578+
code = template.format(func='int')
579+
self._check_in_scopes(code, {"value": ["ab"], "result": None, "snapshot": ["ab"]},
580+
raises=ValueError)
577581

578582
def test_comp_in_try_finally(self):
579-
code = """
580-
def f(value):
581-
try:
582-
[{func}(value) for value in value]
583-
finally:
584-
return value
585-
ret = f(["a"])
586-
"""
587-
self._check_in_scopes(code, {"ret": ["a"]})
583+
template = """
584+
value = ["ab"]
585+
result = snapshot = None
586+
try:
587+
result = [{func}(value) for value in value]
588+
finally:
589+
snapshot = value
590+
"""
591+
# No exception.
592+
code = template.format(func='len')
593+
self._check_in_scopes(code, {"value": ["ab"], "result": [2], "snapshot": ["ab"]})
594+
# Handles exception.
595+
code = template.format(func='int')
596+
self._check_in_scopes(code, {"value": ["ab"], "result": None, "snapshot": ["ab"]},
597+
raises=ValueError)
588598

589599
def test_exception_in_post_comp_call(self):
590600
code = """

0 commit comments

Comments
 (0)