raise value
Iterator = object
+
+ class UnicodeMixin(object):
+ __slots__ = ()
+ def __str__(self):
+ return self.__unicode__()
else:
text_type = unicode
unichr = unichr
exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
+ class UnicodeMixin(object):
+ __slots__ = ()
+ def __str__(self):
+ return self.__unicode__().encode('utf-8')
+
class Iterator(object):
+ __slots__ = ()
def next(self):
return self.__next__()
from jinja2.utils import import_string, LRUCache, Markup, missing, \
concat, consume, internalcode, _encode_filename
from jinja2._compat import imap, ifilter, string_types, iteritems, \
- text_type, reraise, PY2, Iterator, next
+ text_type, reraise, Iterator, next, UnicodeMixin
from functools import reduce
return '<%s %s>' % (self.__class__.__name__, name)
-class TemplateModule(object):
+class TemplateModule(UnicodeMixin):
"""Represents an imported template. All the exported names of the
template are available as attributes on this object. Additionally
converting it into an unicode- or bytestrings renders the contents.
def __unicode__(self):
return concat(self._body_stream)
- if PY2:
- def __str__(self):
- return self.__unicode__().encode('utf-8')
- else:
- __str__ = __unicode__
- del __unicode__
-
def __repr__(self):
if self.__name__ is None:
name = 'memory:%x' % id(self)
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
-from jinja2._compat import imap, text_type, PY2
+from jinja2._compat import imap, text_type, PY2, UnicodeMixin
class TemplateError(Exception):
return message
-class TemplateNotFound(IOError, LookupError, TemplateError):
+class TemplateNotFound(IOError, LookupError, TemplateError, UnicodeMixin):
"""Raised if a template does not exist."""
# looks weird, but removes the warning descriptor that just
self.name = name
self.templates = [name]
- def __str__(self):
- return self.message.encode('utf-8')
-
- # unicode goes after __str__ because we configured 2to3 to rename
- # __unicode__ to __str__. because the 2to3 tree is not designed to
- # remove nodes from it, we leave the above __str__ around and let
- # it override at runtime.
def __unicode__(self):
return self.message
self.templates = list(names)
-class TemplateSyntaxError(TemplateError):
+class TemplateSyntaxError(UnicodeMixin, TemplateError):
"""Raised to tell the user that there is a problem with the template."""
def __init__(self, message, lineno, name=None, filename=None):
return u'\n'.join(lines)
- if PY2:
- def __str__(self):
- return self.__unicode__().encode('utf-8')
- else:
- __str__ = __unicode__
- del __unicode__
-
class TemplateAssertionError(TemplateSyntaxError):
"""Like a template syntax error, but covers cases where something in the
from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \
TemplateNotFound
from jinja2._compat import next, imap, text_type, iteritems, Iterator, \
- string_types, PY2
+ string_types, PY2, UnicodeMixin
# these variables are exported to the template runtime
)
-class Undefined(object):
+class Undefined(UnicodeMixin):
"""The default undefined type. This undefined type can be printed and
iterated over, but every other access will raise an :exc:`UndefinedError`:
def __unicode__(self):
return u''
- if PY2:
- def __str__(self):
- return self.__unicode__().encode('utf-8')
- else:
- __str__ = __unicode__
- del __unicode__
-
def __len__(self):
return 0
)
return u'{{ undefined value printed: %s }}' % self._undefined_hint
- if not PY2:
- __str__ = __unicode__
- del __unicode__
-
class StrictUndefined(Undefined):
"""An undefined that barks on print and iteration as well as boolean
UndefinedError: 'foo' is undefined
"""
__slots__ = ()
- __iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \
+ __iter__ = __unicode__ = __len__ = __nonzero__ = __eq__ = \
__ne__ = __bool__ = Undefined._fail_with_undefined_error
- if PY2:
- __unicode__ = Undefined._fail_with_undefined_error
-
# remove remaining slots attributes, after the metaclass did the magic they
# are unneeded and irritating as they contain wrong data for the subclasses.
from jinja2.testsuite import JinjaTestCase
from jinja2 import Markup, Environment
-from jinja2._compat import text_type, PY2
+from jinja2._compat import text_type, UnicodeMixin
env = Environment()
assert tmpl.render() == "['Bar', 'blah', 'foo']"
def test_sort4(self):
- class Magic(object):
+ class Magic(UnicodeMixin):
def __init__(self, value):
self.value = value
def __unicode__(self):
return text_type(self.value)
- if PY2:
- def __str__(self):
- return self.__unicode__().encode('utf-8')
- else:
- __str__ = __unicode__
- del __unicode__
tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')
assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'