From 96ec24f6e0ff77a24d0151afe20ec7c97118019f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 22 May 2016 11:36:40 +0200 Subject: [PATCH] Fast path for disabled template load explain. --- flask/templating.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/flask/templating.py b/flask/templating.py index 8c95a6a7..2da4926d 100644 --- a/flask/templating.py +++ b/flask/templating.py @@ -52,27 +52,36 @@ class DispatchingJinjaLoader(BaseLoader): self.app = app def get_source(self, environment, template): - explain = self.app.config['EXPLAIN_TEMPLATE_LOADING'] + if self.app.config['EXPLAIN_TEMPLATE_LOADING']: + return self._get_source_explained(environment, template) + return self._get_source_fast(environment, template) + + def _get_source_explained(self, environment, template): attempts = [] - tmplrv = None + trv = None for srcobj, loader in self._iter_loaders(template): try: rv = loader.get_source(environment, template) - if tmplrv is None: - tmplrv = rv - if not explain: - break + if trv is None: + trv = rv except TemplateNotFound: rv = None attempts.append((loader, srcobj, rv)) - if explain: - from .debughelpers import explain_template_loading_attempts - explain_template_loading_attempts(self.app, template, attempts) + from .debughelpers import explain_template_loading_attempts + explain_template_loading_attempts(self.app, template, attempts) + + if trv is not None: + return trv + raise TemplateNotFound(template) - if tmplrv is not None: - return tmplrv + def _get_source_fast(self, environment, template): + for srcobj, loader in self._iter_loaders(template): + try: + return loader.get_source(environment, template) + except TemplateNotFound: + continue raise TemplateNotFound(template) def _iter_loaders(self, template):