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

mako template lookup fails in 1.3.3 #662

Closed
landreville opened this issue Aug 22, 2012 · 14 comments
Closed

mako template lookup fails in 1.3.3 #662

landreville opened this issue Aug 22, 2012 · 14 comments

Comments

@landreville
Copy link
Contributor

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
Copy link
Contributor Author

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

@blaflamme
Copy link
Member

Your project demo raises a 404 error :(

@landreville
Copy link
Contributor Author

I just corrected the link

@landreville
Copy link
Contributor Author

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
Copy link
Member

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
Copy link
Member

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

@landreville
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@blaflamme
Copy link
Member

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
Copy link
Contributor Author

That fix works, thanks!

@blaflamme
Copy link
Member

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

@mcdonc
Copy link
Member

mcdonc commented Sep 11, 2012

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 as completed Sep 11, 2012
@blaflamme
Copy link
Member

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

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

@mcdonc
Copy link
Member

mcdonc commented Sep 11, 2012

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants