summaryrefslogtreecommitdiff
path: root/simplejson
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2018-06-26 15:49:55 -0700
committerBenjamin Peterson <benjamin@python.org>2018-06-26 15:49:55 -0700
commitfad6c2b09cb4e9c352ed4c4cc55a2259a97a02be (patch)
tree953d9b92adf3c4014e31d58e5b5aab71873ad25c /simplejson
parent00ed20da4c0e5f0396661f73482418651ff4d8c7 (diff)
downloadsimplejson-fad6c2b09cb4e9c352ed4c4cc55a2259a97a02be.tar.gz
On Python 2, decode empty strings as str not unicode.
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.
Diffstat (limited to 'simplejson')
-rw-r--r--simplejson/_speedups.c15
1 files changed, 8 insertions, 7 deletions
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)