PY3 -> PY2 macro
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 630804f · Browse files · 10 files·+95 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+95 added−58 removed
- other:+95 added−58 removed
Provenance
- Commit
7e245e2- Tree
8c31273- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
630804f
10 files changed+95 added−58 removed
modified:jinja2/testsuite/__init__.py+2 added-2 removed
14 unmodified lines15 import sys16 import unittest17 from traceback import format_exception18 from jinja2 import loaders19-from jinja2._compat import PY319+from jinja2._compat import PY220 21 22 here = os.path.dirname(os.path.abspath(__file__))23 65 unmodified lines89 suite.addTest(utils.suite())90 91 # doctests will not run on python 3 currently. Too many issues92 # with that, do not test that on that platform.93- if not PY3:93+ if PY2:94 suite.addTest(doctests.suite())95 96 return suitemodified:jinja2/testsuite/filters.py+5 added-5 removed
10 unmodified lines11 import unittest12 from jinja2.testsuite import JinjaTestCase13 14 from jinja2 import Markup, Environment15-from jinja2._compat import text_type, PY315+from jinja2._compat import text_type, PY216 17 env = Environment()18 19 278 unmodified lines298 def __init__(self, value):299 self.value = value300 def __unicode__(self):301 return text_type(self.value)302- if PY3:303- __str__ = __unicode__304- del __unicode__305- else:302+ if PY2:303 def __str__(self):304 return self.__unicode__().encode('utf-8')305+ else:306+ __str__ = __unicode__307+ del __unicode__308 tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')309 assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'310 311 def test_groupby(self):195 unmodified linesmodified:jinja2/testsuite/lexnparse.py+2 added-2 removed
12 unmodified lines13 from jinja2.testsuite import JinjaTestCase14 15 from jinja2 import Environment, Template, TemplateSyntaxError, \16 UndefinedError, nodes17-from jinja2._compat import next, iteritems, text_type, PY317+from jinja2._compat import next, iteritems, text_type, PY218 from jinja2.lexer import Token, TokenStream, TOKEN_EOF, \19 TOKEN_BLOCK_BEGIN, TOKEN_BLOCK_END20 21 env = Environment()22 23 24 # how does a string look like in jinja syntax?25-if not PY3:25+if PY2:26 def jinja_string_repr(string):27 return repr(string)[1:]28 else:29 jinja_string_repr = repr564 unmodified linesmodified:jinja2/_compat.py+60 added-26 removed
1 unmodified line2 """3 jinja2._compat4 ~~~~~~~~~~~~~~5 6- Some py2/py3 compatibility support that is not yet available in7- "six" 1.3.0. Generally all uses of six should go through this module8- so that we have one central place to remove stuff from when we9- eventually drop 2.x.6+ Some py2/py3 compatibility support based on a stripped down7+ version of six so we don't have to depend on a specific version8+ of it.9 10 :copyright: Copyright 2013 by the Jinja team, see AUTHORS.11 :license: BSD, see LICENSE for details.12 """14-import six13 import sys14 17-PY3 = six.PY315+PY2 = sys.version_info[0] == 216 19-# https://bitbucket.org/gutworth/six/issue/25/add-unichr20-try:21- unichr = unichr # py222-except NameError:23- unichr = chr # py317 25-range_type = six.moves.xrange26-next = six.advance_iterator27-imap = six.moves.map28-izip = six.moves.zip29-text_type = six.text_type30-string_types = six.string_types18+if not PY2:19+ unichr = chr20+ range_type = range21+ text_type = str22+ string_types = (str,)23 32-iteritems = six.iteritems33-iterkeys = six.iterkeys34-itervalues = six.itervalues24+ _iterkeys = 'keys'25+ _itervalues = 'values'26+ _iteritems = 'items'27 36-if six.PY3:28 from io import BytesIO, StringIO29 NativeStringIO = StringIO30+31+ ifilter = filter32+ imap = map33+ izip = zip34+35+ def reraise(tp, value, tb=None):36+ if value.__traceback__ is not tb:37+ raise value.with_traceback(tb)38+ raise value39+40+ Iterator = object41 else:42+ text_type = unicode43+ unichr = unichr44+ string_types = (str, unicode)45+46+ _iterkeys = 'iterkeys'47+ _itervalues = 'itervalues'48+ _iteritems = 'iteritems'49+50+ from itertools import imap, izip, ifilter51+ range_type = xrange52+53 from cStringIO import StringIO as BytesIO54 from StringIO import StringIO55 NativeStringIO = BytesIO56 57+ exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')58+59+ class Iterator(object):60+ def next(self):61+ return self.__next__()62+63 try:64+ next = next65+except NameError:66+ def next(it):67+ return it.next()68+69+70+def with_metaclass(meta, *bases):71+ """Create a base class with a metaclass."""72+ return meta('NewBase', bases, {})73+74+def iterkeys(d, **kw):75+ return iter(getattr(d, _iterkeys)(**kw))76+77+def itervalues(d, **kw):78+ return iter(getattr(d, _itervalues)(**kw))79+80+def iteritems(d, **kw):81+ return iter(getattr(d, _iteritems)(**kw))82+83+try:84 import cPickle as pickle85 except ImportError:86 import pickle87 49-ifilter = six.moves.filter50-reraise = six.reraise51-Iterator = six.Iterator52-with_metaclass = six.with_metaclass53-88 try:89 from collections import Mapping as mapping_types90 except ImportError:91 import UserDict21 unmodified linesmodified:jinja2/bccache.py+2 added-2 removed
19 unmodified lines20 import tempfile21 import fnmatch22 from hashlib import sha123 from jinja2.utils import open_if_exists24-from jinja2._compat import BytesIO, pickle, PY324+from jinja2._compat import BytesIO, pickle, PY225 26 27 # marshal works better on 3.x, one hack less required28-if PY3:28+if not PY2:29 marshal_dump = marshal.dump30 marshal_load = marshal.load31 else:32 279 unmodified linesmodified:jinja2/environment.py+8 added-5 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, PY3, Iterator, next31+ text_type, reraise, PY2, Iterator, next32 from functools import reduce33 34 35 # for direct template usage we have up to ten living environments1028 unmodified lines1064 1065 def __html__(self):1066 return Markup(concat(self._body_stream))1067 1068- def __str__(self):1069- s = self.__unicode__()1070- return s if PY3 else s.encode('utf-8')1071-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+1078 def __repr__(self):1079 if self.__name__ is None:1080 name = 'memory:%x' % id(self)1081 else:107 unmodified linesmodified:jinja2/exceptions.py+6 added-6 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, PY311+from jinja2._compat import imap, text_type, PY212 13 14 class TemplateError(Exception):15 """Baseclass for all template errors."""16 17- if not PY3:17+ if PY2:18 def __init__(self, message=None):19 if message is not None:20 message = text_type(message).encode('utf-8')21 Exception.__init__(self, message)92 unmodified lines114 lines.append(' ' + line.strip())115 116 return u'\n'.join(lines)117 118- if PY3:119- __str__ = __unicode__120- del __unicode__121- else:118+ if PY2:119 def __str__(self):120 return self.__unicode__().encode('utf-8')121+ else:122+ __str__ = __unicode__123+ del __unicode__124 125 126 class TemplateAssertionError(TemplateSyntaxError):127 """Like a template syntax error, but covers cases where something in the26 unmodified linesmodified:jinja2/runtime.py+7 added-7 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, PY318+ string_types, PY219 20 21 # these variables are exported to the template runtime22 __all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',477 unmodified lines500 501 def __unicode__(self):502 return u''503 504- if PY3:505- __str__ = __unicode__506- del __unicode__507- else:504+ if PY2:505 def __str__(self):506 return self.__unicode__().encode('utf-8')507+ else:508+ __str__ = __unicode__509+ del __unicode__510 511 def __len__(self):512 return 0513 32 unmodified lines546 self._undefined_name547 )548 return u'{{ undefined value printed: %s }}' % self._undefined_hint549 550- if PY3:550+ if not PY2:551 __str__ = __unicode__552 del __unicode__553 554 19 unmodified lines574 __slots__ = ()575 __iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \576 __ne__ = __bool__ = Undefined._fail_with_undefined_error577 578- if not PY3:578+ if PY2:579 __unicode__ = Undefined._fail_with_undefined_error580 581 582 # remove remaining slots attributes, after the metaclass did the magic they2 unmodified linesmodified:jinja2/utils.py+2 added-2 removed
21 unmodified lines22 from _thread import allocate_lock # py 323 except ImportError:24 from dummy_thread import allocate_lock25 from collections import deque26-from jinja2._compat import text_type, string_types, Iterator, PY326+from jinja2._compat import text_type, string_types, Iterator, PY227 28 29 _word_split_re = re.compile(r'(\s+)')30 _punctuation_re = re.compile(21 unmodified lines52 # when passed to encode we let this function encode it properly.53 # This is used in a couple of places. As far as Jinja is concerned54 # filenames are unicode *or* bytestrings in 2.x and unicode only in55 # 3.x because compile cannot handle bytes56-if not PY3:56+if PY2:57 def _encode_filename(filename):58 if isinstance(filename, unicode):59 return filename.encode('utf-8')60 return filename489 unmodified linesmodified:setup.py+1 added-1 removed
78 unmodified lines79 'Topic :: Software Development :: Libraries :: Python Modules',80 'Topic :: Text Processing :: Markup :: HTML'81 ],82 packages=['jinja2', 'jinja2.testsuite', 'jinja2.testsuite.res'],83- install_requires=['six>=1.3.0', 'markupsafe'],83+ install_requires=['markupsafe'],84 extras_require={'i18n': ['Babel>=0.8']},85 test_suite='jinja2.testsuite.suite',86 include_package_data=True,87 entry_points="""4 unmodified lines