# HG changeset patch # User cmlenz # Date 1206629346 0 # Node ID cfe3b4f02d77ef9a482fb49822dcd6f0a17b4dee # Parent 408ab81767c769c89a7adb82da709d3b135af0a3 Fix for caching in template loader for templates included by templates loaded from an absolute path. Closes #193. Thanks to Waldemar Kornewald for the test case. diff --git a/genshi/template/loader.py b/genshi/template/loader.py --- a/genshi/template/loader.py +++ b/genshi/template/loader.py @@ -166,15 +166,16 @@ if relative_to and not os.path.isabs(relative_to): filename = os.path.join(os.path.dirname(relative_to), filename) filename = os.path.normpath(filename) + cachekey = filename self._lock.acquire() try: # First check the cache to avoid reparsing the same file try: - tmpl = self._cache[filename] + tmpl = self._cache[cachekey] if not self.auto_reload: return tmpl - mtime = self._mtime[filename] + mtime = self._mtime[cachekey] if mtime and mtime == os.path.getmtime(tmpl.filepath): return tmpl except KeyError, OSError: @@ -221,8 +222,8 @@ filename, encoding=encoding) if self.callback: self.callback(tmpl) - self._cache[filename] = tmpl - self._mtime[filename] = mtime + self._cache[cachekey] = tmpl + self._mtime[cachekey] = mtime finally: if hasattr(fileobj, 'close'): fileobj.close() diff --git a/genshi/template/tests/loader.py b/genshi/template/tests/loader.py --- a/genshi/template/tests/loader.py +++ b/genshi/template/tests/loader.py @@ -229,6 +229,38 @@
Included from sub
""", tmpl.generate().render()) + def test_abspath_caching(self): + abspath = os.path.join(self.dirname, 'abs') + os.mkdir(abspath) + file1 = open(os.path.join(abspath, 'tmpl1.html'), 'w') + try: + file1.write(""" + + """) + finally: + file1.close() + + file2 = open(os.path.join(abspath, 'tmpl2.html'), 'w') + try: + file2.write("""
Included from abspath.
""") + finally: + file2.close() + + searchpath = os.path.join(self.dirname, 'searchpath') + os.mkdir(searchpath) + file3 = open(os.path.join(searchpath, 'tmpl2.html'), 'w') + try: + file3.write("""
Included from searchpath.
""") + finally: + file3.close() + + loader = TemplateLoader(searchpath) + tmpl1 = loader.load(os.path.join(abspath, 'tmpl1.html')) + self.assertEqual(""" +
Included from searchpath.
+ """, tmpl1.generate().render()) + assert 'tmpl2.html' in loader._cache + def test_load_with_default_encoding(self): f = open(os.path.join(self.dirname, 'tmpl.html'), 'w') try: