Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

mako template lookup fails in 1.3.3 #662

Closed
landreville opened this Issue · 14 comments

3 participants

@landreville

Starting in 1.3.3 mako template lookup fails when the renderer argument to view_config is an asset specification to a mako template and within that mako template an inherit tag contains an absolute URI (eg '/somedir/mytemplate.mako'). This bug was not present in pyramid 1.3.2.

The follow traceback will be displayed when rendering the page:

Traceback (most recent call last):
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.2-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
    result = handler(request)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/tweens.py", line 20, in excview_tween
    response = handler(request)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/router.py", line 164, in handle_request
    response = view_callable(context, request)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/config/views.py", line 352, in rendered_view
    context)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/renderers.py", line 396, in render_view
    return self.render_to_response(response, system, request=request)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/renderers.py", line 426, in render_to_response
    result = self.render(value, system_values, request=request)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/renderers.py", line 422, in render
    result = renderer(value, system_values)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/mako_templating.py", line 200, in __call__
    reraise(MakoRenderingException(errtext), None, exc_info[2])
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/mako_templating.py", line 192, in __call__
    result = template.render_unicode(**system)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/template.py", line 421, in render_unicode
    as_unicode=True)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 766, in _render
    **_kwargs_for_callable(callable_, data))
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 797, in _render_context
    (inherit, lclcontext) = _populate_self_namespace(context, tmpl)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 743, in _populate_self_namespace
    ret = template.module._mako_inherit(template, context)
  File "bugtest_templates_blarg_mako", line 24, in _mako_inherit

  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 702, in _inherit_from
    template = _lookup_template(context, uri, calling_uri)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 734, in _lookup_template
    raise exceptions.TemplateLookupException(str(e))
MakoRenderingException: 

Traceback (most recent call last):
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/pyramid-1.3.3-py2.7.egg/pyramid/mako_templating.py", line 192, in __call__
    result = template.render_unicode(**system)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/template.py", line 421, in render_unicode
    as_unicode=True)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 766, in _render
    **_kwargs_for_callable(callable_, data))
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 797, in _render_context
    (inherit, lclcontext) = _populate_self_namespace(context, tmpl)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 743, in _populate_self_namespace
    ret = template.module._mako_inherit(template, context)
  File "/Users/jason/workspace/regression/mako-lookup-error/bugtest/templates/blarg.mako", line 1, in _mako_inherit
    <%inherit file="/base.mako"/>
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 702, in _inherit_from
    template = _lookup_template(context, uri, calling_uri)
  File "/Users/jason/workspace/virtualenv/regress/lib/python2.7/site-packages/Mako-0.7.2-py2.7.egg/mako/runtime.py", line 734, in _lookup_template
    raise exceptions.TemplateLookupException(str(e))
TemplateLookupException: Can not locate template for uri 'bugtest:/base.mako'
@landreville

A simple Pyramid project that will demo this bug is located at https://github.com/landreville/mako-lookup-error

@blaflamme
Owner

Your project demo raises a 404 error :(

@landreville

I just corrected the link

@landreville

It looks like the recent fixes for mako lookups with asset specs in 1.3.3 have caused this bug. In pyramid/mako_templating.py line 49 (in PkgResourceTemplateLookup.adjust_uri()) it splits the renderer asset spec and makes the inherit URI relative to the same place as the path in the asset spec (which is the python package). The inherit URI is actually relative to the mako template directory so of course it cannot find the template.

if relativeto is not None:
            relativeto = relativeto.replace('$', ':') # bugtest:templates/blarg.mako
            if not(':' in uri) and (':' in relativeto):
                pkg, relto = relativeto.split(':') # bugtest, templates/blarg.mako
                _uri = posixpath.join(posixpath.dirname(relto), uri) # /base.mako
                return '{0}:{1}'.format(pkg, _uri) # bugtest:/base.mako 
@blaflamme
Owner

You're right.. However I'm not sure it makes sense to mixup asset specs and mako templates absolute paths? If you are using both relative and absolute mako paths it works as it should, and then it works with asset spec and relative mako paths. Why do you need to mixup both?

@blaflamme
Owner

try this branch and see if it fixes your problem: https://github.com/Pylons/pyramid/tree/fix.mako-662

@landreville

I should mention that in my project the inherited template is also in a different mako lookup directory so a relative path would not work.

Using asset specifications for every template URI would limit my ability to easily change template locations. If I use the mako directories configuration I can move the directory and change a single setting instead of updating every reference to a template. However, sometimes it is more straightforward to use an asset specification, especially if the template isn't in a mako lookup directory. I don't see why I should be limited to only using asset specifications or paths instead of both.
Even if I wanted to switch to asset specifications only it would be a huge update that I would prefer to do incrementally since I have projects with several hundred templates.

@landreville

I think that branch fixes it -- I will try it out a bit more

@blaflamme
Owner

You reached the case that was not covered, an asset spec template inheriting from an absolute uri. Of course you should not be limited ;)

@landreville

That fix works, thanks!

@blaflamme
Owner

awesome... will be included in the next version update

@mcdonc
Owner

A fix will be in 1.4. If someone is industrious enough to port the fixes to the 1.3 branch, we could release a 1.3 version with it in it too.

@mcdonc mcdonc closed this
@blaflamme
Owner

Already back ported to 1.3 branch, ready for next release:

https://github.com/Pylons/pyramid/blob/1.3-branch/CHANGES.txt#L13

@mcdonc
Owner

Cool. It'll go out with the next 1.3 release then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.