Skip to content

Commit

Permalink
backport fix.mako-606
Browse files Browse the repository at this point in the history
  • Loading branch information
blaflamme committed Aug 3, 2012
1 parent c8a84b2 commit 560e362
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CHANGES.txt
Expand Up @@ -9,6 +9,12 @@ Next release
dictionary changed size during iteration`` exception. It no longer does.
See https://github.com/Pylons/pyramid/issues/635 for more information.

- Backport bug fix from master: - In Mako Templates lookup, check if the uri
is already adjusted and bring it back to an asset spec. Normally occurs with
inherited templates or included components.
https://github.com/Pylons/pyramid/issues/606
https://github.com/Pylons/pyramid/issues/607

1.3.2 (2012-05-19)
==================

Expand Down Expand Up @@ -691,7 +697,7 @@ Bug Fixes
predicate argument. See https://github.com/Pylons/pyramid/pull/308

- The AuthTktCookieHelper could potentially generate Unicode headers
inappropriately when the ``tokens`` argument to remember was used. See
inappropriately when the ``tokens`` argument to remember was used. See
https://github.com/Pylons/pyramid/pull/314.

- The AuthTktAuthenticationPolicy did not use a timing-attack-aware string
Expand Down
15 changes: 10 additions & 5 deletions pyramid/mako_templating.py
Expand Up @@ -41,12 +41,17 @@ def adjust_uri(self, uri, relativeto):
def get_template(self, uri):
"""Fetch a template from the cache, or check the filesystem
for it
In addition to the basic filesystem lookup, this subclass will
use pkg_resource to load a file using the asset
specification syntax.
"""
if '$' in uri:
# Checks if the uri is already adjusted and brings it back to
# an asset spec. Normally occurs with inherited templates or
# included components.
uri = uri.replace('$', ':')
isabs = os.path.isabs(uri)
if (not isabs) and (':' in uri):
# Windows can't cope with colons in filenames, so we replace the
Expand All @@ -69,7 +74,7 @@ def get_template(self, uri):
return TemplateLookup.get_template(self, uri)


registry_lock = threading.Lock()
registry_lock = threading.Lock()

class MakoRendererFactoryHelper(object):
def __init__(self, settings_prefix=None):
Expand Down Expand Up @@ -136,7 +141,7 @@ def sget(name, default=None):

registry_lock.acquire()
try:
registry.registerUtility(lookup, IMakoLookup,
registry.registerUtility(lookup, IMakoLookup,
name=settings_prefix)
finally:
registry_lock.release()
Expand All @@ -159,7 +164,7 @@ class MakoLookupTemplateRenderer(object):
def __init__(self, path, lookup):
self.path = path
self.lookup = lookup

def implementation(self):
return self.lookup.get_template(self.path)

Expand Down
32 changes: 21 additions & 11 deletions pyramid/tests/test_mako_templating.py
Expand Up @@ -135,7 +135,7 @@ def test_with_input_encoding(self):
self._callFUT(info)
lookup = self._getLookup()
self.assertEqual(lookup.template_args['input_encoding'], 'utf-16')

def test_with_error_handler(self):
settings = {'mako.directories':self.templates_dir,
'mako.error_handler':'pyramid.tests'}
Expand Down Expand Up @@ -368,7 +368,7 @@ def test_implementation(self):
result = instance.implementation().render_unicode()
self.assertTrue(isinstance(result, text_type))
self.assertEqual(result, text_('result'))

class TestIntegration(unittest.TestCase):
def setUp(self):
import pyramid.mako_templating
Expand All @@ -391,7 +391,7 @@ def test_render_from_fs(self):
self.config.add_settings({'reload_templates': True})
result = render('helloworld.mak', {'a':1}).replace('\r','')
self.assertEqual(result, text_('\nHello föö\n', 'utf-8'))

def test_render_inheritance(self):
from pyramid.renderers import render
result = render('helloinherit.mak', {}).replace('\r','')
Expand All @@ -414,7 +414,7 @@ def test_render_to_response_pkg_spec(self):
{'a':1})
self.assertEqual(result.ubody.replace('\r', ''),
text_('\nHello föö\n', 'utf-8'))

def test_render_with_abs_path(self):
from pyramid.renderers import render
result = render('/helloworld.mak', {'a':1}).replace('\r','')
Expand All @@ -426,7 +426,7 @@ def test_get_renderer(self):
self.assertEqual(
result.implementation().render_unicode().replace('\r',''),
text_('\nHello föö\n', 'utf-8'))

def test_template_not_found(self):
from pyramid.renderers import render
from mako.exceptions import TemplateLookupException
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_get_template_not_asset_spec(self):
inst = self._makeOne(directories=[fixturedir])
result = inst.get_template('helloworld.mak')
self.assertFalse(result is None)

def test_get_template_asset_spec_with_filesystem_checks(self):
inst = self._makeOne(filesystem_checks=True)
result = inst.get_template('pyramid.tests:fixtures/helloworld.mak')
Expand All @@ -478,7 +478,17 @@ def test_get_template_asset_spec_with_module_dir(self):
self.assertFalse(result is None)
finally:
shutil.rmtree(tmpdir, ignore_errors=True)


def test_get_template_asset_spec_with_uri_adjusted(self):
inst = self._makeOne(filesystem_checks=True)
result = inst.get_template('pyramid.tests$fixtures/helloworld.mak')
self.assertFalse(result is None)

def test_get_template_asset_spec_with_uri_not_adjusted(self):
inst = self._makeOne(filesystem_checks=True)
result = inst.get_template('pyramid.tests:fixtures/helloworld.mak')
self.assertFalse(result is None)

def test_get_template_asset_spec_missing(self):
from mako.exceptions import TopLevelLookupException
fixturedir = self.get_fixturedir()
Expand All @@ -490,7 +500,7 @@ class TestMakoRenderingException(unittest.TestCase):
def _makeOne(self, text):
from pyramid.mako_templating import MakoRenderingException
return MakoRenderingException(text)

def test_repr_and_str(self):
exc = self._makeOne('text')
self.assertEqual(str(exc), 'text')
Expand All @@ -499,7 +509,7 @@ def test_repr_and_str(self):
class DummyLookup(object):
def __init__(self, exc=None):
self.exc = exc

def get_template(self, path):
self.path = path
return self
Expand All @@ -513,8 +523,8 @@ def render_unicode(self, **values):
raise self.exc
self.values = values
return text_('result')

class DummyRendererInfo(object):
def __init__(self, kw):
self.__dict__.update(kw)

0 comments on commit 560e362

Please sign in to comment.