Improve __getstate__ for bootstrapinfo

This approach may be a little hacked, but it works for now and
if it breaks at some point in the future because of e.g. circular references
that bridge will have to be crossed then
This commit is contained in:
Anders Ingemann 2015-03-25 20:36:06 +01:00
parent 51bb3dd57f
commit 25051d4c04

View file

@ -115,13 +115,22 @@ class BootstrapInformation(object):
return manifest_vars return manifest_vars
def __getstate__(self): def __getstate__(self):
state = self.__dict__.copy() from bootstrapvz.remote import supported_classes
exclude_keys = ['source_lists', 'preference_lists', 'packages']
for key in exclude_keys: def can_serialize(obj):
# We may have been serialized before, if hasattr(obj, '__class__') and hasattr(obj, '__module__'):
# so don't fail if the keys do not exist class_name = obj.__module__ + '.' + obj.__class__.__name__
if key in state: return class_name in supported_classes or isinstance(obj, (BaseException, Exception))
del state[key] return True
def filter_state(state):
if isinstance(state, dict):
return {key: filter_state(val) for key, val in state.items() if can_serialize(val)}
if isinstance(state, (set, tuple, list, frozenset)):
return type(state)(filter_state(val) for val in state if can_serialize(val))
return state
state = filter_state(self.__dict__)
state['__class__'] = self.__module__ + '.' + self.__class__.__name__ state['__class__'] = self.__module__ + '.' + self.__class__.__name__
return state return state