File size: 13,878 Bytes
76f9669 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | """Method decorator helpers."""
__all__ = ()
import functools
import warnings
import weakref
def _warn_classmethod(stacklevel):
warnings.warn(
"decorating class methods with @cachedmethod is deprecated",
DeprecationWarning,
stacklevel=stacklevel,
)
def _warn_instance_dict(msg, stacklevel):
warnings.warn(
msg,
DeprecationWarning,
stacklevel=stacklevel,
)
class _WrapperBase:
"""Wrapper base class providing default implementations for properties."""
def __init__(self, obj, method, cache, key, lock=None, cond=None):
if isinstance(obj, type):
_warn_classmethod(stacklevel=5)
functools.update_wrapper(self, method)
self._obj = obj # protected
self.__cache = cache
self.__key = key
self.__lock = lock
self.__cond = cond
def __call__(self, *args, **kwargs):
raise NotImplementedError() # pragma: no cover
def cache_clear(self):
raise NotImplementedError() # pragma: no cover
@property
def cache(self):
return self.__cache(self._obj)
@property
def cache_key(self):
return self.__key
@property
def cache_lock(self):
return None if self.__lock is None else self.__lock(self._obj)
@property
def cache_condition(self):
return None if self.__cond is None else self.__cond(self._obj)
class _DescriptorBase:
"""Descriptor base class implementing the basic descriptor protocol."""
def __init__(self, deprecated=False):
self.__attrname = None
self.__deprecated = deprecated
def __set_name__(self, owner, name):
if self.__attrname is None:
self.__attrname = name
elif name != self.__attrname:
raise TypeError(
"Cannot assign the same @cachedmethod to two different names "
f"({self.__attrname!r} and {name!r})."
)
def __get__(self, obj, objtype=None):
wrapper = self.Wrapper(obj)
if self.__attrname is not None:
# replace descriptor instance with wrapper in instance dict
try:
# In case of a race condition where another thread already replaced
# the descriptor, prefer the initial wrapper.
wrapper = obj.__dict__.setdefault(self.__attrname, wrapper)
except AttributeError:
# not all objects have __dict__ (e.g. class defines slots)
msg = (
f"No '__dict__' attribute on {type(obj).__name__!r} "
f"instance to cache {self.__attrname!r} property."
)
if self.__deprecated:
_warn_instance_dict(msg, 3)
else:
raise TypeError(msg) from None
except TypeError:
msg = (
f"The '__dict__' attribute on {type(obj).__name__!r} "
f"instance does not support item assignment for "
f"caching {self.__attrname!r} property."
)
if self.__deprecated:
_warn_instance_dict(msg, 3)
else:
raise TypeError(msg) from None
elif self.__deprecated:
pass # deprecated @classmethod, warning already raised elsewhere
else:
msg = "Cannot use @cachedmethod instance without calling __set_name__ on it"
raise TypeError(msg) from None
return wrapper
class _DeprecatedDescriptorBase(_DescriptorBase):
"""Descriptor base class supporting deprecated @classmethod use."""
def __init__(self, wrapper, cache_clear):
super().__init__(deprecated=True)
self.__wrapper = wrapper
self.__cache_clear = cache_clear
# called for @classmethod with Python >= 3.13
def __call__(self, *args, **kwargs):
_warn_classmethod(stacklevel=3)
return self.__wrapper(*args, **kwargs)
# backward-compatible @classmethod handling with Python >= 3.13
def cache_clear(self, objtype):
_warn_classmethod(stacklevel=3)
return self.__cache_clear(objtype)
# At least for now, the implementation prefers clarity and performance
# over ease of maintenance, thus providing separate descriptors for
# all valid combinations of decorator parameters lock, condition and
# info.
def _condition_info(method, cache, key, lock, cond, info):
class Descriptor(_DescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key, lock, cond)
self.__hits = self.__misses = 0
self.__pending = set()
def __call__(self, *args, **kwargs):
cache = self.cache
lock = self.cache_lock
cond = self.cache_condition
key = self.cache_key(self._obj, *args, **kwargs)
with lock:
cond.wait_for(lambda: key not in self.__pending)
try:
result = cache[key]
self.__hits += 1
return result
except KeyError:
self.__pending.add(key)
self.__misses += 1
try:
val = method(self._obj, *args, **kwargs)
with lock:
try:
cache[key] = val
except ValueError:
pass # value too large
return val
finally:
with lock:
self.__pending.remove(key)
cond.notify_all()
def cache_clear(self):
with self.cache_lock:
self.cache.clear()
self.__hits = self.__misses = 0
def cache_info(self):
with self.cache_lock:
return info(self.cache, self.__hits, self.__misses)
return Descriptor()
def _locked_info(method, cache, key, lock, info):
class Descriptor(_DescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key, lock)
self.__hits = self.__misses = 0
def __call__(self, *args, **kwargs):
cache = self.cache
lock = self.cache_lock
key = self.cache_key(self._obj, *args, **kwargs)
with lock:
try:
result = cache[key]
self.__hits += 1
return result
except KeyError:
self.__misses += 1
val = method(self._obj, *args, **kwargs)
with lock:
try:
# In case of a race condition, i.e. if another thread
# stored a value for this key while we were calling
# method(), prefer the cached value.
return cache.setdefault(key, val)
except ValueError:
return val # value too large
def cache_clear(self):
with self.cache_lock:
self.cache.clear()
self.__hits = self.__misses = 0
def cache_info(self):
with self.cache_lock:
return info(self.cache, self.__hits, self.__misses)
return Descriptor()
def _unlocked_info(method, cache, key, info):
class Descriptor(_DescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key)
self.__hits = self.__misses = 0
def __call__(self, *args, **kwargs):
cache = self.cache
key = self.cache_key(self._obj, *args, **kwargs)
try:
result = cache[key]
self.__hits += 1
return result
except KeyError:
self.__misses += 1
val = method(self._obj, *args, **kwargs)
try:
cache[key] = val
except ValueError:
pass # value too large
return val
def cache_clear(self):
self.cache.clear()
self.__hits = self.__misses = 0
def cache_info(self):
return info(self.cache, self.__hits, self.__misses)
return Descriptor()
def _condition(method, cache, key, lock, cond):
# backward-compatible weakref dictionary for Python >= 3.13
pending = weakref.WeakKeyDictionary()
def wrapper(self, pending, *args, **kwargs):
c = cache(self)
k = key(self, *args, **kwargs)
with lock(self):
cond(self).wait_for(lambda: k not in pending)
try:
return c[k]
except KeyError:
pending.add(k)
try:
v = method(self, *args, **kwargs)
with lock(self):
try:
c[k] = v
except ValueError:
pass # value too large
return v
finally:
with lock(self):
pending.remove(k)
cond(self).notify_all()
def cache_clear(self):
c = cache(self)
with lock(self):
c.clear()
def classmethod_wrapper(self, *args, **kwargs):
p = pending.setdefault(self, set())
return wrapper(self, p, *args, **kwargs)
class Descriptor(_DeprecatedDescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key, lock, cond)
self.__pending = set()
def __call__(self, *args, **kwargs):
return wrapper(self._obj, self.__pending, *args, **kwargs)
# objtype: backward-compatible @classmethod handling with Python < 3.13
def cache_clear(self, _objtype=None):
return cache_clear(self._obj)
return Descriptor(classmethod_wrapper, cache_clear)
def _locked(method, cache, key, lock):
def wrapper(self, *args, **kwargs):
c = cache(self)
k = key(self, *args, **kwargs)
with lock(self):
try:
return c[k]
except KeyError:
pass # key not found
v = method(self, *args, **kwargs)
with lock(self):
try:
# In case of a race condition, i.e. if another thread
# stored a value for this key while we were calling
# method(), prefer the cached value.
return c.setdefault(k, v)
except ValueError:
return v # value too large
def cache_clear(self):
c = cache(self)
with lock(self):
c.clear()
class Descriptor(_DeprecatedDescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key, lock)
def __call__(self, *args, **kwargs):
return wrapper(self._obj, *args, **kwargs)
# objtype: backward-compatible @classmethod handling with Python < 3.13
def cache_clear(self, _objtype=None):
return cache_clear(self._obj)
return Descriptor(wrapper, cache_clear)
def _unlocked(method, cache, key):
def wrapper(self, *args, **kwargs):
c = cache(self)
k = key(self, *args, **kwargs)
try:
return c[k]
except KeyError:
pass # key not found
v = method(self, *args, **kwargs)
try:
c[k] = v
except ValueError:
pass # value too large
return v
def cache_clear(self):
c = cache(self)
c.clear()
class Descriptor(_DeprecatedDescriptorBase):
class Wrapper(_WrapperBase):
def __init__(self, obj):
super().__init__(obj, method, cache, key)
def __call__(self, *args, **kwargs):
return wrapper(self._obj, *args, **kwargs)
# objtype: backward-compatible @classmethod handling with Python < 3.13
def cache_clear(self, _objtype=None):
return cache_clear(self._obj)
return Descriptor(wrapper, cache_clear)
def _wrapper(method, cache, key, lock=None, cond=None, info=None):
if info is not None:
if cond is not None and lock is not None:
wrapper = _condition_info(method, cache, key, lock, cond, info)
elif cond is not None:
wrapper = _condition_info(method, cache, key, cond, cond, info)
elif lock is not None:
wrapper = _locked_info(method, cache, key, lock, info)
else:
wrapper = _unlocked_info(method, cache, key, info)
else:
if cond is not None and lock is not None:
wrapper = _condition(method, cache, key, lock, cond)
elif cond is not None:
wrapper = _condition(method, cache, key, cond, cond)
elif lock is not None:
wrapper = _locked(method, cache, key, lock)
else:
wrapper = _unlocked(method, cache, key)
# backward-compatible properties for deprecated @classmethod use
wrapper.cache = cache
wrapper.cache_key = key
wrapper.cache_lock = lock if lock is not None else cond
wrapper.cache_condition = cond
return functools.update_wrapper(wrapper, method)
|