diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2013-05-19 14:43:18 +0100 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2013-05-19 14:43:18 +0100 |
commit | 7e239b4d165975d72beedcf6c653ce3d0cf18aa4 (patch) | |
tree | 7f094bb6ae584ddb50a123b4a846ee2ea9e4934a | |
parent | 7e245e26788d140d661034047f1f59619436ad4b (diff) | |
download | jinja2-7e239b4d165975d72beedcf6c653ce3d0cf18aa4.tar.gz |
Added unicode mixin for unified string logic
-rw-r--r-- | jinja2/_compat.py | 11 | ||||
-rw-r--r-- | jinja2/environment.py | 11 | ||||
-rw-r--r-- | jinja2/exceptions.py | 20 | ||||
-rw-r--r-- | jinja2/runtime.py | 20 | ||||
-rw-r--r-- | jinja2/testsuite/filters.py | 10 |
5 files changed, 21 insertions, 51 deletions
diff --git a/jinja2/_compat.py b/jinja2/_compat.py index d770f1a..f72e99b 100644 --- a/jinja2/_compat.py +++ b/jinja2/_compat.py @@ -38,6 +38,11 @@ if not PY2: raise value Iterator = object + + class UnicodeMixin(object): + __slots__ = () + def __str__(self): + return self.__unicode__() else: text_type = unicode unichr = unichr @@ -56,7 +61,13 @@ else: 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__() diff --git a/jinja2/environment.py b/jinja2/environment.py index cbac8c1..bf56d0f 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -28,7 +28,7 @@ from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound, \ 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 @@ -1051,7 +1051,7 @@ class Template(object): 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. @@ -1068,13 +1068,6 @@ class TemplateModule(object): 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) diff --git a/jinja2/exceptions.py b/jinja2/exceptions.py index 1bbceee..4d12a47 100644 --- a/jinja2/exceptions.py +++ b/jinja2/exceptions.py @@ -8,7 +8,7 @@ :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): @@ -36,7 +36,7 @@ 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 @@ -51,13 +51,6 @@ class TemplateNotFound(IOError, LookupError, TemplateError): 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 @@ -78,7 +71,7 @@ class TemplatesNotFound(TemplateNotFound): 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): @@ -115,13 +108,6 @@ class TemplateSyntaxError(TemplateError): 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 diff --git a/jinja2/runtime.py b/jinja2/runtime.py index b5e203f..605de6a 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -15,7 +15,7 @@ from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \ 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 @@ -440,7 +440,7 @@ class Macro(object): ) -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`: @@ -501,13 +501,6 @@ class Undefined(object): 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 @@ -547,10 +540,6 @@ class DebugUndefined(Undefined): ) 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 @@ -572,12 +561,9 @@ class StrictUndefined(Undefined): 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. diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index c1f32c3..b432c60 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -12,7 +12,7 @@ import unittest 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() @@ -294,17 +294,11 @@ class FilterTestCase(JinjaTestCase): 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' |