From c8892b39a588a2ea093614c82844c4e00feda4d2 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 9 Jan 2024 17:05:06 -0600 Subject: [PATCH] Align compile closer with jinja upstream --- lib/ansible/template/__init__.py | 42 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 0daaf9263a4..248a5273180 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -592,25 +592,31 @@ class AnsibleEnvironment(NativeEnvironment): if not C.JINJA2_BYTECODE_CACHE: return super().compile(source, name=name, filename=filename, raw=raw, defer_init=defer_init) # type: ignore[call-overload] - # Environment._parse - parsed = Parser(self, source, name, filename).parse() - - # This wrapper ensures that all templates are not considered literal/constant - eval_ctx = nodes.ScopedEvalContextModifier(lineno=-1) - eval_ctx.options = [nodes.Keyword('volatile', nodes.Const(True))] - eval_ctx.body = parsed.body - - # Environment._generate - generated = generate( - nodes.Template([eval_ctx], lineno=-1), - self, - name, - filename, - defer_init=False, - optimized=self.optimized, - ) + try: + # Environment._parse + parsed = Parser(self, source, name, filename).parse() + + # This wrapper ensures that all templates are not considered literal/constant + eval_ctx = nodes.ScopedEvalContextModifier(lineno=-1) + eval_ctx.options = [nodes.Keyword('volatile', nodes.Const(True))] + eval_ctx.body = parsed.body + + # Environment._generate + generated = generate( + nodes.Template([eval_ctx], lineno=-1), + self, + name, + filename, + defer_init=False, + optimized=self.optimized, + ) + + if raw: + return generated - return compile(generated, filename or '