Skip to content

Commit 65381bc

Browse files
committed
Add new cache_parameters() method for functools.lru_cache() to better support pickling
1 parent 96b06ae commit 65381bc

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

Doc/library/functools.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ The :mod:`functools` module defines the following functions:
111111
To help measure the effectiveness of the cache and tune the *maxsize*
112112
parameter, the wrapped function is instrumented with a :func:`cache_info`
113113
function that returns a :term:`named tuple` showing *hits*, *misses*,
114-
*maxsize* and *currsize*. In a multi-threaded environment, the hits
114+
*maxsize* and *currsize*. the wrapped function is also instrumented woth
115+
a :func:`cache_parameters` function that returns a :class:`dict` which has
116+
showing *maxsize* and *typed*. In a multi-threaded environment, the hits
115117
and misses are approximate.
116118

117119
The decorator also provides a :func:`cache_clear` function for clearing or
@@ -178,6 +180,9 @@ The :mod:`functools` module defines the following functions:
178180
.. versionchanged:: 3.8
179181
Added the *user_function* option.
180182

183+
.. versionadded:: 3.9
184+
Added the function :func:`cache_parameters`
185+
181186
.. decorator:: total_ordering
182187

183188
Given a class defining one or more rich comparison ordering methods, this

Lib/functools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,15 @@ def lru_cache(maxsize=128, typed=False):
499499
# The user_function was passed in directly via the maxsize argument
500500
user_function, maxsize = maxsize, 128
501501
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
502+
wrapper.cache_parameters = lambda: {'maxsize': maxsize, 'typed': typed}
502503
return update_wrapper(wrapper, user_function)
503504
elif maxsize is not None:
504505
raise TypeError(
505506
'Expected first argument to be an integer, a callable, or None')
506507

507508
def decorating_function(user_function):
508509
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
510+
wrapper.cache_parameters = lambda: {'maxsize': maxsize, 'typed': typed}
509511
return update_wrapper(wrapper, user_function)
510512

511513
return decorating_function
@@ -621,7 +623,6 @@ def cache_clear():
621623
root[:] = [root, root, None, None]
622624
hits = misses = 0
623625
full = False
624-
625626
wrapper.cache_info = cache_info
626627
wrapper.cache_clear = cache_clear
627628
return wrapper

Lib/test/test_functools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,19 @@ def orig(x, y):
16551655
f_copy = copy.deepcopy(f)
16561656
self.assertIs(f_copy, f)
16571657

1658+
def test_lru_cache_parameters(self):
1659+
def f():
1660+
return 1
1661+
f = self.module.lru_cache(2)(f)
1662+
self.assertEqual(f.cache_parameters().get("maxsize"), 2)
1663+
self.assertEqual(f.cache_parameters().get("typed"), False)
1664+
1665+
@self.module.lru_cache(maxsize=1000, typed=True)
1666+
def f():
1667+
return 1
1668+
self.assertEqual(f.cache_parameters().get("maxsize"), 1000)
1669+
self.assertEqual(f.cache_parameters().get("typed"), True)
1670+
16581671

16591672
@py_functools.lru_cache()
16601673
def py_cached_func(x, y):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add new cache_parameters() method for functools.lru_cache() to better support pickling.

0 commit comments

Comments
 (0)