gh-145856: Fix plistlib.dumps() skipkeys behavior with mixed key types#150109
Open
ankhikarmakar wants to merge 3 commits into
Open
gh-145856: Fix plistlib.dumps() skipkeys behavior with mixed key types#150109ankhikarmakar wants to merge 3 commits into
ankhikarmakar wants to merge 3 commits into
Conversation
a516b1a to
9be8728
Compare
encukou
approved these changes
May 19, 2026
Member
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
Member
|
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
Contributor
Author
|
Reverted to the old commit. Please review @encukou . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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