Skip to content

Commit

Permalink
python 3 port: manual fixes, remove 2to3 from setup.py, remove fixers
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed May 17, 2013
1 parent e000355 commit 7d29562
Show file tree
Hide file tree
Showing 28 changed files with 235 additions and 269 deletions.
Empty file removed custom_fixers/__init__.py
Empty file.
13 changes: 0 additions & 13 deletions custom_fixers/fix_alt_unicode.py

This file was deleted.

21 changes: 0 additions & 21 deletions custom_fixers/fix_broken_reraising.py

This file was deleted.

11 changes: 0 additions & 11 deletions custom_fixers/fix_xrange2.py

This file was deleted.

50 changes: 26 additions & 24 deletions jinja2/_markupsafe/__init__.py
Expand Up @@ -9,11 +9,13 @@
:license: BSD, see LICENSE for more details.
"""
import re
from itertools import imap
import six
from six.moves import map
from six.moves import zip

try:
unichr = unichr # py2
except NameError:
unichr = chr # py3

__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent']

Expand All @@ -22,7 +24,7 @@
_entity_re = re.compile(r'&([^;]+);')


class Markup(unicode):
class Markup(six.text_type):
r"""Marks a string as being safe for inclusion in HTML/XML output without
needing to be escaped. This implements the `__html__` interface a couple
of frameworks and web applications use. :class:`Markup` is a direct
Expand Down Expand Up @@ -71,56 +73,56 @@ def __new__(cls, base=u'', encoding=None, errors='strict'):
if hasattr(base, '__html__'):
base = base.__html__()
if encoding is None:
return unicode.__new__(cls, base)
return unicode.__new__(cls, base, encoding, errors)
return six.text_type.__new__(cls, base)
return six.text_type.__new__(cls, base, encoding, errors)

def __html__(self):
return self

def __add__(self, other):
if hasattr(other, '__html__') or isinstance(other, basestring):
if hasattr(other, '__html__') or isinstance(other, six.string_types):
return self.__class__(six.text_type(self) + six.text_type(escape(other)))
return NotImplemented

def __radd__(self, other):
if hasattr(other, '__html__') or isinstance(other, basestring):
if hasattr(other, '__html__') or isinstance(other, six.string_types):
return self.__class__(six.text_type(escape(other)) + six.text_type(self))
return NotImplemented

def __mul__(self, num):
if isinstance(num, (int, long)):
return self.__class__(unicode.__mul__(self, num))
return self.__class__(six.text_type.__mul__(self, num))
return NotImplemented
__rmul__ = __mul__

def __mod__(self, arg):
if isinstance(arg, tuple):
arg = tuple(imap(_MarkupEscapeHelper, arg))
arg = tuple(map(_MarkupEscapeHelper, arg))
else:
arg = _MarkupEscapeHelper(arg)
return self.__class__(unicode.__mod__(self, arg))
return self.__class__(six.text_type.__mod__(self, arg))

def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
unicode.__repr__(self)
six.text_type.__repr__(self)
)

def join(self, seq):
return self.__class__(unicode.join(self, imap(escape, seq)))
join.__doc__ = unicode.join.__doc__
return self.__class__(six.text_type.join(self, map(escape, seq)))
join.__doc__ = six.text_type.join.__doc__

def split(self, *args, **kwargs):
return map(self.__class__, unicode.split(self, *args, **kwargs))
split.__doc__ = unicode.split.__doc__
return map(self.__class__, six.text_type.split(self, *args, **kwargs))
split.__doc__ = six.text_type.split.__doc__

def rsplit(self, *args, **kwargs):
return map(self.__class__, unicode.rsplit(self, *args, **kwargs))
rsplit.__doc__ = unicode.rsplit.__doc__
return map(self.__class__, six.text_type.rsplit(self, *args, **kwargs))
rsplit.__doc__ = six.text_type.rsplit.__doc__

def splitlines(self, *args, **kwargs):
return map(self.__class__, unicode.splitlines(self, *args, **kwargs))
splitlines.__doc__ = unicode.splitlines.__doc__
return map(self.__class__, six.text_type.splitlines(self, *args, **kwargs))
splitlines.__doc__ = six.text_type.splitlines.__doc__

