Skip to content

Commit 49f6449

Browse files
bpo-30936: Fix a reference leak in json when fail to sort keys. (#2712)
1 parent 95bebb7 commit 49f6449

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/test/test_json/test_speedups.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ def test(name):
4444
self.assertRaises(ZeroDivisionError, test, 'check_circular')
4545
self.assertRaises(ZeroDivisionError, test, 'allow_nan')
4646
self.assertRaises(ZeroDivisionError, test, 'sort_keys')
47+
48+
def test_unsortable_keys(self):
49+
with self.assertRaises(TypeError):
50+
self.json.encoder.JSONEncoder(sort_keys=True).encode({'a': 1, 1: 'a'})

Modules/_json.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1589,8 +1589,10 @@ encoder_listencode_dict(PyEncoderObject *s, _PyAccu *acc,
15891589
items = PyMapping_Items(dct);
15901590
if (items == NULL)
15911591
goto bail;
1592-
if (s->sort_keys && PyList_Sort(items) < 0)
1592+
if (s->sort_keys && PyList_Sort(items) < 0) {
1593+
Py_DECREF(items);
15931594
goto bail;
1595+
}
15941596
it = PyObject_GetIter(items);
15951597
Py_DECREF(items);
15961598
if (it == NULL)

0 commit comments

Comments
 (0)