Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bug in pygen module #68

Closed
sqlalchemy-bot opened this issue Jan 10, 2008 · 2 comments
Closed

Minor bug in pygen module #68

sqlalchemy-bot opened this issue Jan 10, 2008 · 2 comments

Comments

@sqlalchemy-bot
Copy link

@sqlalchemy-bot sqlalchemy-bot commented Jan 10, 2008

Migrated issue, originally created by Anonymous

Here's the description. When identificators starting with else, elif, try, except,
finally, while, for, def and class are present inside template context, code generator
gets confused about indentions.
So the generated code looks like this:

def render_body(context,**pageargs):
    context.caller_stack.push_frame()
    try:
        __M_locals = dict(pageargs=pageargs)
    except_val = context.get('except_val', UNDEFINED)
    # SOURCE LINE 1
    context.write(u'Internal Server Error')
    context.write(unicode(except_val))
    return ''
    finally:
        context.caller_stack.pop_frame()

and

def render_body(context,**pageargs):
    context.caller_stack.push_frame()
    try:
        __M_locals = dict(pageargs=pageargs)
        exc_val = context.get('exc_val', UNDEFINED)
        # SOURCE LINE 1
        context.write(u'Internal Server Error')
        context.write(unicode(exc_val))
        return ''
    finally:
        context.caller_stack.pop_frame()

Of course, prefixes and abbreviations can be used, but I think this is unmotivated
constrain. Especially when it is only about syntax test for variable name in Python.
Only few changes in regular expressions are needed inside '''mako.pygen.PythonPrinter.writeline''' and
'''mako.pygen.PythonPrinter._is_unindentor''', simply by writing this:

(base_regexp)[^\da-zA-Z_]+'''

Maybe, I've missed something, but the code seems to work now.
Here's (if you can say it) the patch from version which I currently use.

10c10,13
< from StringIO import StringIO
---
> try:
>     from cStringIO import StringIO
> except ImportError:
>     from StringIO import StringIO
105c108
<             match = re.match(r"^\s*(if|try|elif|while|for)", line)
---
>             match = re.match(r"^\s*(if|try|elif|while|for)[^\da-zA-Z_]+", line)
116c119
<                 m2 = re.match(r"^\s*(def|class|else|elif|except|finally)", line)
---
>                 m2 = re.match(r"^\s*(def|class|else|elif|except|finally)[^\da-zA-Z_]", line)
141c144
<         match = re.match(r"^\s*(else|elif|except|finally)", line)
---
>         match = re.match(r"^\s*(else|elif|except|finally)[^\da-zA-Z_]+", line)

Attachments: pygen.py-1.patch

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Jan 11, 2008

Michael Bayer (@zzzeek) wrote:

I have a short fix for this in f636709, which resolves the failing test case I was able to create. But I don't know what tests fail for you since you didn't post any, so feel free to post those if this fix doesn't resolve the issue.

Loading

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Jan 11, 2008

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant