diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2015-01-11 15:41:01 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2015-01-11 15:41:01 (GMT) |
commit | dc3eaa80d493cd23a5589f16a2ead559b0fd376c (patch) | |
tree | 31c141587230c00bdd02003f4e9719ea6fc60c37 /Lib | |
parent | 2cae11e87eac4a086b3188112f08ee9ed6269556 (diff) | |
download | cpython-dc3eaa80d493cd23a5589f16a2ead559b0fd376c.zip cpython-dc3eaa80d493cd23a5589f16a2ead559b0fd376c.tar.gz cpython-dc3eaa80d493cd23a5589f16a2ead559b0fd376c.tar.bz2 |
Issue #23206: Make ``json.dumps(..., ensure_ascii=False)`` as fast as the default case of ``ensure_ascii=True``. Patch by Naoki Inada.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/json/encoder.py | 9 | ||||
-rw-r--r-- | Lib/test/test_json/test_encode_basestring_ascii.py | 3 |
2 files changed, 8 insertions, 4 deletions
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 0513838..26e9eb2 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -7,6 +7,10 @@ try: except ImportError: c_encode_basestring_ascii = None try: + from _json import encode_basestring as c_encode_basestring +except ImportError: + c_encode_basestring = None +try: from _json import make_encoder as c_make_encoder except ImportError: c_make_encoder = None @@ -30,7 +34,7 @@ for i in range(0x20): INFINITY = float('inf') FLOAT_REPR = repr -def encode_basestring(s): +def py_encode_basestring(s): """Return a JSON representation of a Python string """ @@ -39,6 +43,9 @@ def encode_basestring(s): return '"' + ESCAPE.sub(replace, s) + '"' +encode_basestring = (c_encode_basestring or py_encode_basestring) + + def py_encode_basestring_ascii(s): """Return an ASCII-only JSON representation of a Python string diff --git a/Lib/test/test_json/test_encode_basestring_ascii.py b/Lib/test/test_json/test_encode_basestring_ascii.py index 480afd6..3a4ad18 100644 --- a/Lib/test/test_json/test_encode_basestring_ascii.py +++ b/Lib/test/test_json/test_encode_basestring_ascii.py @@ -11,9 +11,6 @@ CASES = [ (' s p a c e d ', '" s p a c e d "'), ('\U0001d120', '"\\ud834\\udd20"'), ('\u03b1\u03a9', '"\\u03b1\\u03a9"'), - ('\u03b1\u03a9', '"\\u03b1\\u03a9"'), - ('\u03b1\u03a9', '"\\u03b1\\u03a9"'), - ('\u03b1\u03a9', '"\\u03b1\\u03a9"'), ("`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'), ('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'), ('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'), |