annotate Lib/abc.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Copyright 2007 Google, Inc. All Rights Reserved.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 """Abstract Base Classes (ABCs) according to PEP 3119."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
62439
108f94d53edb keep UserDict an old-style class
Benjamin Peterson <benjamin@python.org>
parents: 59474
diff changeset
6 import types
108f94d53edb keep UserDict an old-style class
Benjamin Peterson <benjamin@python.org>
parents: 59474
diff changeset
7
64199
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
8 from _weakrefset import WeakSet
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
10 # Instance of old-style class
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
11 class _C: pass
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
12 _InstanceType = type(_C())
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
13
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
14
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 def abstractmethod(funcobj):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 """A decorator indicating abstract methods.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 Requires that the metaclass is ABCMeta or derived from it. A
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 class that has a metaclass derived from ABCMeta cannot be
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 instantiated unless all of its abstract methods are overridden.
53736
3088d2ebf7be Merged revisions 72278 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 51927
diff changeset
21 The abstract methods can be called using any of the normal
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 'super' call mechanisms.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 Usage:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
51927
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
26 class C:
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
27 __metaclass__ = ABCMeta
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 @abstractmethod
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def my_abstract_method(self, ...):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 ...
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 """
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 funcobj.__isabstractmethod__ = True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 return funcobj
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 class abstractproperty(property):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 """A decorator indicating abstract properties.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 Requires that the metaclass is ABCMeta or derived from it. A
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 class that has a metaclass derived from ABCMeta cannot be
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 instantiated unless all of its abstract properties are overridden.
53736
3088d2ebf7be Merged revisions 72278 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 51927
diff changeset
42 The abstract properties can be called using any of the normal
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 'super' call mechanisms.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 Usage:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
51927
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
47 class C:
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
48 __metaclass__ = ABCMeta
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 @abstractproperty
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 def my_abstract_property(self):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 ...
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 This defines a read-only property; you can also define a read-write
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 abstract property using the 'long' form of property declaration:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
51927
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
56 class C:
2a458106afbf Merged revisions 70078 via svnmerge from
Georg Brandl <georg@python.org>
parents: 48957
diff changeset
57 __metaclass__ = ABCMeta
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 def getx(self): ...
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 def setx(self, value): ...
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 x = abstractproperty(getx, setx)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 """
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 __isabstractmethod__ = True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 class ABCMeta(type):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 """Metaclass for defining Abstract Base Classes (ABCs).
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 Use this metaclass to create an ABC. An ABC can be subclassed
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 directly, and then acts as a mix-in class. You can also register
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 unrelated concrete classes (even built-in classes) and unrelated
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 ABCs as 'virtual subclasses' -- these and their descendants will
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 be considered subclasses of the registering ABC by the built-in
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 issubclass() function, but the registering ABC won't show up in
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 their MRO (Method Resolution Order) nor will method
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 implementations defined by the registering ABC be callable (not
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 even via super()).
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 """
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 # A global counter that is incremented each time a class is
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 # registered as a virtual subclass of anything. It forces the
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 # negative cache to be cleared before its next use.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 _abc_invalidation_counter = 0
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 def __new__(mcls, name, bases, namespace):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 # Compute set of abstract method names
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 abstracts = set(name
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 for name, value in namespace.items()
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 if getattr(value, "__isabstractmethod__", False))
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 for base in bases:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 for name in getattr(base, "__abstractmethods__", set()):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 value = getattr(cls, name, None)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if getattr(value, "__isabstractmethod__", False):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 abstracts.add(name)
45543
6592e5a90c2c Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT,
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45310
diff changeset
97 cls.__abstractmethods__ = frozenset(abstracts)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 # Set up inheritance registry
64199
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
99 cls._abc_registry = WeakSet()
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
100 cls._abc_cache = WeakSet()
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
101 cls._abc_negative_cache = WeakSet()
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 return cls
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 def register(cls, subclass):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 """Register a virtual subclass of an ABC."""
62439
108f94d53edb keep UserDict an old-style class
Benjamin Peterson <benjamin@python.org>
parents: 59474
diff changeset
107 if not isinstance(subclass, (type, types.ClassType)):
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 raise TypeError("Can only register classes")
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 if issubclass(subclass, cls):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return # Already a subclass
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 # Subtle: test for cycles *after* testing for "already a subclass";
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 # this means we allow X.register(X) and interpret it as a no-op.
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 if issubclass(cls, subclass):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 # This would create a cycle, which is bad for the algorithm below
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 raise RuntimeError("Refusing to create an inheritance cycle")
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 cls._abc_registry.add(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 ABCMeta._abc_invalidation_counter += 1 # Invalidate negative cache
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
118
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 def _dump_registry(cls, file=None):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 """Debug helper to print the ABC registry."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 print >> file, "Class: %s.%s" % (cls.__module__, cls.__name__)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 print >> file, "Inv.counter: %s" % ABCMeta._abc_invalidation_counter
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 for name in sorted(cls.__dict__.keys()):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 if name.startswith("_abc_"):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 value = getattr(cls, name)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 print >> file, "%s: %r" % (name, value)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
127
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 def __instancecheck__(cls, instance):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 """Override for isinstance(instance, cls)."""
45788
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45543
diff changeset
130 # Inline the cache checking when it's simple.
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45543
diff changeset
131 subclass = getattr(instance, '__class__', None)
64199
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
132 if subclass is not None and subclass in cls._abc_cache:
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
133 return True
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
134 subtype = type(instance)
59948
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
135 # Old-style instances
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
136 if subtype is _InstanceType:
6e1dad853007 Merged revisions 78800 via svnmerge from
Florent Xicluna <florent.xicluna@gmail.com>
parents: 58549
diff changeset
137 subtype = subclass
45788
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45543
diff changeset
138 if subtype is subclass or subclass is None:
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
139 if (cls._abc_negative_cache_version ==
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
140 ABCMeta._abc_invalidation_counter and
45788
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45543
diff changeset
141 subtype in cls._abc_negative_cache):
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
142 return False
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
143 # Fall back to the subclass check.
45788
4d39d4a6a9db Make isinstance(OldstyleClass, NewstyleClass) return False instead of raising
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 45543
diff changeset
144 return cls.__subclasscheck__(subtype)
45310
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
145 return (cls.__subclasscheck__(subclass) or
c5205587b2ab Working on issue #1762: Brought
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44551
diff changeset
146 cls.__subclasscheck__(subtype))
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
147
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 def __subclasscheck__(cls, subclass):
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 """Override for issubclass(subclass, cls)."""
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 # Check cache
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 if subclass in cls._abc_cache:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 return True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # Check negative cache; may have to invalidate
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if cls._abc_negative_cache_version < ABCMeta._abc_invalidation_counter:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # Invalidate the negative cache
64199
7d2bd8955edb Use weakrefs to hold onto classes #2521.
Benjamin Peterson <benjamin@python.org>
parents: 62439
diff changeset
156 cls._abc_negative_cache = WeakSet()
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 cls._abc_negative_cache_version = ABCMeta._abc_invalidation_counter
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 elif subclass in cls._abc_negative_cache:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return False
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 # Check the subclass hook
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 ok = cls.__subclasshook__(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 if ok is not NotImplemented:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 assert isinstance(ok, bool)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 if ok:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 cls._abc_cache.add(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 else:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 cls._abc_negative_cache.add(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 return ok
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 # Check if it's a direct subclass
44551
f9c950e8f2e0 Fix issue 1747: allow classic classes to be checked for being subclasses of
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 43958
diff changeset
170 if cls in getattr(subclass, '__mro__', ()):
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 cls._abc_cache.add(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 return True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 # Check if it's a subclass of a registered class (recursive)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 for rcls in cls._abc_registry:
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 if issubclass(subclass, rcls):
48957
4da4f9679672 Issue 3747: Fix caching in ABCMeta.__subclasscheck__ (R: Georg Brandl)
Nick Coghlan <ncoghlan@gmail.com>
parents: 45788
diff changeset
176 cls._abc_cache.add(subclass)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 # Check if it's a subclass of a subclass (recursive)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 for scls in cls.__subclasses__():
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if issubclass(subclass, scls):
48957
4da4f9679672 Issue 3747: Fix caching in ABCMeta.__subclasscheck__ (R: Georg Brandl)
Nick Coghlan <ncoghlan@gmail.com>
parents: 45788
diff changeset
181 cls._abc_cache.add(subclass)
43327
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 return True
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 # No dice; update negative cache
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 cls._abc_negative_cache.add(subclass)
33817da53fd7 Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 return False