Fixed a bug with template syntax errors not handled properly.
authorArmin Ronacher <armin.ronacher@active-4.com>
Thu, 16 Apr 2009 21:15:22 +0000 (23:15 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Thu, 16 Apr 2009 21:15:22 +0000 (23:15 +0200)
--HG--
branch : trunk

jinja2/debug.py
jinja2/environment.py

index ce794e243b38e8ae558957c9e235eae2a7f48ade..beceecb03367a275426b479ecfe9a1ec7898e7d4 100644 (file)
@@ -86,7 +86,10 @@ def make_traceback(exc_info, source_hint=None):
     exc_type, exc_value, tb = exc_info
     if isinstance(exc_value, TemplateSyntaxError):
         exc_info = translate_syntax_error(exc_value, source_hint)
-    return translate_exception(exc_info)
+        initial_skip = 0
+    else:
+        initial_skip = 1
+    return translate_exception(exc_info, initial_skip)
 
 
 def translate_syntax_error(error, source=None):
@@ -100,13 +103,19 @@ def translate_syntax_error(error, source=None):
     return fake_exc_info(exc_info, filename, error.lineno)
 
 
-def translate_exception(exc_info):
+def translate_exception(exc_info, initial_skip=0):
     """If passed an exc_info it will automatically rewrite the exceptions
     all the way down to the correct line numbers and frames.
     """
-    initial_tb = tb = exc_info[2].tb_next
+    tb = exc_info[2]
     frames = []
 
+    # skip some internal frames if wanted
+    for x in xrange(initial_skip):
+        if tb is not None:
+            tb = tb.tb_next
+    initial_tb = tb
+
     while tb is not None:
         # skip frames decorated with @internalcode.  These are internal
         # calls we can't avoid and that are useless in template debugging
@@ -133,6 +142,7 @@ def translate_exception(exc_info):
     # reraise it unchanged.
     # XXX: can we backup here?  when could this happen?
     if not frames:
+        print "bummer"
         raise exc_info[0], exc_info[1], exc_info[2]
 
     traceback = ProcessedTraceback(exc_info[0], exc_info[1], frames)
index c2fee169844982ac8bd5b662c049935fb1940f7c..09705c13e19fb8d5d370d35ae0fb7f3542e74cf9 100644 (file)
@@ -368,7 +368,7 @@ class Environment(object):
             filename = filename.encode('utf-8')
         try:
             return Parser(self, source, name, filename).parse()
-        except TemplateSyntaxError, e:
+        except TemplateSyntaxError:
             self.handle_exception(sys.exc_info(), source_hint=source)
 
     def lex(self, source, name=None, filename=None):
@@ -384,7 +384,7 @@ class Environment(object):
         source = unicode(source)
         try:
             return self.lexer.tokeniter(source, name, filename)
-        except TemplateSyntaxError, e:
+        except TemplateSyntaxError:
             self.handle_exception(sys.exc_info(), source_hint=source)
 
     def preprocess(self, source, name=None, filename=None):
@@ -469,9 +469,8 @@ class Environment(object):
                 raise TemplateSyntaxError('chunk after expression',
                                           parser.stream.current.lineno,
                                           None, None)
-        except TemplateSyntaxError, e:
-            e.source = source
-            raise e
+        except TemplateSyntaxError:
+            self.handle_exception(sys.exc_info(), source_hint=source)
         body = [nodes.Assign(nodes.Name('result', 'store'), expr, lineno=1)]
         template = self.from_string(nodes.Template(body, lineno=1))
         return TemplateExpression(template, undefined_to_none)