Strange syntax error

319 views
Skip to first unread message

Branko Vukelic

unread,
Mar 8, 2010, 2:46:00 PM3/8/10
to mako-d...@googlegroups.com
Hi,

I've been using Mako for just about two weeks. It's been very smooth
and enjoyable until a few days ago.

I've hit a strange syntax error.

The web.py error page says this:

(SyntaxError) invalid syntax (<unknown>, line 10) ('#url_for =
Template(prefix).safe_substitute\ntotal_') in file
'templates/utils.mako' at line: 49 char: 5

The full traceback:

-- Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/web/application.py", line
242, in process
return self.handle()
File "/usr/lib/python2.6/site-packages/web/application.py", line
233, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/lib/python2.6/site-packages/web/application.py", line
412, in _delegate
return handle_class(cls)
File "/usr/lib/python2.6/site-packages/web/application.py", line
387, in handle_class
return tocall(*args)
File "/home/foxbunny/code/pyblog/app.py", line 39, in GET
per_page=per_page, logged_in=True, lang='en')
File "/usr/lib/python2.6/site-packages/mako/template.py", line 133, in render
return runtime._render(self, self.callable_, args, data)
File "/usr/lib/python2.6/site-packages/mako/runtime.py", line 364, in _render
_render_context(template, callable_, context, *args,
**_kwargs_for_callable(callable_, data))
File "/usr/lib/python2.6/site-packages/mako/runtime.py", line 380,
in _render_context
(inherit, lclcontext) = _populate_self_namespace(context, tmpl)
File "/usr/lib/python2.6/site-packages/mako/runtime.py", line 348,
in _populate_self_namespace
ret = template.module._mako_inherit(template, context)
File "posts_html", line 26, in _mako_inherit
File "posts_html", line 22, in _mako_generate_namespaces
File "/usr/lib/python2.6/site-packages/mako/runtime.py", line 158, in __init__
self.template = _lookup_template(context, templateuri, calling_uri)
File "/usr/lib/python2.6/site-packages/mako/runtime.py", line 339,
in _lookup_template
return lookup.get_template(uri)
File "/usr/lib/python2.6/site-packages/mako/lookup.py", line 85, in
get_template
return self.__load(srcfile, uri)
File "/usr/lib/python2.6/site-packages/mako/lookup.py", line 127, in __load
self.__collection[uri] = Template(uri=uri,
filename=posixpath.normpath(filename), lookup=self,
module_filename=(self.modulename_callable is not None and
self.modulename_callable(filename, uri) or None),
**self.template_args)
File "/usr/lib/python2.6/site-packages/mako/template.py", line 93, in __init__
(code, module) = _compile_text(self, file(filename).read(), filename)
File "/usr/lib/python2.6/site-packages/mako/template.py", line 257,
in _compile_text
node = lexer.parse()
File "/usr/lib/python2.6/site-packages/mako/lexer.py", line 160, in parse
if self.match_python_block():
File "/usr/lib/python2.6/site-packages/mako/lexer.py", line 276, in
match_python_block
self.append_node(parsetree.Code, self.escape_code(text),
match.group(1)=='!', lineno=line, pos=pos)
File "/usr/lib/python2.6/site-packages/mako/lexer.py", line 94, in append_node
node = nodecls(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/mako/parsetree.py", line 127,
in __init__
self.code = ast.PythonCode(text, **self.exception_kwargs)
File "/usr/lib/python2.6/site-packages/mako/ast.py", line 30, in __init__
expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
File "/usr/lib/python2.6/site-packages/mako/pyparser.py", line 37, in parse
raise exceptions.SyntaxException("(%s) %s (%s)" %
(e.__class__.__name__, str(e), repr(code[0:50])), **exception_kwargs)
SyntaxException: (SyntaxError) invalid syntax (<unknown>, line 10)
('#url_for = Template(prefix).safe_substitute\ntotal_') in file
'templates/utils.mako' at line: 49 char: 5

Here's the part of the template that error occurred in:

<%def name="pager(current, last, first=1, bracket=3,
prefix='?page=$page', id=None, title='pager')">
<%
#url_for = Template(prefix).safe_substitute
total_pages = last - first
page_set = 2 * bracket + 1
pages = []

