Skip to content

Commit

Permalink
Use a Python __reduce__ method to make pickles match C.
Browse files Browse the repository at this point in the history
On it's own, this breaks on protocols >= 2 (including Python 3 by
default) because the pickled object's class is required to match the
reduce output. One fix would be to change the pickle format to use a
custom constructor function, but then we'd have different pickle formats
in C and Python.

Rather than do that, we instead create a __class__ property for the
python implementation. This bypasses the protocol limitations. The
__class__ property wasn't assignable in the C implementation, so the
compatibility risk should be very small.

All the special pickle machinery is only defined in Python if needed.
  • Loading branch information
jamadden committed Jan 12, 2016
1 parent f1f88b6 commit 0718a45
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 53 deletions.
5 changes: 4 additions & 1 deletion BTrees/IFBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'IFBucket', 'IFSet', 'IFBTree', 'IFTreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection', 'multiunion',
)

Expand All @@ -38,6 +38,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 120
_TREE_SIZE = 500
Expand Down Expand Up @@ -134,4 +135,6 @@ class IFTreeIteratorPy(_TreeIterator):
BTree = IFBTree
TreeSet = IFTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerFloatBTreeModule)
5 changes: 4 additions & 1 deletion BTrees/IIBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'IIBucket', 'IISet', 'IIBTree', 'IITreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection', 'multiunion',
)

Expand All @@ -38,6 +38,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 120
_TREE_SIZE = 500
Expand Down Expand Up @@ -135,4 +136,6 @@ class IITreeIteratorPy(_TreeIterator):
BTree = IIBTree
TreeSet = IITreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerIntegerBTreeModule)
3 changes: 3 additions & 0 deletions BTrees/IOBTree.py
Expand Up @@ -33,6 +33,7 @@
from ._base import to_int as _to_key
from ._base import to_ob as _to_value
from ._base import union as _union
from ._base import _fix_pickle

_BUCKET_SIZE = 60
_TREE_SIZE = 500
Expand Down Expand Up @@ -113,4 +114,6 @@ class IOTreeIteratorPy(_TreeIterator):
BTree = IOBTree
TreeSet = IOTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerObjectBTreeModule)
5 changes: 4 additions & 1 deletion BTrees/LFBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'LFBucket', 'LFSet', 'LFBTree', 'LFTreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection', 'multiunion',
)

Expand All @@ -38,6 +38,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 120
_TREE_SIZE = 500
Expand Down Expand Up @@ -135,4 +136,6 @@ class LFTreeIteratorPy(_TreeIterator):
BTree = LFBTree
TreeSet = LFTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerFloatBTreeModule)
5 changes: 4 additions & 1 deletion BTrees/LLBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'LLBucket', 'LLSet', 'LLBTree', 'LLTreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection', 'multiunion',
)

Expand All @@ -38,6 +38,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 120
_TREE_SIZE = 500
Expand Down Expand Up @@ -135,4 +136,6 @@ class LLTreeIteratorPy(_TreeIterator):
BTree = LLBTree
TreeSet = LLTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerIntegerBTreeModule)
3 changes: 3 additions & 0 deletions BTrees/LOBTree.py
Expand Up @@ -33,6 +33,7 @@
from ._base import to_long as _to_key
from ._base import to_ob as _to_value
from ._base import union as _union
from ._base import _fix_pickle

_BUCKET_SIZE = 60
_TREE_SIZE = 500
Expand Down Expand Up @@ -114,4 +115,6 @@ class LOTreeIteratorPy(_TreeIterator):
BTree = LOBTree
TreeSet = LOTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IIntegerObjectBTreeModule)
5 changes: 4 additions & 1 deletion BTrees/OIBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'OIBucket', 'OISet', 'OIBTree', 'OITreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection',
)

Expand All @@ -37,6 +37,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 60
_TREE_SIZE = 250
Expand Down Expand Up @@ -131,4 +132,6 @@ class OITreeIteratorPy(_TreeIterator):
BTree = OIBTree
TreeSet = OITreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IObjectIntegerBTreeModule)
5 changes: 4 additions & 1 deletion BTrees/OLBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'OLBucket', 'OLSet', 'OLBTree', 'OLTreeSet',
'union', 'intersection', 'difference',
'union', 'intersection', 'difference',
'weightedUnion', 'weightedIntersection',
)

Expand All @@ -37,6 +37,7 @@
from ._base import union as _union
from ._base import weightedIntersection as _weightedIntersection
from ._base import weightedUnion as _weightedUnion
from ._base import _fix_pickle

_BUCKET_SIZE = 60
_TREE_SIZE = 250
Expand Down Expand Up @@ -131,4 +132,6 @@ class OLTreeIteratorPy(_TreeIterator):
BTree = OLBTree
TreeSet = OLTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IObjectIntegerBTreeModule)
7 changes: 6 additions & 1 deletion BTrees/OOBTree.py
Expand Up @@ -14,7 +14,7 @@

__all__ = ('Bucket', 'Set', 'BTree', 'TreeSet',
'OOBucket', 'OOSet', 'OOBTree', 'OOTreeSet',
'union', 'intersection','difference',
'union', 'intersection','difference',
)

from zope.interface import moduleProvides
Expand All @@ -31,6 +31,7 @@
from ._base import to_ob as _to_key
from ._base import to_ob as _to_value
from ._base import union as _union
from ._base import _fix_pickle

_BUCKET_SIZE = 30
_TREE_SIZE = 250
Expand Down Expand Up @@ -102,9 +103,13 @@ class OOTreeIteratorPy(_TreeIterator):
from ._OOBTree import union
from ._OOBTree import intersection



Bucket = OOBucket
Set = OOSet
BTree = OOBTree
TreeSet = OOTreeSet

_fix_pickle(globals(), __name__)

moduleProvides(IObjectObjectBTreeModule)

0 comments on commit 0718a45

Please sign in to comment.