# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b
            ("<>&\"", "&lt;&gt;&amp;&quot;"),
            ("&amp;", "&amp;amp;"),
            ]
        for unescaped, escaped in tests:
            self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped))
            self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))

    def test_url_escape(self):
        tests = [
            # byte strings are passed through as-is
            (u'\u00e9'.encode('utf8'), '%C3%A9'),
            (u'\u00e9'.encode('latin1'), '%E9'),

            # unicode strings become utf8
            (u'\u00e9', '%C3%A9'),
            ]
        for unescaped, escaped in tests:
            self.assertEqual(url_escape(unescaped), escaped)

    def test_url_unescape(self):
        tests = [
            ('%C3%A9', u'\u00e9', 'utf8'),
            ('%C3%A9', u'\u00c3\u00a9', 'latin1'),
            ('%C3%A9', utf8(u'\u00e9'), None),
            ]
        for escaped, unescaped, encoding in tests:
            # input strings to url_unescape should only contain ascii
            # characters, but make sure the function accepts both byte
            # and unicode strings.
            self.assertEqual(url_unescape(to_unicode(escaped), encoding), unescaped)

# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


# Path: tornado/test/escape_test.py
import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b

            ("<>&\"", "&lt;&gt;&amp;&quot;"),
            ("&amp;", "&amp;amp;"),
            ]
        for unescaped, escaped in tests:
            self.assertEqual(utf8(xhtml_escape(unescaped)), utf8(escaped))
            self.assertEqual(utf8(unescaped), utf8(xhtml_unescape(escaped)))

    def test_url_escape(self):
        tests = [
            # byte strings are passed through as-is
            (u'\u00e9'.encode('utf8'), '%C3%A9'),
            (u'\u00e9'.encode('latin1'), '%E9'),

            # unicode strings become utf8
            (u'\u00e9', '%C3%A9'),
            ]
        for unescaped, escaped in tests:
            self.assertEqual(url_escape(unescaped), escaped)

    def test_url_unescape(self):
        tests = [
            ('%C3%A9', u'\u00e9', 'utf8'),
            ('%C3%A9', u'\u00c3\u00a9', 'latin1'),
            ('%C3%A9', utf8(u'\u00e9'), None),
            ]
        for escaped, unescaped, encoding in tests:
            # input strings to url_unescape should only contain ascii
            # characters, but make sure the function accepts both byte
            # and unicode strings.
            self.assertEqual(url_unescape(to_unicode(escaped), encoding), unescaped)

            self.assertEqual(url_unescape(utf8(escaped), encoding), unescaped)