]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38565: add new cache_parameters method for lru_cache (GH-16916)
authorManjusaka <me@manjusaka.me>
Tue, 12 Nov 2019 07:30:18 +0000 (15:30 +0800)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 12 Nov 2019 07:30:18 +0000 (23:30 -0800)
Doc/library/functools.rst
Lib/functools.py
Lib/test/test_functools.py
Misc/NEWS.d/next/Library/2019-10-24-08-10-30.bpo-38565.SWSUst.rst [new file with mode: 0644]

index d3debac8432b19808b84b9c50810540e6670f077..cedc3ad5ec0a61f9ea9bec2d491bbcce883a2c92 100644 (file)
@@ -108,6 +108,11 @@ The :mod:`functools` module defines the following functions:
    cached separately.  For example, ``f(3)`` and ``f(3.0)`` will be treated
    as distinct calls with distinct results.
 
+   The wrapped function is instrumented with a :func:`cache_parameters`
+   function that returns a new :class:`dict` showing the values for *maxsize*
+   and *typed*.  This is for information purposes only.  Mutating the values
+   has no effect.
+
    To help measure the effectiveness of the cache and tune the *maxsize*
    parameter, the wrapped function is instrumented with a :func:`cache_info`
    function that returns a :term:`named tuple` showing *hits*, *misses*,
@@ -178,6 +183,9 @@ The :mod:`functools` module defines the following functions:
    .. versionchanged:: 3.8
       Added the *user_function* option.
 
+   .. versionadded:: 3.9
+      Added the function :func:`cache_parameters`
+
 .. decorator:: total_ordering
 
    Given a class defining one or more rich comparison ordering methods, this
index 3192bd02d93e56b7cadb25ad05fd7aff1960b7e0..2c01b2e59524bfdf21dc7af8ddde193d8bda6c5c 100644 (file)
@@ -499,6 +499,7 @@ def lru_cache(maxsize=128, typed=False):
         # The user_function was passed in directly via the maxsize argument
         user_function, maxsize = maxsize, 128
         wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
+        wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
         return update_wrapper(wrapper, user_function)
     elif maxsize is not None:
         raise TypeError(
@@ -506,6 +507,7 @@ def lru_cache(maxsize=128, typed=False):
 
     def decorating_function(user_function):
         wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
+        wrapper.cache_parameters = lambda : {'maxsize': maxsize, 'typed': typed}
         return update_wrapper(wrapper, user_function)
 
     return decorating_function
index c300270d49e5ec4f2600e5684840b2eaecac704e..a97ca398e77a332fdf7a44d13c8cde9e21048814 100644 (file)
@@ -1655,6 +1655,17 @@ class TestLRU:
                 f_copy = copy.deepcopy(f)
                 self.assertIs(f_copy, f)
 
+    def test_lru_cache_parameters(self):
+        @self.module.lru_cache(maxsize=2)
+        def f():
+            return 1
+        self.assertEqual(f.cache_parameters(), {'maxsize': 2, "typed": False})
+
+        @self.module.lru_cache(maxsize=1000, typed=True)
+        def f():
+            return 1
+        self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True})
+
 
 @py_functools.lru_cache()
 def py_cached_func(x, y):
diff --git a/Misc/NEWS.d/next/Library/2019-10-24-08-10-30.bpo-38565.SWSUst.rst b/Misc/NEWS.d/next/Library/2019-10-24-08-10-30.bpo-38565.SWSUst.rst
new file mode 100644 (file)
index 0000000..34d7afc
--- /dev/null
@@ -0,0 +1 @@
+Add new cache_parameters() method for functools.lru_cache() to better support pickling.
\ No newline at end of file