home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / copy_reg.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  4.9 KB  |  185 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  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.     'add_extension',
  14.     'remove_extension',
  15.     'clear_extension_cache']
  16. dispatch_table = { }
  17.  
  18. def pickle(ob_type, pickle_function, constructor_ob = None):
  19.     if type(ob_type) is _ClassType:
  20.         raise TypeError('copy_reg is not intended for use with classes')
  21.     
  22.     if not callable(pickle_function):
  23.         raise TypeError('reduction functions must be callable')
  24.     
  25.     dispatch_table[ob_type] = pickle_function
  26.     if constructor_ob is not None:
  27.         constructor(constructor_ob)
  28.     
  29.  
  30.  
  31. def constructor(object):
  32.     if not callable(object):
  33.         raise TypeError('constructors must be callable')
  34.     
  35.  
  36.  
  37. try:
  38.     complex
  39. except NameError:
  40.     pass
  41.  
  42.  
  43. def pickle_complex(c):
  44.     return (complex, (c.real, c.imag))
  45.  
  46. pickle(complex, pickle_complex, complex)
  47.  
  48. def _reconstructor(cls, base, state):
  49.     if base is object:
  50.         obj = object.__new__(cls)
  51.     else:
  52.         obj = base.__new__(cls, state)
  53.         base.__init__(obj, state)
  54.     return obj
  55.  
  56. _HEAPTYPE = 1 << 9
  57.  
  58. def _reduce_ex(self, proto):
  59.     for base in self.__class__.__mro__:
  60.         if hasattr(base, '__flags__') and not (base.__flags__ & _HEAPTYPE):
  61.             break
  62.             continue
  63.     else:
  64.         base = object
  65.     if base is object:
  66.         state = None
  67.     elif base is self.__class__:
  68.         raise TypeError, "can't pickle %s objects" % base.__name__
  69.     
  70.     state = base(self)
  71.     args = (self.__class__, base, state)
  72.     
  73.     try:
  74.         getstate = self.__getstate__
  75.     except AttributeError:
  76.         if getattr(self, '__slots__', None):
  77.             raise TypeError('a class that defines __slots__ without defining __getstate__ cannot be pickled')
  78.         
  79.         
  80.         try:
  81.             dict = self.__dict__
  82.         except AttributeError:
  83.             dict = None
  84.         except:
  85.             None<EXCEPTION MATCH>AttributeError
  86.         
  87.  
  88.         None<EXCEPTION MATCH>AttributeError
  89.  
  90.     dict = getstate()
  91.     if dict:
  92.         return (_reconstructor, args, dict)
  93.     else:
  94.         return (_reconstructor, args)
  95.  
  96.  
  97. def __newobj__(cls, *args):
  98.     return cls.__new__(cls, *args)
  99.  
  100.  
  101. def _slotnames(cls):
  102.     """Return a list of slot names for a given class.
  103.  
  104.     This needs to find slots defined by the class and its bases, so we
  105.     can't simply return the __slots__ attribute.  We must walk down
  106.     the Method Resolution Order and concatenate the __slots__ of each
  107.     class found there.  (This assumes classes don't modify their
  108.     __slots__ attribute to misrepresent their slots after the class is
  109.     defined.)
  110.     """
  111.     names = cls.__dict__.get('__slotnames__')
  112.     if names is not None:
  113.         return names
  114.     
  115.     names = []
  116.     if not hasattr(cls, '__slots__'):
  117.         pass
  118.     else:
  119.         for c in cls.__mro__:
  120.             if '__slots__' in c.__dict__:
  121.                 slots = c.__dict__['__slots__']
  122.                 if isinstance(slots, basestring):
  123.                     slots = (slots,)
  124.                 
  125.                 for name in slots:
  126.                     if name in ('__dict__', '__weakref__'):
  127.                         continue
  128.                         continue
  129.                     if name.startswith('__') and not name.endswith('__'):
  130.                         names.append('_%s%s' % (c.__name__, name))
  131.                         continue
  132.                     names.append(name)
  133.                 
  134.         
  135.     
  136.     try:
  137.         cls.__slotnames__ = names
  138.     except:
  139.         pass
  140.  
  141.     return names
  142.  
  143. _extension_registry = { }
  144. _inverted_registry = { }
  145. _extension_cache = { }
  146.  
  147. def add_extension(module, name, code):
  148.     '''Register an extension code.'''
  149.     code = int(code)
  150.     if code <= code:
  151.         pass
  152.     elif not code <= 2147483647:
  153.         raise ValueError, 'code out of range'
  154.     
  155.     key = (module, name)
  156.     if _extension_registry.get(key) == code and _inverted_registry.get(code) == key:
  157.         return None
  158.     
  159.     if key in _extension_registry:
  160.         raise ValueError('key %s is already registered with code %s' % (key, _extension_registry[key]))
  161.     
  162.     if code in _inverted_registry:
  163.         raise ValueError('code %s is already in use for key %s' % (code, _inverted_registry[code]))
  164.     
  165.     _extension_registry[key] = code
  166.     _inverted_registry[code] = key
  167.  
  168.  
  169. def remove_extension(module, name, code):
  170.     '''Unregister an extension code.  For testing only.'''
  171.     key = (module, name)
  172.     if _extension_registry.get(key) != code or _inverted_registry.get(code) != key:
  173.         raise ValueError('key %s is not registered with code %s' % (key, code))
  174.     
  175.     del _extension_registry[key]
  176.     del _inverted_registry[code]
  177.     if code in _extension_cache:
  178.         del _extension_cache[code]
  179.     
  180.  
  181.  
  182. def clear_extension_cache():
  183.     _extension_cache.clear()
  184.  
  185.