| 1 |
|
| 2 |
""" |
| 3 |
Plain Python definition of the builtin ABC-related functions. |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
import _abc |
| 8 |
from _weakref import ref |
| 9 |
|
| 10 |
COLLECTION_FLAGS = (1 << 5) | (1 << 6) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class SimpleWeakSet: |
| 16 |
def __init__(self, data=None): |
| 17 |
self.data = set() |
| 18 |
def _remove(item, selfref=ref(self)): |
| 19 |
self = selfref() |
| 20 |
if self is not None: |
| 21 |
self.data.discard(item) |
| 22 |
self._remove = _remove |
| 23 |
|
| 24 |
def __iter__(self): |
| 25 |
|
| 26 |
|
| 27 |
copy = list(self.data) |
| 28 |
for itemref in copy: |
| 29 |
item = itemref() |
| 30 |
if item is not None: |
| 31 |
yield item |
| 32 |
|
| 33 |
def __contains__(self, item): |
| 34 |
try: |
| 35 |
wr = ref(item) |
| 36 |
except TypeError: |
| 37 |
return False |
| 38 |
return wr in self.data |
| 39 |
|
| 40 |
def add(self, item): |
| 41 |
self.data.add(ref(item, self._remove)) |
| 42 |
|
| 43 |
def clear(self): |
| 44 |
self.data.clear() |
| 45 |
|
| 46 |
|
| 47 |
abc_invalidation_counter = 0 |
| 48 |
|
| 49 |
|
| 50 |
def get_cache_token(): |
| 51 |
"""Returns the current ABC cache token. |
| 52 |
|
| 53 |
The token is an opaque object (supporting equality testing) identifying the |
| 54 |
current version of the ABC cache for virtual subclasses. The token changes |
| 55 |
with every call to ``register()`` on any ABC. |
| 56 |
""" |
| 57 |
return abc_invalidation_counter |
| 58 |
|
| 59 |
|
| 60 |
def _abc_init(cls): |
| 61 |
"""Internal ABC helper for class set-up. Should be never used outside abc module.""" |
| 62 |
namespace = cls.__dict__ |
| 63 |
abstracts = {name |
| 64 |
for name, value in namespace.items() |
| 65 |
if getattr(value, "__isabstractmethod__", False)} |
| 66 |
bases = cls.__bases__ |
| 67 |
for base in bases: |
| 68 |
for name in getattr(base, "__abstractmethods__", set()): |
| 69 |
value = getattr(cls, name, None) |
| 70 |
if getattr(value, "__isabstractmethod__", False): |
| 71 |
abstracts.add(name) |
| 72 |
cls.__abstractmethods__ = frozenset(abstracts) |
| 73 |
|
| 74 |
cls._abc_registry = SimpleWeakSet() |
| 75 |
cls._abc_cache = SimpleWeakSet() |
| 76 |
cls._abc_negative_cache = SimpleWeakSet() |
| 77 |
cls._abc_negative_cache_version = abc_invalidation_counter |
| 78 |
|
| 79 |
if isinstance(cls, type): |
| 80 |
flags = cls.__dict__.get("__abc_tpflags__") |
| 81 |
if flags is not None: |
| 82 |
if flags & COLLECTION_FLAGS == COLLECTION_FLAGS: |
| 83 |
raise TypeError("__abc_tpflags__ cannot be both Py_TPFLAGS_SEQUENCE and Py_TPFLAGS_MAPPING") |
| 84 |
_abc._internal_set_collection_flag(cls, flags) |
| 85 |
del cls.__abc_tpflags__ |
| 86 |
|
| 87 |
|
| 88 |
def _abc_register(cls, subclass): |
| 89 |
"""Internal ABC helper for subclasss registration. Should be never used outside abc module.""" |
| 90 |
if not isinstance(subclass, type): |
| 91 |
raise TypeError("Can only register classes") |
| 92 |
if issubclass(subclass, cls): |
| 93 |
return subclass |
| 94 |
|
| 95 |
|
| 96 |
if issubclass(cls, subclass): |
| 97 |
|
| 98 |
raise RuntimeError("Refusing to create an inheritance cycle") |
| 99 |
cls._abc_registry.add(subclass) |
| 100 |
global abc_invalidation_counter |
| 101 |
abc_invalidation_counter += 1 |
| 102 |
if isinstance(cls, type): |
| 103 |
collection_flag = cls.__flags__ & COLLECTION_FLAGS |
| 104 |
if collection_flag: |
| 105 |
_abc._internal_set_collection_flag_recursive(subclass, collection_flag) |
| 106 |
return subclass |
| 107 |
|
| 108 |
|
| 109 |
def _abc_instancecheck(cls, instance): |
| 110 |
"""Internal ABC helper for instance checks. Should be never used outside abc module.""" |
| 111 |
|
| 112 |
subclass = instance.__class__ |
| 113 |
if subclass in cls._abc_cache: |
| 114 |
return True |
| 115 |
subtype = type(instance) |
| 116 |
if subtype is subclass: |
| 117 |
if (cls._abc_negative_cache_version == abc_invalidation_counter and |
| 118 |
subclass in cls._abc_negative_cache): |
| 119 |
return False |
| 120 |
|
| 121 |
return cls.__subclasscheck__(subclass) |
| 122 |
return any(cls.__subclasscheck__(c) for c in (subclass, subtype)) |
| 123 |
|
| 124 |
|
| 125 |
def _abc_subclasscheck(cls, subclass): |
| 126 |
"""Internal ABC helper for subclasss checks. Should be never used outside abc module.""" |
| 127 |
if not isinstance(subclass, type): |
| 128 |
raise TypeError('issubclass() arg 1 must be a class') |
| 129 |
|
| 130 |
if subclass in cls._abc_cache: |
| 131 |
return True |
| 132 |
|
| 133 |
if cls._abc_negative_cache_version < abc_invalidation_counter: |
| 134 |
|
| 135 |
cls._abc_negative_cache = SimpleWeakSet() |
| 136 |
cls._abc_negative_cache_version = abc_invalidation_counter |
| 137 |
elif subclass in cls._abc_negative_cache: |
| 138 |
return False |
| 139 |
|
| 140 |
ok = cls.__subclasshook__(subclass) |
| 141 |
if ok is not NotImplemented: |
| 142 |
assert isinstance(ok, bool) |
| 143 |
if ok: |
| 144 |
cls._abc_cache.add(subclass) |
| 145 |
else: |
| 146 |
cls._abc_negative_cache.add(subclass) |
| 147 |
return ok |
| 148 |
|
| 149 |
if cls in getattr(subclass, '__mro__', ()): |
| 150 |
cls._abc_cache.add(subclass) |
| 151 |
return True |
| 152 |
|
| 153 |
for rcls in cls._abc_registry: |
| 154 |
if issubclass(subclass, rcls): |
| 155 |
cls._abc_cache.add(subclass) |
| 156 |
return True |
| 157 |
|
| 158 |
for scls in cls.__subclasses__(): |
| 159 |
if issubclass(subclass, scls): |
| 160 |
cls._abc_cache.add(subclass) |
| 161 |
return True |
| 162 |
|
| 163 |
cls._abc_negative_cache.add(subclass) |
| 164 |
return False |
| 165 |
|
| 166 |
|
| 167 |
def _get_dump(cls): |
| 168 |
"""Internal ABC helper for cache and registry debugging |
| 169 |
|
| 170 |
Return shallow copies of registry, of both caches, and |
| 171 |
negative cache version. Don't call this function directly, |
| 172 |
instead use ABC._dump_registry() for a nice repr.""" |
| 173 |
return (cls._abc_registry.data, |
| 174 |
cls._abc_cache.data, |
| 175 |
cls._abc_negative_cache.data, |
| 176 |
cls._abc_negative_cache_version) |
| 177 |
|
| 178 |
|
| 179 |
def _reset_registry(cls): |
| 180 |
"""Internal ABC helper to reset registry of a given class. |
| 181 |
|
| 182 |
Should be only used by refleak.py""" |
| 183 |
cls._abc_registry.clear() |
| 184 |
|
| 185 |
|
| 186 |
def _reset_caches(cls): |
| 187 |
"""Internal ABC helper to reset both caches of a given class. |
| 188 |
|
| 189 |
Should be only used by refleak.py""" |
| 190 |
cls._abc_cache.clear() |
| 191 |
cls._abc_negative_cache.clear() |