Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit warnings when safe_eval() raises a SyntaxError or other Exception #14304

Merged
merged 1 commit into from
Aug 12, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ansible/template/safe_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
from ansible import constants as C
from ansible.plugins import filter_loader, test_loader

try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()

def safe_eval(expr, locals={}, include_exceptions=False):
'''
This is intended for allowing things like:
Expand Down Expand Up @@ -123,13 +129,14 @@ def generic_visit(self, node, inside_call=False):
else:
return result
except SyntaxError as e:
display.warning('SyntaxError in safe_eval() on expr: %s (%s)' % (expr, e))
Copy link
Contributor

@brandond brandond Sep 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that this merits displaying error - as per the comment below this line, there is some special handling for this exception that results in passing the string back as-is for later processing. I appear to have been relying on this behavior, as I am now getting a ton of SyntaxError warnings on Jinja2 templates inlined in my group_vars YAML.

# special handling for syntax errors, we just return
# the expression string back as-is
if include_exceptions:
return (expr, None)
return expr
except Exception as e:
display.warning('Exception in safe_eval() on expr: %s (%s)' % (expr, e))
if include_exceptions:
return (expr, e)
return expr