From: Armin Ronacher Date: Tue, 29 May 2007 22:27:32 +0000 (+0200) Subject: [svn] choice loader now doesn't screen template not found exception of imports /... X-Git-Tag: 2.0rc1~308 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f61000dede3daaf81f4c9e7ceb8690d9a78f0721;p=jinja2.git [svn] choice loader now doesn't screen template not found exception of imports / extends --HG-- branch : trunk --- diff --git a/jinja/exceptions.py b/jinja/exceptions.py index 1994467..1e7c632 100644 --- a/jinja/exceptions.py +++ b/jinja/exceptions.py @@ -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): diff --git a/jinja/loaders.py b/jinja/loaders.py index 8c049cd..ea1b265 100644 --- a/jinja/loaders.py +++ b/jinja/loaders.py @@ -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 index 0000000..e3c106e --- /dev/null +++ b/tests/loaderres/templates/brokenimport.html @@ -0,0 +1 @@ +{% extends "missing.html" %} diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 32cf8fe..0111dd7 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -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)