changeset 699:cfe3b4f02d77 trunk

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.
author cmlenz
date Thu, 27 Mar 2008 14:49:06 +0000
parents 408ab81767c7
children 08f22328303d
files genshi/template/loader.py genshi/template/tests/loader.py
diffstat 2 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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()
--- a/genshi/template/tests/loader.py
+++ b/genshi/template/tests/loader.py
@@ -229,6 +229,38 @@
               <div>Included from sub</div>
             </html>""", 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("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl2.html" />
+            </html>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(abspath, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<div>Included from abspath.</div>""")
+        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("""<div>Included from searchpath.</div>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader(searchpath)
+        tmpl1 = loader.load(os.path.join(abspath, 'tmpl1.html'))
+        self.assertEqual("""<html>
+              <div>Included from searchpath.</div>
+            </html>""", 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:
Copyright (C) 2012-2017 Edgewall Software