eval(source, {'builtins': {}}) archived as Faq

p.lavarre at ieee.org p.lavarre at ieee.org
Fri Sep 29 01:07:21 CEST 2006

Absent from http://www.python.org/doc/current/lib/built-in-funcs.html
but now copied to the Faq list of http://pyfaq.infogami.com/suggest,
from these clp archives:


Q: How can I tell Python to calculate what quoted strings and numbers
mean, without also accidentally accepting OS commands as input?

A: eval(source, {'builtins': {}})

Note: What eval may do to you remains as surprising as ever if you
mistype this idiom as: eval(source, {})

Note: This idiom makes sense of ordinary Python literals (such as 010,
0x8, 8.125e+0, and "\x45ight").  This idiom also correctly interprets
simple literal expressions, such as 64**0.5.

More information about the Python-list mailing list