Skip to content
Browse files

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.
  • Loading branch information...
1 parent 5867170 commit 54d91d5e1312eb2e3d0c9ae0b7f5c8333a4ac50e @bdarnell bdarnell committed
Showing with 8 additions and 0 deletions.
  1. +4 −0 tornado/template.py
  2. +4 −0 tornado/test/template_test.py
View
4 tornado/template.py
@@ -501,6 +501,8 @@ def generate(self, writer):
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 @@ def __init__(self, statement, line):
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)
View
4 tornado/test/template_test.py
@@ -101,6 +101,10 @@ def test_if(self):
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 %}

0 comments on commit 54d91d5

Please sign in to comment.
Something went wrong with that request. Please try again.