gh-109182: Fix and improve tests for gh-108654 (GH-109189)
This commit is contained in:
parent
4a69301ea4
commit
c0f488b88f
@ -125,7 +125,7 @@ class ListComprehensionTest(unittest.TestCase):
|
|||||||
self.assertIs(type(e), raises)
|
self.assertIs(type(e), raises)
|
||||||
else:
|
else:
|
||||||
for k, v in (outputs or {}).items():
|
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):
|
def test_lambdas_with_iteration_var_as_default(self):
|
||||||
code = """
|
code = """
|
||||||
@ -563,28 +563,38 @@ class ListComprehensionTest(unittest.TestCase):
|
|||||||
|
|
||||||
def test_comp_in_try_except(self):
|
def test_comp_in_try_except(self):
|
||||||
template = """
|
template = """
|
||||||
value = ["a"]
|
value = ["ab"]
|
||||||
|
result = snapshot = None
|
||||||
try:
|
try:
|
||||||
[{func}(value) for value in value]
|
result = [{func}(value) for value in value]
|
||||||
except:
|
except:
|
||||||
pass
|
snapshot = value
|
||||||
|
raise
|
||||||
"""
|
"""
|
||||||
for func in ["str", "int"]:
|
# No exception.
|
||||||
code = template.format(func=func)
|
code = template.format(func='len')
|
||||||
raises = func != "str"
|
self._check_in_scopes(code, {"value": ["ab"], "result": [2], "snapshot": None})
|
||||||
with self.subTest(raises=raises):
|
# Handles exception.
|
||||||
self._check_in_scopes(code, {"value": ["a"]})
|
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):
|
def test_comp_in_try_finally(self):
|
||||||
code = """
|
template = """
|
||||||
def f(value):
|
value = ["ab"]
|
||||||
|
result = snapshot = None
|
||||||
try:
|
try:
|
||||||
[{func}(value) for value in value]
|
result = [{func}(value) for value in value]
|
||||||
finally:
|
finally:
|
||||||
return value
|
snapshot = value
|
||||||
ret = f(["a"])
|
|
||||||
"""
|
"""
|
||||||
self._check_in_scopes(code, {"ret": ["a"]})
|
# 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):
|
def test_exception_in_post_comp_call(self):
|
||||||
code = """
|
code = """
|
||||||
|
Loading…
x
Reference in New Issue
Block a user