summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2016-10-21 14:54:32 +0700
committerBob Ippolito <bob@redivi.com>2016-10-21 14:54:32 +0700
commitd78256146d2831e3a7080b7212d7edb90bc2d611 (patch)
treeece8b9d9c5a6be0242c3ec1083c08297f6c75811
parent9902c546bcf2133b4961502d71bac908d55da40c (diff)
downloadsimplejson-d78256146d2831e3a7080b7212d7edb90bc2d611.tar.gz
Fixes #144. Workaround for bad behavior in string subclasses.
-rw-r--r--CHANGES.txt2
-rw-r--r--simplejson/encoder.py10
-rw-r--r--simplejson/tests/__init__.py1
-rw-r--r--simplejson/tests/test_str_subclass.py16
4 files changed, 28 insertions, 1 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 4fa3ccf..abf04dd 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,7 @@
Version 3.8.3 released XXXX-XX-XX
+* Workaround for bad behavior in string subclasses
+ https://github.com/simplejson/simplejson/issues/144
* Fix warnings flagged by -3
https://github.com/simplejson/simplejson/pull/146
* Update readthedocs documentation links
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index 5b9bda7..d453d09 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -5,7 +5,7 @@ import re
from operator import itemgetter
# Do not import Decimal directly to avoid reload issues
import decimal
-from .compat import u, unichr, binary_type, string_types, integer_types, PY3
+from .compat import u, unichr, binary_type, text_type, string_types, integer_types, PY3
def _import_speedups():
try:
from . import _speedups
@@ -46,9 +46,13 @@ def encode_basestring(s, _PY3=PY3, _q=u('"')):
if _PY3:
if isinstance(s, binary_type):
s = s.decode('utf-8')
+ if type(s) is not text_type:
+ s = text_type(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)
def replace(match):
return ESCAPE_DCT[match.group(0)]
return _q + ESCAPE.sub(replace, s) + _q
@@ -61,9 +65,13 @@ def py_encode_basestring_ascii(s, _PY3=PY3):
if _PY3:
if isinstance(s, binary_type):
s = s.decode('utf-8')
+ if type(s) is not text_type:
+ s = text_type(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)
def replace(match):
s = match.group(0)
try:
diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py
index 88249d1..3a00b08 100644
--- a/simplejson/tests/__init__.py
+++ b/simplejson/tests/__init__.py
@@ -57,6 +57,7 @@ def all_tests_suite():
'simplejson.tests.test_scanstring',
'simplejson.tests.test_separators',
'simplejson.tests.test_speedups',
+ 'simplejson.tests.test_str_subclass',
'simplejson.tests.test_unicode',
'simplejson.tests.test_decimal',
'simplejson.tests.test_tuple',
diff --git a/simplejson/tests/test_str_subclass.py b/simplejson/tests/test_str_subclass.py
new file mode 100644
index 0000000..771eb67
--- /dev/null
+++ b/simplejson/tests/test_str_subclass.py
@@ -0,0 +1,16 @@
+from unittest import TestCase
+
+import simplejson
+from simplejson.compat import text_type, u
+
+# Tests for issue demonstrated in https://github.com/simplejson/simplejson/issues/144
+class WonkyTextSubclass(text_type):
+ def __getslice__(self, start, end):
+ return self.__class__('not what you wanted!')
+
+class TestStrSubclass(TestCase):
+ def test_dump_load(self):
+ for s in ['', '"hello"', 'text', u('\u005c')]:
+ self.assertEqual(
+ s,
+ simplejson.loads(simplejson.dumps(WonkyTextSubclass(s))))