The hopefully final fix for the bug apollo13 spotted earlier.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 234cad6 · Browse files · 2 files·+22 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+22 added−12 removed
- tests:+13 added−1 removed
- other:+9 added−11 removed
Provenance
- Commit
7966895- Tree
f76b961- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
234cad6
2 files changed+22 added−12 removed
modified:jinja2/compiler.py+9 added-11 removed
136 unmodified lines137 # conditions. This information is used to optimize inheritance138 # situations.139 self.rootlevel = False140 141- # An extends statement flags the frame as silent if it occours.142- # This is used to suppress unwanted output.143- self.silent = False141+ # in some dynamic inheritance situations the compiler needs to add142+ # write tests around output statements.143+ self.require_output_check = parent and parent.require_output_check144 145 # inside some tags we are using a buffer rather than yield statements.146 # this for example affects {% filter %} or {% macro %}. If a frame147 # is buffered this variable points to the name of the list used as533 unmodified lines681 # process the root682 frame = Frame()683 frame.inspect(node.body)684 frame.toplevel = frame.rootlevel = True685+ frame.require_output_check = have_extends and not self.has_known_extends686 self.indent()687 if have_extends:688 self.writeline('parent_template = None')689 if 'self' in find_undeclared(node.body, ('self',)):67 unmodified lines757 """Calls the extender."""758 if not frame.toplevel:759 self.fail('cannot use extend from a non top-level scope',760 node.lineno)760- frame.silent = True761 762 # if the number of extends statements in general is zero so763 # far, we don't have to add a check if something extended764 # the template before this one.7 unmodified lines772 self.writeline('if parent_template is not None:')773 self.indent()774 self.writeline('raise TemplateRuntimeError(%r)' %775 'extended multiple times')776+ self.outdent()777 778 # if we have a known extends already we don't need that code here779 # as we know that the template execution will end here.780 if self.has_known_extends:781 raise CompilerExit()781- self.outdent()782 783 self.writeline('parent_template = environment.get_template(', node)784 self.visit(node.template, frame)785 self.write(', %r)' % self.name)271 unmodified lines1057 self.visit(node.node, frame)1058 1059 def visit_Output(self, node, frame):1060 # if we have a known extends statement, we don't output anything1061- # if we are in a silent section1062- if self.has_known_extends and frame.silent:1061+ # if we are in a require_output_check section1062+ if self.has_known_extends and frame.require_output_check:1063 return1064 1065 if self.environment.finalize:1066 finalize = lambda x: unicode(self.environment.finalize(x))1 unmodified line1068 finalize = unicode1069 1070 self.newline(node)1071 1072- # if we are not in a block and there was already an extends1073- # statement we have to add a check that disables our yield(s) here1074- # so that they don't appear in the output.1072+ # if we are inside a frame that requires output checking, we do so1073 outdent_later = False1076- if frame.silent and self.extends_so_far != 0:1074+ if frame.require_output_check:1075 self.writeline('if parent_template is None:')1076 self.indent()1077 outdent_later = True1078 347 unmodified linesmodified:tests/test_old_bugs.py+13 added-1 removed
6 unmodified lines7 8 :copyright: Copyright 2008 by Armin Ronacher.9 :license: BSD.10 """11-from jinja2 import Environment11+from jinja2 import Environment, DictLoader12 13+14 def test_keyword_folding():15 env = Environment()16 env.filters['testing'] = lambda value, some: value + some17 assert env.from_string("{{ 'test'|testing(some='stuff') }}") \18 .render() == 'teststuff'19 20+21+def test_extends_output_bugs():22+ env = Environment(loader=DictLoader({23+ 'parent.html': '(({% block title %}{% endblock %}))'24+ }))25+26+ t = env.from_string('{% if expr %}{% extends "parent.html" %}{% endif %}'27+ '[[{% block title %}title{% endblock %}]]'28+ '{% for item in [1, 2, 3] %}({{ item }}){% endfor %}')29+ assert t.render(expr=False) == '[[title]](1)(2)(3)'30+ assert t.render(expr=True) == '((title))'