implicit tuple expressions can no longer be totally empty.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This change makes ``{% if %}...{% endif %}`` a syntax error now. (#364) --HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 5dcb724 · Browse files · 3 files·+29 added−5 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−5 removed
- tests:+6 added−0 removed
- other:+23 added−5 removed
Provenance
- Commit
e614e88- Tree
cfc8df2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5dcb724
3 files changed+29 added−5 removed
modified:jinja2/parser.py+19 added-4 removed
546 unmodified lines547 next(self.stream)548 node = nodes.Const(token.value, lineno=token.lineno)549 elif token.type == 'lparen':550 next(self.stream)551- node = self.parse_tuple()551+ node = self.parse_tuple(explicit_parentheses=True)552 self.stream.expect('rparen')553 elif token.type == 'lbracket':554 node = self.parse_list()555 elif token.type == 'lbrace':4 unmodified lines560 node = self.parse_postfix(node)561 return node562 563 def parse_tuple(self, simplified=False, with_condexpr=True,564- extra_end_rules=None):564+ extra_end_rules=None, explicit_parentheses=False):565 """Works like `parse_expression` but if multiple expressions are566 delimited by a comma a :class:`~jinja2.nodes.Tuple` node is created.567 This method could also return a regular expression instead of a tuple568 if no commas where found.5 unmodified lines574 Because tuples do not require delimiters and may end in a bogus comma575 an extra hint is needed that marks the end of a tuple. For example576 for loops support tuples between `for` and `in`. In that case the577 `extra_end_rules` is set to ``['name:in']``.578+579+ `explicit_parentheses` is true if the parsing was triggered by an580+ expression in parentheses. This is used to figure out if an empty581+ tuple is a valid expression or not.582 """583 lineno = self.stream.current.lineno584 if simplified:585 parse = lambda: self.parse_primary(with_postfix=False)13 unmodified lines599 is_tuple = True600 else:601 break602 lineno = self.stream.current.lineno603+604+ if not is_tuple:599- if not is_tuple and args:600- return args[0]605+ if args:606+ return args[0]607+608+ # if we don't have explicit parentheses, an empty tuple is609+ # not a valid expression. This would mean nothing (literally610+ # nothing) in the spot of an expression would be an empty611+ # tuple.612+ if not explicit_parentheses:613+ self.fail('Expected an expression, got \'%s\'' %614+ describe_token(self.stream.current))615+616 return nodes.Tuple(args, 'load', lineno=lineno)617 618 def parse_list(self):619 token = self.stream.expect('lbracket')249 unmodified linesmodified:tests/test_old_bugs.py+6 added
132 unmodified lines133 u'<dd>test</dd>',134 u'</dl>',135 u'</li></ul>'136 ]137+138+139+def test_empty_if_condition_fails():140+ assert_raises(TemplateSyntaxError, Template, '{% if %}....{% endif %}')141+ assert_raises(TemplateSyntaxError, Template, '{% if foo %}...{% elif %}...{% endif %}')142+ assert_raises(TemplateSyntaxError, Template, '{% for x in %}..{% endfor %}')modified:CHANGES+4 added-1 removed
11 unmodified lines12 and take the first that exists, if a list of templates is13 given.14 - fixed a problem with having call blocks in outer scopes that15 have an argument that is also used as local variable in an16- inner frame [#360].16+ inner frame (#360).17 - greatly improved error message reporting (#339)18+- implicit tuple expressions can no longer be totally empty.19+ This change makes ``{% if %}...{% endif %}`` a syntax error20+ now. (#364)21 22 Version 2.2.123 -------------24 (bugfix release, released on September 14th 2009)114 unmodified lines