From: Armin Ronacher Date: Thu, 1 May 2008 11:14:30 +0000 (+0200) Subject: improved debugging for syntax errors X-Git-Tag: 2.0rc1~115 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=aaf010ddf6e5896b2937ffd232712d61c654d901;p=jinja2.git improved debugging for syntax errors --HG-- branch : trunk --- diff --git a/jinja2/datastructure.py b/jinja2/datastructure.py index 9dab02b..51ffc93 100644 --- a/jinja2/datastructure.py +++ b/jinja2/datastructure.py @@ -29,7 +29,9 @@ class Token(tuple): return self.type elif self.type in reverse_operators: return reverse_operators[self.type] - return '%s:%s' % (self.type, self.value) + elif self.type is 'name': + return self.value + return self.type def test(self, expr): """Test a token against a token expression. This can either be a @@ -147,8 +149,15 @@ class TokenStream(object): def expect(self, expr): """Expect a given token type and return it""" if not self.current.test(expr): + if ':' in expr: + expr = expr.split(':')[1] + if self.current.type is 'eof': + raise TemplateSyntaxError('unexpected end of template, ' + 'expected %r.' % expr, + self.current.lineno, + self.filename) raise TemplateSyntaxError("expected token %r, got %r" % - (expr, self.current), + (expr, str(self.current)), self.current.lineno, self.filename) try: diff --git a/jinja2/debug.py b/jinja2/debug.py index 622f2b3..a9b4439 100644 --- a/jinja2/debug.py +++ b/jinja2/debug.py @@ -33,6 +33,24 @@ def translate_exception(exc_info): return exc_info[:2] + (result_tb or initial_tb,) +def translate_syntax_error(error): + """When passed a syntax error it will generate a new traceback with + more debugging information. + """ + filename = error.filename + if filename is None: + filename = '