summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-05-26 18:27:35 (GMT)
committerGitHub <noreply@github.com>2019-05-26 18:27:35 (GMT)
commitb821868e6d909f4805499db519ebc2cdc01cf611 (patch)
treeae3da900a9fa80bc5098dd4eb3150ebe6e25ac2e /Lib
parentaaf47caf35984e614d93bd8bea5227df55e0e3e6 (diff)
downloadcpython-b821868e6d909f4805499db519ebc2cdc01cf611.zip
cpython-b821868e6d909f4805499db519ebc2cdc01cf611.tar.gz
cpython-b821868e6d909f4805499db519ebc2cdc01cf611.tar.bz2
bpo-36772 Allow lru_cache to be used as decorator without making a function call (GH-13048)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/functools.py12
-rw-r--r--Lib/test/test_functools.py19
2 files changed, 20 insertions, 11 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index c863341..30964a6 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -518,14 +518,18 @@ def lru_cache(maxsize=128, typed=False):
# The internals of the lru_cache are encapsulated for thread safety and
# to allow the implementation to change (including a possible C version).
- # Early detection of an erroneous call to @lru_cache without any arguments
- # resulting in the inner function being passed to maxsize instead of an
- # integer or None. Negative maxsize is treated as 0.
if isinstance(maxsize, int):
+ # Negative maxsize is treated as 0
if maxsize < 0:
maxsize = 0
+ elif callable(maxsize) and isinstance(typed, bool):
+ # The user_function was passed in directly via the maxsize argument
+ user_function, maxsize = maxsize, 128
+ wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
+ return update_wrapper(wrapper, user_function)
elif maxsize is not None:
- raise TypeError('Expected maxsize to be an integer or None')
+ raise TypeError(
+ 'Expected first argument to be an integer, a callable, or None')
def decorating_function(user_function):
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index b89d779..8fee1c6 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1251,6 +1251,18 @@ class TestLRU:
self.assertEqual(misses, 4)
self.assertEqual(currsize, 2)
+ def test_lru_no_args(self):
+ @self.module.lru_cache
+ def square(x):
+ return x ** 2
+
+ self.assertEqual(list(map(square, [10, 20, 10])),
+ [100, 400, 100])
+ self.assertEqual(square.cache_info().hits, 1)
+ self.assertEqual(square.cache_info().misses, 2)
+ self.assertEqual(square.cache_info().maxsize, 128)
+ self.assertEqual(square.cache_info().currsize, 2)
+
def test_lru_bug_35780(self):
# C version of the lru_cache was not checking to see if
# the user function call has already modified the cache
@@ -1582,13 +1594,6 @@ class TestLRU:
self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call
DoubleEq(2)) # Verify the correct return value
- def test_early_detection_of_bad_call(self):
- # Issue #22184
- with self.assertRaises(TypeError):
- @functools.lru_cache
- def f():
- pass
-
def test_lru_method(self):
class X(int):
f_cnt = 0