Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 52209d3

Browse files
committed
#14835: Make plistlib output empty arrays & dicts like OS X
Patch by Sidney San Martín.
1 parent 737b173 commit 52209d3

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

Lib/plistlib.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,20 +237,26 @@ def writeData(self, data):
237237
self.endElement("data")
238238

239239
def writeDict(self, d):
240-
self.beginElement("dict")
241-
items = sorted(d.items())
242-
for key, value in items:
243-
if not isinstance(key, str):
244-
raise TypeError("keys must be strings")
245-
self.simpleElement("key", key)
246-
self.writeValue(value)
247-
self.endElement("dict")
240+
if d:
241+
self.beginElement("dict")
242+
items = sorted(d.items())
243+
for key, value in items:
244+
if not isinstance(key, str):
245+
raise TypeError("keys must be strings")
246+
self.simpleElement("key", key)
247+
self.writeValue(value)
248+
self.endElement("dict")
249+
else:
250+
self.simpleElement("dict")
248251

249252
def writeArray(self, array):
250-
self.beginElement("array")
251-
for value in array:
252-
self.writeValue(value)
253-
self.endElement("array")
253+
if array:
254+
self.beginElement("array")
255+
for value in array:
256+
self.writeValue(value)
257+
self.endElement("array")
258+
else:
259+
self.simpleElement("array")
254260

255261

256262
class _InternalDict(dict):

Lib/test/test_plistlib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
</array>
5656
<key>aString</key>
5757
<string>Doodah</string>
58+
<key>anEmptyDict</key>
59+
<dict/>
60+
<key>anEmptyList</key>
61+
<array/>
5862
<key>anInt</key>
5963
<integer>728</integer>
6064
<key>nestedData</key>
@@ -112,6 +116,8 @@ def _create(self):
112116
someMoreData = plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10),
113117
nestedData = [plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10)],
114118
aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
119+
anEmptyDict = dict(),
120+
anEmptyList = list()
115121
)
116122
pl['\xc5benraa'] = "That was a unicode key."
117123
return pl

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ Alex Martelli
662662
Anthony Martin
663663
Owen Martin
664664
Sébastien Martini
665+
Sidney San Martín
665666
Roger Masse
666667
Nick Mathewson
667668
Simon Mathieu

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 4?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14835: Make plistlib output empty arrays & dicts like OS X.
14+
Patch by Sidney San Martín.
15+
1316
- Issue #14930: Make memoryview objects weakrefable.
1417

1518
- Issue #14775: Fix a potential quadratic dict build-up due to the garbage

0 commit comments

Comments
 (0)