]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
PY3 -> PY2 macro
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 19 May 2013 13:34:54 +0000 (14:34 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 19 May 2013 13:34:54 +0000 (14:34 +0100)
jinja2/_compat.py
jinja2/bccache.py
jinja2/environment.py
jinja2/exceptions.py
jinja2/runtime.py
jinja2/testsuite/__init__.py
jinja2/testsuite/filters.py
jinja2/testsuite/lexnparse.py
jinja2/utils.py
setup.py

index 13b1382696b5fdec3c18c62d2eb4eff99ec1275e..d770f1a6b7bb9a2d01c9a1a826ddaaea025672ca 100644 (file)
@@ -3,54 +3,88 @@
     jinja2._compat
     ~~~~~~~~~~~~~~
 
-    Some py2/py3 compatibility support that is not yet available in
-    "six" 1.3.0.  Generally all uses of six should go through this module
-    so that we have one central place to remove stuff from when we
-    eventually drop 2.x.
+    Some py2/py3 compatibility support based on a stripped down
+    version of six so we don't have to depend on a specific version
+    of it.
 
     :copyright: Copyright 2013 by the Jinja team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
-import six
 import sys
 
-PY3 = six.PY3
+PY2 = sys.version_info[0] == 2
 
-# https://bitbucket.org/gutworth/six/issue/25/add-unichr
-try:
-    unichr = unichr  # py2
-except NameError:
-    unichr = chr  # py3
 
-range_type = six.moves.xrange
-next = six.advance_iterator
-imap = six.moves.map
-izip = six.moves.zip
-text_type = six.text_type
-string_types = six.string_types
+if not PY2:
+    unichr = chr
+    range_type = range
+    text_type = str
+    string_types = (str,)
 
-iteritems = six.iteritems
-iterkeys = six.iterkeys
-itervalues = six.itervalues
+    _iterkeys = 'keys'
+    _itervalues = 'values'
+    _iteritems = 'items'
 
-if six.PY3:
     from io import BytesIO, StringIO
     NativeStringIO = StringIO
+
+    ifilter = filter
+    imap = map
+    izip = zip
+
+    def reraise(tp, value, tb=None):
+        if value.__traceback__ is not tb:
+            raise value.with_traceback(tb)
+        raise value
+
+    Iterator = object
 else:
+    text_type = unicode
+    unichr = unichr
+    string_types = (str, unicode)
+
+    _iterkeys = 'iterkeys'
+    _itervalues = 'itervalues'
+    _iteritems = 'iteritems'
+
+    from itertools import imap, izip, ifilter
+    range_type = xrange
+
     from cStringIO import StringIO as BytesIO
     from StringIO import StringIO
     NativeStringIO = BytesIO
 
+    exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')
+
+    class Iterator(object):
+        def next(self):
+            return self.__next__()
+
+try:
+    next = next
+except NameError:
+    def next(it):
+        return it.next()
+
+
+def with_metaclass(meta, *bases):
+    """Create a base class with a metaclass."""
+    return meta('NewBase', bases, {})
+
+def iterkeys(d, **kw):
+    return iter(getattr(d, _iterkeys)(**kw))
+
+def itervalues(d, **kw):
+    return iter(getattr(d, _itervalues)(**kw))
+
+def iteritems(d, **kw):
+    return iter(getattr(d, _iteritems)(**kw))
+
 try:
     import cPickle as pickle
 except ImportError:
     import pickle
 
-ifilter = six.moves.filter
-reraise = six.reraise
-Iterator = six.Iterator
-with_metaclass = six.with_metaclass
-
 try:
     from collections import Mapping as mapping_types
 except ImportError:
index a3bb00233380dfd26a7e55ae5d13b725f0c7e47c..b534e27123d2a3d5f1fe8c47c169a6fd1996ef45 100644 (file)
@@ -21,11 +21,11 @@ import tempfile
 import fnmatch
 from hashlib import sha1
 from jinja2.utils import open_if_exists
-from jinja2._compat import BytesIO, pickle, PY3
+from jinja2._compat import BytesIO, pickle, PY2
 
 
 # marshal works better on 3.x, one hack less required
-if PY3:
+if not PY2:
     marshal_dump = marshal.dump
     marshal_load = marshal.load
 else:
index ee35b89704ab4c7d5a1edfcb6499120581695425..cbac8c1c53f56fb88a86097eaa762befdb5b4c24 100644 (file)
@@ -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, PY3, Iterator, next
+     text_type, reraise, PY2, Iterator, next
 from functools import reduce
 
 
@@ -1065,13 +1065,16 @@ class TemplateModule(object):
     def __html__(self):
         return Markup(concat(self._body_stream))
 
-    def __str__(self):
-        s = self.__unicode__()
-        return s if PY3 else s.encode('utf-8')
-
     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)
