Approaching python3 compatibility
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent b36ddc9 · Browse files · 9 files·+46 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+46 added−31 removed
- other:+46 added−31 removed
Provenance
- Commit
790b8a8- Tree
f2bf66a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b36ddc9
9 files changed+46 added−31 removed
modified:custom_fixers/fix_alt_unicode.py+2 added-13 removed
2 unmodified lines3 4 5 class FixAltUnicode(fixer_base.BaseFix):6 PATTERN = """7- func=funcdef< 'def' name=NAME7+ func=funcdef< 'def' name='__unicode__'8 parameters< '(' NAME ')' > any+ >9 """10 11- run_order = 512-11 def transform(self, node, results):12 name = results['name']15-16- # rename __unicode__ to __str__17- if name.value == '__unicode__':18- name.replace(Name('__str__', prefix=name.prefix))13+ name.replace(Name('__str__', prefix=name.prefix))19-20- # get rid of other __str__'s21- elif name.value == '__str__':22- next = BlankLine()23- next.prefix = results['func'].prefix24- return nextadded:custom_fixers/fix_xrange2.py+11 added
1+from lib2to3 import fixer_base2+from lib2to3.fixer_util import Name, BlankLine3+4+5+# whyever this is necessary..6+7+class FixXrange2(fixer_base.BaseFix):8+ PATTERN = "'xrange'"9+10+ def transform(self, node, results):11+ node.replace(Name('range', prefix=node.prefix))modified:jinja2/testsuite/__init__.py+7 added-2 removed
9 unmodified lines10 :copyright: (c) 2010 by the Jinja Team.11 :license: BSD, see LICENSE for more details.12 """13 import os14-import sys14 import re15+import sys16 import unittest17 from traceback import format_exception18 from jinja2 import loaders19 53 unmodified lines73 suite.addTest(api.suite())74 suite.addTest(regression.suite())75 suite.addTest(debug.suite())76 suite.addTest(utils.suite())77+78+ # doctests will not run on python 3 currently. Too many issues79+ # with that, do not test that on that platform.80+ if sys.version_info < (3, 0):77- suite.addTest(doctests.suite())81+ suite.addTest(doctests.suite())82+83 return suitemodified:jinja2/environment.py+3 added-3 removed
806 unmodified lines807 self._body_stream = list(template.root_render_func(context))808 self.__dict__.update(context.get_exported())809 self.__name__ = template.name810 811- def __unicode__(self):812- return concat(self._body_stream)813-811 def __html__(self):812 return Markup(concat(self._body_stream))813 814 def __str__(self):815 return unicode(self).encode('utf-8')816 817+ def __unicode__(self):818+ return concat(self._body_stream)819+820 def __repr__(self):821 if self.__name__ is None:822 name = 'memory:%x' % id(self)823 else:107 unmodified linesmodified:jinja2/exceptions.py+6 added-6 removed
39 unmodified lines40 self.message = message41 self.name = name42 self.templates = [name]43 44- def __unicode__(self):45- return self.message46-44 def __str__(self):45 return self.message.encode('utf-8')46 47+ def __unicode__(self):48+ return self.message49 50+51 class TemplatesNotFound(TemplateNotFound):52 """Like :class:`TemplateNotFound` but raised if multiple templates53 are selected. This is a subclass of :class:`TemplateNotFound`54 exception, so just catching the base exception will catch both.22 unmodified lines77 # this is set to True if the debug.translate_syntax_error78 # function translated the syntax error into a new traceback79 self.translated = False80 81+ def __str__(self):82+ return unicode(self).encode('utf-8')83+84 def __unicode__(self):85 # for translated errors we only return the message86 if self.translated:87 return self.message.encode('utf-8')15 unmodified lines103 lines.append(' ' + line.strip())104 105 return u'\n'.join(lines)106 104- def __str__(self):105- return unicode(self).encode('utf-8')107 107-108 class TemplateAssertionError(TemplateSyntaxError):109 """Like a template syntax error, but covers cases where something in the110 template caused an error at compile time that wasn't necessarily caused111 by a syntax error. However it's a direct subclass of24 unmodified linesmodified:jinja2/sandbox.py+13 added-5 removed
35 unmodified lines36 # make sure we don't warn in python 2.6 about stuff we don't care about37 warnings.filterwarnings('ignore', 'the sets module', DeprecationWarning,38 module='jinja2.sandbox')39 40-40 from collections import deque42-from UserDict import UserDict, DictMixin43-from UserList import UserList41+42 _mutable_set_types = (set,)45-_mutable_mapping_types = (UserDict, DictMixin, dict)46-_mutable_sequence_types = (UserList, list)43+_mutable_mapping_types = (dict,)44+_mutable_sequence_types = (list,)45 46+47+# on python 2.x we can register the user collection types48+try:49+ from UserDict import UserDict, DictMixin50+ from UserList import UserList51+ _mutable_mapping_types += (UserDict, DictMixin)52+ _mutable_set_types += (UserList,)53+except ImportError:54+ pass55+56 # if sets is still available, register the mutable set from there as well57 try:58 from sets import Set59 _mutable_set_types += (Set,)212 unmodified linesmodified:jinja2/utils.py+2 added-2 removed
201 unmodified lines202 """Returns a file descriptor for the filename if that file exists,203 otherwise `None`.204 """205 try:206- return file(filename, mode)206+ return open(filename, mode)207 except IOError, e:208 if e.errno not in (errno.ENOENT, errno.EISDIR):209 raise210 294 unmodified lines505 def __init__(self, obj):506 self.obj = obj507 508 __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x])509- __unicode__ = lambda s: unicode(escape(s.obj))509 __str__ = lambda s: str(escape(s.obj))510+ __unicode__ = lambda s: unicode(escape(s.obj))511 __repr__ = lambda s: str(escape(repr(s.obj)))512 __int__ = lambda s: int(s.obj)513 __float__ = lambda s: float(s.obj)514 270 unmodified linesmodified:MANIFEST.in+1 added
2 unmodified lines3 recursive-include tests *4 recursive-include ext *5 recursive-include artwork *6 recursive-include examples *7+recursive-include jinja2/testsuite/res *8 recursive-exclude docs/_build/doctrees *modified:setup.py+1 added
85 unmodified lines86 )87 },88 extras_require={'i18n': ['Babel>=0.8']},89 test_suite='jinja2.testsuite.suite',90+ include_package_data=True,91 entry_points="""92 [babel.extractors]93 jinja2 = jinja2.ext:babel_extract[i18n]94 """,2 unmodified lines