summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-08-12 19:44:52 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-08-12 19:44:52 (GMT)
commit4d58897fdb0f123340246fac79174a52d7cbcf85 (patch)
tree312b779808cbfe4b8ce9d9a9e142e0ccb778cca5
parent4d83192ea05f5c14534edbff2b3a74bb19f01068 (diff)
downloadcpython-4d58897fdb0f123340246fac79174a52d7cbcf85.zip
cpython-4d58897fdb0f123340246fac79174a52d7cbcf85.tar.gz
cpython-4d58897fdb0f123340246fac79174a52d7cbcf85.tar.bz2
Issue 22184: Early detection and reporting of missing lru_cache parameters
-rw-r--r--Lib/functools.py6
-rw-r--r--Lib/test/test_functools.py7
-rw-r--r--Misc/NEWS3
3 files changed, 16 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py
index b8463ad..24fdb16 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -392,6 +392,12 @@ 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.
+ if maxsize is not None and not isinstance(maxsize, int):
+ raise TypeError('Expected maxsize to be an integer or None')
+
# Constants shared by all lru cache instances:
sentinel = object() # unique object used to signal cache misses
make_key = _make_key # build a key from the function arguments
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 75ae7f3..a7e5c7e 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1070,6 +1070,13 @@ class TestLRU(unittest.TestCase):
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
+
class TestSingleDispatch(unittest.TestCase):
def test_simple_overloads(self):
diff --git a/Misc/NEWS b/Misc/NEWS
index 1c87874..310a3b0 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,9 @@ Library
- Issue #21448: Changed FeedParser feed() to avoid O(N**2) behavior when
parsing long line. Original patch by Raymond Hettinger.
+- Issue #22184: The functools LRU Cache decorator factory now gives an earlier
+ and clearer error message when the user forgets the required parameters.
+
- Issue #17923: glob() patterns ending with a slash no longer match non-dirs on
AIX. Based on patch by Delhallt.