-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
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 2 commits
e75d9fb
8498a21
60aca1e
eaa7c50
3b02a05
47a0688
467cd16
8b2224f
6286a4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| 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) { | ||||||
|
||||||
| if (key_type == &PyTuple_Type) { | |
| if ( (keys_are_all_same_type == 0) || (key_type == &PyTuple_Type) ) { |
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.
And instead modify this check, which sets tuple_elem_compare to safe_object_compare in the case of nested tuples, to also fall back when the tuple first-elements are heterogeneous.
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.
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 key_type and inner_key_type to know if it's item[0] or item that changed type.
Does this make sense?
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.