diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:11 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-25 13:26:35 +0000 |
commit | 4fee3e091a8d79a40f70ff9c1f87b29b9340049a (patch) | |
tree | 465ad9629a8ee56548bd6c51c3fc710907564911 /gitlint/cache.py | |
parent | Releasing debian version 0.14.0-1. (diff) | |
download | gitlint-4fee3e091a8d79a40f70ff9c1f87b29b9340049a.tar.xz gitlint-4fee3e091a8d79a40f70ff9c1f87b29b9340049a.zip |
Merging upstream version 0.15.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gitlint/cache.py')
-rw-r--r-- | gitlint/cache.py | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/gitlint/cache.py b/gitlint/cache.py index b7f9e6c..1b6558f 100644 --- a/gitlint/cache.py +++ b/gitlint/cache.py @@ -1,4 +1,4 @@ -class PropertyCache(object): +class PropertyCache: """ Mixin class providing a simple cache. """ def __init__(self): @@ -13,7 +13,7 @@ class PropertyCache(object): return self._cache[cache_key] -def cache(original_func=None, cachekey=None): +def cache(original_func=None, cachekey=None): # pylint: disable=unused-argument """ Cache decorator. Caches function return values. Requires the parent class to extend and initialize PropertyCache. Usage: @@ -28,27 +28,23 @@ def cache(original_func=None, cachekey=None): ... """ - # Decorators with optional arguments are a bit convoluted in python, especially if you want to support both - # Python 2 and 3. See some of the links below for details. + # Decorators with optional arguments are a bit convoluted in python, see some of the links below for details. def cache_decorator(func): - - # If no specific cache key is given, use the function name as cache key - if not cache_decorator.cachekey: - cache_decorator.cachekey = func.__name__ + # Use 'nonlocal' keyword to access parent function variable: + # https://stackoverflow.com/a/14678445/381010 + nonlocal cachekey + if not cachekey: + cachekey = func.__name__ def wrapped(*args): def cache_func_result(): # Call decorated function and store its result in the cache - args[0]._cache[cache_decorator.cachekey] = func(*args) - return args[0]._try_cache(cache_decorator.cachekey, cache_func_result) + args[0]._cache[cachekey] = func(*args) + return args[0]._try_cache(cachekey, cache_func_result) return wrapped - # Passing parent function variables to child functions requires special voodoo in python2: - # https://stackoverflow.com/a/14678445/381010 - cache_decorator.cachekey = cachekey # attribute on the function - # To support optional kwargs for decorators, we need to check if a function is passed as first argument or not. # https://stackoverflow.com/a/24617244/381010 if original_func: |