Mercurial > cpython-lru_cache-weakref
annotate Lib/abc.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | b91059e59d0d |
| children | db7ef34eedaf |
| 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): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
29 """A decorator indicating abstract classmethods. |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
30 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
31 Similar to abstractmethod. |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
32 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
33 Usage: |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
34 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
35 class C(metaclass=ABCMeta): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
36 @abstractclassmethod |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
37 def my_abstract_classmethod(cls, ...): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
38 ... |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
39 """ |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
40 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
41 __isabstractmethod__ = True |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
42 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
43 def __init__(self, callable): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
44 callable.__isabstractmethod__ = True |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
45 super().__init__(callable) |
|
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 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
48 class abstractstaticmethod(staticmethod): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
49 """A decorator indicating abstract staticmethods. |
|
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 Similar to abstractmethod. |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
52 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
53 Usage: |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
54 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
55 class C(metaclass=ABCMeta): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
56 @abstractstaticmethod |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
57 def my_abstract_staticmethod(...): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
58 ... |
|
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 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
61 __isabstractmethod__ = True |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
62 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
63 def __init__(self, callable): |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
64 callable.__isabstractmethod__ = True |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
65 super().__init__(callable) |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
66 |
|
b91059e59d0d
add support for abstract class and static methods #5867
Benjamin Peterson <benjamin@python.org>
parents:
58533
diff
changeset
|
67 |
|
42413
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
68 class abstractproperty(property): |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
69 """A decorator indicating abstract properties. |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
70 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
71 Requires that the metaclass is ABCMeta or derived from it. A |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
72 class that has a metaclass derived from ABCMeta cannot be |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
73 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
|
74 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
|
75 'super' call mechanisms. |
|
42413
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
76 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
77 Usage: |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
78 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
79 class C(metaclass=ABCMeta): |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
80 @abstractproperty |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
81 def my_abstract_property(self): |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
82 ... |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
83 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
84 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
|
85 abstract property using the 'long' form of property declaration: |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
86 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
87 class C(metaclass=ABCMeta): |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
88 def getx(self): ... |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
89 def setx(self, value): ... |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
90 x = abstractproperty(getx, setx) |
|
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 __isabstractmethod__ = True |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
93 |
|
392f98221d13
Add @abstractproperty.
Guido van Rossum <guido@python.org>
parents:
42092
diff
changeset
|
94 |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 class ABCMeta(type): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 """Metaclass for defining Abstract Base Classes (ABCs). |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 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
|
100 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
|
101 unrelated concrete classes (even built-in classes) and unrelated |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 ABCs as 'virtual subclasses' -- these and their descendants will |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 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
|
104 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
|
105 their MRO (Method Resolution Order) nor will method |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 implementations defined by the registering ABC be callable (not |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 even via super()). |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 |
|
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 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 # 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
|
112 # registered as a virtual subclass of anything. It forces the |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 # 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
|
114 _abc_invalidation_counter = 0 |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 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
|
117 cls = super().__new__(mcls, name, bases, namespace) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 # Compute set of abstract method names |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 abstracts = {name |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 for name, value in namespace.items() |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 if getattr(value, "__isabstractmethod__", False)} |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 for base in bases: |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 for name in getattr(base, "__abstractmethods__", set()): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 value = getattr(cls, name, None) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 if getattr(value, "__isabstractmethod__", False): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 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
|
127 cls.__abstractmethods__ = frozenset(abstracts) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 # 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
|
129 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
|
130 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
|
131 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
|
132 cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 return cls |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 |
|
44067
3cebde478cc4
Backmerge -r59233:59232
Christian Heimes <christian@cheimes.de>
parents:
44065
diff
changeset
|
135 def register(cls, subclass): |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 """Register a virtual subclass of an ABC.""" |
|
58533
e660cb52bf00
Merged revisions 77789 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53738
diff
changeset
|
137 if not isinstance(subclass, type): |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 raise TypeError("Can only register classes") |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 if issubclass(subclass, cls): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 return # Already a subclass |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 # Subtle: test for cycles *after* testing for "already a subclass"; |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 # 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
|
143 if issubclass(cls, subclass): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 # 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
|
145 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
|
146 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
|
147 ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 def _dump_registry(cls, file=None): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 """Debug helper to print the ABC registry.""" |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 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
|
152 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
|
153 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
|
154 if name.startswith("_abc_"): |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 value = getattr(cls, name) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 print("%s: %r" % (name, value), file=file) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 def __instancecheck__(cls, instance): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 """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
|
160 # 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 # 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
|
171 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
|
172 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
|
173 |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def __subclasscheck__(cls, subclass): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 """Override for issubclass(subclass, cls).""" |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 # 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
|
177 if subclass in cls._abc_cache: |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 return True |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 # 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
|
180 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
|
181 # 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
|
182 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
|
183 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
|
184 elif subclass in cls._abc_negative_cache: |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 return False |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 # Check the subclass hook |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 ok = cls.__subclasshook__(subclass) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 if ok is not NotImplemented: |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 assert isinstance(ok, bool) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 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
|
191 cls._abc_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 else: |
|
42680
2f4679301132
Rename __whatever variables defined by ABCMeta to _abc_whatever, so as
Guido van Rossum <guido@python.org>
parents:
42662
diff
changeset
|
193 cls._abc_negative_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 return ok |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 # 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
|
196 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
|
197 cls._abc_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 return True |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 # 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
|
200 for rcls in cls._abc_registry: |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 if issubclass(subclass, rcls): |
|
48960
7a6e6a04e28d
Merged revisions 66144 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45549
diff
changeset
|
202 cls._abc_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return True |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 # Check if it's a subclass of a subclass (recursive) |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 for scls in cls.__subclasses__(): |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 if issubclass(subclass, scls): |
|
48960
7a6e6a04e28d
Merged revisions 66144 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
45549
diff
changeset
|
207 cls._abc_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 return True |
|
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 # 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
|
210 cls._abc_negative_cache.add(subclass) |
|
42087
973992a7440a
Somehow this needed adding.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 return False |
