183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh# Copyright 2007 Google, Inc. All Rights Reserved.
283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh# Licensed to PSF under a Contributor Agreement.
383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh"""Abstract Base Classes (ABCs) according to PEP 3119."""
583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehimport types
783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehfrom _weakrefset import WeakSet
983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
1083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh# Instance of old-style class
1183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass _C: pass
1283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh_InstanceType = type(_C())
1383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
1483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
1583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehdef abstractmethod(funcobj):
1683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """A decorator indicating abstract methods.
1783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
1883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    Requires that the metaclass is ABCMeta or derived from it.  A
1983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    class that has a metaclass derived from ABCMeta cannot be
2083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    instantiated unless all of its abstract methods are overridden.
2183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    The abstract methods can be called using any of the normal
2283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    'super' call mechanisms.
2383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
2483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    Usage:
2583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
2683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        class C:
2783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            __metaclass__ = ABCMeta
2883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            @abstractmethod
2983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            def my_abstract_method(self, ...):
3083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                ...
3183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """
3283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    funcobj.__isabstractmethod__ = True
3383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    return funcobj
3483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
3583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
3683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass abstractproperty(property):
3783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """A decorator indicating abstract properties.
3883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
3983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    Requires that the metaclass is ABCMeta or derived from it.  A
4083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    class that has a metaclass derived from ABCMeta cannot be
4183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    instantiated unless all of its abstract properties are overridden.
4283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    The abstract properties can be called using any of the normal
4383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    'super' call mechanisms.
4483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
4583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    Usage:
4683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
4783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        class C:
4883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            __metaclass__ = ABCMeta
4983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            @abstractproperty
5083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            def my_abstract_property(self):
5183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                ...
5283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
5383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    This defines a read-only property; you can also define a read-write
5483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    abstract property using the 'long' form of property declaration:
5583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
5683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        class C:
5783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            __metaclass__ = ABCMeta
5883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            def getx(self): ...
5983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            def setx(self, value): ...
6083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            x = abstractproperty(getx, setx)
6183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """
6283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    __isabstractmethod__ = True
6383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
6483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
6583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsiehclass ABCMeta(type):
6683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
6783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """Metaclass for defining Abstract Base Classes (ABCs).
6883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
6983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    Use this metaclass to create an ABC.  An ABC can be subclassed
7083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    directly, and then acts as a mix-in class.  You can also register
7183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    unrelated concrete classes (even built-in classes) and unrelated
7283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    ABCs as 'virtual subclasses' -- these and their descendants will
7383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    be considered subclasses of the registering ABC by the built-in
7483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    issubclass() function, but the registering ABC won't show up in
7583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    their MRO (Method Resolution Order) nor will method
7683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    implementations defined by the registering ABC be callable (not
7783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    even via super()).
7883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
7983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    """
8083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
8183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    # A global counter that is incremented each time a class is
8283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    # registered as a virtual subclass of anything.  It forces the
8383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    # negative cache to be cleared before its next use.
8483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    _abc_invalidation_counter = 0
8583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
8683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    def __new__(mcls, name, bases, namespace):
8783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
8883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Compute set of abstract method names
8983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        abstracts = set(name
9083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                     for name, value in namespace.items()
9183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                     if getattr(value, "__isabstractmethod__", False))
9283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        for base in bases:
9383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            for name in getattr(base, "__abstractmethods__", set()):
9483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                value = getattr(cls, name, None)
9583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                if getattr(value, "__isabstractmethod__", False):
9683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                    abstracts.add(name)
9783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls.__abstractmethods__ = frozenset(abstracts)
9883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Set up inheritance registry
9983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_registry = WeakSet()
10083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_cache = WeakSet()
10183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_negative_cache = WeakSet()
10283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
10383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        return cls
10483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
10583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    def register(cls, subclass):
10683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        """Register a virtual subclass of an ABC."""
10783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if not isinstance(subclass, (type, types.ClassType)):
10883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            raise TypeError("Can only register classes")
10983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if issubclass(subclass, cls):
11083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return  # Already a subclass
11183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Subtle: test for cycles *after* testing for "already a subclass";
11283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # this means we allow X.register(X) and interpret it as a no-op.
11383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if issubclass(cls, subclass):
11483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            # This would create a cycle, which is bad for the algorithm below
11583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            raise RuntimeError("Refusing to create an inheritance cycle")
11683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_registry.add(subclass)
11783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        ABCMeta._abc_invalidation_counter += 1  # Invalidate negative cache
11883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
11983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    def _dump_registry(cls, file=None):
12083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        """Debug helper to print the ABC registry."""
12183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        print >> file, "Class: %s.%s" % (cls.__module__, cls.__name__)
12283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        print >> file, "Inv.counter: %s" % ABCMeta._abc_invalidation_counter
12383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        for name in sorted(cls.__dict__.keys()):
12483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            if name.startswith("_abc_"):
12583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                value = getattr(cls, name)
12683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                print >> file, "%s: %r" % (name, value)
12783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
12883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    def __instancecheck__(cls, instance):
12983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        """Override for isinstance(instance, cls)."""
13083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Inline the cache checking when it's simple.
13183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        subclass = getattr(instance, '__class__', None)
13283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if subclass is not None and subclass in cls._abc_cache:
13383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return True
13483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        subtype = type(instance)
13583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Old-style instances
13683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if subtype is _InstanceType:
13783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            subtype = subclass
13883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if subtype is subclass or subclass is None:
13983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            if (cls._abc_negative_cache_version ==
14083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                ABCMeta._abc_invalidation_counter and
14183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                subtype in cls._abc_negative_cache):
14283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                return False
14383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            # Fall back to the subclass check.
14483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return cls.__subclasscheck__(subtype)
14583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        return (cls.__subclasscheck__(subclass) or
14683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                cls.__subclasscheck__(subtype))
14783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh
14883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh    def __subclasscheck__(cls, subclass):
14983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        """Override for issubclass(subclass, cls)."""
15083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check cache
15183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if subclass in cls._abc_cache:
15283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return True
15383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check negative cache; may have to invalidate
15483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
15583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            # Invalidate the negative cache
15683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            cls._abc_negative_cache = WeakSet()
15783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
15883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        elif subclass in cls._abc_negative_cache:
15983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return False
16083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check the subclass hook
16183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        ok = cls.__subclasshook__(subclass)
16283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if ok is not NotImplemented:
16383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            assert isinstance(ok, bool)
16483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            if ok:
16583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                cls._abc_cache.add(subclass)
16683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            else:
16783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                cls._abc_negative_cache.add(subclass)
16883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return ok
16983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check if it's a direct subclass
17083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        if cls in getattr(subclass, '__mro__', ()):
17183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            cls._abc_cache.add(subclass)
17283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            return True
17383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check if it's a subclass of a registered class (recursive)
17483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        for rcls in cls._abc_registry:
17583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            if issubclass(subclass, rcls):
17683760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                cls._abc_cache.add(subclass)
17783760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                return True
17883760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # Check if it's a subclass of a subclass (recursive)
17983760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        for scls in cls.__subclasses__():
18083760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh            if issubclass(subclass, scls):
18183760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                cls._abc_cache.add(subclass)
18283760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh                return True
18383760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        # No dice; update negative cache
18483760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        cls._abc_negative_cache.add(subclass)
18583760d213fb3bec7b4117d266fcfbf6fe2ba14abAndrew Hsieh        return False
186