Skip to content

Commit

Permalink
Approaching python3 compatibility
Browse files Browse the repository at this point in the history
--HG--
branch : trunk
  • Loading branch information
mitsuhiko committed Feb 9, 2010
1 parent b36ddc9 commit 790b8a8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 31 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ recursive-include tests *
recursive-include ext *
recursive-include artwork *
recursive-include examples *
recursive-include jinja2/testsuite/res *
recursive-exclude docs/_build/doctrees *
15 changes: 2 additions & 13 deletions custom_fixers/fix_alt_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,10 @@

class FixAltUnicode(fixer_base.BaseFix):
PATTERN = """
func=funcdef< 'def' name=NAME
func=funcdef< 'def' name='__unicode__'
parameters< '(' NAME ')' > any+ >
"""

run_order = 5

def transform(self, node, results):
name = results['name']

# rename __unicode__ to __str__
if name.value == '__unicode__':
name.replace(Name('__str__', prefix=name.prefix))

# get rid of other __str__'s
elif name.value == '__str__':
next = BlankLine()
next.prefix = results['func'].prefix
return next
name.replace(Name('__str__', prefix=name.prefix))
11 changes: 11 additions & 0 deletions custom_fixers/fix_xrange2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, BlankLine


# whyever this is necessary..

class FixXrange2(fixer_base.BaseFix):
PATTERN = "'xrange'"

def transform(self, node, results):
node.replace(Name('range', prefix=node.prefix))
6 changes: 3 additions & 3 deletions jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,15 +808,15 @@ def __init__(self, template, context):
self.__dict__.update(context.get_exported())
self.__name__ = template.name

def __unicode__(self):
return concat(self._body_stream)

def __html__(self):
return Markup(concat(self._body_stream))

def __str__(self):
return unicode(self).encode('utf-8')

def __unicode__(self):
return concat(self._body_stream)

def __repr__(self):
if self.__name__ is None:
name = 'memory:%x' % id(self)
Expand Down
12 changes: 6 additions & 6 deletions jinja2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def __init__(self, name, message=None):
self.name = name
self.templates = [name]

def __unicode__(self):
return self.message

def __str__(self):
return self.message.encode('utf-8')

def __unicode__(self):
return self.message


class TemplatesNotFound(TemplateNotFound):
"""Like :class:`TemplateNotFound` but raised if multiple templates
Expand Down Expand Up @@ -78,6 +78,9 @@ def __init__(self, message, lineno, name=None, filename=None):
# function translated the syntax error into a new traceback
self.translated = False

def __str__(self):
return unicode(self).encode('utf-8')

def __unicode__(self):
# for translated errors we only return the message
if self.translated:
Expand All @@ -101,9 +104,6 @@ def __unicode__(self):

return u'\n'.join(lines)

def __str__(self):
return unicode(self).encode('utf-8')


class TemplateAssertionError(TemplateSyntaxError):
"""Like a template syntax error, but covers cases where something in the
Expand Down
18 changes: 13 additions & 5 deletions jinja2/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@
warnings.filterwarnings('ignore', 'the sets module', DeprecationWarning,
module='jinja2.sandbox')


from collections import deque
from UserDict import UserDict, DictMixin
from UserList import UserList

_mutable_set_types = (set,)
_mutable_mapping_types = (UserDict, DictMixin, dict)
_mutable_sequence_types = (UserList, list)
_mutable_mapping_types = (dict,)
_mutable_sequence_types = (list,)


# on python 2.x we can register the user collection types
try:
from UserDict import UserDict, DictMixin
from UserList import UserList
_mutable_mapping_types += (UserDict, DictMixin)
_mutable_set_types += (UserList,)
except ImportError:
pass

# if sets is still available, register the mutable set from there as well
try:
Expand Down
9 changes: 7 additions & 2 deletions jinja2/testsuite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
:license: BSD, see LICENSE for more details.
"""
import os
import sys
import re
import sys
import unittest
from traceback import format_exception
from jinja2 import loaders
Expand Down Expand Up @@ -74,5 +74,10 @@ def suite():
suite.addTest(regression.suite())
suite.addTest(debug.suite())
suite.addTest(utils.suite())
suite.addTest(doctests.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 < (3, 0):
suite.addTest(doctests.suite())

return suite
4 changes: 2 additions & 2 deletions jinja2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def open_if_exists(filename, mode='rb'):
otherwise `None`.
"""
try:
return file(filename, mode)
return open(filename, mode)
except IOError, e:
if e.errno not in (errno.ENOENT, errno.EISDIR):
raise
Expand Down Expand Up @@ -506,8 +506,8 @@ def __init__(self, obj):
self.obj = obj

__getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x])
__unicode__ = lambda s: unicode(escape(s.obj))
__str__ = lambda s: str(escape(s.obj))
__unicode__ = lambda s: unicode(escape(s.obj))
__repr__ = lambda s: str(escape(repr(s.obj)))
__int__ = lambda s: int(s.obj)
__float__ = lambda s: float(s.obj)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
},
extras_require={'i18n': ['Babel>=0.8']},
test_suite='jinja2.testsuite.suite',
include_package_data=True,
entry_points="""
[babel.extractors]
jinja2 = jinja2.ext:babel_extract[i18n]
Expand Down

0 comments on commit 790b8a8

Please sign in to comment.