Skip to content

Commit 7a19fbe

Browse files
cocolatosqla-tester
authored andcommitted
Fix the unexpected error that occurs when an empty control block is used.
Fixes: #146 When the first control block is empty or comment, the correct result will now be rendered. ```python from mako.template import Template template = r""" % if False: % elif False: % else: test % endif """ t = Template(template) print(t.render()) ``` Result: ``` test ``` Closes: #387 Pull-request: #387 Pull-request-sha: fb257ff Change-Id: Ief78cdf4eb39b6e8257332ed034dab052c715420
1 parent 2815589 commit 7a19fbe

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

doc/build/unreleased/146.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. change::
2+
:tags: bug, codegen
3+
:tickets: 146
4+
5+
Fixed unexpected error when use control lines which the
6+
first control block with no bodies other than comments,
7+
as `pass` is now added to the first empty block.
8+
Pull request courtesy Hai Zhu.

mako/codegen.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -838,13 +838,24 @@ def visitControlLine(self, node):
838838
text = node.text
839839
self.printer.writeline(text)
840840
children = node.get_children()
841-
# this covers the three situations where we want to insert a pass:
842-
# 1) a ternary control line with no children,
843-
# 2) a primary control line with nothing but its own ternary
844-
# and end control lines, and
845-
# 3) any control line with no content other than comments
846-
if not children or (
847-
all(
841+
842+
# this covers the four situations where we want to insert a pass:
843+
# 1) a ternary control line with no children,
844+
# 2) a primary control line with nothing but its own ternary
845+
# and end control lines, and
846+
# 3) any control line with no content other than comments
847+
# 4) the first control block with no content other than comments
848+
def _search_for_control_line():
849+
for c in children:
850+
if isinstance(c, parsetree.Comment):
851+
continue
852+
elif isinstance(c, parsetree.ControlLine):
853+
return True
854+
return False
855+
856+
if (
857+
not children
858+
or all(
848859
isinstance(c, (parsetree.Comment, parsetree.ControlLine))
849860
for c in children
850861
)
@@ -853,6 +864,7 @@ def visitControlLine(self, node):
853864
for c in children
854865
if isinstance(c, parsetree.ControlLine)
855866
)
867+
or _search_for_control_line()
856868
):
857869
self.printer.writeline("pass")
858870

test/test_template.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,47 @@ def test_blank_control_8(self):
10361036
template_args={"ctx": ctx},
10371037
)
10381038

1039+
def test_blank_control_9(self):
1040+
self._do_memory_test(
1041+
"""
1042+
% if True:
1043+
% elif False:
1044+
false
1045+
% else:
1046+
broken
1047+
% endif
1048+
""",
1049+
"",
1050+
filters=lambda s: s.strip(),
1051+
template_args={"ctx": ctx},
1052+
)
1053+
1054+
def test_blank_control_10(self):
1055+
self._do_memory_test(
1056+
"""
1057+
% if True:
1058+
% else:
1059+
test
1060+
% endif
1061+
""",
1062+
"",
1063+
filters=lambda s: s.strip(),
1064+
template_args={"ctx": ctx},
1065+
)
1066+
1067+
def test_blank_control_11(self):
1068+
self._do_memory_test(
1069+
"""
1070+
% try:
1071+
% except:
1072+
error
1073+
% endtry
1074+
""",
1075+
"",
1076+
filters=lambda s: s.strip(),
1077+
template_args={"ctx": ctx},
1078+
)
1079+
10391080
def test_commented_blank_control_1(self):
10401081
self._do_memory_test(
10411082
"""
@@ -1135,6 +1176,36 @@ def test_commented_blank_control_8(self):
11351176
template_args={"ctx": ctx},
11361177
)
11371178

1179+
def test_commented_blank_control_9(self):
1180+
self._do_memory_test(
1181+
"""
1182+
% if True:
1183+
## comment
1184+
% elif False:
1185+
false
1186+
% else:
1187+
broken
1188+
% endif
1189+
""",
1190+
"",
1191+
filters=lambda s: s.strip(),
1192+
template_args={"ctx": ctx},
1193+
)
1194+
1195+
def test_commented_blank_control_10(self):
1196+
self._do_memory_test(
1197+
"""
1198+
% try:
1199+
## comment
1200+
% except:
1201+
error
1202+
% endtry
1203+
""",
1204+
"",
1205+
filters=lambda s: s.strip(),
1206+
template_args={"ctx": ctx},
1207+
)
1208+
11381209
def test_multiline_control(self):
11391210
t = Template(
11401211
"""

0 commit comments

Comments
 (0)