From ff1d43ddf92f5c5a85d72766e01208566bfc1d1c Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Thu, 9 May 2019 14:21:38 -0400 Subject: [PATCH] Refine @cached decorator from #3258 Don't restrict to Python 2 precisely. --- beets/util/functemplate.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/beets/util/functemplate.py b/beets/util/functemplate.py index 9c625b7f9..5d9900f0b 100644 --- a/beets/util/functemplate.py +++ b/beets/util/functemplate.py @@ -554,12 +554,15 @@ def _parse(template): return Expression(parts) -# Decorator that enables lru_cache on py3, and no caching on py2. def cached(func): - if six.PY2: - # Sorry python2 users, no caching for you :( + """Like the `functools.lru_cache` decorator, but works (as a no-op) + on Python < 3.2. + """ + if hasattr(functools, 'lru_cache'): + return functools.lru_cache(maxsize=128)(func) + else: + # Do nothing when lru_cache is not available. return func - return functools.lru_cache(maxsize=128)(func) @cached