From eca6fbfd6f4f60d3503dba7253cfb941d0751365 Mon Sep 17 00:00:00 2001 From: Steve Norum Date: Sat, 12 Jan 2019 11:39:49 -0500 Subject: [PATCH] Fixing compiler handling of parens around boolean comparisons. --- jinja2/compiler.py | 2 ++ tests/test_lexnparse.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 5135a771..c0eb2400 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -1529,9 +1529,11 @@ class CodeGenerator(NodeVisitor): @optimizeconst def visit_Compare(self, node, frame): + self.write('(') self.visit(node.expr, frame) for op in node.ops: self.visit(op, frame) + self.write(')') def visit_Operand(self, node, frame): self.write(' %s ' % operators[node.op]) diff --git a/tests/test_lexnparse.py b/tests/test_lexnparse.py index a61c1467..5c4c2737 100644 --- a/tests/test_lexnparse.py +++ b/tests/test_lexnparse.py @@ -332,6 +332,12 @@ class TestSyntax(object): '{{ 2 == 2 }}|{{ 1 <= 1 }}') assert tmpl.render() == 'True|True|True|True|True' + def test_compare_parens(self, env): + tmpl = env.from_string( + "{{ i*(j<5) }}" + ) + assert tmpl.render(i=2, j=3) == '2' + def test_inop(self, env): tmpl = env.from_string('{{ 1 in [1, 2, 3] }}|{{ 1 not in [1, 2, 3] }}') assert tmpl.render() == 'True|False' -- 2.47.2