]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Unified version checks where appropriate
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 19 May 2013 13:22:08 +0000 (14:22 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 19 May 2013 13:22:08 +0000 (14:22 +0100)
jinja2/_compat.py
jinja2/bccache.py
jinja2/exceptions.py
jinja2/testsuite/__init__.py
jinja2/testsuite/ext.py
jinja2/testsuite/filters.py
jinja2/testsuite/lexnparse.py
jinja2/testsuite/regression.py
jinja2/testsuite/security.py
jinja2/utils.py

index f10ef651ce863dcf71e93825e07ed5ada0a40c59..13b1382696b5fdec3c18c62d2eb4eff99ec1275e 100644 (file)
@@ -30,6 +30,8 @@ text_type = six.text_type
 string_types = six.string_types
 
 iteritems = six.iteritems
+iterkeys = six.iterkeys
+itervalues = six.itervalues
 
 if six.PY3:
     from io import BytesIO, StringIO
index dbfc3c2dce3f46a09b6ab3dcd11716de7c445e8f..a3bb00233380dfd26a7e55ae5d13b725f0c7e47c 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
+from jinja2._compat import BytesIO, pickle, PY3
 
 
 # marshal works better on 3.x, one hack less required
-if sys.version_info[0] >= 3:
+if PY3:
     marshal_dump = marshal.dump
     marshal_load = marshal.load
 else:
index 0075be00aaa5aae971c1ae0eb348a6214bba8184..b137353b87cd044919f8442100dee7807819091e 100644 (file)
@@ -8,14 +8,13 @@
     :copyright: (c) 2010 by the Jinja Team.
     :license: BSD, see LICENSE for more details.
 """
-import sys
 from jinja2._compat import imap, text_type, PY3
 
 
 class TemplateError(Exception):
     """Baseclass for all template errors."""
 
-    if sys.version_info[0] < 3:
+    if not PY3:
         def __init__(self, message=None):
             if message is not None:
                 message = text_type(message).encode('utf-8')
index 335292de12b3a19b39ab86cd612019323e6654a4..95166f56b9e4043236226c044fc2752165874933 100644 (file)
@@ -16,6 +16,7 @@ import sys
 import unittest
 from traceback import format_exception
 from jinja2 import loaders
+from jinja2._compat import PY3
 
 
 here = os.path.dirname(os.path.abspath(__file__))
@@ -89,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 sys.version_info[0] < 3:
+    if not PY3:
         suite.addTest(doctests.suite())
 
     return suite
index 9fd75ea60e22dd347df4060a38d89db315a3dcf3..0f93be94586e46ae4ac06a2b0fa201c66eff862d 100644 (file)
@@ -9,7 +9,6 @@
     :license: BSD, see LICENSE for more details.
 """
 import re
-import six
 import unittest
 
 from jinja2.testsuite import JinjaTestCase
@@ -18,8 +17,7 @@ from jinja2 import Environment, DictLoader, contextfunction, nodes
 from jinja2.exceptions import TemplateAssertionError
 from jinja2.ext import Extension
 from jinja2.lexer import Token, count_newlines
-from jinja2._compat import next
-from six import BytesIO
+from jinja2._compat import next, BytesIO, itervalues, text_type
 
 importable_object = 23
 
@@ -219,7 +217,7 @@ class ExtensionsTestCase(JinjaTestCase):
         original = Environment(extensions=[TestExtension])
         overlay = original.overlay()
         for env in original, overlay:
-            for ext in six.itervalues(env.extensions):
+            for ext in itervalues(env.extensions):
                 assert ext.environment is env
 
     def test_preprocessor_extension(self):
@@ -438,7 +436,7 @@ class AutoEscapeTestCase(JinjaTestCase):
         '''
         tmpl = env.from_string(tmplsource)
         assert tmpl.render(val=True).split()[0] == 'Markup'
-        assert tmpl.render(val=False).split()[0] == six.text_type.__name__
+        assert tmpl.render(val=False).split()[0] == text_type.__name__
 
         # looking at the source we should see <testing> there in raw
         # (and then escaped as well)
index 88f93f97e007bb944e7d4d65f74bffab95dfa2a5..31c9559cd751d61478be5fa7322d020b7dc5df4a 100644 (file)
@@ -12,8 +12,7 @@ import unittest
 from jinja2.testsuite import JinjaTestCase
 
 from jinja2 import Markup, Environment
-import six
-from six.moves import map
+from jinja2._compat import text_type, PY3
 
 env = Environment()
 
@@ -190,7 +189,7 @@ class FilterTestCase(JinjaTestCase):
     def test_string(self):
         x = [1, 2, 3, 4, 5]
         tmpl = env.from_string('''{{ obj|string }}''')
-        assert tmpl.render(obj=x) == six.text_type(x)
+        assert tmpl.render(obj=x) == text_type(x)
 
     def test_title(self):
         tmpl = env.from_string('''{{ "foo bar"|title }}''')
@@ -299,8 +298,13 @@ class FilterTestCase(JinjaTestCase):
             def __init__(self, value):
                 self.value = value
             def __unicode__(self):
-                return six.text_type(self.value)
-            __str__ = __unicode__
+                return text_type(self.value)
+            if PY3:
+                __str__ = __unicode__
+                del __unicode__
+            else:
+                def __str__(self):
+                    return self.__unicode__().encode('utf-8')
         tmpl = env.from_string('''{{ items|sort(attribute='value')|join }}''')
         assert tmpl.render(items=map(Magic, [3, 2, 4, 1])) == '1234'
 
index cbe0f2c01df1e66adaed66c3e25fb61344433eb7..e266111b37b1cc2a1e4d9963b670ddc6629f4996 100644 (file)
@@ -8,22 +8,20 @@
     :copyright: (c) 2010 by the Jinja Team.
     :license: BSD, see LICENSE for more details.
 """
-import sys
-import six
 import unittest
 
 from jinja2.testsuite import JinjaTestCase
 
 from jinja2 import Environment, Template, TemplateSyntaxError, \
      UndefinedError, nodes
-from jinja2._compat import next
+from jinja2._compat import next, iteritems, text_type, PY3
 from jinja2.lexer import Token, TokenStream, TOKEN_EOF, TOKEN_BLOCK_BEGIN, TOKEN_BLOCK_END
 
 env = Environment()
 
 
 # how does a string look like in jinja syntax?
-if sys.version_info[0] < 3:
+if not PY3:
     def jinja_string_repr(string):
         return repr(string)[1:]
 else:
@@ -95,7 +93,7 @@ class LexerTestCase(JinjaTestCase):
 
     def test_operators(self):
         from jinja2.lexer import operators
-        for test, expect in six.iteritems(operators):
+        for test, expect in iteritems(operators):
             if test in '([{}])':
                 continue
             stream = env.lexer.tokenize('{{ %s }}' % test)
@@ -377,7 +375,7 @@ class SyntaxTestCase(JinjaTestCase):
     def test_notin(self):
         bar = range(100)
         tmpl = env.from_string('''{{ not 42 in bar }}''')
-        assert tmpl.render(bar=bar) == six.text_type(not 42 in bar)
+        assert tmpl.render(bar=bar) == text_type(not 42 in bar)
 
     def test_implicit_subscribed_tuple(self):
         class Foo(object):
index a4e66311b8c7012ceaf2bc61a657c7cc3bdfaf62..c5f7d5c659c8ecc2bbc725953fb1904b64b36a96 100644 (file)
@@ -14,7 +14,7 @@ from jinja2.testsuite import JinjaTestCase
 
 from jinja2 import Template, Environment, DictLoader, TemplateSyntaxError, \
      TemplateNotFound, PrefixLoader
-import six
+from jinja2._compat import text_type
 
 env = Environment()
 
@@ -119,7 +119,7 @@ class BugTestCase(JinjaTestCase):
 
         ''')
 
