From: Ben Darnell Date: Mon, 27 Aug 2012 01:03:12 +0000 (-0700) Subject: Fix template errors with empty blocks. X-Git-Tag: v2.4.0~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54d91d5e1312eb2e3d0c9ae0b7f5c8333a4ac50e;p=thirdparty%2Ftornado.git Fix template errors with empty blocks. Emits a "pass" statement in every generated block whether it's empty or not; I verified with the disassembler that the unnecessary pass statements don't generate extra bytecodes. Closes #546. --- diff --git a/tornado/template.py b/tornado/template.py index 13eb78081..14306d9a5 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -501,6 +501,8 @@ class _ControlBlock(_Node): writer.write_line("%s:" % self.statement, self.line) with writer.indent(): self.body.generate(writer) + # Just in case the body was empty + writer.write_line("pass", self.line) class _IntermediateControlBlock(_Node): @@ -509,6 +511,8 @@ class _IntermediateControlBlock(_Node): self.line = line def generate(self, writer): + # In case the previous block was empty + writer.write_line("pass", self.line) writer.write_line("%s:" % self.statement, self.line, writer.indent_size() - 1) diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index a4b9453b7..6e14abb92 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -101,6 +101,10 @@ class TemplateTest(LogTrapTestCase): self.assertEqual(template.generate(x=5), b("yes")) self.assertEqual(template.generate(x=3), b("no")) + def test_if_empty_body(self): + template = Template(utf8("{% if True %}{% else %}{% end %}")) + self.assertEqual(template.generate(), b("")) + def test_try(self): template = Template(utf8("""{% try %} try{% set y = 1/x %}