Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Sep 8, 2024
1 parent 37ae4e9 commit e30c848
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
14 changes: 7 additions & 7 deletions asteval/asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def eval(self, expr, lineno=0, show_errors=True, raise_errors=False):
lerr = self.error[-1]
errmsg = lerr.get_error()[1]
if raise_errors:
raise lerr.exc(errmsg)
raise lerr.exc(errmsg) from exc
if show_errors:
print(errmsg, file=self.err_writer)
return None
Expand All @@ -334,7 +334,7 @@ def eval(self, expr, lineno=0, show_errors=True, raise_errors=False):
if len(self.error) > 0:
errmsg = self.error[-1].get_error()[1]
if raise_errors:
raise self.error[-1].exc(errmsg)
raise self.error[-1].exc(errmsg) from exc
if show_errors:
print(errmsg, file=self.err_writer)
return None
Expand Down Expand Up @@ -490,8 +490,7 @@ def on_formattedvalue(self, node): # ('value', 'conversion', 'format_spec')
fmt = '{__fstring__}'
if node.format_spec is not None:
fmt = f'{{__fstring__:{self.run(node.format_spec)}}}'
else:
return fmt.format(__fstring__=val)
return fmt.format(__fstring__=val)

def _getsym(self, node):
val = self.symtable.get(node.id, ReturnedNone)
Expand Down Expand Up @@ -645,7 +644,7 @@ 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)
multi = len(node.ops) > 1
for oper, rnode in zip(node.ops, node.comparators):
rval = self.run(rnode)
ret = op2func(oper)(lval, rval)
Expand Down Expand Up @@ -750,16 +749,17 @@ def _comp_save_syms(self, node):
saved_syms[tnode.target.id] = copy.deepcopy(self._getsym(tnode.target))

elif tnode.target.__class__ == ast.Tuple:
target = []
for tval in tnode.target.elts:
if tval.id in self.symtable:
saved_syms[tval.id] = copy.deepcopy(self._getsym(tval))
return saved_syms


def do_generator(self, gnodes, node, out):
"""general purpose generator """
gnode = gnodes[0]
nametype = True
target = None
if gnode.target.__class__ == ast.Name:
if (not valid_symbol_name(gnode.target.id) or
gnode.target.id in self.readonly_symbols):
Expand All @@ -771,7 +771,7 @@ def do_generator(self, gnodes, node, out):
target = tuple([gval.id for gval in gnode.target.elts])

for val in self.run(gnode.iter):
if nametype:
if nametype and target is not None:
self.symtable[target] = val
else:
for telem, tval in zip(target, val):
Expand Down
5 changes: 3 additions & 2 deletions asteval/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def _open(filename, mode='r', buffering=-1, encoding=None):
return open(filename, mode, buffering, encoding=encoding)


def _type(obj):
def _type(x):
"""type that prevents varargs and varkws"""
return type(obj).__name__
return type(x).__name__


LOCALFUNCS = {'open': _open, 'type': _type}
Expand Down Expand Up @@ -465,6 +465,7 @@ def make_symbol_table(use_numpy=True, nested=False, top=True, **kws):
"""
if nested:
name = '_'
if top:
name = '_main'
if 'name' in kws:
Expand Down

0 comments on commit e30c848

Please sign in to comment.