Commit 6fe7ef89 authored by Fred Drake's avatar Fred Drake

fix picklability for the family objects; we cannot rely on modules being

unpicklable, so implement pickle support directly (with tests)
parent a4ccd5f0
...@@ -8,19 +8,62 @@ ...@@ -8,19 +8,62 @@
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE.
# #
############################################################################# #############################################################################
import BTrees.family64 import zope.interface
import BTrees.family32 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 class _Family(object):
BTrees.family32.OI.family = BTrees.family32 zope.interface.implements(BTrees.Interfaces.IBTreeFamily)
BTrees.family32.IF.family = BTrees.family32
BTrees.family32.II.family = BTrees.family32 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
#############################################################################
#
# 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)
#############################################################################
#
# 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)
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
import pickle
import random import random
import StringIO
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
from types import ClassType from types import ClassType
import zope.interface.verify import zope.interface.verify
...@@ -1712,6 +1714,7 @@ class FamilyTest(TestCase): ...@@ -1712,6 +1714,7 @@ class FamilyTest(TestCase):
s = IOTreeSet() s = IOTreeSet()
s.insert(BTrees.family32.minint - 1) s.insert(BTrees.family32.minint - 1)
self.assert_(BTrees.family32.minint - 1 not in s) self.assert_(BTrees.family32.minint - 1 not in s)
self.check_pickling(BTrees.family32)
def test64(self): def test64(self):
self.assert_( self.assert_(
...@@ -1736,6 +1739,41 @@ class FamilyTest(TestCase): ...@@ -1736,6 +1739,41 @@ class FamilyTest(TestCase):
s = LOTreeSet() s = LOTreeSet()
self.assertRaises(ValueError, s.insert, BTrees.family64.maxint + 1) self.assertRaises(ValueError, s.insert, BTrees.family64.maxint + 1)
self.assertRaises(ValueError, s.insert, BTrees.family64.minint - 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(): def test_suite():
s = TestSuite() s = TestSuite()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment