1. Michael Bayer
  2. mako

Issues

Issue #68 resolved

Minor bug in pygen module

guest
created an issue

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)

  1. 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)

  1. 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)

Comments (1)

  1. Log in to comment