diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-03-25 00:47:55 -0700 |
---|---|---|
committer | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-03-25 00:47:55 -0700 |
commit | 9dbb09fc27b99d2c08b8f56db71018eb828cc7cd (patch) | |
tree | f94682f665dc72fdca95b91ed43bc693a4a776e4 /Objects | |
parent | 0e05d8a82dedf6f020b71a780507fb45ad5fd00f (diff) | |
download | cpython-git-9dbb09fc27b99d2c08b8f56db71018eb828cc7cd.tar.gz |
bpo-36218: Fix handling of heterogeneous values in list.sort (GH-12209) GH-12532)
(cherry picked from commit dd5417afcf8924bcdd7077351941ad21727ef644)
Co-authored-by: RĂ©mi Lapeyre <remi.lapeyre@henki.fr>
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/listobject.c | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c index 7dc68a73bd..d795f66e6e 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2250,19 +2250,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) { @@ -2290,10 +2299,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; } |