]> granicus.if.org Git - python/commitdiff
#14835: Make plistlib output empty arrays & dicts like OS X
authorHynek Schlawack <hs@ox.cx>
Tue, 29 May 2012 10:04:54 +0000 (12:04 +0200)
committerHynek Schlawack <hs@ox.cx>
Tue, 29 May 2012 10:04:54 +0000 (12:04 +0200)
Patch by Sidney San Martín.

Lib/plistlib.py
Lib/test/test_plistlib.py
Misc/ACKS
Misc/NEWS

index 21076db9a5d4e05cf6082a103aabe452821413fa..41fd8f2efd12088ccc86b298b9389b0cba883ada 100644 (file)
@@ -237,20 +237,26 @@ class PlistWriter(DumbXMLWriter):
         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):
index 5f980d0a2d6de18a62d3e89ec188a732c66f4aae..a9e343eb155a1280c596d940c05de96e8a8931e3 100644 (file)
@@ -55,6 +55,10 @@ TESTDATA = b"""<?xml version="1.0" encoding="UTF-8"?>
         </array>
         <key>aString</key>
         <string>Doodah</string>
+        <key>anEmptyDict</key>
+        <dict/>
+        <key>anEmptyList</key>
+        <array/>
         <key>anInt</key>
         <integer>728</integer>
         <key>nestedData</key>
@@ -112,6 +116,8 @@ class TestPlistlib(unittest.TestCase):
             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
index 74bf123622b36ae2d36b276805fb649965371bf7..3abd77e011660e8421cba25d76365d8666c1b666 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -662,6 +662,7 @@ Alex Martelli
 Anthony Martin
 Owen Martin
 Sébastien Martini
+Sidney San Martín
 Roger Masse
 Nick Mathewson
 Simon Mathieu
index 10f05d803b72d2de01b7c69fdf591d04b7f8ae8c..0d36966e02f4dd8296bcb92426f7a36b65cb0e38 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.0 Alpha 4?
 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