Skip to content

Commit

Permalink
make non-boolean return values from comparison operators behave like …
Browse files Browse the repository at this point in the history
…Python - not immediately testing as bool
  • Loading branch information
newville committed Aug 18, 2024
1 parent 1cd191f commit 7e2050d
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions asteval/asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,23 +651,18 @@ def on_compare(self, node): # ('left', 'ops', 'comparators')
"""comparison operators, including chained comparisons (a<b<c)"""
lval = self.run(node.left)
results = []
multi = (len(node.ops) > 1)
for oper, rnode in zip(node.ops, node.comparators):
rval = self.run(rnode)
ret = op2func(oper)(lval, rval)
results.append(ret)
try:
if not ret:
break
except ValueError:
pass
lval = rval
if len(results) == 1:
out = results[0]
else:
out = True
for ret in results:
out = out and ret
return out
if multi:
results.append(ret)
if not all(results):
return False
lval = rval
if multi:
ret = all(results)
return ret

def _printer(self, *out, **kws):
"""Generic print function."""
Expand Down

0 comments on commit 7e2050d

Please sign in to comment.