Skip to content

Commit

Permalink
cleanup new list/dict comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jun 30, 2024
1 parent ba0783f commit 29cbd10
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions asteval/asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,23 +785,19 @@ def _comp_save_syms(self, node):

def do_generator(self, gnodes, node, out):
gnode = gnodes[0]
ttype = None
nametype = True
if gnode.target.__class__ == ast.Name:
if (not valid_symbol_name(gnode.target.id) or
gnode.target.id in self.readonly_symbols):
errmsg = f"invalid symbol name (reserved word?) {gnode.target.id}"
self.raise_exception(gnode.target, exc=NameError, msg=errmsg)
ttype = 'name'
target = gnode.target.id
elif gnode.target.__class__ == ast.Tuple:
ttype = 'tuple'
nametype = False
target = tuple([gval.id for gval in gnode.target.elts])
if ttype is None:
errmsg = f"invalid comprehension {gnode.target.id}"
self.raise_exception(gnode.target, exc=NameError, msg=errmsg)

for val in self.run(gnode.iter):
if ttype == 'name':
if nametype:
self.symtable[target] = val
else:
for telem, tval in zip(target, val):
Expand All @@ -812,9 +808,9 @@ def do_generator(self, gnodes, node, out):
if not add:
break
if add:
if len(gnodes[1:]) > 0:
if len(gnodes) > 1:
self.do_generator(gnodes[1:], node, out)
elif isinstance(out, (list, tuple)):
elif isinstance(out, list):
out.append(self.run(node.elt))
elif isinstance(out, dict):
out[self.run(node.key)] = self.run(node.value)
Expand Down

0 comments on commit 29cbd10

Please sign in to comment.