Skip to content

Commit

Permalink
Merge "Fix the unexpected error that occurs when an empty control blo…
Browse files Browse the repository at this point in the history
…ck is used." into main
  • Loading branch information
zzzeek authored and Gerrit Code Review committed Apr 3, 2024
2 parents e2606d5 + 7a19fbe commit b90eabe
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
8 changes: 8 additions & 0 deletions doc/build/unreleased/146.rst
@@ -0,0 +1,8 @@
.. change::
:tags: bug, codegen
:tickets: 146

Fixed unexpected error when use control lines which the
first control block with no bodies other than comments,
as `pass` is now added to the first empty block.
Pull request courtesy Hai Zhu.
26 changes: 19 additions & 7 deletions mako/codegen.py
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
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 b90eabe

Please sign in to comment.