diff --git a/mako/codegen.py b/mako/codegen.py index 03c400e..6ab6095 100644 --- a/mako/codegen.py +++ b/mako/codegen.py @@ -778,15 +778,18 @@ def visitControlLine(self, node): text = node.text self.printer.writeline(text) children = node.get_children() - # this covers the two situations where we want to insert a pass: - # 1) a ternary control line with no children and + # this covers the three situations where we want to insert a pass: + # 1) a ternary control line with no children, # 2) a primary control line with nothing but its own ternary - # and end control lines - if (not node.get_children or - util.all(isinstance(c, parsetree.ControlLine) - for c in children) and - util.all((node.is_ternary(c.keyword) or c.isend) - for c in children)): + # and end control lines, and + # 3) any control line with no content other than comments + if not children or ( + util.all(isinstance(c, (parsetree.Comment, + parsetree.ControlLine)) + for c in children) and + util.all((node.is_ternary(c.keyword) or c.isend) + for c in children + if isinstance(c, parsetree.ControlLine))): self.printer.writeline("pass") def visitText(self, node): diff --git a/test/test_template.py b/test/test_template.py index ba1f58e..6a617ef 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -882,6 +882,104 @@ def test_blank_control_8(self): "", filters=lambda s:s.strip() ) + + def test_commented_blank_control_1(self): + self._do_memory_test( + """ + % if True: + ## comment + % endif + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_2(self): + self._do_memory_test( + """ + % if True: + ## comment + % elif True: + ## comment + % endif + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_3(self): + self._do_memory_test( + """ + % if True: + ## comment + % else: + ## comment + % endif + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_4(self): + self._do_memory_test( + """ + % if True: + ## comment + % elif True: + ## comment + % else: + ## comment + % endif + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_5(self): + self._do_memory_test( + """ + % for x in range(10): + ## comment + % endfor + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_6(self): + self._do_memory_test( + """ + % while False: + ## comment + % endwhile + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_7(self): + self._do_memory_test( + """ + % try: + ## comment + % except: + ## comment + % endtry + """, + "", + filters=lambda s:s.strip() + ) + + def test_commented_blank_control_8(self): + self._do_memory_test( + """ + % with open('x', 'w') as fp: + ## comment + % endwith + """, + "", + filters=lambda s:s.strip() + ) def test_multiline_control(self): t = Template("""