string to list of numbers conversion

Peter Otten __peter__ at web.de
Sun Nov 5 17:50:13 CET 2006


jm.suresh at no.spam.gmail.com wrote:

> This recipe fails when negative numbers are used.
> 
> safe_eval('(12, -12)')
> *** Unsafe_Source_Error: Line 1.  Unsupported source construct:
> compiler.ast.UnarySub
> 
> But, I think it could  be easily fixed for somebody who understands the
> script. 

I think that somebody could be you.

> Can somebody help. 

Start with

class SafeEval(object):
    # ...
    def visitUnarySub(self, node, **kw):
        return -node.expr.value

and then add some error handling.

Peter




More information about the Python-list mailing list