Skip to content

Commit

Permalink
Python 2.6+ supports conditional expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasa committed May 18, 2013
1 parent 0d3b389 commit e8acd5b
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions jinja2/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@
'notin': 'not in'
}

try:
exec('(0 if 0 else 0)')
except SyntaxError:
have_condexpr = False
else:
have_condexpr = True


# what method to iterate over items do we want to use for dict iteration
# in generated code? on 2.x let's go with iteritems, on 3.x with items
if hasattr(dict, 'iteritems'):
Expand Down Expand Up @@ -1565,22 +1557,13 @@ def write_expr2():
'expression on %s evaluated to false and '
'no else section was defined.' % self.position(node)))

if not have_condexpr:
self.write('((')
self.visit(node.test, frame)
self.write(') and (')
self.visit(node.expr1, frame)
self.write(',) or (')
write_expr2()
self.write(',))[0]')
else:
self.write('(')
self.visit(node.expr1, frame)
self.write(' if ')
self.visit(node.test, frame)
self.write(' else ')
write_expr2()
self.write(')')
self.write('(')
self.visit(node.expr1, frame)
self.write(' if ')
self.visit(node.test, frame)
self.write(' else ')
write_expr2()
self.write(')')

def visit_Call(self, node, frame, forward_caller=False):
if self.environment.sandboxed:
Expand Down

0 comments on commit e8acd5b

Please sign in to comment.