[Zodb-checkins] SVN: ZODB/trunk/src/BTrees/ fix picklability for the family objects; we cannot rely on modules being

Fred L. Drake, Jr. fdrake at gmail.com
Fri Apr 27 09:48:39 EDT 2007


Log message for revision 74856:
  fix picklability for the family objects; we cannot rely on modules being
  unpicklable, so implement pickle support directly (with tests)
  

Changed:
  U   ZODB/trunk/src/BTrees/__init__.py
  D   ZODB/trunk/src/BTrees/family32.py
  D   ZODB/trunk/src/BTrees/family64.py
  U   ZODB/trunk/src/BTrees/tests/testBTrees.py

-=-
Modified: ZODB/trunk/src/BTrees/__init__.py
===================================================================
--- ZODB/trunk/src/BTrees/__init__.py	2007-04-27 13:41:53 UTC (rev 74855)
+++ ZODB/trunk/src/BTrees/__init__.py	2007-04-27 13:48:39 UTC (rev 74856)
@@ -8,19 +8,62 @@
 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
+# FOR A PARTICULAR PURPOSE.
 #
 #############################################################################
 
-import BTrees.family64
-import BTrees.family32
+import zope.interface
+import BTrees.Interfaces
 
-BTrees.family64.IO.family = BTrees.family64
-BTrees.family64.OI.family = BTrees.family64
-BTrees.family64.IF.family = BTrees.family64
-BTrees.family64.II.family = BTrees.family64
 
-BTrees.family32.IO.family = BTrees.family32
-BTrees.family32.OI.family = BTrees.family32
-BTrees.family32.IF.family = BTrees.family32
-BTrees.family32.II.family = BTrees.family32
+class _Family(object):
+    zope.interface.implements(BTrees.Interfaces.IBTreeFamily)
+
+    from BTrees import OOBTree as OO
+
+class _Family32(_Family):
+    from BTrees import OIBTree as OI
+    from BTrees import IIBTree as II
+    from BTrees import IOBTree as IO
+    from BTrees import IFBTree as IF
+
+    maxint = int(2**31-1)
+    minint = -maxint - 1
+
+    def __reduce__(self):
+        return _family32, ()
+
+class _Family64(_Family):
+    from BTrees import OLBTree as OI
+    from BTrees import LLBTree as II
+    from BTrees import LOBTree as IO
+    from BTrees import LFBTree as IF
+
+    maxint = 2**63-1
+    minint = -maxint - 1
+
+    def __reduce__(self):
+        return _family64, ()
+
+def _family32():
+    return family32
+_family32.__safe_for_unpickling__ = True
+
+def _family64():
+    return family64
+_family64.__safe_for_unpickling__ = True
+
+
+family32 = _Family32()
+family64 = _Family64()
+
+
+BTrees.family64.IO.family = family64
+BTrees.family64.OI.family = family64
+BTrees.family64.IF.family = family64
+BTrees.family64.II.family = family64
+
+BTrees.family32.IO.family = family32
+BTrees.family32.OI.family = family32
+BTrees.family32.IF.family = family32
+BTrees.family32.II.family = family32

Deleted: ZODB/trunk/src/BTrees/family32.py
===================================================================
--- ZODB/trunk/src/BTrees/family32.py	2007-04-27 13:41:53 UTC (rev 74855)
+++ ZODB/trunk/src/BTrees/family32.py	2007-04-27 13:48:39 UTC (rev 74856)
@@ -1,27 +0,0 @@
-#############################################################################
-#
-# Copyright (c) 2007 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
-#
-#############################################################################
-
-import zope.interface
-import BTrees.Interfaces
-
-from BTrees import IOBTree as IO
-from BTrees import OIBTree as OI
-from BTrees import IFBTree as IF
-from BTrees import IIBTree as II
-from BTrees import OOBTree as OO
-
-maxint = int(2**31-1)
-minint = -maxint - 1
-
-zope.interface.moduleProvides(BTrees.Interfaces.IBTreeFamily)

Deleted: ZODB/trunk/src/BTrees/family64.py
===================================================================
--- ZODB/trunk/src/BTrees/family64.py	2007-04-27 13:41:53 UTC (rev 74855)
+++ ZODB/trunk/src/BTrees/family64.py	2007-04-27 13:48:39 UTC (rev 74856)
@@ -1,27 +0,0 @@
-#############################################################################
-#
-# Copyright (c) 2007 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE
-#
-#############################################################################
-
-import zope.interface
-import BTrees.Interfaces
-
-from BTrees import LOBTree as IO
-from BTrees import OLBTree as OI
-from BTrees import LFBTree as IF
-from BTrees import LLBTree as II
-from BTrees import OOBTree as OO
-
-maxint = 2**63-1
-minint = -maxint - 1
-
-zope.interface.moduleProvides(BTrees.Interfaces.IBTreeFamily)

Modified: ZODB/trunk/src/BTrees/tests/testBTrees.py
===================================================================
--- ZODB/trunk/src/BTrees/tests/testBTrees.py	2007-04-27 13:41:53 UTC (rev 74855)
+++ ZODB/trunk/src/BTrees/tests/testBTrees.py	2007-04-27 13:48:39 UTC (rev 74856)
@@ -11,7 +11,9 @@
 # FOR A PARTICULAR PURPOSE
 #
 ##############################################################################
+import pickle
 import random
+import StringIO
 from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
 from types import ClassType
 import zope.interface.verify
@@ -1712,6 +1714,7 @@
             s = IOTreeSet()
             s.insert(BTrees.family32.minint - 1)
             self.assert_(BTrees.family32.minint - 1 not in s)
+        self.check_pickling(BTrees.family32)
 
     def test64(self):
         self.assert_(
@@ -1736,7 +1739,42 @@
         s = LOTreeSet()
         self.assertRaises(ValueError, s.insert, BTrees.family64.maxint + 1)
         self.assertRaises(ValueError, s.insert, BTrees.family64.minint - 1)
+        self.check_pickling(BTrees.family64)
 
+    def check_pickling(self, family):
+        # The "family" objects are singletons; they can be pickled and
+        # unpickled, and the same instances will always be returned on
+        # unpickling, whether from the same unpickler or different
+        # unpicklers.
+        s = pickle.dumps((family, family))
+        (f1, f2) = pickle.loads(s)
+        self.failUnless(f1 is family)
+        self.failUnless(f2 is family)
+
+        # Using a single memo across multiple pickles:
+        sio = StringIO.StringIO()
+        p = pickle.Pickler(sio)
+        p.dump(family)
+        p.dump([family])
+        u = pickle.Unpickler(StringIO.StringIO(sio.getvalue()))
+        f1 = u.load()
+        f2, = u.load()
+        self.failUnless(f1 is family)
+        self.failUnless(f2 is family)
+
+        # Using separate memos for each pickle:
+        sio = StringIO.StringIO()
+        p = pickle.Pickler(sio)
+        p.dump(family)
+        p.clear_memo()
+        p.dump([family])
+        u = pickle.Unpickler(StringIO.StringIO(sio.getvalue()))
+        f1 = u.load()
+        f2, = u.load()
+        self.failUnless(f1 is family)
+        self.failUnless(f2 is family)
+
+
 def test_suite():
     s = TestSuite()
 



More information about the Zodb-checkins mailing list