Commits

Andrew Wooster committed d8319c0 Merge

Merge pull request #4 from geekman/master

Bug when writing non-binary (XML) plist with Data values

Comments (0)

Files changed (2)

         try:
             pathOrFile.seek(0)
             result = plistlib.readPlist(pathOrFile)
+            result = wrapDataObject(result, for_binary=True)
         except Exception, e:
             raise InvalidPlistException(e)
     if didOpen:
         pathOrFile.close()
     return result
 
+def wrapDataObject(o, for_binary=False):
+    if isinstance(o, Data) and not for_binary:
+        o = plistlib.Data(o)
+    elif isinstance(o, plistlib.Data) and for_binary:
+        o = Data(o.data)
+    elif isinstance(o, tuple):
+        o = wrapDataObject(list(o), for_binary)
+        o = tuple(o)
+    elif isinstance(o, list):
+        for i in xrange(len(o)):
+            o[i] = wrapDataObject(o[i], for_binary)
+    elif isinstance(o, dict):
+        for k in o:
+            o[k] = wrapDataObject(o[k], for_binary)
+    return o
+
 def writePlist(rootObject, pathOrFile, binary=True):
     if not binary:
+        rootObject = wrapDataObject(rootObject, binary)
         return plistlib.writePlist(rootObject, pathOrFile)
     else:
         didOpen = False
 
 def writePlistToString(rootObject, binary=True):
     if not binary:
+        rootObject = wrapDataObject(rootObject, binary)
         return plistlib.writePlistToString(rootObject)
     else:
         io = StringIO()
     
     def testXMLPlist(self):
         self.roundTrip({'hello':'world'}, xml=True)
+
+    def testXMLPlistWithData(self):
+        for binmode in (True, False):
+            binplist = writePlistToString({'data': Data('\x01\xac\xf0\xff')}, binary=binmode)
+            plist = readPlistFromString(binplist)
+            self.assertTrue(isinstance(plist['data'], Data), \
+                "unable to encode then decode Data into %s plist" % ("binary" if binmode else "XML"))
+
+    def testConvertToXMLPlistWithData(self):
+        binplist = writePlistToString({'data': Data('\x01\xac\xf0\xff')})
+        plist = readPlistFromString(binplist)
+        xmlplist = writePlistToString(plist, binary=False)
+        self.assertTrue(len(xmlplist) > 0, "unable to convert plist with Data from binary to XML")
     
     def testBoolRoot(self):
         self.roundTrip(True)