[svn] fixed weird python2.4 debugging problem
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 12 Mar 2007 16:04:27 +0000 (17:04 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 12 Mar 2007 16:04:27 +0000 (17:04 +0100)
--HG--
branch : trunk

jinja/translators/python.py
jinja/utils.py

index 1583a7ee04a0e777410db947c71f73242170577e..a891f021d122fae1ce181e3972b39c05d5e2f2d2 100644 (file)
@@ -15,7 +15,7 @@ from jinja.nodes import get_nodes
 from jinja.parser import Parser
 from jinja.exceptions import TemplateSyntaxError
 from jinja.translators import Translator
-from jinja.utils import translate_exception
+from jinja.utils import translate_exception, capture_generator
 
 
 def _to_tuple(args):
@@ -72,12 +72,14 @@ class Template(object):
             self.generate_func = ns['generate']
         ctx = self.environment.context_class(self.environment, *args, **kwargs)
         try:
-            return u''.join(self.generate_func(ctx))
+            return capture_generator(self.generate_func(ctx))
         except:
             exc_type, exc_value, traceback = sys.exc_info()
-            traceback = translate_exception(self, exc_type,
-                                            exc_value, traceback.tb_next,
-                                            ctx)
+            # translate the exception, We skip two frames. One
+            # frame that is the "capture_generator" frame, and another
+            # one which is the frame of this function
+            traceback = translate_exception(self, exc_type, exc_value,
+                                            traceback.tb_next.tb_next, ctx)
             raise exc_type, exc_value, traceback
 
 
index 5ebc17e909db4aad3aae82c31572af1c1dea6bc1..d122da6c77388d436d651946c23812c340f22895 100644 (file)
@@ -56,13 +56,30 @@ def find_translations(environment, source):
         queue.extend(node.getChildNodes())
 
 
+# python2.4 and lower has a bug regarding joining of broken generators
+if sys.hexversion < (2, 5):
+    def capture_generator(gen):
+        """
+        Concatenate the generator output.
+        """
+        return u''.join(tuple(gen))
+
+# this should be faster and used in python2.5 and higher
+else:
+    def capture_generator(gen):
+        """
+        Concatenate the generator output
+        """
+        return u''.join(gen)
+
+
 def buffereater(f):
     """
     Used by the python translator to capture output of substreams.
     (macros, filter sections etc)
     """
     def wrapped(*args, **kwargs):
-        return u''.join(f(*args, **kwargs))
+        return capture_generator(f(*args, **kwargs))
     return wrapped
 
 
@@ -71,7 +88,7 @@ def raise_template_exception(template, exception, filename, lineno, context):
     Raise an exception "in a template". Return a traceback
     object.
     """
-    offset = '\n'.join([''] * lineno)
+    offset = '\n' * (lineno - 1)
     code = compile(offset + 'raise __exception_to_raise__', filename, 'exec')
     namespace = context.to_dict()
     globals = {
@@ -97,7 +114,6 @@ def translate_exception(template, exc_type, exc_value, traceback, context):
     # looks like we loaded the template from string. we cannot
     # do anything here.
     if startpos > len(sourcelines):
-        print startpos, len(sourcelines)
         return traceback
 
     while startpos > 0: