Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes when evaluating an empty string #67

Closed
Gnarflord opened this issue Nov 19, 2019 · 2 comments
Closed

Crashes when evaluating an empty string #67

Gnarflord opened this issue Nov 19, 2019 · 2 comments

Comments

@Gnarflord
Copy link

When giving simpleeval an empty string it raises this error:
Traceback (most recent call last):

Traceback (most recent call last):
  File "simpleeval.py", line 613, in simple_eval
    return s.eval(expr)
  File "simpleeval.py", line 334, in eval
    return self._eval(parsed.body[0].value)
IndexError: list index out of range

I propose to either throw an appropriate error or maybe return something. Wether that'd be an empty string or None is too philosophical for me but maybe there's an PIP on this? Anyways, something like this should suffice:

parsed = ast.parse(expr.strip())
if len(parsed.body) > 0:
    # and evaluate if not empty:
    return self._eval(parsed.body[0].value)
else:
    return None
@zhudotexe
Copy link
Contributor

An interesting note: eval("") in normal Python raises a SyntaxError.

>>> eval("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

@kurtmckee
Copy link
Contributor

Looks like this can be closed due to #108.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants