exceptions.py
This file
Path
jinja2/exceptions.py
143 lines · 4.4 KiB · Python
Last changed by
Down to 7 failures for Python 3. We're onto something.Armin Ronacherbefore adoption0d242be
History of this fileTake it
The walk stopped after 200 commits: lines older than 8f45555 point at it.
Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 jinja2.exceptions4 ~~~~~~~~~~~~~~~~~56 Jinja exceptions.7It's a new yearArmin Ronacherbefore adoption55494e4
line 8why, one step back8 :copyright: (c) 2010 by the Jinja Team.Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 9–15why, one step back9 :license: BSD, see LICENSE for more details.10"""111213class TemplateError(Exception):14 """Baseclass for all template errors."""15Added a (ugly) hack to the exceptions so that they don't warn under 2.6Armin Ronacherbefore adoption1d02108
lines 16–19why, one step back16 def __init__(self, message=None):17 if message is not None:18 message = unicode(message).encode('utf-8')19 Exception.__init__(self, message)Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
line 20why, one step back20Added a (ugly) hack to the exceptions so that they don't warn under 2.6Armin Ronacherbefore adoption1d02108
lines 21–28why, one step back21 @property22 def message(self):23 if self.args:24 message = self.args[0]25 if message is not None:26 return message.decode('utf-8', 'replace')2728Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 29–31why, one step back29class TemplateNotFound(IOError, LookupError, TemplateError):30 """Raised if a template does not exist."""31include tags are now able to select between multiple templatesArmin Ronacherbefore adoption31bbd9e
lines 32–40why, one step back32 # looks weird, but removes the warning descriptor that just33 # bogusly warns us about message being deprecated34 message = None3536 def __init__(self, name, message=None):37 IOError.__init__(self)38 if message is None:39 message = name40 self.message = messageNearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
line 41why, one step back41 self.name = nameinclude tags are now able to select between multiple templatesArmin Ronacherbefore adoption31bbd9e
lines 42–46why, one step back42 self.templates = [name]4344 def __str__(self):45 return self.message.encode('utf-8')46Added comment to why __unicode__ goes after __str__.Armin Ronacherbefore adoptionacbd408
lines 47–50why, one step back47 # unicode goes after __str__ because we configured 2to3 to rename48 # __unicode__ to __str__. because the 2to3 tree is not designed to49 # remove nodes from it, we leave the above __str__ around and let50 # it override at runtime.Approaching python3 compatibilityArmin Ronacherbefore adoption790b8a8
lines 51–52why, one step back51 def __unicode__(self):52 return self.messageinclude tags are now able to select between multiple templatesArmin Ronacherbefore adoption31bbd9e
line 53why, one step back53Approaching python3 compatibilityArmin Ronacherbefore adoption790b8a8
line 54why, one step back54include tags are now able to select between multiple templatesArmin Ronacherbefore adoption31bbd9e
lines 55–65why, one step back55class TemplatesNotFound(TemplateNotFound):56 """Like :class:`TemplateNotFound` but raised if multiple templates57 are selected. This is a subclass of :class:`TemplateNotFound`58 exception, so just catching the base exception will catch both.5960 .. versionadded:: 2.261 """6263 def __init__(self, names=(), message=None):64 if message is None:65 message = u'non of the templates given were found: ' + \Fixed the choice include tests.Armin Ronacherbefore adoption7e40df0
line 66why, one step back66 u', '.join(map(unicode, names))include tags are now able to select between multiple templatesArmin Ronacherbefore adoption31bbd9e
lines 67–68why, one step back67 TemplateNotFound.__init__(self, names and names[-1] or None, message)68 self.templates = list(names)Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 69–74why, one step back697071class TemplateSyntaxError(TemplateError):72 """Raised to tell the user that there is a problem with the template."""7374 def __init__(self, message, lineno, name=None, filename=None):Added a (ugly) hack to the exceptions so that they don't warn under 2.6Armin Ronacherbefore adoption1d02108
line 75why, one step back75 TemplateError.__init__(self, message)Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 76–78why, one step back76 self.lineno = lineno77 self.name = name78 self.filename = filenameVarious cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055
lines 79–80why, one step back79 self.source = None80Improved Jinja's debugging support by introducing "@internalcode" which marks code objects that are skipped on tracebacks. Also template errors are now translated as well to help the pylons debugger.Armin Ronacherbefore adoptiond416a97
lines 81–84why, one step back81 # this is set to True if the debug.translate_syntax_error82 # function translated the syntax error into a new traceback83 self.translated = False84Approaching python3 compatibilityArmin Ronacherbefore adoption790b8a8
lines 85–87why, one step back85 def __str__(self):86 return unicode(self).encode('utf-8')87Added comment to why __unicode__ goes after __str__.Armin Ronacherbefore adoptionacbd408
lines 88–91why, one step back88 # unicode goes after __str__ because we configured 2to3 to rename89 # __unicode__ to __str__. because the 2to3 tree is not designed to90 # remove nodes from it, we leave the above __str__ around and let91 # it override at runtime.Various cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055
line 92why, one step back92 def __unicode__(self):Improved Jinja's debugging support by introducing "@internalcode" which marks code objects that are skipped on tracebacks. Also template errors are now translated as well to help the pylons debugger.Armin Ronacherbefore adoptiond416a97
lines 93–94why, one step back93 # for translated errors we only return the message94 if self.translated:Down to 7 failures for Python 3. We're onto something.Armin Ronacherbefore adoption0d242be
line 95why, one step back95 return self.messageImproved Jinja's debugging support by introducing "@internalcode" which marks code objects that are skipped on tracebacks. Also template errors are now translated as well to help the pylons debugger.Armin Ronacherbefore adoptiond416a97
lines 96–97why, one step back9697 # otherwise attach some stuffVarious cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055
lines 98–113why, one step back98 location = 'line %d' % self.lineno99 name = self.filename or self.name100 if name:101 location = 'File "%s", %s' % (name, location)102 lines = [self.message, ' ' + location]103104 # if the source is set, add the line to the output105 if self.source is not None:106 try:107 line = self.source.splitlines()[self.lineno - 1]108 except IndexError:109 line = None110 if line:111 lines.append(' ' + line.strip())112113 return u'\n'.join(lines)Nearly complete core django template code coverage.Armin Ronacherbefore adoption8f45555
lines 114–143why, one step back114115116class TemplateAssertionError(TemplateSyntaxError):117 """Like a template syntax error, but covers cases where something in the118 template caused an error at compile time that wasn't necessarily caused119 by a syntax error. However it's a direct subclass of120 :exc:`TemplateSyntaxError` and has the same attributes.121 """122123124class TemplateRuntimeError(TemplateError):125 """A generic runtime error in the template engine. Under some situations126 Jinja may raise this exception.127 """128129130class UndefinedError(TemplateRuntimeError):131 """Raised if a template tries to operate on :class:`Undefined`."""132133134class SecurityError(TemplateRuntimeError):135 """Raised if a template tries to do something insecure if the136 sandbox is enabled.137 """138139140class FilterArgumentError(TemplateRuntimeError):141 """This error is raised if a filter was called with inappropriate142 arguments143 """