Skip to content

Commit c403793

Browse files
committed
gh-144030: check that argument is callable in py version of lru_cache
1 parent d8ab1c7 commit c403793

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Lib/functools.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ def decorating_function(user_function):
602602
return decorating_function
603603

604604
def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
605+
if not callable(user_function):
606+
raise TypeError("the first argument must be callable")
607+
605608
# Constants shared by all lru cache instances:
606609
sentinel = object() # unique object used to signal cache misses
607610
make_key = _make_key # build a key from the function arguments

Lib/test/test_functools.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,11 @@ def fib(n):
21572157
with self.assertRaises(RecursionError):
21582158
fib(support.exceeds_recursion_limit())
21592159

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

21612166
@py_functools.lru_cache()
21622167
def py_cached_func(x, y):

0 commit comments

Comments
 (0)