summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_json/test_speedups.py4
-rw-r--r--Modules/_json.c4
2 files changed, 7 insertions, 1 deletions
diff --git a/Lib/test/test_json/test_speedups.py b/Lib/test/test_json/test_speedups.py
index 8d98ab5..56f1882 100644
--- a/Lib/test/test_json/test_speedups.py
+++ b/Lib/test/test_json/test_speedups.py
@@ -44,3 +44,7 @@ class TestEncode(CTest):
self.assertRaises(ZeroDivisionError, test, 'check_circular')
self.assertRaises(ZeroDivisionError, test, 'allow_nan')
self.assertRaises(ZeroDivisionError, test, 'sort_keys')
+
+ def test_unsortable_keys(self):
+ with self.assertRaises(TypeError):
+ self.json.encoder.JSONEncoder(sort_keys=True).encode({'a': 1, 1: 'a'})
diff --git a/Modules/_json.c b/Modules/_json.c
index 07bcd31..6cc31c6 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1589,8 +1589,10 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
items = PyMapping_Items(dct);
if (items == NULL)
goto bail;
- if (s->sort_keys && PyList_Sort(items) < 0)
+ if (s->sort_keys && PyList_Sort(items) < 0) {
+ Py_DECREF(items);
goto bail;
+ }
it = PyObject_GetIter(items);
Py_DECREF(items);
if (it == NULL)