# number of pages is less than brackets + current page
if page_set > total_pages:
pages = [i + 1 for i in range(total_pages)
# current page is closer to beginning
elif current - bracket <= 1:
pages = [i + 1 for i in range(page_set)
# current page is closer to end
elif current + bracket >= total_pages:
pages = [total_pages - 1 for i in range(page_set)
pages.reverse()
# current page is somewhere in middle
else:
pages = [current - bracket + i for i in range(page_set)]
%>
<ul title="${title}" ${'id="%s"' % str(id) if id | x}>
%if not current == first:
<li class="pager_first">
<a href="${url_for(page=first) | u}">${_('&lt;&lt; first')}</a>
</li>
<li class="pager_previous">
<a href="${url_for(page=current - 1) | u">${_('&lt;
previous')}</a>
</li>
%endif
%for page in pages:
<li class="pager_page">
%if not page == current:
<a href="${url_for(page=page) | u}">${page | x}</a>
%else:
${page | x}
%endif
</li>
%endfor
%if not current == last:
<li class="pager_next">
<a href="${url_for(page=current + 1) | u}">${_('next &gt;')}</a>
</li>
<li class="pager_last">
<a href="${url_for(page=last) | u}">${_('last &gt;&gt;')}</a>
</li>
%endif
</ul>
</%def>

I use the above def by importing it into a template using this line:

<%namespace file="utils.mako" import="pager" />

And then:

${pager(current_page, total_pages)}


Any idea?

--
Branko Vukelić

http://foxbunny.tumblr.com/
http://www.flickr.com/photos/16889956@N04/
http://www.twitter.com/foxbunny
http://github.com/foxbunny

Michael Bayer

unread,
Mar 8, 2010, 4:03:47 PM3/8/10
to mako-d...@googlegroups.com
Branko Vukelic wrote:
> Hi,
>
> I've been using Mako for just about two weeks. It's been very smooth
> and enjoyable until a few days ago.
>
> I've hit a strange syntax error.
>
> The web.py error page says this:
>
>
> Here's the part of the template that error occurred in:
>
> <%def name="pager(current, last, first=1, bracket=3,
> prefix='?page=$page', id=None, title='pager')">
> <%
> #url_for = Template(prefix).safe_substitute
> total_pages = last - first
> page_set = 2 * bracket + 1
> pages = []
>
> # number of pages is less than brackets + current page
> if page_set > total_pages:
> pages = [i + 1 for i in range(total_pages)
> # current page is closer to beginning
> elif current - bracket <= 1:
> pages = [i + 1 for i in range(page_set)
> # current page is closer to end
> elif current + bracket >= total_pages:
> pages = [total_pages - 1 for i in range(page_set)
> pages.reverse()
> # current page is somewhere in middle
> else:
> pages = [current - bracket + i for i in range(page_set)]
> %>

this is a syntax error, so these are easy to debug. Just put the block of
code into a file and remove elements until it compiles. Doing that here,
the issue is revealed as the fact that you have several list
comprehensions that are missing the closing bracket:

pages = [i + 1 for i in range(total_pages)

should be:

pages = [i + 1 for i in range(total_pages)]

there are two of those total.

> --
> You received this message because you are subscribed to the Google Groups
> "Mako Templates for Python" group.
> To post to this group, send email to mako-d...@googlegroups.com.
> To unsubscribe from this group, send email to
> mako-discuss...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mako-discuss?hl=en.
>
>

Branko Vukelic

unread,
Mar 8, 2010, 4:23:47 PM3/8/10
to mako-d...@googlegroups.com
On Mon, Mar 8, 2010 at 10:03 PM, Michael Bayer <mik...@zzzcomputing.com> wrote:
> this is a syntax error, so these are easy to debug.  Just put the block of
> code into a file and remove elements until it compiles.   Doing that here,
> the issue is revealed as the fact that you have several list
> comprehensions that are missing the closing bracket:
>
>  pages = [i + 1 for i in range(total_pages)
>
> should be:
>
>  pages = [i + 1 for i in range(total_pages)]
>
> there are two of those total.

That's embarrassing. :P

Ok, so the SyntaxError is actually a Python syntax error, not Mako
syntax error, right? That's really helpful. Thanks.

Michael Bayer

unread,
Mar 8, 2010, 6:54:30 PM3/8/10
to mako-d...@googlegroups.com
Branko Vukelic wrote:
> On Mon, Mar 8, 2010 at 10:03 PM, Michael Bayer <mik...@zzzcomputing.com>
> wrote:
>> this is a syntax error, so these are easy to debug.  Just put the block
>> of
>> code into a file and remove elements until it compiles.   Doing that
>> here,
>> the issue is revealed as the fact that you have several list
>> comprehensions that are missing the closing bracket:
>>
>>  pages = [i + 1 for i in range(total_pages)
>>
>> should be:
>>
>>  pages = [i + 1 for i in range(total_pages)]
>>
>> there are two of those total.
>
> That's embarrassing. :P
>
> Ok, so the SyntaxError is actually a Python syntax error, not Mako
> syntax error, right? That's really helpful. Thanks.

well they're related. we wrap Python's SyntaxError in a mako
SyntaxException that includes the location in the original template.

Branko Vukelic

unread,
Mar 9, 2010, 2:55:57 AM3/9/10
to mako-d...@googlegroups.com
On Tue, Mar 9, 2010 at 12:54 AM, Michael Bayer <mik...@zzzcomputing.com> wrote:
>> Ok, so the SyntaxError is actually a Python syntax error, not Mako
>> syntax error, right? That's really helpful. Thanks.
>
> well they're related.  we wrap Python's SyntaxError in a mako
> SyntaxException that includes the location in the original template.

I've noticed, it's awesome.

As a sidenote to web.py developers new to Mako: the
mako.exceptions.html_error_template has also been very helpful in
revealing the location.

from mako import exceptions

try:
render.some_template(*args)
except:
if web.config.debug:
return exceptions.html_error_template().render()
raise

Reply all
Reply to author
Forward
0 new messages