diff --git a/asteval/asteval.py b/asteval/asteval.py index a698b1e..52e53f9 100644 --- a/asteval/asteval.py +++ b/asteval/asteval.py @@ -225,6 +225,7 @@ def raise_exception(self, node, exc=None, msg='', expr=None, lineno=None): err = ExceptionHolder(node, exc=exc, msg=msg, expr=self.expr, lineno=lineno) self._interrupt = ast.Raise() + self.error.append(err) if self.error_msg is None: self.error_msg = msg @@ -301,14 +302,19 @@ def run(self, node, expr=None, lineno=None, with_raise=True): # avoid too many repeated error messages (yes, this needs to be "2") if len(self.error) > 2: - error = [self.error[0]] - for err in self.error[1:]: - le = error[-1] - if err.exc != le.exc or err.expr != le.expr or err.msg != le.msg: - error.append(err) - self.error = error + self._remove_duplicate_errors() + return None + def _remove_duplicate_errors(self): + """remove duplicate exceptions""" + error = [self.error[0]] + for err in self.error[1:]: + lerr = error[-1] + if err.exc != lerr.exc or err.expr != lerr.expr or err.msg != lerr.msg: + error.append(err) + self.error = error + def __call__(self, expr, **kw): """Call class instance as function.""" return self.eval(expr, **kw) @@ -343,6 +349,7 @@ def eval(self, expr, lineno=0, show_errors=True, raise_errors=False): errmsg = self.error[-1].get_error()[1] print(errmsg, file=self.err_writer) if raise_errors and len(self.error) > 0: + self._remove_duplicate_errors() err = self.error[-1] raise err.exc(err.get_error()[1]) return None diff --git a/tests/test_asteval.py b/tests/test_asteval.py index a85d3d2..3d3f107 100644 --- a/tests/test_asteval.py +++ b/tests/test_asteval.py @@ -1541,6 +1541,8 @@ def test_raise_errors_unknown_symbol(nested): except NameError: saw_exception = True assert saw_exception + assert len(interp.error) > 0 + assert interp.error[0].exc == NameError @pytest.mark.parametrize("nested", [False, True])