diff -r 1c72e9c1b3b4 Modules/_json.c --- a/Modules/_json.c Sat May 02 09:47:28 2015 +0100 +++ b/Modules/_json.c Sat May 02 13:15:06 2015 +0300 @@ -1663,36 +1663,17 @@ encoder_listencode_dict(PyEncoderObject */ } + items = PyMapping_Items(dct); + if (items == NULL) + goto bail; if (PyObject_IsTrue(s->sort_keys)) { - /* First sort the keys then replace them with (key, value) tuples. */ - Py_ssize_t i, nitems; - items = PyMapping_Keys(dct); - if (items == NULL) - goto bail; if (!PyList_Check(items)) { PyErr_SetString(PyExc_ValueError, "keys must return list"); goto bail; } if (PyList_Sort(items) < 0) goto bail; - nitems = PyList_GET_SIZE(items); - for (i = 0; i < nitems; i++) { - PyObject *key, *value; - key = PyList_GET_ITEM(items, i); - value = PyDict_GetItem(dct, key); - item = PyTuple_Pack(2, key, value); - if (item == NULL) - goto bail; - PyList_SET_ITEM(items, i, item); - item = NULL; - Py_DECREF(key); - } } - else { - items = PyMapping_Items(dct); - } - if (items == NULL) - goto bail; it = PyObject_GetIter(items); Py_DECREF(items); if (it == NULL)