Commits

Hynek Schlawack committed 9e64084

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

Patch by Sidney San Martín.

  • Participants
  • Parent commits 7d252db

Comments (0)

Files changed (4)

File Lib/plistlib.py

         self.endElement("data")
 
     def writeDict(self, d):
-        self.beginElement("dict")
-        items = sorted(d.items())
-        for key, value in items:
-            if not isinstance(key, str):
-                raise TypeError("keys must be strings")
-            self.simpleElement("key", key)
-            self.writeValue(value)
-        self.endElement("dict")
+        if d:
+            self.beginElement("dict")
+            items = sorted(d.items())
+            for key, value in items:
+                if not isinstance(key, str):
+                    raise TypeError("keys must be strings")
+                self.simpleElement("key", key)
+                self.writeValue(value)
+            self.endElement("dict")
+        else:
+            self.simpleElement("dict")
 
     def writeArray(self, array):
-        self.beginElement("array")
-        for value in array:
-            self.writeValue(value)
-        self.endElement("array")
+        if array:
+            self.beginElement("array")
+            for value in array:
+                self.writeValue(value)
+            self.endElement("array")
+        else:
+            self.simpleElement("array")
 
 
 class _InternalDict(dict):

File Lib/test/test_plistlib.py

         </array>
         <key>aString</key>
         <string>Doodah</string>
+        <key>anEmptyDict</key>
+        <dict/>
+        <key>anEmptyList</key>
+        <array/>
         <key>anInt</key>
         <integer>728</integer>
         <key>nestedData</key>
             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)],
             aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
+            anEmptyDict = dict(),
+            anEmptyList = list()
         )
         pl['\xc5benraa'] = "That was a unicode key."
         return pl
 Anthony Martin
 Owen Martin
 Sébastien Martini
+Sidney San Martín
 Roger Masse
 Nick Mathewson
 Simon Mathieu
 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 #14775: Fix a potential quadratic dict build-up due to the garbage