]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30936: Fix a reference leak in json when fail to sort keys. (#2712)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 16 Jul 2017 04:29:16 +0000 (07:29 +0300)
committerGitHub <noreply@github.com>
Sun, 16 Jul 2017 04:29:16 +0000 (07:29 +0300)
Lib/test/test_json/test_speedups.py
Modules/_json.c

index 8d98ab5ae1c39862d6b67cfce1a3950b57d31efa..56f1882001d666807998f3ebb973b40b8fb3b5ca 100644 (file)
@@ -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'})
index 07bcd318b1eb7f8c06534fced85b2ae303d59651..6cc31c6e3712245b51e2e36c11c6e75187d2395c 100644 (file)
@@ -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)