Skip to content
Myr

Fixed long standing wrong operator precedence

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent a78bee7 · Browse files · 3 files+29 added−52 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+29 added−52 removed

  • tests:+4 added−0 removed
  • other:+25 added−52 removed

3 files+29 added−52 removed

modified:jinja2/parser.py+23 added-52 removed
17 unmodified lines
18 'macro', 'include', 'from', 'import',19 'set'])20_compare_operators = frozenset(['eq', 'ne', 'lt', 'lteq', 'gt', 'gteq'])2122_math_nodes = {23 'add': nodes.Add,24 'sub': nodes.Sub,25 'mul': nodes.Mul,26 'div': nodes.Div,27 'floordiv': nodes.FloorDiv,28 'mod': nodes.Mod,29}303132class Parser(object):33 """This is the central parsing class Jinja2 uses. It's passed to34 extensions and can be used to parse expressions or statements.35 """
401 unmodified lines
437 return self.parse_compare()438439 def parse_compare(self):440 lineno = self.stream.current.lineno432 expr = self.parse_add()441 expr = self.parse_math1()442 ops = []443 while 1:444 token_type = self.stream.current.type445 if token_type in _compare_operators:446 next(self.stream)438 ops.append(nodes.Operand(token_type, self.parse_add()))447 ops.append(nodes.Operand(token_type, self.parse_math1()))448 elif self.stream.skip_if('name:in'):440 ops.append(nodes.Operand('in', self.parse_add()))449 ops.append(nodes.Operand('in', self.parse_math1()))450 elif (self.stream.current.test('name:not') and451 self.stream.look().test('name:in')):452 self.stream.skip(2)444 ops.append(nodes.Operand('notin', self.parse_add()))453 ops.append(nodes.Operand('notin', self.parse_math1()))454 else:455 break456 lineno = self.stream.current.lineno457 if not ops:458 return expr459 return nodes.Compare(expr, ops, lineno=lineno)460452 def parse_add(self):461 def parse_math1(self):462 lineno = self.stream.current.lineno454 left = self.parse_sub()455 while self.stream.current.type == 'add':456 next(self.stream)457 right = self.parse_sub()458 left = nodes.Add(left, right, lineno=lineno)459 lineno = self.stream.current.lineno460 return left461462 def parse_sub(self):463 lineno = self.stream.current.lineno463 left = self.parse_concat()465 while self.stream.current.type == 'sub':464 while self.stream.current.type in ('add', 'sub'):465 cls = _math_nodes[self.stream.current.type]466 next(self.stream)467 right = self.parse_concat()468 left = nodes.Sub(left, right, lineno=lineno)468 left = cls(left, right, lineno=lineno)469 lineno = self.stream.current.lineno470 return left471472 def parse_concat(self):473 lineno = self.stream.current.lineno474 args = [self.parse_mul()]474 args = [self.parse_math2()]475 while self.stream.current.type == 'tilde':476 next(self.stream)477 args.append(self.parse_mul())477 args.append(self.parse_math2())478 if len(args) == 1:479 return args[0]480 return nodes.Concat(args, lineno=lineno)481482 def parse_mul(self):482 def parse_math2(self):483 lineno = self.stream.current.lineno484 left = self.parse_div()485 while self.stream.current.type == 'mul':486 next(self.stream)487 right = self.parse_div()488 left = nodes.Mul(left, right, lineno=lineno)489 lineno = self.stream.current.lineno490 return left491492 def parse_div(self):493 lineno = self.stream.current.lineno494 left = self.parse_floordiv()495 while self.stream.current.type == 'div':496 next(self.stream)497 right = self.parse_floordiv()498 left = nodes.Div(left, right, lineno=lineno)499 lineno = self.stream.current.lineno500 return left501502 def parse_floordiv(self):503 lineno = self.stream.current.lineno504 left = self.parse_mod()505 while self.stream.current.type == 'floordiv':506 next(self.stream)507 right = self.parse_mod()508 left = nodes.FloorDiv(left, right, lineno=lineno)509 lineno = self.stream.current.lineno510 return left511512 def parse_mod(self):513 lineno = self.stream.current.lineno484 left = self.parse_pow()515 while self.stream.current.type == 'mod':485 while self.stream.current.type in ('mul', 'div', 'floordiv', 'mod'):486 cls = _math_nodes[self.stream.current.type]487 next(self.stream)488 right = self.parse_pow()518 left = nodes.Mod(left, right, lineno=lineno)489 left = cls(left, right, lineno=lineno)490 lineno = self.stream.current.lineno491 return left492493 def parse_pow(self):
377 unmodified lines
modified:tests/test_lexnparse.py+4 added
393 unmodified lines
394 bar = range(100)395 tmpl = env.from_string('''{{ not 42 in bar }}''')396 assert tmpl.render(bar=bar) == text_type(not 42 in bar)397398 def test_operator_precedence(self, env):399 tmpl = env.from_string('''{{ 2 * 3 + 4 % 2 + 1 - 2 }}''')400 assert tmpl.render() == text_type(2 * 3 + 4 % 2 + 1 - 2)401402 def test_implicit_subscribed_tuple(self, env):403 class Foo(object):404 def __getitem__(self, x):405 return x
208 unmodified lines
modified:CHANGES+2 added
6 unmodified lines
7- Change cache key definition in environment. This fixes a performance8 regression introduced in 2.8.9- Added support for `generator_stop` on supported Python versions10 (Python 3.5 and later)11- Corrected a long standing issue with operator precedence of math operations12 not being what was expected.1314Version 2.8.115-------------16
370 unmodified lines