Original question:
Executing mathematical user code on a python web server, what is the simplest secure way?
The Openerp's source code contains a safe_eval.py that do a similar thing. But Instead of checking the ast of the source, it restrict the byte code that is allowed to execute. I think you may also have a look on it :)