Skip to content

Commit

Permalink
bpo-30936: Fix a reference leak in json when fail to sort keys. (#2712)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Jul 16, 2017
1 parent 95bebb7 commit 49f6449
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Lib/test/test_json/test_speedups.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ def test(name):
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'})
4 changes: 3 additions & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 49f6449

Please sign in to comment.