From: Armin Ronacher Date: Mon, 12 Mar 2007 16:04:27 +0000 (+0100) Subject: [svn] fixed weird python2.4 debugging problem X-Git-Tag: 2.0rc1~449 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fc214ccf326f0361bdd304cbaf178afce02c126b;p=jinja2.git [svn] fixed weird python2.4 debugging problem --HG-- branch : trunk --- diff --git a/jinja/translators/python.py b/jinja/translators/python.py index 1583a7e..a891f02 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -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 diff --git a/jinja/utils.py b/jinja/utils.py index 5ebc17e..d122da6 100644 --- a/jinja/utils.py +++ b/jinja/utils.py @@ -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: