Started to work on Python 3 support.
authorArmin Ronacher <armin.ronacher@active-4.com>
Wed, 5 Aug 2009 16:45:39 +0000 (18:45 +0200)
committerArmin Ronacher <armin.ronacher@active-4.com>
Wed, 5 Aug 2009 16:45:39 +0000 (18:45 +0200)
--HG--
branch : trunk

.hgignore
Makefile
jinja2/bccache.py
jinja2/defaults.py
jinja2/lexer.py
jinja2/runtime.py
jinja2/tests.py
jinja2/utils.py

index eebc869f522b7d50d3f5887eb1739b6713a7ba61..7a1b935bfc6edfc066659982ed07be1ad062cc27 100644 (file)
--- a/.hgignore
+++ b/.hgignore
@@ -6,3 +6,4 @@
 \.py[co]$
 \.DS_Store$
 ^env/
+^py3k/
index cfc315c8692a852e6bdcbcebf568e1c434f38a09..6cd734ae4b627fa393951783d551779b903bed21 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,11 @@
 test:
        cd tests; nosetests -v
 
+2to3:
+       rm -rf py3k
+       mkdir py3k
+       cp -R jinja2 py3k
+       2to3 jinja2 > py3k/convert.patch
+       cd py3k; patch -p0 < convert.patch
+
 .PHONY: test
index 2786082e0cfcf32e7576cfacb88662ca4925e9c4..6415158f1ae121fefa2a9147be34d2a6fb657ff0 100644 (file)
@@ -28,7 +28,7 @@ from jinja2.utils import open_if_exists
 
 
 bc_version = 1
-bc_magic = 'j2' + pickle.dumps(bc_version, 2)
+bc_magic = 'j2'.encode('ascii') + pickle.dumps(bc_version, 2)
 
 
 class Bucket(object):
index 458485ea2c9b97a96d22b06b16fdae48cacc169e..13e303b7131553a882decdbd026290084695c96d 100644 (file)
@@ -24,11 +24,17 @@ TRIM_BLOCKS = False
 NEWLINE_SEQUENCE = '\n'
 
 
+try:
+    range_func = xrange
+except NameError:
+    range_func = range
+
+
 # default filters, tests and namespace
 from jinja2.filters import FILTERS as DEFAULT_FILTERS
 from jinja2.tests import TESTS as DEFAULT_TESTS
 DEFAULT_NAMESPACE = {
-    'range':        xrange,
+    'range':        range_func,
     'dict':         lambda **kw: kw,
     'lipsum':       generate_lorem_ipsum,
     'cycler':       Cycler,
index 919cba8695e9c1494b3936ace088483629622e56..335e9cac7cbf2a0a885e6431824213d7bb94932f 100644 (file)
@@ -253,10 +253,9 @@ class TokenStream(object):
         return TokenStreamIterator(self)
 
     def __nonzero__(self):
-        """Are we at the end of the stream?"""
         return bool(self._pushed) or self.current.type is not TOKEN_EOF
 
-    eos = property(lambda x: not x.__nonzero__(), doc=__nonzero__.__doc__)
+    eos = property(lambda x: not x, doc="Are we at the end of the stream?")
 
     def push(self, token):
         """Push a token back to the stream."""
index 669ff2100faf9c8a8b64584c656eef03f5501f6e..5fe2cd64d212408d2abf985e2cccd0139f2f2591 100644 (file)
@@ -188,9 +188,12 @@ class Context(object):
     keys = _all('keys')
     values = _all('values')
     items = _all('items')
-    iterkeys = _all('iterkeys')
-    itervalues = _all('itervalues')
-    iteritems = _all('iteritems')
+
+    # not available on python 3
+    if hasattr(dict, 'iterkeys'):
+        iterkeys = _all('iterkeys')
+        itervalues = _all('itervalues')
+        iteritems = _all('iteritems')
     del _all
 
     def __contains__(self, name):
index 58dc0c05f9f9f8c5999317c30c79a3ca8a297ff1..9fbf1639438c101b0a5972d34a54b80eeb78bf9c 100644 (file)
@@ -16,6 +16,13 @@ number_re = re.compile(r'^-?\d+(\.\d+)?$')
 regex_type = type(number_re)
 
 
+try:
+    test_callable = callable
+except NameError:
+    def test_callable(x):
+        return hasattr(x, '__call__')
+
+
 def test_odd(value):
     """Return true if the variable is odd."""
     return value % 2 == 1
@@ -130,7 +137,7 @@ TESTS = {
     'number':           test_number,
     'sequence':         test_sequence,
     'iterable':         test_iterable,
-    'callable':         callable,
+    'callable':         test_callable,
     'sameas':           test_sameas,
     'escaped':          test_escaped
 }
index 6c3805a001bb3e7d5a820c41fd4cbb72e1db7aa2..3654fe79b00ba750e9993c545d4b609226f83b87 100644 (file)
@@ -460,7 +460,7 @@ class Markup(unicode):
         func.__doc__ = orig.__doc__
         return func
 
-    for method in '__getitem__', '__getslice__', 'capitalize', \
+    for method in '__getitem__', 'capitalize', \
                   'title', 'lower', 'upper', 'replace', 'ljust', \
                   'rjust', 'lstrip', 'rstrip', 'center', 'strip', \
                   'translate', 'expandtabs', 'swapcase', 'zfill':
@@ -475,6 +475,10 @@ class Markup(unicode):
     if hasattr(unicode, 'format'):
         format = make_wrapper('format')
 
+    # not in python 3
+    if hasattr(unicode, '__getslice__'):
+        __getslice__ = make_wrapper('__getslice__')
+
     del method, make_wrapper