Skip to content

Commit

Permalink
Merge pull request #225 from benjaminp/empty-str
Browse files Browse the repository at this point in the history
On Python 2, decode empty strings as str not unicode.
  • Loading branch information
etrepum committed Jun 27, 2018
2 parents 00ed20d + 48d4f13 commit 131d225
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 8 additions & 7 deletions simplejson/_speedups.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions simplejson/tests/test_scanstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_c_scanstring(self):
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(
Expand Down

0 comments on commit 131d225

Please sign in to comment.