Skip to content

Commit ba15d6c

Browse files
authored
Merge pull request #535 from akonradi/empty-generator
Ignore failure to delete list comp variables
2 parents 84181df + b6e5b6c commit ba15d6c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

custom_components/pyscript/eval.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,12 @@ async def loopvar_scope_restore(self, var_names, save_vars):
17531753
if var_name in save_vars:
17541754
self.sym_table[var_name] = save_vars[var_name]
17551755
else:
1756-
del self.sym_table[var_name]
1756+
try:
1757+
del self.sym_table[var_name]
1758+
except KeyError:
1759+
# If the iterator was empty, the loop variables were never
1760+
# assigned to, so deleting them will fail.
1761+
pass
17571762

17581763
async def listcomp_loop(self, generators, elt):
17591764
"""Recursive list comprehension."""

tests/test_unit_eval.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
["kw = {'x': 1, 'y': 5}; kw2 = {'z': 10}; {**kw, **kw2}", {"x": 1, "y": 5, "z": 10}],
8282
["[*iter([1, 2, 3])]", [1, 2, 3]],
8383
["{*iter([1, 2, 3])}", {1, 2, 3}],
84+
["[x for x in []]", []],
85+
["[x for x in [] if x]", []],
8486
["if 1: x = 10\nelse: x = 20\nx", 10],
8587
["if 0: x = 10\nelse: x = 20\nx", 20],
8688
["i = 0\nwhile i < 5: i += 1\ni", 5],

0 commit comments

Comments
 (0)