logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

copyreg.py (7426B)


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