[Zope-CVS] CVS: Products/AdaptableStorage/zodb - RemainingState.py:1.4

Shane Hathaway shane@zope.com
Thu, 9 Jan 2003 18:03:40 -0500


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

Modified Files:
	RemainingState.py 
Log Message:
Made RemainingState show more info about UnpickleableErrors.


=== Products/AdaptableStorage/zodb/RemainingState.py 1.3 => 1.4 ===
--- Products/AdaptableStorage/zodb/RemainingState.py:1.3	Mon Jan  6 10:36:52 2003
+++ Products/AdaptableStorage/zodb/RemainingState.py	Thu Jan  9 18:03:36 2003
@@ -16,8 +16,9 @@
 $Id$
 """
 
+import os
 from cStringIO import StringIO
-from cPickle import Pickler, Unpickler
+from cPickle import Pickler, Unpickler, UnpickleableError
 from types import DictType
 
 from ZODB import Persistent
@@ -69,7 +70,40 @@
             return ref
 
         p.persistent_id = persistent_id
-        p.dump(state)
+        try:
+            p.dump(state)
+        except UnpickleableError, exc:
+            # Try to reveal which attribute is unpickleable.
+            attrname = None
+            attrvalue = None
+            for key, value in state.items():
+                del unmanaged[:]
+                outfile.seek(0)
+                outfile.truncate()
+                p = Pickler(outfile)
+                p.persistent_id = persistent_id
+                try:
+                    p.dump(value)
+                except UnpickleableError:
+                    attrname = key
+                    attrvalue = value
+                    break
+            if attrname is not None:
+                # Provide a more informative exception.
+                if os.environ.get('ZOPE_TRACE_UNPICKLEABLE'):
+                    # Provide an opportunity to examine
+                    # the "attrvalue" attribute.
+                    import pdb
+                    pdb.set_trace()
+                raise RuntimeError(
+                    'Unable to pickle the %s attribute, %s, '
+                    'of %s at %s.  %s.' % (
+                    repr(attrname), repr(attrvalue), repr(object),
+                    repr(event.getKeychain()), str(exc)))
+            else:
+                # Couldn't help.
+                raise
+
         p.dump(unmanaged)
         s = outfile.getvalue()
         event.addUnmanagedPersistentObjects(unmanaged)
@@ -88,7 +122,7 @@
             try:
                 unmanaged = u.load()
             except EOFError:
-                # old pickle
+                # old pickle with no list of unmanaged objects
                 pass
             else:
                 event.addUnmanagedPersistentObjects(unmanaged)