summaryrefslogtreecommitdiff
path: root/simplejson
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2017-11-23 13:52:00 -0800
committerBob Ippolito <bob@redivi.com>2017-11-23 13:52:00 -0800
commit47bb9950941aed34aef4fcad3fd20a2585c696f2 (patch)
treedda702219b4e599c445aa74c1074cb237ff9d409 /simplejson
parent0fd73bc8c6ad200754cce837ba8c97715e95ee25 (diff)
downloadsimplejson-47bb9950941aed34aef4fcad3fd20a2585c696f2.tar.gz
Ensure that encoding text subtypes is consistent with or without speedups. Fixes #185
Diffstat (limited to 'simplejson')
-rw-r--r--simplejson/__init__.py2
-rw-r--r--simplejson/encoder.py8
-rw-r--r--simplejson/tests/test_dump.py14
3 files changed, 18 insertions, 6 deletions
diff --git a/simplejson/__init__.py b/simplejson/__init__.py
index ca8e7b5..8a67437 100644
--- a/simplejson/__init__.py
+++ b/simplejson/__init__.py
@@ -98,7 +98,7 @@ Using simplejson.tool from the shell to validate and pretty-print::
Expecting property name: line 1 column 3 (char 2)
"""
from __future__ import absolute_import
-__version__ = '3.12.2'
+__version__ = '3.13.0'
__all__ = [
'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index 25ddf25..d3ea3d6 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -48,12 +48,12 @@ def encode_basestring(s, _PY3=PY3, _q=u('"')):
if isinstance(s, binary_type):
s = s.decode('utf-8')
if type(s) is not text_type:
- s = text_type(s)
+ s = text_type.__str__(s)
else:
if isinstance(s, str) and HAS_UTF8.search(s) is not None:
s = s.decode('utf-8')
if type(s) not in string_types:
- s = text_type(s)
+ s = text_type.__str__(s)
def replace(match):
return ESCAPE_DCT[match.group(0)]
return _q + ESCAPE.sub(replace, s) + _q
@@ -67,12 +67,12 @@ def py_encode_basestring_ascii(s, _PY3=PY3):
if isinstance(s, binary_type):
s = s.decode('utf-8')
if type(s) is not text_type:
- s = text_type(s)
+ s = text_type.__str__(s)
else:
if isinstance(s, str) and HAS_UTF8.search(s) is not None:
s = s.decode('utf-8')
if type(s) not in string_types:
- s = text_type(s)
+ s = text_type.__str__(s)
def replace(match):
s = match.group(0)
try:
diff --git a/simplejson/tests/test_dump.py b/simplejson/tests/test_dump.py
index 3661de0..f5f4c1f 100644
--- a/simplejson/tests/test_dump.py
+++ b/simplejson/tests/test_dump.py
@@ -1,7 +1,11 @@
from unittest import TestCase
-from simplejson.compat import StringIO, long_type, b, binary_type, PY3
+from simplejson.compat import StringIO, long_type, b, binary_type, text_type, PY3
import simplejson as json
+class MisbehavingTextSubtype(text_type):
+ def __str__(self):
+ return "FAIL!"
+
def as_text_type(s):
if PY3 and isinstance(s, binary_type):
return s.decode('ascii')
@@ -128,3 +132,11 @@ class TestDump(TestCase):
json.dump(p, sio, sort_keys=True)
self.assertEqual(sio.getvalue(), json.dumps(p, sort_keys=True))
self.assertEqual(json.loads(sio.getvalue()), p)
+
+ def test_misbehaving_text_subtype(self):
+ # https://github.com/simplejson/simplejson/issues/185
+ text = "this is some text"
+ self.assertEqual(
+ json.dumps(MisbehavingTextSubtype(text)),
+ json.dumps(text)
+ )