From 10dae5bb3dea186dc96e419574f592ee1f0a88f6 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 31 Mar 2007 00:02:32 +0200 Subject: [PATCH] [svn] improved debugging support. it's not possible to catch errors of templates without loaders too --HG-- branch : trunk --- jinja/environment.py | 19 ++++++++++++++----- jinja/translators/python.py | 3 +-- jinja/utils.py | 23 ++++++++++++----------- tests/runtime/exception.py | 17 ++++++++++++++--- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/jinja/environment.py b/jinja/environment.py index 92ed4ca..abbaa4b 100644 --- a/jinja/environment.py +++ b/jinja/environment.py @@ -14,7 +14,8 @@ from jinja.parser import Parser from jinja.loaders import LoaderWrapper from jinja.datastructure import Undefined, Markup, Context, FakeTranslator from jinja.utils import escape, collect_translations, get_attribute -from jinja.exceptions import FilterNotFound, TestNotFound, SecurityException +from jinja.exceptions import FilterNotFound, TestNotFound, \ + SecurityException, TemplateSyntaxError from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE @@ -87,10 +88,18 @@ class Environment(object): """Load a template from a string.""" from jinja.parser import Parser from jinja.translators.python import PythonTranslator - rv = PythonTranslator.process(self, Parser(self, source).parse()) - # attach the source for debugging - rv._source = source - return rv + try: + rv = PythonTranslator.process(self, Parser(self, source).parse()) + except TemplateSyntaxError, e: + # if errors occour raise a better traceback + from jinja.utils import raise_syntax_error + __traceback_hide__ = True + raise_syntax_error(e, self, source) + else: + # everything went well. attach the source and return it + # attach the source for debugging + rv._source = source + return rv def get_template(self, filename): """Load a template from a filename. Only works diff --git a/jinja/translators/python.py b/jinja/translators/python.py index 4bc4df1..db9f52c 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -389,9 +389,8 @@ class PythonTranslator(Translator): if m is not None: d = m.groupdict() this = (d['filename'] or None, int(d['lineno'])) - # if there is no filename in this debug symbol # if it's the same as the line before we ignore it - if this[0] and this != last: + if this != last: debug_mapping.append((idx - offset,) + this) last = this # for each debug symbol the line number and so the offset diff --git a/jinja/utils.py b/jinja/utils.py index 4e0183e..b430312 100644 --- a/jinja/utils.py +++ b/jinja/utils.py @@ -222,7 +222,7 @@ def buffereater(f): return wrapped -def fake_template_exception(exception, filename, lineno, template, +def fake_template_exception(exception, filename, lineno, source, context_or_env): """ Raise an exception "in a template". Return a traceback @@ -238,11 +238,12 @@ def fake_template_exception(exception, filename, lineno, template, namespace = {} offset = '\n' * (lineno - 1) - code = compile(offset + 'raise __exception_to_raise__', filename, 'exec') + code = compile(offset + 'raise __exception_to_raise__', + filename or '