summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2018-06-27 11:23:18 -0700
committerGitHub <noreply@github.com>2018-06-27 11:23:18 -0700
commit131d225786a98c2c2da5a1dfbbf9db0491fe9ac6 (patch)
treea06a18426ce219b0bb6a1add75ef4b65ae29171b
parent00ed20da4c0e5f0396661f73482418651ff4d8c7 (diff)
parent48d4f134f971c4cb7a67804a5aa8402e81298057 (diff)
downloadsimplejson-131d225786a98c2c2da5a1dfbbf9db0491fe9ac6.tar.gz
Merge pull request #225 from benjaminp/empty-str
On Python 2, decode empty strings as str not unicode.
-rw-r--r--simplejson/_speedups.c15
-rw-r--r--simplejson/tests/test_scanstring.py2
2 files changed, 10 insertions, 7 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c
index 1b5d441..e710128 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)
diff --git a/simplejson/tests/test_scanstring.py b/simplejson/tests/test_scanstring.py
index 3d98f0d..d5de180 100644
--- a/simplejson/tests/test_scanstring.py
+++ b/simplejson/tests/test_scanstring.py
@@ -22,6 +22,8 @@ class TestScanString(TestCase):
return
self._test_scanstring(simplejson.decoder.c_scanstring)
+ self.assertTrue(isinstance(simplejson.decoder.c_scanstring('""', 0)[0], str))
+
def _test_scanstring(self, scanstring):
if sys.maxunicode == 65535:
self.assertEqual(