home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_copy_reg.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  1.0 KB  |  36 lines

  1. """Helper to provide extensibility for pickle/cPickle.
  2.  
  3. This is only useful to add pickle support for extension types defined in
  4. C, not for instances of user-defined classes.
  5. """
  6.  
  7. from types import ClassType as _ClassType
  8.  
  9. __all__ = ["pickle","constructor"]
  10.  
  11. dispatch_table = {}
  12. safe_constructors = {}
  13.  
  14. def 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")
  20.     dispatch_table[ob_type] = pickle_function
  21.  
  22.     if constructor_ob is not None:
  23.         constructor(constructor_ob)
  24.  
  25. def constructor(object):
  26.     if not callable(object):
  27.         raise TypeError("constructors must be callable")
  28.     safe_constructors[object] = 1
  29.  
  30. # Example: provide pickling support for complex numbers.
  31.  
  32. def pickle_complex(c):
  33.     return complex, (c.real, c.imag)
  34.  
  35. pickle(type(1j), pickle_complex, complex)
  36.