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

Handling NameError is not working #129

Closed
shazarivf opened this issue Jul 25, 2024 · 3 comments · Fixed by #130
Closed

Handling NameError is not working #129

shazarivf opened this issue Jul 25, 2024 · 3 comments · Fixed by #130

Comments

@shazarivf
Copy link
Contributor

We recently upgraded to a new version and we have a several scripts that relied on handling NameError as we couldn't ensure all user_symbols to be present always.
The following is a small example snippet doesn't work as expected anymore.

from asteval import Interpreter

aeval = Interpreter(user_symbols={"one": 1, "two": 2, "fallback_var": 0})

code = """
try:
    output = some_var
except NameError:
    output = None
output or fallback_var
"""

a = aeval(code)

This results in:

NameError
   
try:
    output = some_var
except NameError:
    output = None
output or fallback_var
                 ^^^
name 'some_var' is not defined

NameError doesn't get handled and the code just breaks and returns None
It looks like this behavior changed with version 0.9.17 and this commit.

@newville
Copy link
Member

@shazarivf Thasnks - I agree that this looks wrong. I'll need to investigate, and I can't look at this in depth this week and can't promise this will be fixed quickly.

Does undoing that check of if len(self.error) == 0 fix this?

@shazarivf
Copy link
Contributor Author

@newville yes, removing the check fixes it. I can raise a PR if you think the check is safe to remove

@newville
Copy link
Member

@shazarivf Yes please -- I'd be interested to see what tests fail!

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

Successfully merging a pull request may close this issue.

2 participants