From 42a198801392e81a1632c9e3914759e6862f5e82 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 5 Aug 2009 18:45:39 +0200 Subject: [PATCH] Started to work on Python 3 support. --HG-- branch : trunk --- .hgignore | 1 + Makefile | 7 +++++++ jinja2/bccache.py | 2 +- jinja2/defaults.py | 8 +++++++- jinja2/lexer.py | 3 +-- jinja2/runtime.py | 9 ++++++--- jinja2/tests.py | 9 ++++++++- jinja2/utils.py | 6 +++++- 8 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.hgignore b/.hgignore index eebc869..7a1b935 100644 --- a/.hgignore +++ b/.hgignore @@ -6,3 +6,4 @@ \.py[co]$ \.DS_Store$ ^env/ +^py3k/ diff --git a/Makefile b/Makefile index cfc315c..6cd734a 100644 --- 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 diff --git a/jinja2/bccache.py b/jinja2/bccache.py index 2786082..6415158 100644 --- a/jinja2/bccache.py +++ b/jinja2/bccache.py @@ -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): diff --git a/jinja2/defaults.py b/jinja2/defaults.py index 458485e..13e303b 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -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, diff --git a/jinja2/lexer.py b/jinja2/lexer.py index 919cba8..335e9ca 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -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.""" diff --git a/jinja2/runtime.py b/jinja2/runtime.py index 669ff21..5fe2cd6 100644 --- a/jinja2/runtime.py +++ b/jinja2/runtime.py @@ -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): diff --git a/jinja2/tests.py b/jinja2/tests.py index 58dc0c0..9fbf163 100644 --- a/jinja2/tests.py +++ b/jinja2/tests.py @@ -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 } diff --git a/jinja2/utils.py b/jinja2/utils.py index 6c3805a..3654fe7 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -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 -- 2.26.2