summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOldřich Jedlička <oldium@users.noreply.github.com>2021-01-19 18:20:26 +0100
committerGitHub <noreply@github.com>2021-01-19 12:20:26 -0500
commitec55a221f6f4531d8da0c496c994b0d586b3f68b (patch)
tree6a7bda6fd4c1369c4a9c54d6281793de3e12b33c
parente8d4b62b411fb7e3ba3131906a4122185d417266 (diff)
downloadansible-ec55a221f6f4531d8da0c496c994b0d586b3f68b.tar.gz
Do not pretend expression is filename in compile() Python built-in call (#73113)
When calling compile(), the filename argument should be either a real file name or a string. According to Python docs, suggested one is '<string>'. Keep the current behaviour (encapsulate the actual expression), but enclose it into angle brackets. Signed-off-by: Oldřich Jedlička <oldium.pro@gmail.com>
-rw-r--r--changelogs/fragments/fix_expression_as_filename_in_compile.yaml4
-rw-r--r--lib/ansible/template/safe_eval.py2
2 files changed, 5 insertions, 1 deletions
diff --git a/changelogs/fragments/fix_expression_as_filename_in_compile.yaml b/changelogs/fragments/fix_expression_as_filename_in_compile.yaml
new file mode 100644
index 0000000000..0ddfa0ac72
--- /dev/null
+++ b/changelogs/fragments/fix_expression_as_filename_in_compile.yaml
@@ -0,0 +1,4 @@
+bugfixes:
+ - Pass expression in angle-bracket notation as filename argument to a
+ ``compile()`` built-in function, so that Python debuggers do not try to
+ parse it as filename.
diff --git a/lib/ansible/template/safe_eval.py b/lib/ansible/template/safe_eval.py
index 2c0bfff8f9..06bc768719 100644
--- a/lib/ansible/template/safe_eval.py
+++ b/lib/ansible/template/safe_eval.py
@@ -140,7 +140,7 @@ def safe_eval(expr, locals=None, include_exceptions=False):
try:
parsed_tree = ast.parse(expr, mode='eval')
cnv.visit(parsed_tree)
- compiled = compile(parsed_tree, to_native(expr), 'eval')
+ compiled = compile(parsed_tree, '<expr %s>' % to_native(expr), 'eval')
# Note: passing our own globals and locals here constrains what
# callables (and other identifiers) are recognized. this is in
# addition to the filtering of builtins done in CleansingNodeVisitor