Skip to content

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

Closed

Description

@sqlalchemy-bot

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

Activity

  1. sqlalchemy-bot commented on Aug 10, 2010

    @sqlalchemy-bot
    Author

    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.

  2. sqlalchemy-bot commented on Jun 25, 2011

    @sqlalchemy-bot
    Author

    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.

  3. sqlalchemy-bot commented on Apr 3, 2012

    @sqlalchemy-bot
    Author

    Michael Bayer (@zzzeek) wrote:

    the patch is in 1b4d1fb 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()
    
  4. sqlalchemy-bot commented on Apr 6, 2012

    @sqlalchemy-bot
    Author

    Michael Bayer (@zzzeek) wrote:

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

  5. sqlalchemy-bot commented on Apr 6, 2012

    @sqlalchemy-bot
    Author

    Changes by Michael Bayer (@zzzeek):

    • changed status to closed
  6. smarie commented on Apr 12, 2019

    @smarie

    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.

  7. zzzeek commented on Apr 12, 2019

    @zzzeek
    Member

    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.

  8. smarie commented on Apr 12, 2019

    @smarie

    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!

  9. zzzeek commented on Apr 12, 2019

    @zzzeek
    Member

    Sure! github lets you do all that stuff too.

  10. reopened this on Apr 12, 2019
  11. added a commit that references this issue on Feb 6, 2024
    fb257ff
  12. zzzeek commented on Aug 4, 2026

    @zzzeek
    Member

    The remaining case reported here — where the first control block in the chain is empty (or contains only a comment) — was fixed in commit 7a19fbe via #387 (thanks @cocolato), released in Mako 1.3.3.

    The code generator now inserts a pass for four situations, including "the first control block with no content other than comments." Regression tests (test_blank_control_9/10/11, test_commented_blank_control_9/10 in ControlTest) cover these variants.

    Verified on current main that all reported variants — middle-empty, middle-empty-with-comment, first-empty, first-empty-with-comment, and all-empty — now compile and render correctly. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions