changeset: 92087:780693490c84 branch: 3.4 parent: 92085:c19d3465965f user: Raymond Hettinger date: Tue Aug 12 12:44:52 2014 -0700 files: Lib/functools.py Lib/test/test_functools.py Misc/NEWS description: Issue 22184: Early detection and reporting of missing lru_cache parameters diff -r c19d3465965f -r 780693490c84 Lib/functools.py --- a/Lib/functools.py Tue Aug 12 20:22:48 2014 +0300 +++ b/Lib/functools.py Tue Aug 12 12:44:52 2014 -0700 @@ -392,6 +392,12 @@ # 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 -r c19d3465965f -r 780693490c84 Lib/test/test_functools.py --- a/Lib/test/test_functools.py Tue Aug 12 20:22:48 2014 +0300 +++ b/Lib/test/test_functools.py Tue Aug 12 12:44:52 2014 -0700 @@ -1070,6 +1070,13 @@ 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 -r c19d3465965f -r 780693490c84 Misc/NEWS --- a/Misc/NEWS Tue Aug 12 20:22:48 2014 +0300 +++ b/Misc/NEWS Tue Aug 12 12:44:52 2014 -0700 @@ -30,6 +30,9 @@ - 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.