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

Commit

Permalink
Support serialization of objects that return a single string from __r…
Browse files Browse the repository at this point in the history
…educe__().

This allows us to serialize module globals, especially interfacaes and
classes.
  • Loading branch information
strichter committed Dec 15, 2013
1 parent 57b940d commit 4846b3c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGES.txt
Expand Up @@ -5,6 +5,9 @@ CHANGES
0.9.0 (unreleased)
------------------

- Feature: Support serialization of objects that return a single string from
``__reduce__()``.

- Feature: Added a container-level cache to the ``MongoContainer``
object. This cache is only used within the transaction, but it reduced calls
to Mongo by 40% in a real world use. The cache can be turned off by setting
Expand Down Expand Up @@ -49,7 +52,6 @@ CHANGES
flushed.



0.8.4 (2013-06-13)
------------------

Expand Down
6 changes: 6 additions & 0 deletions src/mongopersist/serialize.py
Expand Up @@ -152,6 +152,10 @@ def get_non_persistent_state(self, obj, seen):
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):
# When the reduced state is just a string it represents a name in
# a module. The module will be extrated from __module__.
return {'_py_constant': obj.__module__+'.'+reduced[0]}
if len(reduced) == 2:
factory, args = reduced
obj_state = {}
Expand Down Expand Up @@ -462,6 +466,8 @@ def resolve(self, dbref):
return klass

def get_non_persistent_object(self, state, obj):
if '_py_constant' in state:
return self.simple_resolve(state.pop('_py_constant'))
if '_py_type' in state:
# Handle the simplified case.
klass = self.simple_resolve(state.pop('_py_type'))
Expand Down

0 comments on commit 4846b3c

Please sign in to comment.