Skip to content

Commit 54d91d5

Browse files
committed
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 tornadoweb#546.
1 parent 5867170 commit 54d91d5

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

tornado/template.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,8 @@ def generate(self, writer):
501501
writer.write_line("%s:" % self.statement, self.line)
502502
with writer.indent():
503503
self.body.generate(writer)
504+
# Just in case the body was empty
505+
writer.write_line("pass", self.line)
504506

505507

506508
class _IntermediateControlBlock(_Node):
@@ -509,6 +511,8 @@ def __init__(self, statement, line):
509511
self.line = line
510512

511513
def generate(self, writer):
514+
# In case the previous block was empty
515+
writer.write_line("pass", self.line)
512516
writer.write_line("%s:" % self.statement, self.line, writer.indent_size() - 1)
513517

514518

tornado/test/template_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def test_if(self):
101101
self.assertEqual(template.generate(x=5), b("yes"))
102102
self.assertEqual(template.generate(x=3), b("no"))
103103

104+
def test_if_empty_body(self):
105+
template = Template(utf8("{% if True %}{% else %}{% end %}"))
106+
self.assertEqual(template.generate(), b(""))
107+
104108
def test_try(self):
105109
template = Template(utf8("""{% try %}
106110
try{% set y = 1/x %}

0 commit comments

Comments
 (0)