home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / COPY_REG.PY < prev    next >
Encoding:
Python Source  |  2000-10-11  |  997 b   |  34 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. dispatch_table = {}
  10. safe_constructors = {}
  11.  
  12. def pickle(ob_type, pickle_function, constructor_ob=None):
  13.     if type(ob_type) is _ClassType:
  14.         raise TypeError("copy_reg is not intended for use with classes")
  15.  
  16.     if not callable(pickle_function):
  17.         raise TypeError("reduction functions must be callable")
  18.     dispatch_table[ob_type] = pickle_function
  19.  
  20.     if constructor_ob is not None:
  21.         constructor(constructor_ob)
  22.  
  23. def constructor(object):
  24.     if not callable(object):
  25.         raise TypeError("constructors must be callable")
  26.     safe_constructors[object] = 1
  27.  
  28. # Example: provide pickling support for complex numbers.
  29.  
  30. def pickle_complex(c):
  31.     return complex, (c.real, c.imag)
  32.  
  33. pickle(type(1j), pickle_complex, complex)
  34.