Skip to content

Commit

Permalink
work on improving error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jan 19, 2025
1 parent 9dbd8ad commit b578ce1
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions asteval/astutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def __repr__(self):
class ExceptionHolder:
"""Basic exception handler."""
def __init__(self, node, exc=None, msg='', expr=None,
text=None, lineno=None):
text=None, lineno=None):
"""TODO: docstring in public method."""
self.node = node
self.expr = expr
Expand Down Expand Up @@ -398,10 +398,11 @@ def get_error(self):
exc_name = 'UnknownError'

out = []
self.code = [f'{l}' for l in self.text.split('\n')]
self.codelines = [f'{i+1}: {l}' for i, l in enumerate(self.code)]

try:
lines = self.text.split('\n')
line = '\n'.join(lines[self.lineno-1:self.end_lineno])
out.append(f"{line}")
out.append('\n'.join(self.code[self.lineno-1:self.end_lineno]))
except:
out.append(f"{self.expr}")
if self.col_offset > 0:
Expand Down Expand Up @@ -558,7 +559,7 @@ class Procedure:
"""

def __init__(self, name, interp, doc=None, lineno=None,
body=None, args=None, kwargs=None,
body=None, text=None, args=None, kwargs=None,
vararg=None, varkws=None):
"""TODO: docstring in public method."""
self.__ininit__ = True
Expand All @@ -573,13 +574,15 @@ def __init__(self, name, interp, doc=None, lineno=None,
self.__vararg__ = vararg
self.__varkws__ = varkws
self.lineno = lineno
self.__text__ = ast.unparse(self.__body__)
self.__text__ = text
if text is None:
self.__text__ = f'{self.__signature__()}\n' + ast.unparse(self.__body__)
self.__ininit__ = False

def __setattr__(self, attr, val):
if not getattr(self, '__ininit__', True):
self.__raise_exc__(None, exc=TypeError,
msg="procedure is read-only")
msg="procedure is read-only")
self.__dict__[attr] = val

def __dir__(self):
Expand Down Expand Up @@ -649,14 +652,15 @@ def __call__(self, *args, **kwargs):
if nargs < nargs_expected:
msg = f"{self.name}() takes at least"
msg = f"{msg} {nargs_expected} arguments, got {nargs}"
self.__raise_exc__(None, exc=TypeError, msg=msg)
self.__raise_exc__(None, exc=TypeError, msg=msg)
# check for multiple values for named argument
if len(self.__argnames__) > 0 and kwargs is not None:
msg = "multiple values for keyword argument"
for targ in self.__argnames__:
if targ in kwargs:
msg = f"{msg} '{targ}' in Procedure {self.name}"
self.__raise_exc__(None, exc=TypeError, msg=msg, lineno=self.lineno)
self.__raise_exc__(None, exc=TypeError, msg=msg,
lineno=self.lineno)

# check more args given than expected, varargs not given
if nargs != nargs_expected:
Expand Down Expand Up @@ -696,7 +700,7 @@ def __call__(self, *args, **kwargs):
msg = f"extra keyword arguments for Procedure {self.name}: "
msg = msg + ','.join(list(kwargs.keys()))
self.__raise_exc__(None, msg=msg, exc=TypeError,
lineno=self.lineno)
lineno=self.lineno)

except (ValueError, LookupError, TypeError,
NameError, AttributeError):
Expand Down

0 comments on commit b578ce1

Please sign in to comment.