home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / copy_reg.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  2.8 KB  |  78 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Helper to provide extensibility for pickle/cPickle.
  5.  
  6. This is only useful to add pickle support for extension types defined in
  7. C, not for instances of user-defined classes.
  8. '''
  9. from types import ClassType as _ClassType
  10. __all__ = [
  11.     'pickle',
  12.     'constructor']
  13. dispatch_table = { }
  14. safe_constructors = { }
  15.  
  16. def pickle(ob_type, pickle_function, constructor_ob = None):
  17.     if type(ob_type) is _ClassType:
  18.         raise TypeError('copy_reg is not intended for use with classes')
  19.     
  20.     if not callable(pickle_function):
  21.         raise TypeError('reduction functions must be callable')
  22.     
  23.     dispatch_table[ob_type] = pickle_function
  24.     if constructor_ob is not None:
  25.         constructor(constructor_ob)
  26.     
  27.  
  28.  
  29. def constructor(object):
  30.     if not callable(object):
  31.         raise TypeError('constructors must be callable')
  32.     
  33.     safe_constructors[object] = 1
  34.  
  35.  
  36. def pickle_complex(c):
  37.     return (complex, (c.real, c.imag))
  38.  
  39. pickle(type((0.0+1.0j)), pickle_complex, complex)
  40.  
  41. def _reconstructor(cls, base, state):
  42.     obj = base.__new__(cls, state)
  43.     base.__init__(obj, state)
  44.     return obj
  45.  
  46. _reconstructor.__safe_for_unpickling__ = 1
  47. _HEAPTYPE = 1 << 9
  48.  
  49. def _reduce(self):
  50.     for base in self.__class__.__mro__:
  51.         if hasattr(base, '__flags__') and not (base.__flags__ & _HEAPTYPE):
  52.             break
  53.         
  54.     else:
  55.         base = object
  56.     if base is object:
  57.         state = None
  58.     else:
  59.         state = base(self)
  60.     args = (self.__class__, base, state)
  61.     
  62.     try:
  63.         getstate = self.__getstate__
  64.     except AttributeError:
  65.         
  66.         try:
  67.             dict = self.__dict__
  68.         except AttributeError:
  69.             dict = None
  70.  
  71.  
  72.     dict = getstate()
  73.     if dict:
  74.         return (_reconstructor, args, dict)
  75.     else:
  76.         return (_reconstructor, args)
  77.  
  78.