19113N/A# User Antoine Pitrou <solipsis@pitrou.net>
19113N/A# Node ID ac61b03c19573cf0ebc27859ec48eddd276a7219
19113N/A# Parent 2cbf5e82b45ed5b95b2075c6bd7fe813bb306974
19113N/ASupport the sort_keys option in C encoding speedups.
19113N/AIt is a backport of CPython
3.x from SVN r72963, r72964, r86169.
19113N/A static PyObject *iteritems = NULL;
19113N/A- /* TODO: C speedup not implemented for sort_keys */
19113N/A+ if (PyObject_IsTrue(s->sort_keys)) {
19113N/A+ /* First sort the keys then replace them with (key, value) tuples. */
19113N/A+ PyErr_SetString(PyExc_ValueError, "keys must return list");
19113N/A+ nitems = PyList_GET_SIZE(items);
19113N/A+ for (i = 0; i < nitems; i++) {
19113N/A+ key = PyList_GET_ITEM(items, i);
19113N/A+ value = PyDict_GetItem(dct, key);
19113N/A+ item = PyTuple_Pack(2, key, value);
19113N/A+ PyList_SET_ITEM(items, i, item);
19113N/A+ iter = PyObject_GetIter(items);
19113N/A skipkeys = PyObject_IsTrue(s->skipkeys);
19113N/A- iter = PyObject_CallMethodObjArgs(dct, iteritems, NULL);
19113N/A while ((item = PyIter_Next(iter))) {
19113N/A- key = PyTuple_GetItem(item, 0);
19113N/A+ PyObject *encoded, *key, *value;
19113N/A+ if (!PyTuple_Check(item) || Py_SIZE(item) != 2) {
19113N/A+ PyErr_SetString(PyExc_ValueError, "items must return 2-tuples");
19113N/A+ key = PyTuple_GET_ITEM(item, 0);
19113N/A- value = PyTuple_GetItem(item, 1);
19113N/A+ value = PyTuple_GET_ITEM(item, 1);
19113N/A encoded = PyDict_GetItem(s->key_memo, key);
19113N/A- else if (PyInt_Check(key) || PyLong_Check(key)) {
19113N/A+ else if (key == Py_True || key == Py_False || key == Py_None) {
19113N/A+ /* This must come before the PyInt_Check because
19113N/A+ True and False are also 1 and 0.*/
19113N/A- else if (key == Py_True || key == Py_False || key == Py_None) {
19113N/A+ else if (PyInt_Check(key) || PyLong_Check(key)) {
19113N/A if (_one_shot and c_make_encoder is not None
19113N/A # result, expect, fname, input_string))
19113N/A '%r != %r for %s(%r)' % (result, expect, fname, input_string))
19113N/A+ items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]