Skip to content

Commit

Permalink
cleanups of exception handling : errors must now have an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Sep 7, 2024
1 parent 473726d commit 37ae4e9
Showing 1 changed file with 6 additions and 18 deletions.
24 changes: 6 additions & 18 deletions asteval/asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ def unimplemented(self, node):
def raise_exception(self, node, exc=None, msg='', expr=None,
lineno=None):
"""Add an exception."""
if self.error is None:
self.error = []
if expr is None:
expr = self.expr
msg = str(msg)
Expand All @@ -234,10 +232,7 @@ def raise_exception(self, node, exc=None, msg='', expr=None,
elif len(msg) > 0:
self.error_msg = f"{exc:s}: {msg}"
if exc is None:
try:
exc = self.error[-1].exc
except:
exc = RuntimeError
exc = self.error[-1].exc
raise exc(self.error_msg)

# main entry point for Ast node evaluation
Expand Down Expand Up @@ -323,13 +318,10 @@ def eval(self, expr, lineno=0, show_errors=True, raise_errors=False):
except Exception:
errmsg = exc_info()[1]
if len(self.error) > 0:
errmsg = self.error[-1].get_error()[1]
if raise_errors:
try:
exc = self.error[-1].exc
except Exception:
exc = RuntimeError
raise exc(errmsg)
lerr = self.error[-1]
errmsg = lerr.get_error()[1]
if raise_errors:
raise lerr.exc(errmsg)
if show_errors:
print(errmsg, file=self.err_writer)
return None
Expand All @@ -342,11 +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:
try:
exc = self.error[-1].exc
except Exception:
exc = RuntimeError
raise exc(errmsg)
raise self.error[-1].exc(errmsg)
if show_errors:
print(errmsg, file=self.err_writer)
return None
Expand Down

0 comments on commit 37ae4e9

Please sign in to comment.