Skip to content

Commit

Permalink
Started to work on Python 3 support.
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
mitsuhiko committed Aug 5, 2009
1 parent c421efe commit 42a1988
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions .hgignore
Expand Up @@ -6,3 +6,4 @@
\.py[co]$
\.DS_Store$
^env/
^py3k/
7 changes: 7 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion jinja2/bccache.py
Expand Up @@ -28,7 +28,7 @@


bc_version = 1
bc_magic = 'j2' + pickle.dumps(bc_version, 2)
bc_magic = 'j2'.encode('ascii') + pickle.dumps(bc_version, 2)


class Bucket(object):
Expand Down
8 changes: 7 additions & 1 deletion jinja2/defaults.py
Expand Up @@ -24,11 +24,17 @@
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,
Expand Down
3 changes: 1 addition & 2 deletions jinja2/lexer.py
Expand Up @@ -253,10 +253,9 @@ def __iter__(self):
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."""
Expand Down
9 changes: 6 additions & 3 deletions jinja2/runtime.py
Expand Up @@ -188,9 +188,12 @@ def _all(meth):
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):
Expand Down
9 changes: 8 additions & 1 deletion jinja2/tests.py
Expand Up @@ -16,6 +16,13 @@
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
Expand Down Expand Up @@ -130,7 +137,7 @@ def test_escaped(value):
'number': test_number,
'sequence': test_sequence,
'iterable': test_iterable,
'callable': callable,
'callable': test_callable,
'sameas': test_sameas,
'escaped': test_escaped
}
6 changes: 5 additions & 1 deletion jinja2/utils.py
Expand Up @@ -460,7 +460,7 @@ def func(self, *args, **kwargs):
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':
Expand All @@ -475,6 +475,10 @@ def func(self, *args, **kwargs):
if hasattr(unicode, 'format'):
format = make_wrapper('format')

# not in python 3
if hasattr(unicode, '__getslice__'):
__getslice__ = make_wrapper('__getslice__')

del method, make_wrapper


Expand Down

0 comments on commit 42a1988

Please sign in to comment.