Skip to content

[3.12] gh-109182: Fix and improve tests for gh-108654 (GH-109189) #109271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions Lib/test/test_listcomps.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def get_output(moddict, name):
self.assertIs(type(e), raises)
else:
for k, v in (outputs or {}).items():
self.assertEqual(get_output(newns, k), v)
self.assertEqual(get_output(newns, k), v, k)

def test_lambdas_with_iteration_var_as_default(self):
code = """
Expand Down Expand Up @@ -563,28 +563,38 @@ def test_iter_var_available_in_locals(self):

def test_comp_in_try_except(self):
template = """
value = ["a"]
value = ["ab"]
result = snapshot = None
try:
[{func}(value) for value in value]
result = [{func}(value) for value in value]
except:
pass
snapshot = value
raise
"""
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"]})
# No exception.
code = template.format(func='len')
self._check_in_scopes(code, {"value": ["ab"], "result": [2], "snapshot": None})
# Handles exception.
code = template.format(func='int')
self._check_in_scopes(code, {"value": ["ab"], "result": None, "snapshot": ["ab"]},
raises=ValueError)

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"]})
template = """
value = ["ab"]
result = snapshot = None
try:
result = [{func}(value) for value in value]
finally:
snapshot = value
"""
# No exception.
code = template.format(func='len')
self._check_in_scopes(code, {"value": ["ab"], "result": [2], "snapshot": ["ab"]})
# Handles exception.
code = template.format(func='int')
self._check_in_scopes(code, {"value": ["ab"], "result": None, "snapshot": ["ab"]},
raises=ValueError)

def test_exception_in_post_comp_call(self):
code = """
Expand Down