summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-05-27 11:19:02 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-05-27 11:19:02 (GMT)
commit491a4cb8d934572b037c83faf30252c96cf6a3ee (patch)
treef19bd0d4495dd60db89f60e3ce035ef83841fa5d /Modules
parentbcf6f92dc50c1f23ca327c5dae4d616cfe212625 (diff)
downloadcpython-491a4cb8d934572b037c83faf30252c96cf6a3ee.zip
cpython-491a4cb8d934572b037c83faf30252c96cf6a3ee.tar.gz
cpython-491a4cb8d934572b037c83faf30252c96cf6a3ee.tar.bz2
Fix TODO: do the sort by just the key, not the key/value pair.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_json.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/Modules/_json.c b/Modules/_json.c
index f33503b..a012560 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1339,6 +1339,8 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
PyObject *item = NULL;
int skipkeys;
Py_ssize_t idx;
+ PyObject *mapping;
+ static PyObject *code = NULL;
if (open_dict == NULL || close_dict == NULL || empty_dict == NULL) {
open_dict = PyUnicode_InternFromString("{");
@@ -1379,25 +1381,28 @@ encoder_listencode_dict(PyEncoderObject *s, PyObject *rval, PyObject *dct, Py_ss
*/
}
- items = PyObject_CallMethod(dct, "items", ""); /* XXX key=itemgetter(0) */
- if (items == NULL)
- goto bail;
if (PyObject_IsTrue(s->sort_keys)) {
- PyObject *rv;
- PyObject *itemlist;
-
- itemlist = PySequence_List(items);
- Py_DECREF(items);
- if (itemlist == NULL)
- goto bail;
+ if (code == NULL) {
+ code = Py_CompileString("sorted(d.items(), key=lambda kv: kv[0])",
+ "_json.c", Py_eval_input);
+ if (code == NULL)
+ goto bail;
+ }
- rv = PyObject_CallMethod(itemlist, "sort", "");
- if (rv == NULL) {
- Py_DECREF(itemlist);
+ mapping = PyDict_New();
+ if (mapping == NULL)
+ goto bail;
+ if (PyDict_SetItemString(mapping, "d", dct) == -1) {
+ Py_DECREF(mapping);
goto bail;
}
- items = itemlist;
- }
+ items = PyEval_EvalCode((PyCodeObject *)code, PyEval_GetGlobals(), mapping);
+ Py_DECREF(mapping);
+ } else {
+ items = PyMapping_Items(dct);
+ }
+ if (items == NULL)
+ goto bail;
it = PyObject_GetIter(items);
Py_DECREF(items);
if (it == NULL)