blob: 1276564b326cda9b2e33931744ac63e068b6170b [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
Guido van Rossum255f3ee2003-01-29 06:14:11 +000073
74# A registry of extension codes. This is an ad-hoc compression
75# mechanism. Whenever a global reference to <module>, <name> is about
76# to be pickled, the (<module>, <name>) tuple is looked up here to see
77# if it is a registered extension code for it. Extension codes are
78# universal, so that the meaning of a pickle does not depend on
79# context. (There are also some codes reserved for local use that
80# don't have this restriction.) Codes are positive ints; 0 is
81# reserved.
82
83extension_registry = {} # key -> code
84inverted_registry = {} # code -> key
85extension_cache = {} # code -> object
86
87def add_extension(module, name, code):
88 """Register an extension code."""
89 code = int(code)
90 if not 1 <= code < 0x7fffffff:
91 raise ValueError, "code out of range"
92 key = (module, name)
93 if (extension_registry.get(key) == code and
94 inverted_registry.get(code) == key):
95 return # Redundant registrations are benign
96 if key in extension_registry:
97 raise ValueError("key %s is already registered with code %s" %
98 (key, extension_registry[key]))
99 if code in inverted_registry:
100 raise ValueError("code %s is already in use for key %s" %
101 (code, inverted_registry[code]))
102 extension_registry[key] = code
103 inverted_registry[code] = key
104
105def remove_extension(module, name, code):
106 """Unregister an extension code. For testing only."""
107 key = (module, name)
108 if (extension_registry.get(key) != code or
109 inverted_registry.get(code) != key):
110 raise ValueError("key %s is not registered with code %s" %
111 (key, code))
112 del extension_registry[key]
113 del inverted_registry[code]
114 if code in extension_cache:
115 del extension_cache[code]
116
117def clear_extension_cache():
118 extension_cache.clear()
119
120# Standard extension code assignments
121
122# Reserved ranges
123
124# First Last Count Purpose
125# 1 127 127 Reserved for Python standard library
126# 128 191 64 Reserved for Zope 3
127# 192 239 48 Reserved for 3rd parties
128# 240 255 16 Reserved for private use (will never be assigned)
129# 256 Inf Inf Reserved for future assignment
130
131# Extension codes are assigned by the Python Software Foundation.