Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

On python 3, json_encode no longer supports byte strings as input.

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.
  • Loading branch information...
commit 8a198cc40381f2b5f5806d3b6078462105a11384 1 parent e72eee1
@bdarnell bdarnell authored
Showing with 13 additions and 11 deletions.
  1. +6 −6 tornado/escape.py
  2. +7 −5 tornado/test/escape_test.py
View
12 tornado/escape.py
@@ -28,9 +28,9 @@
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 @@ def url_unescape(value, encoding='utf-8'):
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 @@ def parse_qs_bytes(qs, keep_blank_values=False, strict_parsing=False):
"""
# 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]
View
12 tornado/test/escape_test.py
@@ -5,7 +5,7 @@
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 @@ def test_json_decode(self):
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")
Please sign in to comment.
Something went wrong with that request. Please try again.