mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
Make sure the list.sort's decorate step unwinds itself before returning
an exception raised by the key function. (Suggested by Michael Hudson.)
This commit is contained in:
parent
b310591169
commit
37e136373e
2 changed files with 16 additions and 2 deletions
|
|
@ -186,6 +186,13 @@ class TestDecorateSortUndecorate(unittest.TestCase):
|
|||
data = 'The quick Brown fox Jumped over The lazy Dog'.split()
|
||||
self.assertRaises(TypeError, data.sort, "bad", str.lower)
|
||||
|
||||
def test_key_with_exception(self):
|
||||
# Verify that the wrapper has been removed
|
||||
data = range(-2,2)
|
||||
dup = data[:]
|
||||
self.assertRaises(ZeroDivisionError, data.sort, None, lambda x: 1/x)
|
||||
self.assertEqual(data, dup)
|
||||
|
||||
def test_reverse(self):
|
||||
data = range(100)
|
||||
random.shuffle(data)
|
||||
|
|
|
|||
|
|
@ -1876,8 +1876,15 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
|
|||
value = PyList_GET_ITEM(self, i);
|
||||
key = PyObject_CallFunctionObjArgs(keyfunc, value,
|
||||
NULL);
|
||||
if (key == NULL)
|
||||
if (key == NULL) {
|
||||
for (i=i-1 ; i>=0 ; i--) {
|
||||
kvpair = PyList_GET_ITEM(self, i);
|
||||
value = sortwrapper_getvalue(kvpair);
|
||||
PyList_SET_ITEM(self, i, value);
|
||||
Py_DECREF(kvpair);
|
||||
}
|
||||
goto dsu_fail;
|
||||
}
|
||||
kvpair = build_sortwrapper(key, value);
|
||||
if (kvpair == NULL)
|
||||
goto dsu_fail;
|
||||
|
|
@ -1885,7 +1892,7 @@ listsort(PyListObject *self, PyObject *args, PyObject *kwds)
|
|||
}
|
||||
}
|
||||
|
||||
/* Reverse sort stability achieved by initialially reversing the list,
|
||||
/* Reverse sort stability achieved by initially reversing the list,
|
||||
applying a stable forward sort, then reversing the final result. */
|
||||
if (reverse && self->ob_size > 1)
|
||||
reverse_slice(self->ob_item, self->ob_item + self->ob_size);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue