Skip to content
Myr

[svn] improved debugging support. it's not possible to catch errors of templates without loaders too

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 7e07670 · Browse files · 4 files+41 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+41 added−21 removed

  • tests:+14 added−3 removed
  • other:+27 added−18 removed

4 files+41 added−21 removed

modified:jinja/translators/python.py+1 added-2 removed
387 unmodified lines
388 m = _debug_re.search(line)389 if m is not None:390 d = m.groupdict()391 this = (d['filename'] or None, int(d['lineno']))392 # if there is no filename in this debug symbol392 # if it's the same as the line before we ignore it394 if this[0] and this != last:393 if this != last:394 debug_mapping.append((idx - offset,) + this)395 last = this396 # for each debug symbol the line number and so the offset397 # changes by one.
533 unmodified lines
modified:jinja/environment.py+14 added-5 removed
12 unmodified lines
13from jinja.parser import Parser14from jinja.loaders import LoaderWrapper15from jinja.datastructure import Undefined, Markup, Context, FakeTranslator16from jinja.utils import escape, collect_translations, get_attribute17from jinja.exceptions import FilterNotFound, TestNotFound, SecurityException17from jinja.exceptions import FilterNotFound, TestNotFound, \18 SecurityException, TemplateSyntaxError19from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE202122class Environment(object):
64 unmodified lines
87 def from_string(self, source):88 """Load a template from a string."""89 from jinja.parser import Parser90 from jinja.translators.python import PythonTranslator91 try:92 rv = PythonTranslator.process(self, Parser(self, source).parse())93 except TemplateSyntaxError, e:94 # if errors occour raise a better traceback95 from jinja.utils import raise_syntax_error96 __traceback_hide__ = True97 raise_syntax_error(e, self, source)98 else:90 rv = PythonTranslator.process(self, Parser(self, source).parse())91 # attach the source for debugging92 rv._source = source93 return rv99 # everything went well. attach the source and return it100 # attach the source for debugging101 rv._source = source102 return rv103104 def get_template(self, filename):105 """Load a template from a filename. Only works106 if a proper loader is set."""
118 unmodified lines
modified:jinja/utils.py+12 added-11 removed
220 unmodified lines
221 return TemplateData(capture_generator(f(*args, **kwargs)))222 return wrapped223224225def fake_template_exception(exception, filename, lineno, template,225def fake_template_exception(exception, filename, lineno, source,226 context_or_env):227 """228 Raise an exception "in a template". Return a traceback229 object. This is used for runtime debugging, not compile time.
7 unmodified lines
237 env = context_or_env238 namespace = {}239240 offset = '\n' * (lineno - 1)241 code = compile(offset + 'raise __exception_to_raise__', filename, 'exec')241 code = compile(offset + 'raise __exception_to_raise__',242 filename or '<template>', 'exec')243 globals = {244 '__name__': filename,245 '__file__': filename,245 '__loader__': TracebackLoader(env, template, filename),246 '__loader__': TracebackLoader(env, source, filename),247 '__exception_to_raise__': exception248 }249 try:250 exec code in globals, namespace
13 unmodified lines
264 # no debug symbol found. give up265 return traceback266267 return fake_template_exception(exc_value, tmpl_filename, tmpl_line,267 template, context)[2]268 template._source, context)[2]269270270def raise_syntax_error(exception, env):271def raise_syntax_error(exception, env, source=None):272 """273 This method raises an exception that includes more debugging274 informations so that debugging works better. Unlike275 `translate_exception` this method raises the exception with276 the traceback.277 """278 exc_info = fake_template_exception(exception, exception.filename,278 exception.lineno, None, env)279 exception.lineno, source, env)280 raise exc_info[0], exc_info[1], exc_info[2]281282283def collect_translations(ast):
31 unmodified lines
315 """316 Fake importer that just returns the source of a template.317 """318318 def __init__(self, environment, template, filename):319 def __init__(self, environment, source, filename):320 self.loader = environment.loader320 self.template = template321 self.source = source322 self.filename = filename323324 def get_source(self, impname):324 if self.loader is not None:325 if self.source is not None:326 return self.source327 elif self.loader is not None:328 try:329 return self.loader.get_source(self.filename)330 except TemplateNotFound:331 pass329 if self.template is not None:330 return self.template._source or ''332 return ''333334335class CacheDict(object):
125 unmodified lines
modified:tests/runtime/exception.py+14 added-3 removed
30 unmodified lines
31 <li><a href="syntax_error">syntax error</a></li>32 <li><a href="runtime_error">runtime error</a></li>33 <li><a href="nested_syntax_error">nested syntax error</a></li>34 <li><a href="nested_runtime_error">nested runtime error</a></li>35 <li><a href="syntax_from_string">a syntax error from string</a></li>36 <li><a href="runtime_from_string">runtime error from a string</a></li>37 </ul>38 </body>39</html>40''',
19 unmodified lines
60This is an included template61{% raw %}just some foo'''62}))6364FAILING_STRING_TEMPLATE = '{{ 1 / 0 }}'65BROKEN_STRING_TEMPLATE = '{% if foo %}...{% endfor %}'666768def test(environ, start_response):69 path = environ.get('PATH_INFO' or '/')70 try:65 tmpl = e.get_template(environ.get('PATH_INFO') or '/')71 tmpl = e.get_template(path)72 except TemplateNotFound:73 if path == '/syntax_from_string':74 tmpl = e.from_string(BROKEN_STRING_TEMPLATE)75 elif path == '/runtime_from_string':76 tmpl = e.from_string(FAILING_STRING_TEMPLATE)77 else:67 start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])68 return ['NOT FOUND']78 start_response('404 NOT FOUND', [('Content-Type', 'text/plain')])79 return ['NOT FOUND']80 start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])81 return [tmpl.render().encode('utf-8')]8283if __name__ == '__main__':
3 unmodified lines