[issue36650] Cached method implementation no longer works on Python 3.7.3

Raymond Hettinger report at bugs.python.org
Fri Apr 19 20:55:12 EDT 2019


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

For the record, here's what is going on.  The method_cache() code uses a slightly different invocation for the first call than for subsequent calls. In particular, the wrapper() uses **kwargs with an empty dict whereas the first call didn't use keyword arguments at all.   The C version of the lru_cache() is treating that first call as distinct from the second call, resulting in a cache miss for the both the first and second invocation but not in subsequent invocations.

The pure python lru_cache() has a memory saving fast path taken when kwds is an empty dict.  The C version is out-of-sync with because it runs the that path only when kwds==NULL and it doesn't check for the case where kwds is an empty dict.  Here's a minimal reproducer:

    @lru_cache()
    def f(x):
        pass

    f(0)
    f(0, **{})
    assert f.cache_info().hits == 1

Here's a possible fix:

diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 3f1c01651d..f118119479 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -751,7 +751,7 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
     Py_ssize_t key_size, pos, key_pos, kwds_size;

     /* short path, key will match args anyway, which is a tuple */
-    if (!typed && !kwds) {
+    if (!typed && (!kwds || PyDict_GET_SIZE(kwds) == 0)) {
         if (PyTuple_GET_SIZE(args) == 1) {
             key = PyTuple_GET_ITEM(args, 0);
             if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) {

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36650>
_______________________________________


More information about the Python-bugs-list mailing list