[svn] choice loader now doesn't screen template not found exception of imports /...
authorArmin Ronacher <armin.ronacher@active-4.com>
Tue, 29 May 2007 22:27:32 +0000 (00:27 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Tue, 29 May 2007 22:27:32 +0000 (00:27 +0200)
--HG--
branch : trunk

jinja/exceptions.py
jinja/loaders.py
tests/loaderres/templates/brokenimport.html [new file with mode: 0644]
tests/test_loaders.py

index 1994467bf46bca6808db4ddd0c695185b03705e3..1e7c632bde0cbbfe643e76356690b7698d2e7ff7 100644 (file)
@@ -61,8 +61,9 @@ class TemplateNotFound(IOError, TemplateError):
     Raised if a template does not exist.
     """
 
-    def __init__(self, message):
-        IOError.__init__(self, message)
+    def __init__(self, name):
+        IOError.__init__(self, name)
+        self.name = name
 
 
 class TemplateSyntaxError(SyntaxError, TemplateError):
index 8c049cd6d9817a2091fa53d546ed40a7a66e1f08..ea1b2659b7cccc23ec2919f88c31c372c431d3f2 100644 (file)
@@ -126,6 +126,12 @@ class BaseLoader(object):
         ast = self.parse(environment, name, None)
         return translator.process(environment, ast)
 
+    def get_source(self, environment, name, parent):
+        """
+        Override this method to get the source for a template.
+        """
+        raise TemplateNotFound(name)
+
 
 class CachedLoaderMixin(object):
     """
@@ -361,11 +367,11 @@ class PackageLoader(CachedLoaderMixin, BaseLoader):
 
     def get_source(self, environment, name, parent):
         from pkg_resources import resource_exists, resource_string
-        name = '/'.join([self.package_path] + [p for p in name.split('/')
-                        if p != '..'])
-        if not resource_exists(self.package_name, name):
+        path = '/'.join([self.package_path] + [p for p in name.split('/')
+                         if p != '..'])
+        if not resource_exists(self.package_name, path):
             raise TemplateNotFound(name)
-        contents = resource_string(self.package_name, name)
+        contents = resource_string(self.package_name, path)
         return contents.decode(environment.template_charset)
 
     def check_source_changed(self, environment, name):
@@ -496,7 +502,9 @@ class ChoiceLoader(object):
         for loader in self.loaders:
             try:
                 return loader.get_source(environment, name, parent)
-            except TemplateNotFound:
+            except TemplateNotFound, e:
+                if e.name != name:
+                    raise
                 continue
         raise TemplateNotFound(name)
 
@@ -504,7 +512,9 @@ class ChoiceLoader(object):
         for loader in self.loaders:
             try:
                 return loader.parse(environment, name, parent)
-            except TemplateNotFound:
+            except TemplateNotFound, e:
+                if e.name != name:
+                    raise
                 continue
         raise TemplateNotFound(name)
 
@@ -512,6 +522,8 @@ class ChoiceLoader(object):
         for loader in self.loaders:
             try:
                 return loader.load(environment, name, translator)
-            except TemplateNotFound:
+            except TemplateNotFound, e:
+                if e.name != name:
+                    raise
                 continue
         raise TemplateNotFound(name)
diff --git a/tests/loaderres/templates/brokenimport.html b/tests/loaderres/templates/brokenimport.html
new file mode 100644 (file)
index 0000000..e3c106e
--- /dev/null
@@ -0,0 +1 @@
+{% extends "missing.html" %}
index 32cf8fe33f815bc465645afaab173d8ca2e37c3d..0111dd70e69fa482502b33b22d80e8bc982e93ee 100644 (file)
@@ -81,6 +81,15 @@ def test_choice_loader():
     else:
         raise AssertionError('expected template exception')
 
+    # this should raise an TemplateNotFound error with the
+    # correct name
+    try:
+        env.get_template('brokenimport.html')
+    except TemplateNotFound, e:
+        assert e.name == 'missing.html'
+    else:
+        raise AssertionError('expected exception')
+
 
 def test_function_loader():
     env = Environment(loader=function_loader)