[Zodb-checkins] CVS: Zope3/lib/python/Persistence/tests - testPersistence.py:1.4

Jim Fulton jim@zope.com
Mon, 7 Oct 2002 10:46:24 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence/tests
In directory cvs.zope.org:/tmp/cvs-serv15925/lib/python/Persistence/tests

Modified Files:
	testPersistence.py 
Log Message:
Fixed bug: Persistent objects were not picklable. 

Even though they could be stored in the ZODB, there could not be
pickled because they didn't implement a working __reduce__ method.

I added a simple function, simple_new, that gets passed a type and
simply returns the result of calling it's __new__ method.  I added a
__reduce__ method that uses this function.  Note that this will not
work for any types with __new__ methods that require any arguments
other than the type itself.



=== Zope3/lib/python/Persistence/tests/testPersistence.py 1.3 => 1.4 ===
--- Zope3/lib/python/Persistence/tests/testPersistence.py:1.3	Fri Jun 21 18:40:14 2002
+++ Zope3/lib/python/Persistence/tests/testPersistence.py	Mon Oct  7 10:46:23 2002
@@ -124,6 +124,7 @@
         p = self.klass()
         self.assert_(IPersistent.isImplementedBy(p),
                      "%s does not implement IPersistent" % p)
+        
 
 from Persistence import Persistent, BasePersistent
 
@@ -133,6 +134,12 @@
     def inc(self):
         self.x += 1
 
+class P2(P):
+    def __getstate__(self):
+        return 42
+    def __setstate__(self, v):
+        self.v = v
+
 class B(BasePersistent):
 
     __slots__ = ["x"]
@@ -160,6 +167,25 @@
 class PersistentTest(Test):
     klass = P
     has_dict = 1
+
+    def testPicklable(self):
+        import pickle
+
+        p = P()
+        p.inc()
+        p2 = pickle.loads(pickle.dumps(p))
+        self.assertEqual(p2.__class__, P);
+        self.assertEqual(p2.__dict__, p.__dict__)
+
+
+    def testPicklableWCustomState(self):
+        import pickle
+            
+        p = P2()
+        p2 = pickle.loads(pickle.dumps(p))
+        self.assertEqual(p2.__class__, P2);
+        self.assertEqual(p2.__dict__, {'v': 42})
+            
 
 class BasePersistentTest(Test):
     klass = B