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):
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
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
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 = {
# 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: