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

NameError oddity, undefined python variable reference #133

Closed
Teriks opened this issue Sep 21, 2024 · 2 comments
Closed

NameError oddity, undefined python variable reference #133

Teriks opened this issue Sep 21, 2024 · 2 comments

Comments

@Teriks
Copy link

Teriks commented Sep 21, 2024

In 1.0.4 I noticed this odd traceback for an undefined identifier.

I am using python 3.12.3

import asteval

interpreter = asteval.Interpreter()

interpreter.eval('i_dont_exist', raise_errors=True)

Output:

REDACT\venv\Scripts\python.exe REDACT\test.py 
Traceback (most recent call last):
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 293, in run
    ret = handler(node)
          ^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 513, in on_name
    return self._getsym(node)
           ^^^^^^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 505, in _getsym
    self.raise_exception(node, exc=NameError, msg=msg)
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 242, in raise_exception
    raise exc(self.error_msg)
NameError: name 'i_dont_exist' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 293, in run
    ret = handler(node)
          ^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 356, in on_expr
    return self.run(node.value)  # ('value',)
           ^^^^^^^^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 299, in run
    self.raise_exception(node, expr=expr)
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 242, in raise_exception
    raise exc(self.error_msg)
NameError: name 'i_dont_exist' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 293, in run
    ret = handler(node)
          ^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 432, in on_module
    out = self.run(tnode)
          ^^^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 299, in run
    self.raise_exception(node, expr=expr)
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 242, in raise_exception
    raise exc(self.error_msg)
NameError: name 'i_dont_exist' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 337, in eval
    return self.run(node, expr=expr, lineno=lineno, with_raise=raise_errors)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 299, in run
    self.raise_exception(node, expr=expr)
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 242, in raise_exception
    raise exc(self.error_msg)
NameError: name 'i_dont_exist' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "REDACT\test.py", line 7, in <module>
    interpreter.eval('i_dont_exist', raise_errors=True)
  File "REDACT\venv\Lib\site-packages\asteval\asteval.py", line 343, in eval
    raise self.error[-1].exc(errmsg) from exc
                                          ^^^
NameError: name 'exc' is not defined. Did you mean: 'exec'?

Process finished with exit code 1

Seems there is a reference to an undefined python variable in this case.

Perhaps it needs: except Exception as exc: in the handlers, it seems this error is possible in two places when looking at eval

def eval(self, expr, lineno=0, show_errors=True, raise_errors=False):
    """Evaluate a single statement."""
    self.lineno = lineno
    self.error = []
    self.start_time = time.time()
    if isinstance(expr, str):
        try:
            node = self.parse(expr)
        except Exception:
            errmsg = exc_info()[1]
            if len(self.error) > 0:
                lerr = self.error[-1]
                errmsg = lerr.get_error()[1]
                if raise_errors:
                    raise lerr.exc(errmsg) from exc # HERE exc does not exist
            if show_errors:
                print(errmsg, file=self.err_writer)
            return None
    else:
        node = expr
    try:
        return self.run(node, expr=expr, lineno=lineno, with_raise=raise_errors)
    except:
        errmsg = exc_info()[1]
        if len(self.error) > 0:
            errmsg = self.error[-1].get_error()[1]
        if raise_errors:
            raise self.error[-1].exc(errmsg) from exc # HERE exc does not exist
        if show_errors:
            print(errmsg, file=self.err_writer)
    return None
@Teriks Teriks changed the title NameError oddity, NameError oddity, undefined python variable reference Sep 21, 2024
@newville
Copy link
Member

@Teriks Thanks, and sorry for the trouble. #129 and #131 were similar issues.

Apparently, having sensible exceptions and reasonable handling of raise_errors is right at the threshold of my ability to do well ;)

I've updated the code and added a test so that this should now work. I'll choose to be cautiously optimistic but maybe not completely confident this is the end of it ;)

@newville
Copy link
Member

@Teriks this should now be fixed, and in version 1.0.5, so closing.

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

2 participants