Skip to content
Myr

Merge pull request #3962 from pallets/cached-property

Before adoption: this commit came in with the history the project adopted, so no landing records it.

locked_cached_property subclasses Werkzeug's cached_property

authored by David Lord · committed by GitHub · parents 8ab4967, 5a7a4ab · Browse files · 1 file+19 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+19 added−20 removed

  • other:+19 added−20 removed

1 file+19 added−20 removed

modified:src/flask/helpers.py+19 added-20 removed
16 unmodified lines
17from .globals import request18from .globals import session19from .signals import message_flashed2021# sentinel22_missing = object()232421# what separators does this operating system provide that are not a slash?22# this is used by the send_from_directory function to ensure that nobody is23# able to access files from outside the filesystem.24_os_alt_seps = list(
647 unmodified lines
672 directory, path, **_prepare_send_file_kwargs(**kwargs)673 )674675680class locked_cached_property:681 """A decorator that converts a function into a lazy property. The682 function wrapped is called the first time to retrieve the result683 and then that calculated result is used the next time you access684 the value. Works like the one in Werkzeug but has a lock for685 thread safety.676class locked_cached_property(werkzeug.utils.cached_property):677 """A :func:`property` that is only evaluated once. Like678 :class:`werkzeug.utils.cached_property` except access uses a lock679 for thread safety.680681 .. versionchanged:: 2.0682 Inherits from Werkzeug's ``cached_property`` (and ``property``).683 """684688 def __init__(self, func, name=None, doc=None):689 self.__name__ = name or func.__name__690 self.__module__ = func.__module__691 self.__doc__ = doc or func.__doc__692 self.func = func685 def __init__(self, fget, name=None, doc=None):686 super().__init__(fget, name=name, doc=doc)687 self.lock = RLock()688689 def __get__(self, obj, type=None):690 if obj is None:691 return self692693 with self.lock:699 value = obj.__dict__.get(self.__name__, _missing)700 if value is _missing:701 value = self.func(obj)702 obj.__dict__[self.__name__] = value703 return value694 return super().__get__(obj, type=type)695696 def __set__(self, obj, value):697 with self.lock:698 super().__set__(obj, value)699700 def __delete__(self, obj):701 with self.lock:702 super().__delete__(obj)703704705def total_seconds(td):706 """Returns the total seconds from a timedelta object.707708 :param timedelta td: the timedelta to be converted in seconds
72 unmodified lines