-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
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
Changes from 1 commit
e75d9fb
8498a21
60aca1e
eaa7c50
3b02a05
47a0688
467cd16
8b2224f
6286a4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2305,6 +2305,7 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse) | |||||
| lo.keys[i]); | ||||||
|
|
||||||
| if (key->ob_type != key_type) { | ||||||
| keys_are_in_tuples = 0; | ||||||
| keys_are_all_same_type = 0; | ||||||
| break; | ||||||
| } | ||||||
|
|
@@ -2338,21 +2339,24 @@ list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse) | |||||
| else { | ||||||
| ms.key_compare = safe_object_compare; | ||||||
| } | ||||||
|
|
||||||
| 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) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And instead modify this check, which sets
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that's going to work because of the early exit at https://github.com/python/cpython/pull/12209/files#diff-5037da2a492e5903f100107e0506f818R2310 you can't be sure that all keys are tuples anyway. I think the simplest way forward would be to use two variables Does this make sense? |
||||||
| ms.tuple_elem_compare = safe_object_compare; | ||||||
| } | ||||||
| else { | ||||||
| ms.tuple_elem_compare = ms.key_compare; | ||||||
| } | ||||||
|
|
||||||
| ms.key_compare = unsafe_tuple_compare; | ||||||
| } | ||||||
| } | ||||||
| else { | ||||||
| ms.key_compare = safe_object_compare; | ||||||
| } | ||||||
|
|
||||||
| 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) | ||||||
| ms.tuple_elem_compare = safe_object_compare; | ||||||
| else | ||||||
| ms.tuple_elem_compare = ms.key_compare; | ||||||
|
|
||||||
| ms.key_compare = unsafe_tuple_compare; | ||||||
| } | ||||||
| } | ||||||
| /* End of pre-sort check: ms is now set properly! */ | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We remove this line, which over-compensates in the case you mentioned.