gh-145856: Fix plistlib.dumps() skipkeys behavior with mixed key types#150109
gh-145856: Fix plistlib.dumps() skipkeys behavior with mixed key types#150109ankhikarmakar wants to merge 3 commits into
Conversation
a516b1a to
9be8728
Compare
encukou
left a comment
There was a problem hiding this comment.
This looks reasonable & complete, and it even simplifies the implementation a bit.
Thank you!
987b4d7 to
0fc8545
Compare
|
Now it fails. Could you fix the Also, please do not force-push to CPython PRs, it makes the PR hard to review. |
…y types When both skipkeys=True and sort_keys=True were passed to plistlib.dump()/dumps(), the sort ran before the non-string keys were filtered out, raising TypeError on dictionaries with mixed key types. Filter non-string keys before sorting in all three write_dict sites (XML, binary pass 1, binary pass 2).
This reverts commit 0fc8545.
ed2b54f to
9d970c8
Compare
|
Reverted to the old commit. Please review @encukou . |
|
There was also equivalent PR #150109, opened a week earlier, which was closed to give the author of the issue chance to submit his own PR, which he didn't. |
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I suggest to add @VanshAgarwal24036 as a co-author. He created an equivalent PR earlier.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Looking at this again I see several issues.
- We create a copy of
itemseven ifsort_keysis false. If the dict was huge, this can consume significant amount of memory. Perhaps we should make a copy only if it is needed for sorting. - We can get different error than "keys must be strings" if
sort_keysis true,skipkeysis false, and the dict contains mixed keys. We should check types before sorting ifskipkeysis false.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
When both
skipkeys=Trueandsort_keys=Truewere passed toplistlib.dump()/plistlib.dumps(), the sort ran before non-string keys were filtered out, so a dictionary with mixed key types raisedTypeErrorinstead of dropping the non-string keys per the documented contract.This PR filters non-string keys before sorting in all three write_dict sites — the XML writer (
PlistWriter.write_dict) and both passes of the binary writer (_BinaryPlistWriter._flattenand_BinaryPlistWriter._write).Test plan:
test_skipkeysto coversort_keys=Truein addition tosort_keys=False.test_skipkeys_with_sort_keys_mixed_types(the reproducer from the issue) — fails without the fix, passes with it, across all formats.plistlib.dumps()raisesTypeErrorwhenskipkeys=Trueand dict has mixed key types #145856