annotate Lib/abc.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents e979b26a9172
children 9347869d1066
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Copyright 2007 Google, Inc. All Rights Reserved.
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 """Abstract Base Classes (ABCs) according to PEP 3119."""
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents: 44568
diff changeset
6 from _weakrefset import WeakSet
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 def abstractmethod(funcobj):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 """A decorator indicating abstract methods.
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 Requires that the metaclass is ABCMeta or derived from it. A
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 class that has a metaclass derived from ABCMeta cannot be
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 instantiated unless all of its abstract methods are overridden.
53738
73af90206cbe Merged revisions 72280 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 48960
diff changeset
14 The abstract methods can be called using any of the normal
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 'super' call mechanisms.
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Usage:
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 class C(metaclass=ABCMeta):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 @abstractmethod
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 def my_abstract_method(self, ...):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 ...
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 """
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 funcobj.__isabstractmethod__ = True
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 return funcobj
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
64094
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
28 class abstractclassmethod(classmethod):
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
29 """
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
30 A decorator indicating abstract classmethods.
64094
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
31
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
32 Similar to abstractmethod.
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
33
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
34 Usage:
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
35
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
36 class C(metaclass=ABCMeta):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
37 @abstractclassmethod
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
38 def my_abstract_classmethod(cls, ...):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
39 ...
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
40
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
41 'abstractclassmethod' is deprecated. Use 'classmethod' with
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
42 'abstractmethod' instead.
64094
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
43 """
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
44
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
45 __isabstractmethod__ = True
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
46
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
47 def __init__(self, callable):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
48 callable.__isabstractmethod__ = True
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
49 super().__init__(callable)
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
50
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
51
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
52 class abstractstaticmethod(staticmethod):
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
53 """
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
54 A decorator indicating abstract staticmethods.
64094
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
55
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
56 Similar to abstractmethod.
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
57
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
58 Usage:
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
59
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
60 class C(metaclass=ABCMeta):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
61 @abstractstaticmethod
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
62 def my_abstract_staticmethod(...):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
63 ...
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
64
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
65 'abstractstaticmethod' is deprecated. Use 'staticmethod' with
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
66 'abstractmethod' instead.
64094
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
67 """
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
68
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
69 __isabstractmethod__ = True
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
70
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
71 def __init__(self, callable):
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
72 callable.__isabstractmethod__ = True
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
73 super().__init__(callable)
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
74
b91059e59d0d add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents: 58533
diff changeset
75
42413
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
76 class abstractproperty(property):
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
77 """
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
78 A decorator indicating abstract properties.
42413
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
79
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
80 Requires that the metaclass is ABCMeta or derived from it. A
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
81 class that has a metaclass derived from ABCMeta cannot be
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
82 instantiated unless all of its abstract properties are overridden.
53738
73af90206cbe Merged revisions 72280 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 48960
diff changeset
83 The abstract properties can be called using any of the normal
42415
a6d26a4d672d Tests for @abstractproperty by Jeffrey Yasskin.
Guido van Rossum <guido@python.org>
parents: 42413
diff changeset
84 'super' call mechanisms.
42413
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
85
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
86 Usage:
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
87
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
88 class C(metaclass=ABCMeta):
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
89 @abstractproperty
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
90 def my_abstract_property(self):
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
91 ...
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
92
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
93 This defines a read-only property; you can also define a read-write
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
94 abstract property using the 'long' form of property declaration:
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
95
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
96 class C(metaclass=ABCMeta):
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
97 def getx(self): ...
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
98 def setx(self, value): ...
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
99 x = abstractproperty(getx, setx)
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
100
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
101 'abstractproperty' is deprecated. Use 'property' with 'abstractmethod'
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
102 instead.
42413
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
103 """
73984
e979b26a9172 improve abstract property support (closes #11610)
Benjamin Peterson <benjamin@python.org>
parents: 68125
diff changeset
104
42413
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
105 __isabstractmethod__ = True
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
106
392f98221d13 Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents: 42092
diff changeset
107
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 class ABCMeta(type):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 """Metaclass for defining Abstract Base Classes (ABCs).
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 Use this metaclass to create an ABC. An ABC can be subclassed
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 directly, and then acts as a mix-in class. You can also register
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 unrelated concrete classes (even built-in classes) and unrelated
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 ABCs as 'virtual subclasses' -- these and their descendants will
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 be considered subclasses of the registering ABC by the built-in
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 issubclass() function, but the registering ABC won't show up in
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 their MRO (Method Resolution Order) nor will method
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 implementations defined by the registering ABC be callable (not
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 even via super()).
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 """
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 # A global counter that is incremented each time a class is
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 # registered as a virtual subclass of anything. It forces the
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 # negative cache to be cleared before its next use.
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
127 _abc_invalidation_counter = 0
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 def __new__(mcls, name, bases, namespace):
42092
d17a27247986 Modernize the super() call in ABCMeta.__new__() -- I had messed with
Guido van Rossum <guido@python.org>
parents: 42087
diff changeset
130 cls = super().__new__(mcls, name, bases, namespace)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 # Compute set of abstract method names
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 abstracts = {name
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 for name, value in namespace.items()
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if getattr(value, "__isabstractmethod__", False)}
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 for base in bases:
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 for name in getattr(base, "__abstractmethods__", set()):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 value = getattr(cls, name, None)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 if getattr(value, "__isabstractmethod__", False):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 abstracts.add(name)
45549
8dd30fe11a4b Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45333
diff changeset
140 cls.__abstractmethods__ = frozenset(abstracts)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 # Set up inheritance registry
43644
95d8c913e8bf #1061 (mainly by Thomas Wouters): use weak sets for abc caches.
Georg Brandl <georg@python.org>
parents: 43334
diff changeset
142 cls._abc_registry = WeakSet()
95d8c913e8bf #1061 (mainly by Thomas Wouters): use weak sets for abc caches.
Georg Brandl <georg@python.org>
parents: 43334
diff changeset
143 cls._abc_cache = WeakSet()
95d8c913e8bf #1061 (mainly by Thomas Wouters): use weak sets for abc caches.
Georg Brandl <georg@python.org>
parents: 43334
diff changeset
144 cls._abc_negative_cache = WeakSet()
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
145 cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 return cls
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
44067
3cebde478cc4 Backmerge -r59233:59232
Christian Heimes <christian@cheimes.de>
parents: 44065
diff changeset
148 def register(cls, subclass):
68125
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
149 """Register a virtual subclass of an ABC.
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
150
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
151 Returns the subclass, to allow usage as a class decorator.
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
152 """
58533
e660cb52bf00 Merged revisions 77789 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 53738
diff changeset
153 if not isinstance(subclass, type):
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 raise TypeError("Can only register classes")
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 if issubclass(subclass, cls):
68125
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
156 return subclass # Already a subclass
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 # Subtle: test for cycles *after* testing for "already a subclass";
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 # this means we allow X.register(X) and interpret it as a no-op.
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 if issubclass(cls, subclass):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # This would create a cycle, which is bad for the algorithm below
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 raise RuntimeError("Refusing to create an inheritance cycle")
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
162 cls._abc_registry.add(subclass)
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
163 ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
68125
db7ef34eedaf Allow usage of SomeABC.register as a class decorator. Patch by Edoardo Spadolini (#10868).
Éric Araujo <merwok@netwok.org>
parents: 64094
diff changeset
164 return subclass
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 def _dump_registry(cls, file=None):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 """Debug helper to print the ABC registry."""
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 print("Class: %s.%s" % (cls.__module__, cls.__name__), file=file)
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
169 print("Inv.counter: %s" % ABCMeta._abc_invalidation_counter, file=file)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 for name in sorted(cls.__dict__.keys()):
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
171 if name.startswith("_abc_"):
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 value = getattr(cls, name)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 print("%s: %r" % (name, value), file=file)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 def __instancecheck__(cls, instance):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 """Override for isinstance(instance, cls)."""
45333
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
177 # Inline the cache checking
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
178 subclass = instance.__class__
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
179 if subclass in cls._abc_cache:
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
180 return True
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
181 subtype = type(instance)
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
182 if subtype is subclass:
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
183 if (cls._abc_negative_cache_version ==
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
184 ABCMeta._abc_invalidation_counter and
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
185 subclass in cls._abc_negative_cache):
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
186 return False
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
187 # Fall back to the subclass check.
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
188 return cls.__subclasscheck__(subclass)
e70f56cf6972 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60766,60769-60786 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45159
diff changeset
189 return any(cls.__subclasscheck__(c) for c in {subclass, subtype})
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 def __subclasscheck__(cls, subclass):
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 """Override for issubclass(subclass, cls)."""
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 # Check cache
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
194 if subclass in cls._abc_cache:
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 return True
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 # Check negative cache; may have to invalidate
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
197 if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 # Invalidate the negative cache
43644
95d8c913e8bf #1061 (mainly by Thomas Wouters): use weak sets for abc caches.
Georg Brandl <georg@python.org>
parents: 43334
diff changeset
199 cls._abc_negative_cache = WeakSet()
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
200 cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
201 elif subclass in cls._abc_negative_cache:
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 return False
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 # Check the subclass hook
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 ok = cls.__subclasshook__(subclass)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 if ok is not NotImplemented:
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 assert isinstance(ok, bool)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if ok:
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
208 cls._abc_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 else:
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
210 cls._abc_negative_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 return ok
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 # Check if it's a direct subclass
44568
305d7d2ce047 Copied doc for reload() from trunk's function.rst to imp.rst
Christian Heimes <christian@cheimes.de>
parents: 44067
diff changeset
213 if cls in getattr(subclass, '__mro__', ()):
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
214 cls._abc_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 return True
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 # Check if it's a subclass of a registered class (recursive)
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
217 for rcls in cls._abc_registry:
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 if issubclass(subclass, rcls):
48960
7a6e6a04e28d Merged revisions 66144 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45549
diff changeset
219 cls._abc_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 return True
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 # Check if it's a subclass of a subclass (recursive)
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 for scls in cls.__subclasses__():
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 if issubclass(subclass, scls):
48960
7a6e6a04e28d Merged revisions 66144 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 45549
diff changeset
224 cls._abc_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 return True
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 # No dice; update negative cache
42680
2f4679301132 Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents: 42662
diff changeset
227 cls._abc_negative_cache.add(subclass)
42087
973992a7440a Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 return False