Commit 4269d010 authored by Hynek Schlawack's avatar Hynek Schlawack

#14835: Make plistlib output empty arrays & dicts like OS X

Patch by Sidney San Martín.
parent 9762a282
...@@ -237,20 +237,26 @@ class PlistWriter(DumbXMLWriter): ...@@ -237,20 +237,26 @@ class PlistWriter(DumbXMLWriter):
self.endElement("data") self.endElement("data")
def writeDict(self, d): def writeDict(self, d):
self.beginElement("dict") if d:
items = sorted(d.items()) self.beginElement("dict")
for key, value in items: items = sorted(d.items())
if not isinstance(key, str): for key, value in items:
raise TypeError("keys must be strings") if not isinstance(key, str):
self.simpleElement("key", key) raise TypeError("keys must be strings")
self.writeValue(value) self.simpleElement("key", key)
self.endElement("dict") self.writeValue(value)
self.endElement("dict")
else:
self.simpleElement("dict")
def writeArray(self, array): def writeArray(self, array):
self.beginElement("array") if array:
for value in array: self.beginElement("array")
self.writeValue(value) for value in array:
self.endElement("array") self.writeValue(value)
self.endElement("array")
else:
self.simpleElement("array")
class _InternalDict(dict): class _InternalDict(dict):
......
...@@ -55,6 +55,10 @@ TESTDATA = b"""<?xml version="1.0" encoding="UTF-8"?> ...@@ -55,6 +55,10 @@ TESTDATA = b"""<?xml version="1.0" encoding="UTF-8"?>
</array> </array>
<key>aString</key> <key>aString</key>
<string>Doodah</string> <string>Doodah</string>
<key>anEmptyDict</key>
<dict/>
<key>anEmptyList</key>
<array/>
<key>anInt</key> <key>anInt</key>
<integer>728</integer> <integer>728</integer>
<key>nestedData</key> <key>nestedData</key>
...@@ -112,6 +116,8 @@ class TestPlistlib(unittest.TestCase): ...@@ -112,6 +116,8 @@ class TestPlistlib(unittest.TestCase):
someMoreData = plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10), someMoreData = plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10),
nestedData = [plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10)], nestedData = [plistlib.Data(b"<lots of binary gunk>\0\1\2\3" * 10)],
aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
anEmptyDict = dict(),
anEmptyList = list()
) )
pl['\xc5benraa'] = "That was a unicode key." pl['\xc5benraa'] = "That was a unicode key."
return pl return pl
......
...@@ -662,6 +662,7 @@ Alex Martelli ...@@ -662,6 +662,7 @@ Alex Martelli
Anthony Martin Anthony Martin
Owen Martin Owen Martin
Sébastien Martini Sébastien Martini
Sidney San Martín
Roger Masse Roger Masse
Nick Mathewson Nick Mathewson
Simon Mathieu Simon Mathieu
......
...@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 4? ...@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 4?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #14835: Make plistlib output empty arrays & dicts like OS X.
Patch by Sidney San Martín.
- Issue #14930: Make memoryview objects weakrefable. - Issue #14930: Make memoryview objects weakrefable.
- Issue #14775: Fix a potential quadratic dict build-up due to the garbage - Issue #14775: Fix a potential quadratic dict build-up due to the garbage
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment