home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Inkscape / Inkscape-0.48.2-1-win32.exe / python / Lib / abc.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-03-27  |  5.7 KB  |  169 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Abstract Base Classes (ABCs) according to PEP 3119.'''
  5.  
  6. def abstractmethod(funcobj):
  7.     """A decorator indicating abstract methods.
  8.  
  9.     Requires that the metaclass is ABCMeta or derived from it.  A
  10.     class that has a metaclass derived from ABCMeta cannot be
  11.     instantiated unless all of its abstract methods are overridden.
  12.     The abstract methods can be called using any of the normal
  13.     'super' call mechanisms.
  14.  
  15.     Usage:
  16.  
  17.         class C:
  18.             __metaclass__ = ABCMeta
  19.             @abstractmethod
  20.             def my_abstract_method(self, ...):
  21.                 ...
  22.     """
  23.     funcobj.__isabstractmethod__ = True
  24.     return funcobj
  25.  
  26.  
  27. class abstractproperty(property):
  28.     """A decorator indicating abstract properties.
  29.  
  30.     Requires that the metaclass is ABCMeta or derived from it.  A
  31.     class that has a metaclass derived from ABCMeta cannot be
  32.     instantiated unless all of its abstract properties are overridden.
  33.     The abstract properties can be called using any of the normal
  34.     'super' call mechanisms.
  35.  
  36.     Usage:
  37.  
  38.         class C:
  39.             __metaclass__ = ABCMeta
  40.             @abstractproperty
  41.             def my_abstract_property(self):
  42.                 ...
  43.  
  44.     This defines a read-only property; you can also define a read-write
  45.     abstract property using the 'long' form of property declaration:
  46.  
  47.         class C:
  48.             __metaclass__ = ABCMeta
  49.             def getx(self): ...
  50.             def setx(self, value): ...
  51.             x = abstractproperty(getx, setx)
  52.     """
  53.     __isabstractmethod__ = True
  54.  
  55.  
  56. class ABCMeta(type):
  57.     """Metaclass for defining Abstract Base Classes (ABCs).
  58.  
  59.     Use this metaclass to create an ABC.  An ABC can be subclassed
  60.     directly, and then acts as a mix-in class.  You can also register
  61.     unrelated concrete classes (even built-in classes) and unrelated
  62.     ABCs as 'virtual subclasses' -- these and their descendants will
  63.     be considered subclasses of the registering ABC by the built-in
  64.     issubclass() function, but the registering ABC won't show up in
  65.     their MRO (Method Resolution Order) nor will method
  66.     implementations defined by the registering ABC be callable (not
  67.     even via super()).
  68.  
  69.     """
  70.     _abc_invalidation_counter = 0
  71.     
  72.     def __new__(mcls, name, bases, namespace):
  73.         cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
  74.         abstracts = set((lambda .0: for name, value in .0:
  75. if getattr(value, '__isabstractmethod__', False):
  76. namecontinue)(namespace.items()))
  77.         for base in bases:
  78.             for name in getattr(base, '__abstractmethods__', set()):
  79.                 value = getattr(cls, name, None)
  80.                 if getattr(value, '__isabstractmethod__', False):
  81.                     abstracts.add(name)
  82.                     continue
  83.             
  84.         
  85.         cls.__abstractmethods__ = frozenset(abstracts)
  86.         cls._abc_registry = set()
  87.         cls._abc_cache = set()
  88.         cls._abc_negative_cache = set()
  89.         cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  90.         return cls
  91.  
  92.     
  93.     def register(cls, subclass):
  94.         '''Register a virtual subclass of an ABC.'''
  95.         if not isinstance(cls, type):
  96.             raise TypeError('Can only register classes')
  97.         isinstance(cls, type)
  98.         if issubclass(subclass, cls):
  99.             return None
  100.         if issubclass(cls, subclass):
  101.             raise RuntimeError('Refusing to create an inheritance cycle')
  102.         issubclass(cls, subclass)
  103.         cls._abc_registry.add(subclass)
  104.         ABCMeta._abc_invalidation_counter += 1
  105.  
  106.     
  107.     def _dump_registry(cls, file = None):
  108.         '''Debug helper to print the ABC registry.'''
  109.         print >>file, 'Class: %s.%s' % (cls.__module__, cls.__name__)
  110.         print >>file, 'Inv.counter: %s' % ABCMeta._abc_invalidation_counter
  111.         for name in sorted(cls.__dict__.keys()):
  112.             if name.startswith('_abc_'):
  113.                 value = getattr(cls, name)
  114.                 print >>file, '%s: %r' % (name, value)
  115.                 continue
  116.         
  117.  
  118.     
  119.     def __instancecheck__(cls, instance):
  120.         '''Override for isinstance(instance, cls).'''
  121.         subclass = getattr(instance, '__class__', None)
  122.         if subclass in cls._abc_cache:
  123.             return True
  124.         subtype = type(instance)
  125.         if subtype is subclass or subclass is None:
  126.             if cls._abc_negative_cache_version == ABCMeta._abc_invalidation_counter and subtype in cls._abc_negative_cache:
  127.                 return False
  128.             return cls.__subclasscheck__(subtype)
  129.         if not cls.__subclasscheck__(subclass):
  130.             pass
  131.         return cls.__subclasscheck__(subtype)
  132.  
  133.     
  134.     def __subclasscheck__(cls, subclass):
  135.         '''Override for issubclass(subclass, cls).'''
  136.         if subclass in cls._abc_cache:
  137.             return True
  138.         if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
  139.             cls._abc_negative_cache = set()
  140.             cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
  141.         elif subclass in cls._abc_negative_cache:
  142.             return False
  143.         ok = cls.__subclasshook__(subclass)
  144.         if ok is not NotImplemented:
  145.             if not isinstance(ok, bool):
  146.                 raise AssertionError
  147.             if ok:
  148.                 cls._abc_cache.add(subclass)
  149.             else:
  150.                 cls._abc_negative_cache.add(subclass)
  151.             return ok
  152.         if cls in getattr(subclass, '__mro__', ()):
  153.             cls._abc_cache.add(subclass)
  154.             return True
  155.         for rcls in cls._abc_registry:
  156.             if issubclass(subclass, rcls):
  157.                 cls._abc_cache.add(subclass)
  158.                 return True
  159.         
  160.         for scls in cls.__subclasses__():
  161.             if issubclass(subclass, scls):
  162.                 cls._abc_cache.add(subclass)
  163.                 return True
  164.         
  165.         cls._abc_negative_cache.add(subclass)
  166.         return False
  167.  
  168.  
  169.