diff options
author | Raymond Hettinger <python@rcn.com> | 2009-05-27 06:50:31 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-05-27 06:50:31 (GMT) |
commit | c8d952dfe4e7c126675d097c921f348584d50ac3 (patch) | |
tree | 83f39606c0d4301eea857a89b107f8e141b4338a /Modules/_json.c | |
parent | 0ffaaa634d59d24d2707adec7ff395f6fe51bb04 (diff) | |
download | cpython-c8d952dfe4e7c126675d097c921f348584d50ac3.zip cpython-c8d952dfe4e7c126675d097c921f348584d50ac3.tar.gz cpython-c8d952dfe4e7c126675d097c921f348584d50ac3.tar.bz2 |
Issue 6105: json encoder to respect iteration order of its inputs.
Diffstat (limited to 'Modules/_json.c')
-rw-r--r-- | Modules/_json.c | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/Modules/_json.c b/Modules/_json.c index c44dbac..90f5736 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -1334,8 +1334,9 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss static PyObject *empty_dict = NULL; PyObject *kstr = NULL; PyObject *ident = NULL; - PyObject *key, *value; - Py_ssize_t pos; + PyObject *key = NULL; + PyObject *value = NULL; + PyObject *it = NULL; int skipkeys; Py_ssize_t idx; @@ -1346,7 +1347,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) return -1; } - if (PyDict_Size(dct) == 0) + if (Py_SIZE(dct) == 0) return PyList_Append(rval, empty_dict); if (s->markers != Py_None) { @@ -1380,10 +1381,12 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss /* TODO: C speedup not implemented for sort_keys */ - pos = 0; + it = PyObject_GetIter(dct); + if (it == NULL) + goto bail; skipkeys = PyObject_IsTrue(s->skipkeys); idx = 0; - while (PyDict_Next(dct, &pos, &key, &value)) { + while ((key = PyIter_Next(it)) != NULL) { PyObject *encoded; if (PyUnicode_Check(key)) { @@ -1406,6 +1409,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss goto bail; } else if (skipkeys) { + Py_DECREF(key); continue; } else { @@ -1430,10 +1434,20 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss Py_DECREF(encoded); if (PyList_Append(rval, s->key_separator)) goto bail; + + value = PyObject_GetItem(dct, key); + if (value == NULL) + goto bail; if (encoder_listencode_obj(s, rval, value, indent_level)) goto bail; idx += 1; + Py_CLEAR(value); + Py_DECREF(key); } + if (PyErr_Occurred()) + goto bail; + Py_CLEAR(it); + if (ident != NULL) { if (PyDict_DelItem(s->markers, ident)) goto bail; @@ -1451,6 +1465,9 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss return 0; bail: + Py_XDECREF(it); + Py_XDECREF(key); + Py_XDECREF(value); Py_XDECREF(kstr); Py_XDECREF(ident); return -1; |