Skip to content
Myr

improved debugging for syntax errors

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 19cf9c2 · Browse files · 4 files+40 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+40 added−8 removed

  • other:+40 added−8 removed

4 files+40 added−8 removed

modified:jinja2/datastructure.py+11 added-2 removed
27 unmodified lines
28 if self.type in keywords:29 return self.type30 elif self.type in reverse_operators:31 return reverse_operators[self.type]32 return '%s:%s' % (self.type, self.value)32 elif self.type is 'name':33 return self.value34 return self.type3536 def test(self, expr):37 """Test a token against a token expression. This can either be a38 token type or 'token_type:token_value'. This can only test against
109 unmodified lines
148149 def expect(self, expr):150 """Expect a given token type and return it"""151 if not self.current.test(expr):152 if ':' in expr:153 expr = expr.split(':')[1]154 if self.current.type is 'eof':155 raise TemplateSyntaxError('unexpected end of template, '156 'expected %r.' % expr,157 self.current.lineno,158 self.filename)159 raise TemplateSyntaxError("expected token %r, got %r" %151 (expr, self.current),160 (expr, str(self.current)),161 self.current.lineno,162 self.filename)163 try:164 return self.current
2 unmodified lines
modified:jinja2/debug.py+18 added
31 unmodified lines
3233 return exc_info[:2] + (result_tb or initial_tb,)343536def translate_syntax_error(error):37 """When passed a syntax error it will generate a new traceback with38 more debugging information.39 """40 filename = error.filename41 if filename is None:42 filename = '<template>'43 elif isinstance(filename, unicode):44 filename = filename.encode('utf-8')45 code = compile('\n' * (error.lineno - 1) + 'raise __jinja_exception__',46 filename, 'exec')47 try:48 exec code in {'__jinja_exception__': error}49 except:50 exc_info = sys.exc_info()51 return exc_info[:2] + (exc_info[2].tb_next,)525354def fake_exc_info(exc_info, filename, lineno, tb_back=None):55 """Helper for `translate_exception`."""56 exc_type, exc_value, tb = exc_info57
132 unmodified lines
modified:jinja2/environment.py+9 added-4 removed
13 unmodified lines
14from jinja2.parser import Parser15from jinja2.optimizer import optimize16from jinja2.compiler import generate17from jinja2.runtime import Undefined, Context, concat18from jinja2.debug import translate_exception18from jinja2.debug import translate_exception, translate_syntax_error19from jinja2.exceptions import TemplateSyntaxError20from jinja2.utils import import_string, LRUCache, Markup, missing212223# for direct template usage we have up to ten living environments
247 unmodified lines
271 return obj[argument]272 except (TypeError, LookupError):273 return self.undefined(obj=obj, name=argument)274274 def parse(self, source, name=None):275 def parse(self, source, filename=None):276 """Parse the sourcecode and return the abstract syntax tree. This277 tree of nodes is used by the compiler to convert the template into278 executable source- or bytecode. This is useful for debugging or to279 extract information from templates.280 """281 try:280 return Parser(self, source, name).parse()282 return Parser(self, source, filename).parse()283 except TemplateSyntaxError, e:284 exc_type, exc_value, tb = translate_syntax_error(e)285 raise exc_type, exc_value, tb286287 def lex(self, source, name=None):288 """Lex the given sourcecode and return a generator that yields289 tokens as tuples in the form ``(lineno, token_type, value)``.
17 unmodified lines
307 code equivalent to the bytecode returned otherwise. This method is308 mainly used internally.309 """310 if isinstance(source, basestring):306 source = self.parse(source, name)311 source = self.parse(source, filename)312 if self.optimized:313 node = optimize(source, self, globals or {})314 source = generate(node, self, name, filename)315 if raw:
342 unmodified lines
modified:jinja2/exceptions.py+2 added-2 removed
27 unmodified lines
2829class TemplateSyntaxError(TemplateError):30 """Raised to tell the user that there is a problem with the template."""3132 def __init__(self, message, lineno, name):32 def __init__(self, message, lineno, filename):33 TemplateError.__init__(self, '%s (line %s)' % (message, lineno))34 self.message = message35 self.lineno = lineno36 self.name = name36 self.filename = filename373839class TemplateAssertionError(TemplateSyntaxError):40 """Like a template syntax error, but covers cases where something in the
13 unmodified lines