summaryrefslogtreecommitdiff
path: root/markupsafe/__init__.py
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 19:11:32 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2013-05-20 19:11:32 +0100
commit08c34a3315ec94b237100dd42d4ddd7f406942d9 (patch)
treef58f4db3470f70d6a9a598927c5731119b3de592 /markupsafe/__init__.py
parent7415f6f8816e8eae1e34db76504062e98e5e6ab0 (diff)
downloadmarkupsafe-08c34a3315ec94b237100dd42d4ddd7f406942d9.tar.gz
Switch to native port and remove 2to3
Diffstat (limited to 'markupsafe/__init__.py')
-rw-r--r--markupsafe/__init__.py60
1 files changed, 31 insertions, 29 deletions
diff --git a/markupsafe/__init__.py b/markupsafe/__init__.py
index 480252c..6a8dc5e 100644
--- a/markupsafe/__init__.py
+++ b/markupsafe/__init__.py
@@ -9,7 +9,7 @@
:license: BSD, see LICENSE for more details.
"""
import re
-from itertools import imap
+from markupsafe._compat import text_type, string_types, imap, unichr, PY2
__all__ = ['Markup', 'soft_unicode', 'escape', 'escape_silent']
@@ -19,7 +19,7 @@ _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
_entity_re = re.compile(r'&([^;]+);')
-class Markup(unicode):
+class Markup(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
@@ -68,25 +68,25 @@ class Markup(unicode):
if hasattr(base, '__html__'):
base = base.__html__()
if encoding is None:
- return unicode.__new__(cls, base)
- return unicode.__new__(cls, base, encoding, errors)
+ return text_type.__new__(cls, base)
+ return text_type.__new__(cls, base, encoding, errors)
def __html__(self):
return self
def __add__(self, other):
- if isinstance(other, basestring) or hasattr(other, '__html__'):
+ if isinstance(other, string_types) or hasattr(other, '__html__'):
return self.__class__(super(Markup, self).__add__(self.escape(other)))
return NotImplemented
def __radd__(self, other):
- if hasattr(other, '__html__') or isinstance(other, basestring):
+ if hasattr(other, '__html__') or isinstance(other, string_types):
return self.escape(other).__add__(self)
return NotImplemented
def __mul__(self, num):
if isinstance(num, (int, long)):
- return self.__class__(unicode.__mul__(self, num))
+ return self.__class__(text_type.__mul__(self, num))
return NotImplemented
__rmul__ = __mul__
@@ -95,32 +95,32 @@ class Markup(unicode):
arg = tuple(imap(_MarkupEscapeHelper, arg, self.escape))
else:
arg = _MarkupEscapeHelper(arg, self.escape)
- return self.__class__(unicode.__mod__(self, arg))
+ return self.__class__(text_type.__mod__(self, arg))
def __repr__(self):
return '%s(%s)' % (
self.__class__.__name__,
- unicode.__repr__(self)
+ text_type.__repr__(self)
)
def join(self, seq):
- return self.__class__(unicode.join(self, imap(self.escape, seq)))
- join.__doc__ = unicode.join.__doc__
+ return self.__class__(text_type.join(self, imap(self.escape, seq)))
+ join.__doc__ = 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__, text_type.split(self, *args, **kwargs))
+ split.__doc__ = 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__, text_type.rsplit(self, *args, **kwargs))
+ rsplit.__doc__ = 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__, text_type.splitlines(self, *args, **kwargs))
+ splitlines.__doc__ = text_type.splitlines.__doc__
def unescape(self):
- r"""Unescape markup again into an unicode string. This also resolves
+ r"""Unescape markup again into an text_type string. This also resolves
known HTML4 and XHTML entities:
>>> Markup("Main &raquo; <em>About</em>").unescape()
@@ -139,10 +139,10 @@ class Markup(unicode):
except ValueError:
pass
return u''
- return _entity_re.sub(handle_match, unicode(self))
+ return _entity_re.sub(handle_match, text_type(self))
def striptags(self):
- r"""Unescape markup into an unicode string and strip all tags. This
+ r"""Unescape markup into an text_type string and strip all tags. This
also resolves known HTML4 and XHTML entities. Whitespace is
normalized to one:
@@ -164,7 +164,7 @@ class Markup(unicode):
return rv
def make_wrapper(name):
- orig = getattr(unicode, name)
+ orig = getattr(text_type, name)
def func(self, *args, **kwargs):
args = _escape_argspec(list(args), enumerate(args), self.escape)
#_escape_argspec(kwargs, kwargs.iteritems(), None)
@@ -180,20 +180,20 @@ class Markup(unicode):
locals()[method] = make_wrapper(method)
# new in python 2.5
- if hasattr(unicode, 'partition'):
+ if hasattr(text_type, 'partition'):
def partition(self, sep):
return tuple(map(self.__class__,
- unicode.partition(self, self.escape(sep))))
+ text_type.partition(self, self.escape(sep))))
def rpartition(self, sep):
return tuple(map(self.__class__,
- unicode.rpartition(self, self.escape(sep))))
+ text_type.rpartition(self, self.escape(sep))))
# new in python 2.6
- if hasattr(unicode, 'format'):
+ if hasattr(text_type, 'format'):
format = make_wrapper('format')
# not in python 3
- if hasattr(unicode, '__getslice__'):
+ if hasattr(text_type, '__getslice__'):
__getslice__ = make_wrapper('__getslice__')
del method, make_wrapper
@@ -202,7 +202,7 @@ class Markup(unicode):
def _escape_argspec(obj, iterable, escape):
"""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, string_types):
obj[key] = escape(value)
return obj
@@ -215,7 +215,7 @@ class _MarkupEscapeHelper(object):
self.escape = escape
__getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x], s.escape)
- __unicode__ = lambda s: unicode(s.escape(s.obj))
+ __unicode__ = __str__ = lambda s: text_type(s.escape(s.obj))
__repr__ = lambda s: str(s.escape(repr(s.obj)))
__int__ = lambda s: int(s.obj)
__float__ = lambda s: float(s.obj)
@@ -228,4 +228,6 @@ try:
except ImportError:
from markupsafe._native import escape, escape_silent, soft_unicode
-# vim:sts=4:sw=4:et:
+if not PY2:
+ soft_str = soft_unicode
+ __all__.append('soft_str')