Issue #23206: Make `json.dumps(..., ensure_ascii=False) as fast as the default case of ensure_ascii=True`. Patch by Naoki Inada.

This commit is contained in:
Antoine Pitrou 2015-01-11 16:41:01 +01:00
parent 2cae11e87e
commit dc3eaa80d4
5 changed files with 142 additions and 7 deletions

View file

@ -6,6 +6,10 @@ try:
from _json import encode_basestring_ascii as c_encode_basestring_ascii
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:
@ -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