Skip to content
Myr

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

5 files+21 added−51 removed

modified:jinja2/testsuite/filters.py+2 added-8 removed
10 unmodified lines
11import unittest12from jinja2.testsuite import JinjaTestCase1314from jinja2 import Markup, Environment15from jinja2._compat import text_type, PY215from jinja2._compat import text_type, UnicodeMixin1617env = Environment()1819
273 unmodified lines
293 tmpl = env.from_string('''{{ ['foo', 'Bar', 'blah']|sort }}''')294 assert tmpl.render() == "['Bar', 'blah', 'foo']"295296 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'304305 def test_groupby(self):
195 unmodified lines
modified:jinja2/_compat.py+11 added
36 unmodified lines
37 raise value.with_traceback(tb)38 raise value3940 Iterator = object4142 class UnicodeMixin(object):43 __slots__ = ()44 def __str__(self):45 return self.__unicode__()46else:47 text_type = unicode48 unichr = unichr49 string_types = (str, unicode)
10 unmodified lines
60 NativeStringIO = BytesIO6162 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')6364 class UnicodeMixin(object):65 __slots__ = ()66 def __str__(self):67 return self.__unicode__().encode('utf-8')6869 class Iterator(object):70 __slots__ = ()71 def next(self):72 return self.__next__()7374try:
49 unmodified lines
modified:jinja2/environment.py+2 added-9 removed
26 unmodified lines
27 TemplatesNotFound, TemplateRuntimeError28from jinja2.utils import import_string, LRUCache, Markup, missing, \29 concat, consume, internalcode, _encode_filename30from jinja2._compat import imap, ifilter, string_types, iteritems, \31 text_type, reraise, PY2, Iterator, next31 text_type, reraise, Iterator, next, UnicodeMixin32from functools import reduce333435# for direct template usage we have up to ten living environments
1014 unmodified lines
1050 name = repr(self.name)1051 return '<%s %s>' % (self.__class__.__name__, name)105210531054class TemplateModule(object):1054class 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 lines
10671068 def __unicode__(self):1069 return concat(self._body_stream)10701071 if PY2:1072 def __str__(self):1073 return self.__unicode__().encode('utf-8')1074 else:1075 __str__ = __unicode__1076 del __unicode__10771071 def __repr__(self):1072 if self.__name__ is None:1073 name = 'memory:%x' % id(self)1074 else:
107 unmodified lines
modified:jinja2/exceptions.py+3 added-17 removed
6 unmodified lines
78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11from jinja2._compat import imap, text_type, PY211from jinja2._compat import imap, text_type, PY2, UnicodeMixin121314class TemplateError(Exception):15 """Baseclass for all template errors."""
19 unmodified lines
35 if message is not None:36 return message373839class TemplateNotFound(IOError, LookupError, TemplateError):39class TemplateNotFound(IOError, LookupError, TemplateError, UnicodeMixin):40 """Raised if a template does not exist."""4142 # looks weird, but removes the warning descriptor that just43 # bogusly warns us about message being deprecated
6 unmodified lines
50 self.message = message51 self.name = name52 self.templates = [name]5354 def __str__(self):55 return self.message.encode('utf-8')5657 # 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.message5657
12 unmodified lines
70 TemplateNotFound.__init__(self, names and names[-1] or None, message)71 self.templates = list(names)727381class TemplateSyntaxError(TemplateError):74class TemplateSyntaxError(UnicodeMixin, TemplateError):75 """Raised to tell the user that there is a problem with the template."""7677 def __init__(self, message, lineno, name=None, filename=None):78 TemplateError.__init__(self, message)
28 unmodified lines
107 lines.append(' ' + line.strip())108109 return u'\n'.join(lines)110118 if PY2:119 def __str__(self):120 return self.__unicode__().encode('utf-8')121 else:122 __str__ = __unicode__123 del __unicode__111125112class 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 of
24 unmodified lines
modified:jinja2/runtime.py+3 added-17 removed
13 unmodified lines
14 internalcode, object_type_repr15from jinja2.exceptions import UndefinedError, TemplateRuntimeError, \16 TemplateNotFound17from jinja2._compat import next, imap, text_type, iteritems, Iterator, \18 string_types, PY218 string_types, PY2, UnicodeMixin192021# these variables are exported to the template runtime22__all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',
416 unmodified lines
439 self.name is None and 'anonymous' or repr(self.name)440 )441442443class Undefined(object):443class 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`:446447 >>> foo = Undefined(name='foo')
52 unmodified lines
500501 def __unicode__(self):502 return u''503504 if PY2:505 def __str__(self):506 return self.__unicode__().encode('utf-8')507 else:508 __str__ = __unicode__509 del __unicode__510504 def __len__(self):505 return 0506507 def __iter__(self):
31 unmodified lines
539 self._undefined_name540 )541 return u'{{ undefined value printed: %s }}' % self._undefined_hint542550 if not PY2:551 __str__ = __unicode__552 del __unicode__543554544class 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 lines
560 ...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_error566578 if PY2:579 __unicode__ = Undefined._fail_with_undefined_error567581568# remove remaining slots attributes, after the metaclass did the magic they569# are unneeded and irritating as they contain wrong data for the subclasses.570del Undefined.__slots__, DebugUndefined.__slots__, StrictUndefined.__slots__