Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ def decorating_function(user_function):
return decorating_function

def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
if not callable(user_function):
Comment thread
picnixz marked this conversation as resolved.
raise TypeError("the first argument must be callable")

# 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
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,11 @@ def fib(n):
with self.assertRaises(RecursionError):
fib(support.exceeds_recursion_limit())

def test_lru_checks_arg_is_callable(self):
with self.assertRaises(TypeError) as te:
self.module.lru_cache(1)('hello')
self.assertIn("the first argument must be callable", str(te.exception))

Comment thread
cfbolz marked this conversation as resolved.
Outdated

@py_functools.lru_cache()
def py_cached_func(x, y):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The Python implementation of :func:`functools.lru_cache` differed from the
default C implementation in that it did not check that its argument is
callable. This discrepancy is now fixed and both raise a :class:`TypeError`.
Comment thread
cfbolz marked this conversation as resolved.
Outdated
Loading