Skip to content

bpo-36218: Fix handling of heterogeneous values in list.sort #12209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ def test_unsafe_tuple_compare(self):
check_against_PyObject_RichCompareBool(self, [float('nan')]*100)
check_against_PyObject_RichCompareBool(self, [float('nan') for
_ in range(100)])

def test_not_all_tuples(self):
self.assertRaises(TypeError, [(1.0, 1.0), (False, "A"), 6].sort)
self.assertRaises(TypeError, [('a', 1), (1, 'a')].sort)
self.assertRaises(TypeError, [(1, 'a'), ('a', 1)].sort)
#==============================================================================

if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a segfault occuring when sorting a list of heterogeneous values. Patch
contributed by Rémi Lapeyre and Elliot Gorokhovsky.
33 changes: 22 additions & 11 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2306,19 +2306,28 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)

if (key->ob_type != key_type) {
keys_are_all_same_type = 0;
break;
/* If keys are in tuple we must loop over the whole list to make
sure all items are tuples */
if (!keys_are_in_tuples) {
break;
}
}

if (key_type == &PyLong_Type) {
if (ints_are_bounded && Py_ABS(Py_SIZE(key)) > 1)
if (keys_are_all_same_type) {
if (key_type == &PyLong_Type &&
ints_are_bounded &&
Py_ABS(Py_SIZE(key)) > 1) {

ints_are_bounded = 0;
}
else if (key_type == &PyUnicode_Type &&
strings_are_latin &&
PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND) {

strings_are_latin = 0;
}
}
}
else if (key_type == &PyUnicode_Type){
if (strings_are_latin &&
PyUnicode_KIND(key) != PyUnicode_1BYTE_KIND)
strings_are_latin = 0;
}
}

/* Choose the best compare, given what we now know about the keys. */
if (keys_are_all_same_type) {
Expand Down Expand Up @@ -2346,10 +2355,12 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse)
if (keys_are_in_tuples) {
/* Make sure we're not dealing with tuples of tuples
* (remember: here, key_type refers list [key[0] for key in keys]) */
if (key_type == &PyTuple_Type)
if (key_type == &PyTuple_Type) {
ms.tuple_elem_compare = safe_object_compare;
else
}
else {
ms.tuple_elem_compare = ms.key_compare;
}

ms.key_compare = unsafe_tuple_compare;
}
Expand Down