From ec55a221f6f4531d8da0c496c994b0d586b3f68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Old=C5=99ich=20Jedli=C4=8Dka?= Date: Tue, 19 Jan 2021 18:20:26 +0100 Subject: [PATCH] Do not pretend expression is filename in compile() Python built-in call (#73113) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When calling compile(), the filename argument should be either a real file name or a string. According to Python docs, suggested one is ''. Keep the current behaviour (encapsulate the actual expression), but enclose it into angle brackets. Signed-off-by: Oldřich Jedlička --- .../fragments/fix_expression_as_filename_in_compile.yaml | 4 ++++ lib/ansible/template/safe_eval.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/fix_expression_as_filename_in_compile.yaml 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 00000000000..0ddfa0ac722 --- /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 2c0bfff8f9c..06bc7687198 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, '' % 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