Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix empty statement block #546

wants to merge 2 commits into from

2 participants


The following code will throw exception, because the generated code didn't add "pass" for empty "if" statement. The same for other ControlBlocks.

template = Template(utf8("{% if x > 4 %}{%elif x < 0%}{%else%}{%end%}"))

I am not sure if this is the cleanest fix though, it should be the simplest one.


There's no harm in emitting a "pass" statement when it's not needed (I verified with the disassembler), so I'm going to commit a simpler version that doesn't check whether the block is empty.

@bdarnell bdarnell closed this pull request from a commit
@bdarnell bdarnell 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.
@bdarnell bdarnell closed this in 54d91d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 24, 2012
  1. @yqz

    fix empty if/try... statement

    yqz committed
Commits on Jun 25, 2012
  1. @yqz

    fix the line number

    yqz committed
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 2 deletions.
  1. +14 −2 tornado/
  2. +5 −0 tornado/test/
16 tornado/
@@ -492,8 +492,20 @@ def each_child(self):
def generate(self, writer):
writer.write_line("%s:" % self.statement, self.line)
- with writer.indent():
- self.body.generate(writer)
+ pass_line_number = self.line
+ for chunk in self.body.chunks:
+ if isinstance(chunk, _IntermediateControlBlock):
+ if pass_line_number:
+ with writer.indent():
+ writer.write_line("pass", pass_line_number)
+ pass_line_number = chunk.line
+ else:
+ pass_line_number = None
+ with writer.indent():
+ chunk.generate(writer)
+ if pass_line_number:
+ with writer.indent():
+ writer.write_line("pass", pass_line_number)
class _IntermediateControlBlock(_Node):
5 tornado/test/
@@ -101,6 +101,11 @@ def test_if(self):
self.assertEqual(template.generate(x=5), b("yes"))
self.assertEqual(template.generate(x=3), b("no"))
+ template = Template(utf8("{% if x > 4 %}{%elif x < 0%}{%else%}{%end%}"))
+ self.assertEqual(template.generate(x=5), b(""))
+ self.assertEqual(template.generate(x=3), b(""))
+ self.assertEqual(template.generate(x=-1), b(""))
def test_try(self):
template = Template(utf8("""{% try %}
try{% set y = 1/x %}
Something went wrong with that request. Please try again.