Skip to content

Commit

Permalink
fix unexpected error when use empty control block
Browse files Browse the repository at this point in the history
  • Loading branch information
cocolato committed Feb 6, 2024
1 parent 2815589 commit f816f1f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
26 changes: 19 additions & 7 deletions mako/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,24 @@ def visitControlLine(self, node):
text = node.text
self.printer.writeline(text)
children = node.get_children()
# 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, and
# 3) any control line with no content other than comments
if not children or (
all(

# this covers the four 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, and
# 3) any control line with no content other than comments
# 4) the first control block with no content other than comments
def _search_for_control_line():
for c in children:
if isinstance(c, parsetree.Comment):
continue
elif isinstance(c, parsetree.ControlLine):
return True
return False

if (
not children
or all(
isinstance(c, (parsetree.Comment, parsetree.ControlLine))
for c in children
)
Expand All @@ -853,6 +864,7 @@ def visitControlLine(self, node):
for c in children
if isinstance(c, parsetree.ControlLine)
)
or _search_for_control_line()
):
self.printer.writeline("pass")

Expand Down
71 changes: 71 additions & 0 deletions test/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,47 @@ def test_blank_control_8(self):
template_args={"ctx": ctx},
)

def test_blank_control_9(self):
self._do_memory_test(
"""
% if True:
% elif False:
false
% else:
broken
% endif
""",
"",
filters=lambda s: s.strip(),
template_args={"ctx": ctx},
)

def test_blank_control_10(self):
self._do_memory_test(
"""
% if True:
% else:
test
% endif
""",
"",
filters=lambda s: s.strip(),
template_args={"ctx": ctx},
)

def test_blank_control_11(self):
self._do_memory_test(
"""
% try:
% except:
error
% endtry
""",
"",
filters=lambda s: s.strip(),
template_args={"ctx": ctx},
)

def test_commented_blank_control_1(self):
self._do_memory_test(
"""
Expand Down Expand Up @@ -1135,6 +1176,36 @@ def test_commented_blank_control_8(self):
template_args={"ctx": ctx},
)

def test_commented_blank_control_9(self):
self._do_memory_test(
"""
% if True:
## comment
% elif False:
false
% else:
broken
% endif
""",
"",
filters=lambda s: s.strip(),
template_args={"ctx": ctx},
)

def test_commented_blank_control_10(self):
self._do_memory_test(
"""
% try:
## comment
% except:
error
% endtry
""",
"",
filters=lambda s: s.strip(),
template_args={"ctx": ctx},
)

def test_multiline_control(self):
t = Template(
"""
Expand Down

0 comments on commit f816f1f

Please sign in to comment.