Skip to content

better exception handling for syntax exception in python blocks #245

Closed

Description

@sqlalchemy-bot

Migrated issue, originally created by jvanasco (@jvanasco)

Here's a trivial/dumb example

<%def name="foo(\
        bar,
        biz = False,
        bash = False,
        bang = None,
    )">
<%
    # ok sample
    # more sample
    if True:
        fizz = 'bang'
    if biz is not True:
        biz = 'fizz'
%>
${biz}
</%def>

Imagine, if you can, that the if biz is not True: is malformed, because of a sticky key -- an "option+Space" combination creates a non-printing character that looks like a space in most editors, but is really bastardized character \xc2.

If normal Python encounters that, we get this error:

SyntaxError: Non-ASCII character '\xc2' in file /... on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

In mako, we get

SyntaxException: (SyntaxError) invalid syntax (<unknown>, line 7) (u'# ok sample') in file '/....mako' at line: 477 char: 1

The mako exception will always point to the first line of the function (after declaration), not the actual line. It also doesn't show the underlying syntax error. I'm not sure where that got lost.

Activity

  1. sqlalchemy-bot commented on Jul 6, 2015

    @sqlalchemy-bot
    Author

    Michael Bayer (@zzzeek) wrote:

    not sure how to fix

  2. sqlalchemy-bot commented on Jul 6, 2015

    @sqlalchemy-bot
    Author

    Changes by Michael Bayer (@zzzeek):

    • removed labels: feature
    • added labels: bug
  3. sqlalchemy-bot commented on Jul 6, 2015

    @sqlalchemy-bot
    Author

    jvanasco (@jvanasco) wrote:

    Yeah I was lost on this. I had a bad version of my pep8 ticket working locally (which is how I found my big), but couldn't figure this one out.

  4. sqla-tester commented on Aug 4, 2026

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the main branch:

    Report the line of a syntax error within a python block https://gerrit.sqlalchemy.org/c/sqlalchemy/mako/+/6865

  5. added a commit that references this issue on Aug 4, 2026
    e2231b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions