blob: 8a3550aad3ff4e24dbe5cf3adc611ba7b9740e42 [file] [log] [blame]
Fred Drake78a6a362000-10-11 22:16:45 +00001"""Helper to provide extensibility for pickle/cPickle.
2
3This is only useful to add pickle support for extension types defined in
4C, not for instances of user-defined classes.
5"""
6
7from types import ClassType as _ClassType
Guido van Rossum72be3061997-05-20 18:03:22 +00008
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00009__all__ = ["pickle","constructor"]
10
Guido van Rossum47065621997-04-09 17:44:11 +000011dispatch_table = {}
12safe_constructors = {}
13
Fred Drake78a6a362000-10-11 22:16:45 +000014def pickle(ob_type, pickle_function, constructor_ob=None):
15 if type(ob_type) is _ClassType:
16 raise TypeError("copy_reg is not intended for use with classes")
17
18 if not callable(pickle_function):
19 raise TypeError("reduction functions must be callable")
Guido van Rossum47065621997-04-09 17:44:11 +000020 dispatch_table[ob_type] = pickle_function
21
Guido van Rossum72be3061997-05-20 18:03:22 +000022 if constructor_ob is not None:
Guido van Rossum47065621997-04-09 17:44:11 +000023 constructor(constructor_ob)
24
25def constructor(object):
Fred Drake78a6a362000-10-11 22:16:45 +000026 if not callable(object):
27 raise TypeError("constructors must be callable")
Guido van Rossum47065621997-04-09 17:44:11 +000028 safe_constructors[object] = 1
29
Guido van Rossum72be3061997-05-20 18:03:22 +000030# Example: provide pickling support for complex numbers.
Guido van Rossum47065621997-04-09 17:44:11 +000031
Guido van Rossum72be3061997-05-20 18:03:22 +000032def pickle_complex(c):
33 return complex, (c.real, c.imag)
34
35pickle(type(1j), pickle_complex, complex)
Guido van Rossum3926a632001-09-25 16:25:58 +000036
37# Support for picking new-style objects
38
Guido van Rossum3926a632001-09-25 16:25:58 +000039def _reconstructor(cls, base, state):
Guido van Rossum698acf92001-09-25 19:46:05 +000040 obj = base.__new__(cls, state)
41 base.__init__(obj, state)
Guido van Rossum3926a632001-09-25 16:25:58 +000042 return obj
43_reconstructor.__safe_for_unpickling__ = 1
44
45_HEAPTYPE = 1<<9
46
47def _reduce(self):
48 for base in self.__class__.__mro__:
Guido van Rossum00fb0c92001-11-24 21:04:31 +000049 if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
Guido van Rossum3926a632001-09-25 16:25:58 +000050 break
51 else:
52 base = object # not really reachable
53 if base is object:
54 state = None
55 else:
Guido van Rossum2a6f5b32001-12-27 16:27:28 +000056 if base is self.__class__:
57 raise TypeError, "can't pickle %s objects" % base.__name__
Guido van Rossum3926a632001-09-25 16:25:58 +000058 state = base(self)
Guido van Rossum6cef6d52001-09-28 18:13:29 +000059 args = (self.__class__, base, state)
60 try:
Guido van Rossum00fb0c92001-11-24 21:04:31 +000061 getstate = self.__getstate__
Guido van Rossum6cef6d52001-09-28 18:13:29 +000062 except AttributeError:
Guido van Rossum00fb0c92001-11-24 21:04:31 +000063 try:
64 dict = self.__dict__
65 except AttributeError:
66 dict = None
67 else:
68 dict = getstate()
Guido van Rossum6cef6d52001-09-28 18:13:29 +000069 if dict:
70 return _reconstructor, args, dict
71 else:
72 return _reconstructor, args