index b137353b87cd044919f8442100dee7807819091e..1bbceeea5aaf1dbfb74bd72ae48a48a339edee5c 100644 (file)
@@ -8,13 +8,13 @@
     :copyright: (c) 2010 by the Jinja Team.
     :license: BSD, see LICENSE for more details.
 """
-from jinja2._compat import imap, text_type, PY3
+from jinja2._compat import imap, text_type, PY2
 
 
 class TemplateError(Exception):
     """Baseclass for all template errors."""
 
-    if not PY3:
+    if PY2:
         def __init__(self, message=None):
             if message is not None:
                 message = text_type(message).encode('utf-8')
@@ -115,12 +115,12 @@ class TemplateSyntaxError(TemplateError):
 
         return u'\n'.join(lines)
 
-    if PY3:
-        __str__ = __unicode__
-        del __unicode__
-    else:
+    if PY2:
         def __str__(self):
             return self.__unicode__().encode('utf-8')
+    else:
+        __str__ = __unicode__
+        del __unicode__
 
 
 class TemplateAssertionError(TemplateSyntaxError):
index 489bcca79c6ae6f8f163c5ba29ec943a2a7bc844..b5e203ff6e5bc6403d94ba337e9dcf02a33255e1 100644 (file)
@@ -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, PY3
+     string_types, PY2
 
 
 # these variables are exported to the template runtime
@@ -501,12 +501,12 @@ class Undefined(object):
     def __unicode__(self):
         return u''
 
-    if PY3:
-        __str__ = __unicode__
-        del __unicode__
-    else:
+    if PY2:
         def __str__(self):
             return self.__unicode__().encode('utf-8')
+    else:
+        __str__ = __unicode__
+        del __unicode__
 
     def __len__(self):
         return 0
@@ -547,7 +547,7 @@ class DebugUndefined(Undefined):
             )
         return u'{{ undefined value printed: %s }}' % self._undefined_hint
 
-    if PY3:
+    if not PY2:
         __str__ = __unicode__
         del __unicode__
 
@@ -575,7 +575,7 @@ class StrictUndefined(Undefined):
     __iter__ = __str__ = __len__ = __nonzero__ = __eq__ = \
         __ne__ = __bool__ = Undefined._fail_with_undefined_error
 
-    if not PY3:
+    if PY2:
         __unicode__ = Undefined._fail_with_undefined_error
 
 
index 95166f56b9e4043236226c044fc2752165874933..781a0e79299b2a07d8efdd385e94ba9eca921c69 100644 (file)
@@ -16,7 +16,7 @@ import sys
 import unittest
 from traceback import format_exception
 from jinja2 import loaders
-from jinja2._compat import PY3
+from jinja2._compat import PY2
 
 
 here = os.path.dirname(os.path.abspath(__file__))
@@ -90,7 +90,7 @@ def suite():
 
     # doctests will not run on python 3 currently.  Too many issues
     # with that, do not test that on that platform.
-    if not PY3:
+    if PY2:
         suite.addTest(doctests.suite())
 
     return suite
index 31c9559cd751d61478be5fa7322d020b7dc5df4a..c1f32c3a0a2c523d557614a2ac23f4bee165b92e 100644 (file)
@@ -12,7 +12,7 @@ import unittest
 from jinja2.testsuite import JinjaTestCase
 
 from jinja2 import Markup, Environment
-from jinja2._compat import text_type, PY3
+from jinja2._compat import text_type, PY2
 
 env = Environment()
 
@@ -299,12 +299,12 @@ class FilterTestCase(JinjaTestCase):
                 self.value = value
             def __unicode__(self):
                 return text_type(self.value)
-            if PY3:
-                __str__ = __unicode__
-                del __unicode__
-            else:
+            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'
 
index f8a8a8da019117cacbf2f01f1bb2813aca8edecf..bd1c94cd344025615c3c929f84840e2c2acc8b95 100644 (file)
@@ -14,7 +14,7 @@ from jinja2.testsuite import JinjaTestCase
 
 from jinja2 import Environment, Template, TemplateSyntaxError, \
      UndefinedError, nodes
-from jinja2._compat import next, iteritems, text_type, PY3
+from jinja2._compat import next, iteritems, text_type, PY2
 from jinja2.lexer import Token, TokenStream, TOKEN_EOF, \
      TOKEN_BLOCK_BEGIN, TOKEN_BLOCK_END
 
@@ -22,7 +22,7 @@ env = Environment()
 
 
 # how does a string look like in jinja syntax?
-if not PY3:
+if PY2:
     def jinja_string_repr(string):
         return repr(string)[1:]
 else:
index d499d49bff79f563ae107fe371aabe1c5cfbf703..610d8e3826135fc279bb82a4696a171cf7e16ce1 100644 (file)
@@ -23,7 +23,7 @@ except ImportError:
     except ImportError:
         from dummy_thread import allocate_lock
 from collections import deque
-from jinja2._compat import text_type, string_types, Iterator, PY3
+from jinja2._compat import text_type, string_types, Iterator, PY2
 
 
 _word_split_re = re.compile(r'(\s+)')
@@ -53,7 +53,7 @@ concat = u''.join
 # This is used in a couple of places.  As far as Jinja is concerned
 # filenames are unicode *or* bytestrings in 2.x and unicode only in
 # 3.x because compile cannot handle bytes
-if not PY3:
+if PY2:
     def _encode_filename(filename):
         if isinstance(filename, unicode):
             return filename.encode('utf-8')
index b8dcfb5ed126ebdbfd03c733c8497fd1f3d8834d..74d1ec39fc914f6073e505d5b8ae7a0618691992 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -80,7 +80,7 @@ setup(
         'Topic :: Text Processing :: Markup :: HTML'
     ],
     packages=['jinja2', 'jinja2.testsuite', 'jinja2.testsuite.res'],
-    install_requires=['six>=1.3.0', 'markupsafe'],
+    install_requires=['markupsafe'],
     extras_require={'i18n': ['Babel>=0.8']},
     test_suite='jinja2.testsuite.suite',
     include_package_data=True,