Skip to content

Commit

Permalink
Merge pull request #938 from stevenorum/master
Browse files Browse the repository at this point in the history
Fixing compiler handling of parens around boolean comparisons.
  • Loading branch information
davidism committed Oct 7, 2019
2 parents c8b1b02 + eca6fbf commit f1d199a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jinja2/compiler.py
Expand Up @@ -1529,9 +1529,11 @@ def visit_Concat(self, node, frame):

@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])
Expand Down
6 changes: 6 additions & 0 deletions tests/test_lexnparse.py
Expand Up @@ -332,6 +332,12 @@ def test_compare(self, env):
'{{ 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'
Expand Down

0 comments on commit f1d199a

Please sign in to comment.