def unescape(self):
r"""Unescape markup again into an unicode string. This also resolves
Expand Down Expand Up @@ -167,7 +169,7 @@ def escape(cls, s):
return rv

def make_wrapper(name):
orig = getattr(unicode, name)
orig = getattr(six.text_type, name)
def func(self, *args, **kwargs):
args = _escape_argspec(list(args), enumerate(args))
_escape_argspec(kwargs, six.iteritems(kwargs))
Expand All @@ -183,16 +185,16 @@ def func(self, *args, **kwargs):
locals()[method] = make_wrapper(method)

# new in python 2.5
if hasattr(unicode, 'partition'):
if hasattr(six.text_type, 'partition'):
partition = make_wrapper('partition'),
rpartition = make_wrapper('rpartition')

# new in python 2.6
if hasattr(unicode, 'format'):
if hasattr(six.text_type, 'format'):
format = make_wrapper('format')

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

del method, make_wrapper
Expand All @@ -201,7 +203,7 @@ def func(self, *args, **kwargs):
def _escape_argspec(obj, iterable):
"""Helper for various string-wrapped functions."""
for key, value in iterable:
if hasattr(value, '__html__') or isinstance(value, basestring):
if hasattr(value, '__html__') or isinstance(value, six.string_types):
obj[key] = escape(value)
return obj

Expand Down
2 changes: 1 addition & 1 deletion jinja2/_markupsafe/_native.py
Expand Up @@ -41,6 +41,6 @@ def soft_unicode(s):
"""Make a string unicode if it isn't already. That way a markup
string is not converted back to unicode.
"""
if not isinstance(s, unicode):
if not isinstance(s, six.text_type):
s = six.text_type(s)
return s
2 changes: 1 addition & 1 deletion jinja2/bccache.py
Expand Up @@ -18,7 +18,7 @@
import sys
import marshal
import tempfile
import cPickle as pickle
from six.moves import cPickle as pickle
import fnmatch
try:
from hashlib import sha1
Expand Down
20 changes: 11 additions & 9 deletions jinja2/compiler.py
Expand Up @@ -8,17 +8,16 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
from cStringIO import StringIO
from itertools import chain
from copy import deepcopy
from jinja2 import nodes
from jinja2.nodes import EvalContext
from jinja2.visitor import NodeVisitor
from jinja2.exceptions import TemplateAssertionError
from jinja2.utils import Markup, concat, escape, is_python_keyword, next
from jinja2.utils import Markup, concat, escape, is_python_keyword
import six
from six.moves import map
from six.moves import zip
from six.moves import cStringIO as StringIO
from six.moves import map, zip


operators = {
Expand Down Expand Up @@ -72,8 +71,11 @@ def has_safe_repr(value):
"""Does the node have a safe representation?"""
if value is None or value is NotImplemented or value is Ellipsis:
return True
if isinstance(value, (bool, int, long, float, complex, basestring,
xrange, Markup)):
try:
range_type = xrange
except NameError:
range_type = range
if isinstance(value, (bool, int, float, complex, range_type, Markup) + six.string_types):
return True
if isinstance(value, (tuple, list, set, frozenset)):
for item in value:
Expand Down Expand Up @@ -933,7 +935,7 @@ def visit_Include(self, node, frame):

func_name = 'get_or_select_template'
if isinstance(node.template, nodes.Const):
if isinstance(node.template.value, basestring):
if isinstance(node.template.value, six.string_types):
func_name = 'get_template'
elif isinstance(node.template.value, (tuple, list)):
func_name = 'select_template'
Expand Down Expand Up @@ -1221,7 +1223,7 @@ def visit_Output(self, node, frame):
if self.environment.finalize:
finalize = lambda x: six.text_type(self.environment.finalize(x))
else:
finalize = unicode
finalize = six.text_type

# if we are inside a frame that requires output checking, we do so
outdent_later = False
Expand Down Expand Up @@ -1355,7 +1357,7 @@ def visit_Assign(self, node, frame):
public_names = [x for x in assignment_frame.toplevel_assignments
if not x.startswith('_')]
if len(assignment_frame.toplevel_assignments) == 1:
name = next(iter(assignment_frame.toplevel_assignments))
name = six.advance_iterator(iter(assignment_frame.toplevel_assignments))
self.writeline('context.vars[%r] = l_%s' % (name, name))
else:
self.writeline('context.vars.update({')
Expand Down
2 changes: 1 addition & 1 deletion jinja2/debug.py
Expand Up @@ -190,7 +190,7 @@ def translate_exception(exc_info, initial_skip=0):
# reraise it unchanged.
# XXX: can we backup here? when could this happen?
if not frames:
raise exc_info[0], exc_info[1], exc_info[2]
six.reraise(exc_info[0], exc_info[1], exc_info[2])

return ProcessedTraceback(exc_info[0], exc_info[1], frames)

Expand Down
1 change: 1 addition & 0 deletions jinja2/defaults.py
Expand Up @@ -8,6 +8,7 @@
:copyright: (c) 2010 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
from six.moves import xrange
from jinja2.utils import generate_lorem_ipsum, Cycler, Joiner


Expand Down
31 changes: 13 additions & 18 deletions jinja2/environment.py
Expand Up @@ -23,9 +23,7 @@
concat, consume, internalcode, _encode_filename
import six
from functools import reduce
from six.moves import filter
from six.moves import map
from six.moves import zip
from six.moves import filter, map, zip


# for direct template usage we have up to ten living environments
Expand Down Expand Up @@ -76,7 +74,7 @@ def load_extensions(environment, extensions):
"""
result = {}
for extension in extensions:
if isinstance(extension, basestring):
if isinstance(extension, six.string_types):
extension = import_string(extension)
result[extension.identifier] = extension(environment)
return result
Expand Down Expand Up @@ -357,7 +355,7 @@ def getitem(self, obj, argument):
try:
return obj[argument]
except (TypeError, LookupError):
if isinstance(argument, basestring):
if isinstance(argument, six.string_types):
try:
attr = str(argument)
except Exception:
Expand Down Expand Up @@ -479,7 +477,7 @@ def compile(self, source, name=None, filename=None, raw=False,
"""
source_hint = None
try:
if isinstance(source, basestring):
if isinstance(source, six.string_types):
source_hint = source
source = self._parse(source, name, filename)
if self.optimized:
Expand Down Expand Up @@ -676,7 +674,7 @@ def handle_exception(self, exc_info=None, rendered=False, source_hint=None):
if self.exception_handler is not None:
self.exception_handler(traceback)
exc_type, exc_value, tb = traceback.standard_exc_info
raise exc_type, exc_value, tb
six.reraise(exc_type, exc_value, tb)

def join_path(self, template, parent):
"""Join a template with the parent. By default all the lookups are
Expand Down Expand Up @@ -763,7 +761,7 @@ def get_or_select_template(self, template_name_or_list,
.. versionadded:: 2.3
"""
if isinstance(template_name_or_list, basestring):
if isinstance(template_name_or_list, six.string_types):
return self.get_template(template_name_or_list, parent, globals)
elif isinstance(template_name_or_list, Template):
return template_name_or_list
Expand Down Expand Up @@ -1008,12 +1006,9 @@ def __html__(self):
return Markup(concat(self._body_stream))

def __str__(self):
return six.text_type(self).encode('utf-8')
s = self.__unicode__()
return s if six.PY3 else s.encode('utf-8')

# unicode goes after __str__ because we configured 2to3 to rename
# __unicode__ to __str__. because the 2to3 tree is not designed to
# remove nodes from it, we leave the above __str__ around and let
# it override at runtime.
def __unicode__(self):
return concat(self._body_stream)

Expand Down Expand Up @@ -1044,7 +1039,7 @@ def __call__(self, *args, **kwargs):
return rv


class TemplateStream(object):
class TemplateStream(six.Iterator):
"""A template stream works pretty much like an ordinary python generator
but it can buffer multiple items to reduce the number of total iterations.
Per default the output is unbuffered which means that for every unbuffered
Expand All @@ -1069,7 +1064,7 @@ def dump(self, fp, encoding=None, errors='strict'):
Template('Hello {{ name }}!').stream(name='foo').dump('hello.html')
"""
close = False
if isinstance(fp, basestring):
if isinstance(fp, six.string_types):
fp = file(fp, 'w')
close = True
try:
Expand All @@ -1088,7 +1083,7 @@ def dump(self, fp, encoding=None, errors='strict'):

def disable_buffering(self):
"""Disable the output buffering."""
self._next = self._gen.next
self._next = lambda: six.next(self._gen)
self.buffered = False

def enable_buffering(self, size=5):
Expand Down Expand Up @@ -1116,12 +1111,12 @@ def generator(next):
c_size = 0

self.buffered = True
self._next = generator(self._gen.next).next
self._next = lambda: six.next(generator(lambda: six.next(self._gen)))

def __iter__(self):
return self

def next(self):
def __next__(self):
return self._next()


Expand Down

0 comments on commit 7d29562

Please sign in to comment.