From 37ae4e95002ec7541cdb65968e05ee17d2889e65 Mon Sep 17 00:00:00 2001 From: Matthew Newville Date: Sat, 7 Sep 2024 18:04:49 -0500 Subject: [PATCH] cleanups of exception handling : errors must now have an exception --- asteval/asteval.py | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/asteval/asteval.py b/asteval/asteval.py index 479c2fc..ba68100 100644 --- a/asteval/asteval.py +++ b/asteval/asteval.py @@ -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) @@ -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 @@ -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 @@ -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