Skip to content

Commit

Permalink
Fix the unexpected error that occurs when an empty control block is u…
Browse files Browse the repository at this point in the history
…sed.

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
  • Loading branch information
cocolato authored and sqla-tester committed Feb 6, 2024
1 parent 2815589 commit 7a19fbe
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
Original file line number Diff line number Diff line change
@@ -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
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 7a19fbe

Please sign in to comment.