Skip to content

Commit 7f7ff4c

Browse files
ankhikarmakarVanshAgarwal24036serhiy-storchakaclaude
authored
gh-145856: Fix plistlib.dump() with skipkeys=True and sort_keys=True (GH-150109)
A dict with non-string keys raised TypeError, because the keys were sorted before skipping non-string keys. Co-authored-by: VanshAgarwal24036 <vanshagbly6@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 659e262 commit 7f7ff4c

3 files changed

Lines changed: 51 additions & 25 deletions

File tree

‎Lib/plistlib.py‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,25 @@ def _date_to_string(d, aware_datetime):
159159
d.hour, d.minute, d.second
160160
)
161161

162+
def _dict_items(d, sort_keys, skipkeys):
163+
"""Return the (key, value) pairs of a dict, sorted if needed.
164+
165+
Sorting fails for keys of different types, so non-string keys are
166+
removed or reported before sorting.
167+
"""
168+
items = d.items()
169+
if sort_keys:
170+
if skipkeys:
171+
items = [item for item in items if isinstance(item[0], str)]
172+
items.sort()
173+
else:
174+
for key in d:
175+
if not isinstance(key, str):
176+
raise TypeError("keys must be strings")
177+
items = sorted(items)
178+
return items
179+
180+
162181
def _escape(text):
163182
m = _controlCharPat.search(text)
164183
if m is not None:
@@ -388,11 +407,7 @@ def write_bytes(self, data):
388407
def write_dict(self, d):
389408
if d:
390409
self.begin_element("dict")
391-
if self._sort_keys:
392-
items = sorted(d.items())
393-
else:
394-
items = d.items()
395-
410+
items = _dict_items(d, self._sort_keys, self._skipkeys)
396411
for key, value in items:
397412
if not isinstance(key, str):
398413
if self._skipkeys:
@@ -718,10 +733,7 @@ def _flatten(self, value):
718733
if isinstance(value, (dict, frozendict)):
719734
keys = []
720735
values = []
721-
items = value.items()
722-
if self._sort_keys:
723-
items = sorted(items)
724-
736+
items = _dict_items(value, self._sort_keys, self._skipkeys)
725737
for k, v in items:
726738
if not isinstance(k, str):
727739
if self._skipkeys:
@@ -839,11 +851,7 @@ def _write_object(self, value):
839851
elif isinstance(value, (dict, frozendict)):
840852
keyRefs, valRefs = [], []
841853

842-
if self._sort_keys:
843-
rootItems = sorted(value.items())
844-
else:
845-
rootItems = value.items()
846-
854+
rootItems = _dict_items(value, self._sort_keys, self._skipkeys)
847855
for k, v in rootItems:
848856
if not isinstance(k, str):
849857
if self._skipkeys:

‎Lib/test/test_plistlib.py‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -722,20 +722,34 @@ def test_skipkeys(self):
722722
'snake': 'aWord',
723723
}
724724

725+
for fmt in ALL_FORMATS:
726+
for sort_keys in (False, True):
727+
with self.subTest(fmt=fmt, sort_keys=sort_keys):
728+
data = plistlib.dumps(
729+
pl, fmt=fmt, skipkeys=True, sort_keys=sort_keys)
730+
731+
pl2 = plistlib.loads(data)
732+
self.assertEqual(pl2, {'snake': 'aWord'})
733+
734+
fp = BytesIO()
735+
plistlib.dump(
736+
pl, fp, fmt=fmt, skipkeys=True, sort_keys=sort_keys)
737+
data = fp.getvalue()
738+
pl2 = plistlib.loads(fp.getvalue())
739+
self.assertEqual(pl2, {'snake': 'aWord'})
740+
741+
def test_skipkeys_with_sort_keys_mixed_types(self):
742+
# gh-145856: skipkeys=True + sort_keys=True with mixed key types
743+
# used to raise TypeError because the sort ran before the filter.
744+
pl = {1: 'a', 'z': 'b', 'a': 'c'}
745+
725746
for fmt in ALL_FORMATS:
726747
with self.subTest(fmt=fmt):
727748
data = plistlib.dumps(
728-
pl, fmt=fmt, skipkeys=True, sort_keys=False)
729-
730-
pl2 = plistlib.loads(data)
731-
self.assertEqual(pl2, {'snake': 'aWord'})
732-
733-
fp = BytesIO()
734-
plistlib.dump(
735-
pl, fp, fmt=fmt, skipkeys=True, sort_keys=False)
736-
data = fp.getvalue()
737-
pl2 = plistlib.loads(fp.getvalue())
738-
self.assertEqual(pl2, {'snake': 'aWord'})
749+
pl, fmt=fmt, skipkeys=True, sort_keys=True)
750+
pl2 = plistlib.loads(data, dict_type=collections.OrderedDict)
751+
self.assertEqual(dict(pl2), {'z': 'b', 'a': 'c'})
752+
self.assertEqual(list(pl2.keys()), ['a', 'z'])
739753

740754
def test_tuple_members(self):
741755
pl = {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix :func:`plistlib.dumps` and :func:`plistlib.dump` so that ``skipkeys=True``
2+
together with ``sort_keys=True`` correctly drops non-string keys when the
3+
dictionary contains a mix of string and non-string keys. Previously the sort
4+
ran before the filter and raised :exc:`TypeError`.

0 commit comments

Comments
 (0)