From fad6c2b09cb4e9c352ed4c4cc55a2259a97a02be Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 26 Jun 2018 15:49:55 -0700 Subject: On Python 2, decode empty strings as str not unicode. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In general on Python 2, simplejson decodes ASCII strings as str, only promoting to unicode when needed: >>> simplejson.loads('["Spaetzle", "SpƤtzle"]') ['Spaetzle', u'Sp\xe4tzle'] Since 83a493db6a8b859ec7b10fa85365dd3fdf144c68, though, simplejson has always decoded empty JSON strings as unicode: >>> simplejson.loads('""') u'' This PR restores the old behavior of decoding empty strings as str. --- simplejson/_speedups.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'simplejson') diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c index 1b5d441..19e3a9f 100644 --- a/simplejson/_speedups.c +++ b/simplejson/_speedups.c @@ -76,6 +76,9 @@ static PyObject *JSON_Infinity = NULL; static PyObject *JSON_NegInfinity = NULL; static PyObject *JSON_NaN = NULL; static PyObject *JSON_EmptyUnicode = NULL; +#if PY_MAJOR_VERSION < 3 +static PyObject *JSON_EmptyStr = NULL; +#endif static PyTypeObject PyScannerType; static PyTypeObject PyEncoderType; @@ -785,12 +788,7 @@ join_list_string(PyObject *lst) /* return ''.join(lst) */ static PyObject *joinfn = NULL; if (joinfn == NULL) { - PyObject *ustr = PyString_FromStringAndSize(NULL, 0); - if (ustr == NULL) - return NULL; - - joinfn = PyObject_GetAttrString(ustr, "join"); - Py_DECREF(ustr); + joinfn = PyObject_GetAttrString(JSON_EmptyStr, "join"); if (joinfn == NULL) return NULL; } @@ -1026,7 +1024,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict, Py_s if (chunk != NULL) rval = chunk; else { - rval = JSON_EmptyUnicode; + rval = JSON_EmptyStr; Py_INCREF(rval); } } @@ -3331,6 +3329,9 @@ init_constants(void) #if PY_MAJOR_VERSION >= 3 JSON_EmptyUnicode = PyUnicode_New(0, 127); #else /* PY_MAJOR_VERSION >= 3 */ + JSON_EmptyStr = PyString_FromString(""); + if (JSON_EmptyStr == NULL) + return 0; JSON_EmptyUnicode = PyUnicode_FromUnicode(NULL, 0); #endif /* PY_MAJOR_VERSION >= 3 */ if (JSON_EmptyUnicode == NULL) -- cgit v1.2.1