Back to index

python3.2  3.2.2
Functions | Variables
copyreg Namespace Reference

Functions

def pickle
def constructor
def pickle_complex
def _reconstructor
def _reduce_ex
def __newobj__
def _slotnames
def add_extension
def remove_extension
def clear_extension_cache

Variables

list __all__
dictionary dispatch_table = {}
int _HEAPTYPE = 1
dictionary _extension_registry = {}
dictionary _inverted_registry = {}
dictionary _extension_cache = {}

Detailed Description

Helper to provide extensibility for pickle.

This is only useful to add pickle support for extension types defined in
C, not for instances of user-defined classes.

Function Documentation

def copyreg.__newobj__ (   cls,
  args 
)

Definition at line 87 of file copyreg.py.

00087 
00088 def __newobj__(cls, *args):
00089     return cls.__new__(cls, *args)

def copyreg._reconstructor (   cls,
  base,
  state 
) [private]

Definition at line 41 of file copyreg.py.

00041 
00042 def _reconstructor(cls, base, state):
00043     if base is object:
00044         obj = object.__new__(cls)
00045     else:
00046         obj = base.__new__(cls, state)
00047         if base.__init__ != object.__init__:
00048             base.__init__(obj, state)
00049     return obj

def copyreg._reduce_ex (   self,
  proto 
) [private]

Definition at line 54 of file copyreg.py.

00054 
00055 def _reduce_ex(self, proto):
00056     assert proto < 2
00057     for base in self.__class__.__mro__:
00058         if hasattr(base, '__flags__') and not base.__flags__ & _HEAPTYPE:
00059             break
00060     else:
00061         base = object # not really reachable
00062     if base is object:
00063         state = None
00064     else:
00065         if base is self.__class__:
00066             raise TypeError("can't pickle %s objects" % base.__name__)
00067         state = base(self)
00068     args = (self.__class__, base, state)
00069     try:
00070         getstate = self.__getstate__
00071     except AttributeError:
00072         if getattr(self, "__slots__", None):
00073             raise TypeError("a class that defines __slots__ without "
00074                             "defining __getstate__ cannot be pickled")
00075         try:
00076             dict = self.__dict__
00077         except AttributeError:
00078             dict = None
00079     else:
00080         dict = getstate()
00081     if dict:
00082         return _reconstructor, args, dict
00083     else:
00084         return _reconstructor, args
00085 
00086 # Helper for __reduce_ex__ protocol 2

Here is the call graph for this function:

def copyreg._slotnames (   cls) [private]
Return a list of slot names for a given class.

This needs to find slots defined by the class and its bases, so we
can't simply return the __slots__ attribute.  We must walk down
the Method Resolution Order and concatenate the __slots__ of each
class found there.  (This assumes classes don't modify their
__slots__ attribute to misrepresent their slots after the class is
defined.)

Definition at line 90 of file copyreg.py.

00090 
00091 def _slotnames(cls):
00092     """Return a list of slot names for a given class.
00093 
00094     This needs to find slots defined by the class and its bases, so we
00095     can't simply return the __slots__ attribute.  We must walk down
00096     the Method Resolution Order and concatenate the __slots__ of each
00097     class found there.  (This assumes classes don't modify their
00098     __slots__ attribute to misrepresent their slots after the class is
00099     defined.)
00100     """
00101 
00102     # Get the value from a cache in the class if possible
00103     names = cls.__dict__.get("__slotnames__")
00104     if names is not None:
00105         return names
00106 
00107     # Not cached -- calculate the value
00108     names = []
00109     if not hasattr(cls, "__slots__"):
00110         # This class has no slots
00111         pass
00112     else:
00113         # Slots found -- gather slot names from all base classes
00114         for c in cls.__mro__:
00115             if "__slots__" in c.__dict__:
00116                 slots = c.__dict__['__slots__']
00117                 # if class has a single slot, it can be given as a string
00118                 if isinstance(slots, str):
00119                     slots = (slots,)
00120                 for name in slots:
00121                     # special descriptors
00122                     if name in ("__dict__", "__weakref__"):
00123                         continue
00124                     # mangled names
00125                     elif name.startswith('__') and not name.endswith('__'):
00126                         names.append('_%s%s' % (c.__name__, name))
00127                     else:
00128                         names.append(name)
00129 
00130     # Cache the outcome in the class if at all possible
00131     try:
00132         cls.__slotnames__ = names
00133     except:
00134         pass # But don't die if we can't
00135 
00136     return names
00137 
00138 # A registry of extension codes.  This is an ad-hoc compression
00139 # mechanism.  Whenever a global reference to <module>, <name> is about
00140 # to be pickled, the (<module>, <name>) tuple is looked up here to see
00141 # if it is a registered extension code for it.  Extension codes are
00142 # universal, so that the meaning of a pickle does not depend on
00143 # context.  (There are also some codes reserved for local use that
00144 # don't have this restriction.)  Codes are positive ints; 0 is
00145 # reserved.

