Dotan Barak wrote:
Hi.

I'm trying to evaluate a string and getting a NameError
(it is expected, since the variable my_number wasn't provided in the "locals" dictionary).

<--snip start-->
>>> eval("my_number < 10", {"__builtins__":None}, {})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 0, in ?
NameError: name 'my_number' is not defined
<--snip end-->

My question is: how can i know which variable name / symbol causes the NameError exception?
In my example, this symbol is my_number.

Using that information, I will be able to print a nice error message to the user.


Thanks
Dotan



Recover the exception, and examine the tuple of args or the message string.
>>> try:
...         eval("my_number < 10", {"__builtins__":None}, {})
... except NameError,e:
...         print e.args
...         print e.message
...
("name 'my_number' is not defined",)
name 'my_number' is not defined

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to