1"""Helper to provide extensibility for pickle.
2
3This is only useful to add pickle support for extension types defined in
4C, not for instances of user-defined classes.
5"""
6
7__all__ = ["pickle", "constructor",
8           "add_extension", "remove_extension", "clear_extension_cache"]
9
10dispatch_table = {}
11
12def pickle(ob_type, pickle_function, constructor_ob=None):
13    if not callable(pickle_function):
14        raise TypeError("reduction functions must be callable")
15    dispatch_table[ob_type] = pickle_function
16
17    # The constructor_ob function is a vestige of safe for unpickling.
18    # There is no reason for the caller to pass it anymore.
19    if constructor_ob is not None:
20        constructor(constructor_ob)
21
22def constructor(object):
23    if not callable(object):
24        raise TypeError("constructors must be callable")
25
26# Example: provide pickling support for complex numbers.
27
28try:
29    complex
30except NameError:
31    pass
32else:
33
34    def pickle_complex(c):
35        return complex, (c.real, c.imag)
36
37    pickle(complex, pickle_complex, complex)
38
39# Support for pickling new-style objects
40
41def _reconstructor(cls, base, state):
42    if base is object:
43        obj = object.__new__(cls)
44    else:
45        obj = base.__new__(cls, state)
46        if base.__init__ != object.__init__:
47            base.__init__(obj, state)
48    return obj
49
50_HEAPTYPE = 1<<9
51
52# Python code for object.__reduce_ex__ for protocols 0 and 1
53
54def _reduce_ex(self, proto):
55    assert proto < 2
56    for base in self.__class__.__mro__:
57        if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
58            break
59    else:
60        base = object # not really reachable
61    if base is object:
62        state = None
63    else:
64        if base is self.__class__:
65            raise TypeError("can't pickle %s objects" % base.__name__)
66        state = base(self)
67    args = (self.__class__, base, state)
68    try:
69        getstate = self.__getstate__
70    except AttributeError:
71        if getattr(self, "__slots__", None):
72            raise TypeError("a class that defines __slots__ without "
73                            "defining __getstate__ cannot be pickled")
74        try:
75            dict = self.__dict__
76        except AttributeError:
77            dict = None
78    else:
79        dict = getstate()
80    if dict:
81        return _reconstructor, args, dict
82    else:
83        return _reconstructor, args
84
85# Helper for __reduce_ex__ protocol 2
86
87def __newobj__(cls, *args):
88    return cls.__new__(cls, *args)
89
90def __newobj_ex__(cls, args, kwargs):
91    """Used by pickle protocol 4, instead of __newobj__ to allow classes with
92    keyword-only arguments to be pickled correctly.
93    """
94    return cls.__new__(cls, *args, **kwargs)
95
96def _slotnames(cls):
97    """Return a list of slot names for a given class.
98
99    This needs to find slots defined by the class and its bases, so we
100    can't simply return the __slots__ attribute.  We must walk down
101    the Method Resolution Order and concatenate the __slots__ of each
102    class found there.  (This assumes classes don't modify their
103    __slots__ attribute to misrepresent their slots after the class is
104    defined.)
105    """
106
107    # Get the value from a cache in the class if possible
108    names = cls.__dict__.get("__slotnames__")
109    if names is not None:
110        return names
111
112    # Not cached -- calculate the value
113    names = []
114    if not hasattr(cls, "__slots__"):
115        # This class has no slots
116        pass
117    else:
118        # Slots found -- gather slot names from all base classes
119        for c in cls.__mro__:
120            if "__slots__" in c.__dict__:
121                slots = c.__dict__['__slots__']
122                # if class has a single slot, it can be given as a string
123                if isinstance(slots, str):
124                    slots = (slots,)
125                for name in slots:
126                    # special descriptors
127                    if name in ("__dict__", "__weakref__"):
128                        continue
129                    # mangled names
130                    elif name.startswith('__') and not name.endswith('__'):
131                        names.append('_%s%s' % (c.__name__, name))
132                    else:
133                        names.append(name)
134
135    # Cache the outcome in the class if at all possible
136    try:
137        cls.__slotnames__ = names
138    except:
139        pass # But don't die if we can't
140
141    return names
142
143# A registry of extension codes.  This is an ad-hoc compression
144# mechanism.  Whenever a global reference to <module>, <name> is about
145# to be pickled, the (<module>, <name>) tuple is looked up here to see
146# if it is a registered extension code for it.  Extension codes are
147# universal, so that the meaning of a pickle does not depend on
148# context.  (There are also some codes reserved for local use that
149# don't have this restriction.)  Codes are positive ints; 0 is
150# reserved.
151
152_extension_registry = {}                # key -> code
153_inverted_registry = {}                 # code -> key
154_extension_cache = {}                   # code -> object
155# Don't ever rebind those names:  pickling grabs a reference to them when
156# it's initialized, and won't see a rebinding.
157
158def add_extension(module, name, code):
159    """Register an extension code."""
160    code = int(code)
161    if not 1 <= code <= 0x7fffffff:
162        raise ValueError("code out of range")
163    key = (module, name)
164    if (_extension_registry.get(key) == code and
165        _inverted_registry.get(code) == key):
166        return # Redundant registrations are benign
167    if key in _extension_registry:
168        raise ValueError("key %s is already registered with code %s" %
169                         (key, _extension_registry[key]))
170    if code in _inverted_registry:
171        raise ValueError("code %s is already in use for key %s" %
172                         (code, _inverted_registry[code]))
173    _extension_registry[key] = code
174    _inverted_registry[code] = key
175
176def remove_extension(module, name, code):
177    """Unregister an extension code.  For testing only."""
178    key = (module, name)
179    if (_extension_registry.get(key) != code or
180        _inverted_registry.get(code) != key):
181        raise ValueError("key %s is not registered with code %s" %
182                         (key, code))
183    del _extension_registry[key]
184    del _inverted_registry[code]
185    if code in _extension_cache:
186        del _extension_cache[code]
187
188def clear_extension_cache():
189    _extension_cache.clear()
190
191# Standard extension code assignments
192
193# Reserved ranges
194
195# First  Last Count  Purpose
196#     1   127   127  Reserved for Python standard library
197#   128   191    64  Reserved for Zope
198#   192   239    48  Reserved for 3rd parties
199#   240   255    16  Reserved for private use (will never be assigned)
200#   256   Inf   Inf  Reserved for future assignment
201
202# Extension codes are assigned by the Python Software Foundation.
203