Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Pay attention to copy_reg
Browse files Browse the repository at this point in the history
Fixes the pickling of zope.securitypolicy.settings.{Allow,Deny,Unset},
makes zopefoundation/zope.securitypolicy#1
unnecessary.
  • Loading branch information
mgedmin committed Jan 20, 2014
1 parent dec66e1 commit 743091c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/mongopersist/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ def get_non_persistent_state(self, obj, seen):
getattr(obj, '_m_reference_safe', False)):
seen.append(id(obj))
# Get the state of the object. Only pickable objects can be reduced.
reduced = obj.__reduce__()
reduce_fn = copy_reg.dispatch_table.get(type(obj))
if reduce_fn is not None:
reduced = reduce_fn(obj)
else:
# XXX: __reduce_ex__
reduced = obj.__reduce__()
# The full object state (item 3) seems to be optional, so let's make
# sure we handle that case gracefully.
if isinstance(reduced, str):
Expand Down
9 changes: 9 additions & 0 deletions src/mongopersist/tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import doctest
import persistent
import pprint
import copy_reg

from bson import binary, dbref, objectid

Expand Down Expand Up @@ -60,6 +61,12 @@ def __reduce__(self):
return 'Constant'
Constant = Constant()

class CopyReggedConstant(object):
def custom_reduce_fn(self):
return 'CopyReggedConstant'
copy_reg.pickle(CopyReggedConstant, CopyReggedConstant.custom_reduce_fn)
CopyReggedConstant = CopyReggedConstant()


def doctest_ObjectSerializer():
"""Test the abstract ObjectSerializer class.
Expand Down Expand Up @@ -328,6 +335,8 @@ def doctest_ObjectWriter_get_state_constant():
{'_py_constant': 'mongopersist.tests.test_serialize.Constant'}
>>> writer.get_state(interfaces.IObjectWriter)
{'_py_constant': 'mongopersist.interfaces.IObjectWriter'}
>>> writer.get_state(CopyReggedConstant)
{'_py_constant': 'mongopersist.tests.test_serialize.CopyReggedConstant'}
"""

def doctest_ObjectWriter_get_state_types():
Expand Down

0 comments on commit 743091c

Please sign in to comment.