]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
On python 3, json_encode no longer supports byte strings as input.
authorBen Darnell <ben@bendarnell.com>
Fri, 8 Mar 2013 22:03:38 +0000 (17:03 -0500)
committerBen Darnell <ben@bendarnell.com>
Fri, 8 Mar 2013 22:03:38 +0000 (17:03 -0500)
This matches the behavior of the underlying json library.
recursive_unicode was more expensive than the actual json encoding for
complex structures, and was useless on python 2.

tornado/escape.py
tornado/test/escape_test.py

index 6d72532d2f6562f5de4721e77cd7f0c0dfcf86d2..e565ff538745bed81f9b93d798ad87d1a1eb4c4c 100644 (file)
@@ -28,9 +28,9 @@ import sys
 from tornado.util import bytes_type, unicode_type, basestring_type, u
 
 try:
-    from urllib.parse import parse_qs  # py3
+    from urllib.parse import parse_qs as _parse_qs  # py3
 except ImportError:
-    from urlparse import parse_qs  # Python 2.6+
+    from urlparse import parse_qs as _parse_qs  # Python 2.6+
 
 try:
     import htmlentitydefs  # py2
@@ -72,7 +72,7 @@ def json_encode(value):
     # 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.dumps(recursive_unicode(value)).replace("</", "<\\/")
+    return json.dumps(value).replace("</", "<\\/")
 
 
 def json_decode(value):
@@ -106,7 +106,7 @@ if sys.version_info[0] < 3:
         else:
             return unicode_type(urllib_parse.unquote_plus(utf8(value)), encoding)
 
-    parse_qs_bytes = parse_qs
+    parse_qs_bytes = _parse_qs
 else:
     def url_unescape(value, encoding='utf-8'):
         """Decodes the given value from a URL.
@@ -131,8 +131,8 @@ else:
         """
         # This is gross, but python3 doesn't give us another way.
         # Latin1 is the universal donor of character encodings.
-        result = parse_qs(qs, keep_blank_values, strict_parsing,
-                          encoding='latin1', errors='strict')
+        result = _parse_qs(qs, keep_blank_values, strict_parsing,
+                           encoding='latin1', errors='strict')
         encoded = {}
         for k, v in result.items():
             encoded[k] = [i.encode('latin1') for i in v]
index 19f61cbeca2b7ce333219573e584c821cd177582..8b4522c0cc699ca8bf1230f1687470fac33a9ada 100644 (file)
@@ -5,7 +5,7 @@ from __future__ import absolute_import, division, print_function, with_statement
 import tornado.escape
 
 from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
-from tornado.util import u, unicode_type
+from tornado.util import u, unicode_type, bytes_type
 from tornado.test.util import unittest
 
 linkify_tests = [
@@ -192,8 +192,10 @@ class EscapeTestCase(unittest.TestCase):
         self.assertEqual(json_decode(utf8(u('"\u00e9"'))), u("\u00e9"))
 
     def test_json_encode(self):
-        # json deals with strings, not bytes, but our encoding function should
-        # accept bytes as well as long as they are utf8.
+        # json deals with strings, not bytes.  On python 2 byte strings will
+        # convert automatically if they are utf8; on python 3 byte strings
+        # are not allowed.
         self.assertEqual(json_decode(json_encode(u("\u00e9"))), u("\u00e9"))
-        self.assertEqual(json_decode(json_encode(utf8(u("\u00e9")))), u("\u00e9"))
-        self.assertRaises(UnicodeDecodeError, json_encode, b"\xe9")
+        if bytes_type is str:
+            self.assertEqual(json_decode(json_encode(utf8(u("\u00e9")))), u("\u00e9"))
+            self.assertRaises(UnicodeDecodeError, json_encode, b"\xe9")