Merge pull request #14336 from dagwieers/fix-eval-json-booleans-1.9

Defined JSON booleans in global context for python eval()
This commit is contained in:
James Cammarata 2016-02-05 11:36:00 -05:00
commit f0c1058b7b

View file

@ -1365,6 +1365,14 @@ def safe_eval(expr, locals={}, include_exceptions=False):
http://stackoverflow.com/questions/12523516/using-ast-and-whitelists-to-make-pythons-eval-safe
'''
# define certain JSON types
# eg. JSON booleans are unknown to python eval()
JSON_TYPES = {
'false': False,
'null': None,
'true': True,
}
# this is the whitelist of AST nodes we are going to
# allow in the evaluation. Any node type other than
# those listed here will raise an exception in our custom
@ -1428,7 +1436,7 @@ def safe_eval(expr, locals={}, include_exceptions=False):
parsed_tree = ast.parse(expr, mode='eval')
cnv.visit(parsed_tree)
compiled = compile(parsed_tree, expr, 'eval')
result = eval(compiled, {}, locals)
result = eval(compiled, JSON_TYPES, dict(locals))
if include_exceptions:
return (result, None)