Is it possible to get the erroneous variable when getting a NameError exception?
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 "", line 1, in ? File "", 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to get the erroneous variable when getting a NameError exception?
On 25/12/2009 19:27, Gary Herron wrote: Dotan Barak wrote: 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 First of all, thank - I really appreciate your response. :) I must admit that i don't like the idea of parsing a string (if tomorrow the format of the message will change, i will be in a deep trouble ...). Is there is another way which doesn't involve string parsing? Thanks Dotan -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to get the erroneous variable when getting a NameError exception?
A simple expression is enough to write to files, for example. Try this expression in Python 3.0: [x for x in ().__class__.__base__.__subclasses__() if x.__name__ == '_FileIO'][0]('hello.txt', 'w').write('Hello, World!') To explain, "().__class__.__base__.__subclasses__()" gives you a list of all object-derived classes, i.e., of *all* classes that exist in the surrounding program. If you can find just one class that allows you to do something subtle or dangerous, you're done. See also: - "Controlling Access to Resources Within The Python Interpreter" (http://people.cs.ubc.ca/~drifty/papers/python_security.pdf) - http://evoque.gizmojo.org/usage/restricted/ To write your own restricted expression parser, the standard module "ast" is quite useful. For example, ast.parse("my_number < 10") gives you a syntax tree similar to this: ast.Expr(ast.Compare(ops=[ast.Lt], left=ast.Name(id="my_number"), comparators=[ast.Num(n=10)])) From there, you can implement your own logic to traverse the tree, which gives you very fine-grained control over the kinds of expressions to allow, where to look up variable names, how to react to errors, etc. Kind Regards, M.F. Hi all. First of all, thank you very much for your response. The answers I got made me think if I'm trying to solve a problem because of the way I'm doing things; I tried to evaluate a string that a user supplied to me and try to get the various python exceptions to make the user understand what was his error. My original idea was to get the bad symbol, and print the user a more "friendly" error message. I think that the direction that Michael showed me is better, and It is a better solution (maybe, first I need to traverse the tree, give the right error messages and only then execute the eval). Thanks!!! Dotan -- http://mail.python.org/mailman/listinfo/python-list
Is it possible to print different levels to different streams using the logging module?
Hi. I would like to use the logging module and print the following levels to the mentioned streams: CRITICAL-> stderr ERROR -> stderr WARNING -> stderr INFO -> stdout DEBUG -> stdout I would like that every message will be printed only once, and for the stream that i choose. (I failed to find a "maximum level" for the handlers in the logging module). Thanks in advanced Dotan Barak -- http://mail.python.org/mailman/listinfo/python-list