From: Armin Ronacher Date: Mon, 12 Mar 2007 06:22:17 +0000 (+0100) Subject: [svn] implemented jinja debugging system X-Git-Tag: 2.0rc1~450 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=7977e5ce8d84b0c66285c424bd2ccdc9ab4c46af;p=jinja2.git [svn] implemented jinja debugging system --HG-- branch : trunk --- diff --git a/jinja/datastructure.py b/jinja/datastructure.py index ecbbf3d..9ba9e9a 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -234,8 +234,9 @@ class LoopContext(object): def __call__(self, seq): if self.loop_function is not None: return self.loop_function(seq) - raise TemplateRuntimeError('Loops are just callable if defined with ' - 'the "recursive" modifier.') + raise TemplateRuntimeError('In order to make loops callable you have ' + 'to define them with the "recursive" ' + 'modifier.') class CycleContext(object): diff --git a/jinja/lexer.py b/jinja/lexer.py index 970a1c6..13ca67a 100644 --- a/jinja/lexer.py +++ b/jinja/lexer.py @@ -115,7 +115,8 @@ class Lexer(object): to use this function since it can be marginally faster. """ source = type(source)('\n').join(source.splitlines()) - pos = lineno = 0 + pos = 0 + lineno = 1 stack = ['root'] statetokens = self.rules['root'] source_length = len(source) diff --git a/jinja/nodes.py b/jinja/nodes.py index da8a740..a4fa2a2 100644 --- a/jinja/nodes.py +++ b/jinja/nodes.py @@ -92,7 +92,7 @@ class Template(NodeList): def __init__(self, filename, body, extends): if body.__class__ is not NodeList: body = (body,) - NodeList.__init__(self, 0, body) + NodeList.__init__(self, 1, body) self.extends = extends set_filename(filename, self) diff --git a/jinja/translators/python.py b/jinja/translators/python.py index 55332e1..1583a7e 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -8,12 +8,14 @@ :copyright: 2007 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import sys from compiler import ast from jinja import nodes 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 def _to_tuple(args): @@ -31,39 +33,52 @@ class Template(object): Represents a finished template. """ - def __init__(self, environment, code): + def __init__(self, environment, code, translated_source=None): self.environment = environment self.code = code + self.translated_source = translated_source self.generate_func = None + def source(self): + """The original sourcecode for this template.""" + return self.environment.loader.get_source(self.code.co_filename) + source = property(source, doc=source.__doc__) + def dump(self, stream=None): """Dump the template into python bytecode.""" if stream is not None: from marshal import dump - dump(self.code, stream) + dump((self.code, self.translated_source), stream) else: from marshal import dumps - return dumps(self.code) + return dumps((self.code, self.translated_source)) def load(environment, data): """Load the template from python bytecode.""" if isinstance(data, basestring): from marshal import loads - code = loads(data) + code, src = loads(data) else: from marshal import load - code = load(data) - return Template(environment, code) + code, src = load(data) + return Template(environment, code, src) load = staticmethod(load) def render(self, *args, **kwargs): """Render a template.""" if self.generate_func is None: - ns = {} + ns = {'environment': self.environment} exec self.code in ns self.generate_func = ns['generate'] ctx = self.environment.context_class(self.environment, *args, **kwargs) - return u''.join(self.generate_func(ctx)) + try: + return u''.join(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) + raise exc_type, exc_value, traceback class PythonTranslator(Translator): @@ -142,8 +157,10 @@ class PythonTranslator(Translator): def process(environment, node): translator = PythonTranslator(environment, node) filename = node.filename or '