mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #14293 from dagwieers/fix-eval-json-booleans
Defined JSON booleans in global context for python eval()
This commit is contained in:
commit
6149685652
1 changed files with 9 additions and 1 deletions
|
@ -41,6 +41,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
|
||||
|
@ -116,7 +124,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, {}, dict(locals))
|
||||
result = eval(compiled, JSON_TYPES, dict(locals))
|
||||
|
||||
if include_exceptions:
|
||||
return (result, None)
|
||||
|
|
Loading…
Reference in a new issue