mako icon indicating copy to clipboard operation
mako copied to clipboard

Empty block in middle of %if...%elif...%elif...%else...%endif doesn't get a pass in the Python, leading to an IndentationError

Open sqlalchemy-bot opened this issue 14 years ago • 9 comments

Migrated issue, originally created by Anonymous

How-To-Reproduce:

% if True:
test
% elif lose:
## do nothing
% elif False:
broken
% endif

Results:

An IndentationError pointing at the next line of code after the do-nothing block, complaining that Python was expecting an indented block.

Workaround: Add <%pass%> to the do-nothing block


Attachments: 146.patch | 146.2.patch | 146.3.patch

sqlalchemy-bot avatar Aug 10 '10 11:08 sqlalchemy-bot

Michael Bayer (@zzzeek) wrote:

a very old bug which I thought we had a bug report for, thought we fixed, but all these assumptions (except the first) are wrong.

sqlalchemy-bot avatar Aug 10 '10 12:08 sqlalchemy-bot

Anonymous wrote:

Don't you have to add a 'pass' in that same situation in Python? Isn't this asking mako behavior to diverge from Python unnecessarily? Although it would indeed be more convenient to not have to manually insert the pass statement.

sqlalchemy-bot avatar Jun 25 '11 12:06 sqlalchemy-bot

Michael Bayer (@zzzeek) wrote:

the patch is in 1b4d1fb7e8a33ce3f285a0bbd5b2a1407a75b96c but doesn't actually fix the above test case, it appears that the comment line screws it up:

from mako.template import Template
template = r"""
% if True:
test
% elif lose:
##  do nothing
% elif False:
broken
% endif
"""

t = Template(template)
print t.render()



Traceback (most recent call last):
  File "test.py", line 12, in <module>
    t = Template(template)
  File "/Users/classic/dev/mako/mako/template.py", line 258, in __init__
    (code, module) = _compile_text(self, text, filename)
  File "/Users/classic/dev/mako/mako/template.py", line 606, in _compile_text
    code = compile(source, cid, 'exec')
  File "memory:0x1004a68d0", line 29
    elif False:
       ^
IndentationError: expected an indented block

taking out the comment, it's fine:

from mako.template import Template
template = r"""
% if True:
test
% elif lose:
% elif False:
broken
% endif
"""

t = Template(template)
print t.render()

sqlalchemy-bot avatar Apr 03 '12 17:04 sqlalchemy-bot

Michael Bayer (@zzzeek) wrote:

great, 8cce6101ac97aa59023f23185a5e1160330a93be and we're done ! thanks!

sqlalchemy-bot avatar Apr 06 '12 18:04 sqlalchemy-bot

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

sqlalchemy-bot avatar Apr 06 '12 18:04 sqlalchemy-bot

Hi there, it seems that if the empty statement is the first one it still fails:

from mako.template import Template
template = r"""
% if True:
% elif lose:
test
% elif False:
broken
% endif
"""

t = Template(template)
print(t.render())  # IndentationError: expected an indented block

So the bug is not entirely fixed.

smarie avatar Apr 12 '19 08:04 smarie

hi there -

unfortunately Mako needs a maintainer to be able to look into these issues, I can only submit PRs with tests I don't have time to diagnose intricate issues like these.

zzzeek avatar Apr 12 '19 13:04 zzzeek

no worries I found a workaround, but could you please just reopen the ticket so that when a maintainer is active, he will know what to fix ? Thanks!

smarie avatar Apr 12 '19 14:04 smarie

Sure! github lets you do all that stuff too.

zzzeek avatar Apr 12 '19 14:04 zzzeek