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