Commits

Armin Ronacher  committed 8742c6e

Fixed an operator precedence bug with unary operations.

  • Participants
  • Parent commits 70cd9dc

Comments (0)

Files changed (3)

 -------------
 (bugfix release, release date to be announced)
 
+- fixed an operator precedence error introduced in 2.5.2.  Statements
+  like "-foo.bar" had their implicit parentheses applied around the
+  first part of the expression ("(-foo).bar") instead of the more
+  correct "-(foo.bar)".
+
 Version 2.5.2
 -------------
 (bugfix release, released on August 18th 2010)

File jinja2/parser.py

             lineno = self.stream.current.lineno
         return left
 
-    def parse_unary(self, with_postfix=True):
+    def parse_unary(self, with_filter=True):
         token_type = self.stream.current.type
         lineno = self.stream.current.lineno
         if token_type == 'sub':
             node = nodes.Pos(self.parse_unary(False), lineno=lineno)
         else:
             node = self.parse_primary()
-        if with_postfix:
-            node = self.parse_postfix(node)
+        node = self.parse_postfix(node)
+        if with_filter:
+            node = self.parse_filter_expr(node)
         return node
 
     def parse_primary(self):
             token_type = self.stream.current.type
             if token_type == 'dot' or token_type == 'lbracket':
                 node = self.parse_subscript(node)
+            # calls are valid both after postfix expressions (getattr
+            # and getitem) as well as filters and tests
             elif token_type == 'lparen':
                 node = self.parse_call(node)
-            elif token_type == 'pipe':
+            else:
+                break
+        return node
+
+    def parse_filter_expr(self, node):
+        while 1:
+            token_type = self.stream.current.type
+            if token_type == 'pipe':
                 node = self.parse_filter(node)
             elif token_type == 'name' and self.stream.current.value == 'is':
                 node = self.parse_test(node)
+            # calls are valid both after postfix expressions (getattr
+            # and getitem) as well as filters and tests
+            elif token_type == 'lparen':
+                node = self.parse_call(node)
             else:
                 break
         return node

File jinja2/testsuite/lexnparse.py

 {{ foo }}''')
         assert tmpl.render() == '0'
 
+    def test_parse_unary(self):
+        tmpl = env.from_string('{{ -foo["bar"] }}')
+        assert tmpl.render(foo={'bar': 42}) == '-42'
+        tmpl = env.from_string('{{ -foo["bar"]|abs }}')
+        assert tmpl.render(foo={'bar': 42}) == '42'
+
 
 def suite():
     suite = unittest.TestSuite()