Here is the caller graph for this function:

def copyreg.add_extension (   module,
  name,
  code 
)
Register an extension code.

Definition at line 152 of file copyreg.py.

00152 
00153 def add_extension(module, name, code):
00154     """Register an extension code."""
00155     code = int(code)
00156     if not 1 <= code <= 0x7fffffff:
00157         raise ValueError("code out of range")
00158     key = (module, name)
00159     if (_extension_registry.get(key) == code and
00160         _inverted_registry.get(code) == key):
00161         return # Redundant registrations are benign
00162     if key in _extension_registry:
00163         raise ValueError("key %s is already registered with code %s" %
00164                          (key, _extension_registry[key]))
00165     if code in _inverted_registry:
00166         raise ValueError("code %s is already in use for key %s" %
00167                          (code, _inverted_registry[code]))
00168     _extension_registry[key] = code
00169     _inverted_registry[code] = key

Here is the caller graph for this function:

Definition at line 182 of file copyreg.py.

00182 
00183 def clear_extension_cache():
00184     _extension_cache.clear()
00185 
00186 # Standard extension code assignments
00187 
00188 # Reserved ranges
00189 
00190 # First  Last Count  Purpose
00191 #     1   127   127  Reserved for Python standard library
00192 #   128   191    64  Reserved for Zope
00193 #   192   239    48  Reserved for 3rd parties
00194 #   240   255    16  Reserved for private use (will never be assigned)
00195 #   256   Inf   Inf  Reserved for future assignment
00196 
00197 # Extension codes are assigned by the Python Software Foundation.
def copyreg.constructor (   object)

Definition at line 22 of file copyreg.py.

00022 
00023 def constructor(object):
00024     if not hasattr(object, '__call__'):
00025         raise TypeError("constructors must be callable")
00026 
00027 # Example: provide pickling support for complex numbers.
00028 
00029 try:
    complex

Here is the caller graph for this function:

def copyreg.pickle (   ob_type,
  pickle_function,
  constructor_ob = None 
)

Definition at line 12 of file copyreg.py.

00012 
00013 def pickle(ob_type, pickle_function, constructor_ob=None):
00014     if not hasattr(pickle_function, '__call__'):
00015         raise TypeError("reduction functions must be callable")
00016     dispatch_table[ob_type] = pickle_function
00017 
00018     # The constructor_ob function is a vestige of safe for unpickling.
00019     # There is no reason for the caller to pass it anymore.
00020     if constructor_ob is not None:
00021         constructor(constructor_ob)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 34 of file copyreg.py.

00034 
00035     def pickle_complex(c):
00036         return complex, (c.real, c.imag)

def copyreg.remove_extension (   module,
  name,
  code 
)
Unregister an extension code.  For testing only.

Definition at line 170 of file copyreg.py.

00170 
00171 def remove_extension(module, name, code):
00172     """Unregister an extension code.  For testing only."""
00173     key = (module, name)
00174     if (_extension_registry.get(key) != code or
00175         _inverted_registry.get(code) != key):
00176         raise ValueError("key %s is not registered with code %s" %
00177                          (key, code))
00178     del _extension_registry[key]
00179     del _inverted_registry[code]
00180     if code in _extension_cache:
00181         del _extension_cache[code]

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 ["pickle", "constructor",
00002            "add_extension", "remove_extension", "clear_extension_cache"]

Definition at line 7 of file copyreg.py.

Definition at line 148 of file copyreg.py.

Definition at line 146 of file copyreg.py.

Definition at line 50 of file copyreg.py.

Definition at line 147 of file copyreg.py.

Definition at line 10 of file copyreg.py.