2000-10-11 19:16:45 -03:00
|
|
|
"""Helper to provide extensibility for pickle/cPickle.
|
|
|
|
|
|
|
|
This is only useful to add pickle support for extension types defined in
|
|
|
|
C, not for instances of user-defined classes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from types import ClassType as _ClassType
|
1997-05-20 15:03:22 -03:00
|
|
|
|
2001-01-20 15:54:20 -04:00
|
|
|
__all__ = ["pickle","constructor"]
|
|
|
|
|
1997-04-09 14:44:11 -03:00
|
|
|
dispatch_table = {}
|
|
|
|
safe_constructors = {}
|
|
|
|
|
2000-10-11 19:16:45 -03:00
|
|
|
def pickle(ob_type, pickle_function, constructor_ob=None):
|
|
|
|
if type(ob_type) is _ClassType:
|
|
|
|
raise TypeError("copy_reg is not intended for use with classes")
|
|
|
|
|
|
|
|
if not callable(pickle_function):
|
|
|
|
raise TypeError("reduction functions must be callable")
|
1997-04-09 14:44:11 -03:00
|
|
|
dispatch_table[ob_type] = pickle_function
|
|
|
|
|
1997-05-20 15:03:22 -03:00
|
|
|
if constructor_ob is not None:
|
1997-04-09 14:44:11 -03:00
|
|
|
constructor(constructor_ob)
|
|
|
|
|
|
|
|
def constructor(object):
|
2000-10-11 19:16:45 -03:00
|
|
|
if not callable(object):
|
|
|
|
raise TypeError("constructors must be callable")
|
1997-04-09 14:44:11 -03:00
|
|
|
safe_constructors[object] = 1
|
|
|
|
|
1997-05-20 15:03:22 -03:00
|
|
|
# Example: provide pickling support for complex numbers.
|
|
|
|
|
1997-04-09 14:44:11 -03:00
|
|
|
def pickle_complex(c):
|
1997-05-20 15:03:22 -03:00
|
|
|
return complex, (c.real, c.imag)
|
1997-04-09 14:44:11 -03:00
|
|
|
|
1997-05-20 15:03:22 -03:00
|
|
|
pickle(type(1j), pickle_complex, complex)
|
2001-09-25 13:25:58 -03:00
|
|
|
|
|
|
|
# Support for picking new-style objects
|
|
|
|
|
|
|
|
def _reconstructor(cls, base, state):
|
2001-09-25 16:46:05 -03:00
|
|
|
obj = base.__new__(cls, state)
|
|
|
|
base.__init__(obj, state)
|
2001-09-25 13:25:58 -03:00
|
|
|
return obj
|
|
|
|
_reconstructor.__safe_for_unpickling__ = 1
|
|
|
|
|
|
|
|
_HEAPTYPE = 1<<9
|
|
|
|
|
|
|
|
def _reduce(self):
|
|
|
|
for base in self.__class__.__mro__:
|
2001-11-24 17:04:31 -04:00
|
|
|
if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
|
2001-09-25 13:25:58 -03:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
base = object # not really reachable
|
|
|
|
if base is object:
|
|
|
|
state = None
|
|
|
|
else:
|
2001-12-27 12:27:28 -04:00
|
|
|
if base is self.__class__:
|
|
|
|
raise TypeError, "can't pickle %s objects" % base.__name__
|
2001-09-25 13:25:58 -03:00
|
|
|
state = base(self)
|
2001-09-28 15:13:29 -03:00
|
|
|
args = (self.__class__, base, state)
|
|
|
|
try:
|
2001-11-24 17:04:31 -04:00
|
|
|
getstate = self.__getstate__
|
2001-09-28 15:13:29 -03:00
|
|
|
except AttributeError:
|
2001-11-24 17:04:31 -04:00
|
|
|
try:
|
|
|
|
dict = self.__dict__
|
|
|
|
except AttributeError:
|
|
|
|
dict = None
|
|
|
|
else:
|
|
|
|
dict = getstate()
|
2001-09-28 15:13:29 -03:00
|
|
|
if dict:
|
|
|
|
return _reconstructor, args, dict
|
|
|
|
else:
|
|
|
|
return _reconstructor, args
|