[Zope3-checkins] CVS: Zope3/src/zope/xmlpickle - xmlpickle.py:1.5

Jim Fulton jim at zope.com
Tue Jan 13 12:22:16 EST 2004


Update of /cvs-repository/Zope3/src/zope/xmlpickle
In directory cvs.zope.org:/tmp/cvs-serv29357

Modified Files:
	xmlpickle.py 
Log Message:
Added support for converting strings with multiple pickles to XML.

(It would be better to convert all of the pickles at once.)



=== Zope3/src/zope/xmlpickle/xmlpickle.py 1.4 => 1.5 ===
--- Zope3/src/zope/xmlpickle/xmlpickle.py:1.4	Sun Sep 21 13:32:12 2003
+++ Zope3/src/zope/xmlpickle/xmlpickle.py	Tue Jan 13 12:21:45 2004
@@ -73,10 +73,68 @@
     _PicklerThatSortsDictItems(file, bin).dump(object)
     return file.getvalue()
 
-def toxml(p):
+def toxml(p, index=0):
     """Convert a standard Python pickle to xml
+
+    You can provide a pickle string and get XML of an individual pickle:
+
+    >>> import pickle
+    >>> s = pickle.dumps(42)
+    >>> print toxml(s).strip()
+    <?xml version="1.0" encoding="utf-8" ?>
+    <pickle> <int>42</int> </pickle>
+
+    If the string contains multiple pickles:
+
+    >>> l = [1]
+    >>> import StringIO
+    >>> f = StringIO.StringIO()
+    >>> pickler = pickle.Pickler(f)
+    >>> pickler.dump(l)
+    >>> pickler.dump(42)
+    >>> pickler.dump([42, l])
+    >>> s = f.getvalue()
+
+    You can supply indexes to access individual pickles:
+    
+    >>> print toxml(s).strip()
+    <?xml version="1.0" encoding="utf-8" ?>
+    <pickle>
+      <list>
+        <int>1</int>
+      </list>
+    </pickle>
+
+    >>> print toxml(s, 0).strip()
+    <?xml version="1.0" encoding="utf-8" ?>
+    <pickle>
+      <list>
+        <int>1</int>
+      </list>
+    </pickle>
+
+    >>> print toxml(s, 1).strip()
+    <?xml version="1.0" encoding="utf-8" ?>
+    <pickle> <int>42</int> </pickle>
+
+    >>> print toxml(s, 2).strip()
+    <?xml version="1.0" encoding="utf-8" ?>
+    <pickle>
+      <list>
+        <int>42</int>
+        <reference id="o0"/>
+      </list>
+    </pickle>
+
+    Note that all of the pickles in a string share a common memo, so
+    the last pickle in the example above has a reference to the
+    list pickled in the first pickle.
+
     """
     u = ppml.ToXMLUnpickler(StringIO(p))
+    while index > 0:
+        xmlob = u.load()
+        index -= 1
     xmlob = u.load()
     r = ['<?xml version="1.0" encoding="utf-8" ?>\n']
     xmlob.output(r.append)




More information about the Zope3-Checkins mailing list