diff options
author | Richard van der Hoff <richard@matrix.org> | 2018-03-27 23:42:43 +0100 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2018-03-27 23:42:43 +0100 |
commit | 4e4ee05652472acb3bde31c5ad138b35d6c7dce9 (patch) | |
tree | e3bf9abcdb362685c73d728212b3588c7fe8055a | |
parent | 6ffddbe5ed6e4e72fbc7f6023439227970139159 (diff) | |
download | simplejson-4e4ee05652472acb3bde31c5ad138b35d6c7dce9.tar.gz |
Avoid copying strings when encoding
d782561 introduced a performance regression which means that every string is
copied during serialisation, when ensure_ascii is False.
This should fix it.
-rw-r--r-- | simplejson/encoder.py | 4 | ||||
-rw-r--r-- | simplejson/tests/test_str_subclass.py | 5 |
2 files changed, 7 insertions, 2 deletions
diff --git a/simplejson/encoder.py b/simplejson/encoder.py index 831527b..ae76ae3 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -52,7 +52,7 @@ def encode_basestring(s, _PY3=PY3, _q=u('"')): 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: + if type(s) not in (str, unicode): if isinstance(s, str): s = str.__str__(s) else: @@ -74,7 +74,7 @@ def py_encode_basestring_ascii(s, _PY3=PY3): 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: + if type(s) not in (str, unicode): if isinstance(s, str): s = str.__str__(s) else: diff --git a/simplejson/tests/test_str_subclass.py b/simplejson/tests/test_str_subclass.py index 771eb67..dc87904 100644 --- a/simplejson/tests/test_str_subclass.py +++ b/simplejson/tests/test_str_subclass.py @@ -14,3 +14,8 @@ class TestStrSubclass(TestCase): self.assertEqual( s, simplejson.loads(simplejson.dumps(WonkyTextSubclass(s)))) + + self.assertEqual( + s, + simplejson.loads(simplejson.dumps(WonkyTextSubclass(s), + ensure_ascii=False))) |