Added unicode mixin for unified string logic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 7e245e2 · Browse files · 5 files·+21 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+21 added−51 removed
- other:+21 added−51 removed
Provenance
- Commit
7e239b4- Tree
7f094bb- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7e245e2
5 files changed+21 added−51 removed
modified:jinja2/testsuite/filters.py+2 added-8 removed
10 unmodified lines11 import unittest12 from jinja2.testsuite import JinjaTestCase13 14 from jinja2 import Markup, Environment15-from jinja2._compat import text_type, PY215+from jinja2._compat import text_type, UnicodeMixin16 17 env = Environment()18 19 273 unmodified lines293 tmpl = env.from_string('''{{ ['foo', 'Bar', 'blah']|sort }}''')294 assert tmpl.render() == "['Bar', 'blah', 'foo']"295 296 def test_sort4(self):297- class Magic(object):297+ class Magic(UnicodeMixin):298 def __init__(self, value):299 self.value = value300 def __unicode__(self):301 return text_type(self.value)302- if PY2:303- def __str__(self):304- return self.__unicode__().encode('utf-8')305- else:306- __str__ = __unicode__307- del __unicode__302 tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')303 assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'304 305 def test_groupby(self):195 unmodified linesmodified:jinja2/_compat.py+11 added
36 unmodified lines37 raise value.with_traceback(tb)38 raise value39 40 Iterator = object41+42+ class UnicodeMixin(object):43+ __slots__ = ()44+ def __str__(self):45+ return self.__unicode__()46 else:47 text_type = unicode48 unichr = unichr49 string_types = (str, unicode)10 unmodified lines60 NativeStringIO = BytesIO61 62 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')63 64+ class UnicodeMixin(object):65+ __slots__ = ()66+ def __str__(self):67+ return self.__unicode__().encode('utf-8')68+69 class Iterator(object):70+ __slots__ = ()71 def next(self):72 return self.__next__()73 74 try:49 unmodified linesmodified:jinja2/environment.py+2 added-9 removed
26 unmodified lines27 TemplatesNotFound, TemplateRuntimeError28 from jinja2.utils import import_string, LRUCache, Markup, missing, \29 concat, consume, internalcode, _encode_filename30 from jinja2._compat import imap, ifilter, string_types, iteritems, \31- text_type, reraise, PY2, Iterator, next31+ text_type, reraise, Iterator, next, UnicodeMixin32 from functools import reduce33 34 35 # for direct template usage we have up to ten living environments1014 unmodified lines1050 name = repr(self.name)1051 return '<%s %s>' % (self.__class__.__name__, name)1052 1053 1054-class TemplateModule(object):1054+class TemplateModule(UnicodeMixin):1055 """Represents an imported template. All the exported names of the1056 template are available as attributes on this object. Additionally1057 converting it into an unicode- or bytestrings renders the contents.1058 """8 unmodified lines1067 1068 def __unicode__(self):1069 return concat(self._body_stream)1070 1071- if PY2:1072- def __str__(self):1073- return self.__unicode__().encode('utf-8')1074- else:1075- __str__ = __unicode__1076- del __unicode__1077-1071 def __repr__(self):1072 if self.__name__ is None:1073 name = 'memory:%x' % id(self)1074 else:107 unmodified linesmodified:jinja2/exceptions.py+3 added-17 removed
6 unmodified lines7 8 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10 """11-from jinja2._compat import imap, text_type, PY211+from jinja2._compat import imap, text_type, PY2, UnicodeMixin12 13 14 class TemplateError(Exception):15 """Baseclass for all template errors."""19 unmodified lines35 if message is not None:36 return message37 38 39-class TemplateNotFound(IOError, LookupError, TemplateError):39+class TemplateNotFound(IOError, LookupError, TemplateError, UnicodeMixin):40 """Raised if a template does not exist."""41 42 # looks weird, but removes the warning descriptor that just43 # bogusly warns us about message being deprecated6 unmodified lines50 self.message = message51 self.name = name52 self.templates = [name]53 54- def __str__(self):55- return self.message.encode('utf-8')56-57- # unicode goes after __str__ because we configured 2to3 to rename58- # __unicode__ to __str__. because the 2to3 tree is not designed to59- # remove nodes from it, we leave the above __str__ around and let60- # it override at runtime.54 def __unicode__(self):55 return self.message56 57 12 unmodified lines70 TemplateNotFound.__init__(self, names and names[-1] or None, message)71 self.templates = list(names)72 73 81-class TemplateSyntaxError(TemplateError):74+class TemplateSyntaxError(UnicodeMixin, TemplateError):75 """Raised to tell the user that there is a problem with the template."""76 77 def __init__(self, message, lineno, name=None, filename=None):78 TemplateError.__init__(self, message)28 unmodified lines107 lines.append(' ' + line.strip())108 109 return u'\n'.join(lines)110 118- if PY2:119- def __str__(self):120- return self.__unicode__().encode('utf-8')121- else:122- __str__ = __unicode__123- del __unicode__111 125-112 class TemplateAssertionError(TemplateSyntaxError):113 """Like a template syntax error, but covers cases where something in the114 template caused an error at compile time that wasn't necessarily caused115 by a syntax error. However it's a direct subclass of24 unmodified linesmodified:jinja2/runtime.py+3 added-17 removed
13 unmodified lines14 internalcode, object_type_repr15 from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \16 TemplateNotFound17 from jinja2._compat import next, imap, text_type, iteritems, Iterator, \18- string_types, PY218+ string_types, PY2, UnicodeMixin19 20 21 # these variables are exported to the template runtime22 __all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',416 unmodified lines439 self.name is None and 'anonymous' or repr(self.name)440 )441 442 443-class Undefined(object):443+class Undefined(UnicodeMixin):444 """The default undefined type. This undefined type can be printed and445 iterated over, but every other access will raise an :exc:`UndefinedError`:446 447 >>> foo = Undefined(name='foo')52 unmodified lines500 501 def __unicode__(self):502 return u''503 504- if PY2:505- def __str__(self):506- return self.__unicode__().encode('utf-8')507- else:508- __str__ = __unicode__509- del __unicode__510-504 def __len__(self):505 return 0506 507 def __iter__(self):31 unmodified lines539 self._undefined_name540 )541 return u'{{ undefined value printed: %s }}' % self._undefined_hint542 550- if not PY2:551- __str__ = __unicode__552- del __unicode__543 554-544 class StrictUndefined(Undefined):545 """An undefined that barks on print and iteration as well as boolean546 tests and all kinds of comparisons. In other words: you can do nothing547 with it except checking if it's defined using the `defined` test.12 unmodified lines560 ...561 UndefinedError: 'foo' is undefined562 """563 __slots__ = ()575- __iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \564+ __iter__ = __unicode__ = __len__ = __nonzero__ = __eq__ = \565 __ne__ = __bool__ = Undefined._fail_with_undefined_error566 578- if PY2:579- __unicode__ = Undefined._fail_with_undefined_error567 581-568 # remove remaining slots attributes, after the metaclass did the magic they569 # are unneeded and irritating as they contain wrong data for the subclasses.570 del Undefined.__slots__, DebugUndefined.__slots__, StrictUndefined.__slots__