-        assert tmpl.render().split() == [six.text_type(x) for x in range(1, 11)] * 5
+        assert tmpl.render().split() == [text_type(x) for x in range(1, 11)] * 5
 
     def test_weird_inline_comment(self):
         env = Environment(line_statement_prefix='%')
index c892fed2d424a5fc5ddd1247883835fe8d5663b0..246d0f07394057c4a8e8508e51886f85048e6c7d 100644 (file)
@@ -18,7 +18,7 @@ from jinja2.sandbox import SandboxedEnvironment, \
 from jinja2 import Markup, escape
 from jinja2.exceptions import SecurityError, TemplateSyntaxError, \
      TemplateRuntimeError
-import six
+from jinja2._compat import text_type
 
 
 class PrivateStuff(object):
@@ -77,7 +77,7 @@ class SandboxTestCase(JinjaTestCase):
         # adding two strings should escape the unsafe one
         unsafe = '<script type="application/x-some-script">alert("foo");</script>'
         safe = Markup('<em>username</em>')
-        assert unsafe + safe == six.text_type(escape(unsafe)) + six.text_type(safe)
+        assert unsafe + safe == text_type(escape(unsafe)) + text_type(safe)
 
         # string interpolations are safe to use too
         assert Markup('<em>%s</em>') % '<bad user>' == \
@@ -115,7 +115,7 @@ class SandboxTestCase(JinjaTestCase):
                             '{{ say_hello("<blink>foo</blink>") }}')
         escaped_out = '<p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>'
         assert t.render() == escaped_out
-        assert six.text_type(t.module) == escaped_out
+        assert text_type(t.module) == escaped_out
         assert escape(t.module) == escaped_out
         assert t.module.say_hello('<blink>foo</blink>') == escaped_out
         assert escape(t.module.say_hello('<blink>foo</blink>')) == escaped_out
index f3cf10efde27c692edd2d9566649176a19982530..d499d49bff79f563ae107fe371aabe1c5cfbf703 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
+from jinja2._compat import text_type, string_types, Iterator, PY3
 
 
 _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 sys.version_info[0] < 3:
+if not PY3:
     def _encode_filename(filename):
         if isinstance(filename, unicode):
             return filename.encode('utf-8')