changeset: 561:cc0544cfe0d6 tag: tip user: Benjamin Trofatter date: Tue Apr 03 17:26:25 2012 -0500 files: mako/codegen.py test/test_template.py description: Fixed lack of comment support for ticket #146. diff -r 1d09b119175d -r cc0544cfe0d6 mako/codegen.py --- a/mako/codegen.py Mon Apr 02 10:57:20 2012 -0500 +++ b/mako/codegen.py Tue Apr 03 17:26:25 2012 -0500 @@ -778,15 +778,18 @@ 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 -r 1d09b119175d -r cc0544cfe0d6 test/test_template.py --- a/test/test_template.py Mon Apr 02 10:57:20 2012 -0500 +++ b/test/test_template.py Tue Apr 03 17:26:25 2012 -0500 @@ -882,6 +882,104 @@ "", 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("""