[Zope-CVS] CVS: Products/AdaptableStorage/zodb - utils.py:1.3

Shane Hathaway shane@zope.com
Fri, 21 Feb 2003 12:17:55 -0500


Update of /cvs-repository/Products/AdaptableStorage/zodb
In directory cvs.zope.org:/tmp/cvs-serv16106/zodb

Modified Files:
	utils.py 
Log Message:
copyOf() can now copy ZClass instances

=== Products/AdaptableStorage/zodb/utils.py 1.2 => 1.3 ===
--- Products/AdaptableStorage/zodb/utils.py:1.2	Mon Jan 20 09:29:17 2003
+++ Products/AdaptableStorage/zodb/utils.py	Fri Feb 21 12:17:55 2003
@@ -18,6 +18,7 @@
 
 from cStringIO import StringIO
 from cPickle import Pickler, Unpickler
+from types import StringType
 
 
 def copyOf(object):
@@ -26,12 +27,33 @@
     Re-ghostifies objects along the way to save memory.
     """
     former_ghosts = []
-    def persistent_id(ob, former_ghosts=former_ghosts):
+    zclass_refs = {}
+
+    def persistent_id(ob, former_ghosts=former_ghosts,
+                      zclass_refs=zclass_refs):
         if getattr(ob, '_p_changed', 0) is None:
-            # Load temporarily
+            # Load temporarily.
             former_ghosts.append(ob)
             ob._p_changed = 0
+        if hasattr(ob, '__bases__'):
+            m = getattr(ob, '__module__', None)
+            if (m is not None
+                and isinstance(m, StringType)
+                and m.startswith('*')):
+                n = getattr(ob, '__name__', None)
+                if n is not None:
+                    # Pickling a ZClass instance.  Store the reference to
+                    # the ZClass class separately, so that the pickler
+                    # and unpickler don't trip over the apparently
+                    # missing module.
+                    ref = (m, n)
+                    zclass_refs[ref] = ob
+                    return ref
         return None
+
+    def persistent_load(ref, zclass_refs=zclass_refs):
+        return zclass_refs[ref]
+
     stream = StringIO()
     p = Pickler(stream, 1)
     p.persistent_id = persistent_id
@@ -42,5 +64,6 @@
         del former_ghosts[:]
     stream.seek(0)
     u = Unpickler(stream)
+    u.persistent_load = persistent_load
     return u.load()