From f61000dede3daaf81f4c9e7ceb8690d9a78f0721 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 30 May 2007 00:27:32 +0200 Subject: [PATCH] [svn] choice loader now doesn't screen template not found exception of imports / extends --HG-- branch : trunk --- jinja/exceptions.py | 5 ++-- jinja/loaders.py | 26 +++++++++++++++------ tests/loaderres/templates/brokenimport.html | 1 + tests/test_loaders.py | 9 +++++++ 4 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 tests/loaderres/templates/brokenimport.html 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) -- 2.26.2