Consistently move the misses update to just before the user function call (GH-11715)
diff --git a/Lib/functools.py b/Lib/functools.py
index 6233c30..fe47600 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -541,10 +541,10 @@
if maxsize == 0:
def wrapper(*args, **kwds):
- # No caching -- just a statistics update after a successful call
+ # No caching -- just a statistics update
nonlocal misses
- result = user_function(*args, **kwds)
misses += 1
+ result = user_function(*args, **kwds)
return result
elif maxsize is None:
@@ -557,9 +557,9 @@
if result is not sentinel:
hits += 1
return result
+ misses += 1
result = user_function(*args, **kwds)
cache[key] = result
- misses += 1
return result
else:
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 1412102..d72aaff 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -796,10 +796,12 @@
static PyObject *
uncached_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds)
{
- PyObject *result = PyObject_Call(self->func, args, kwds);
+ PyObject *result;
+
+ self->misses++;
+ result = PyObject_Call(self->func, args, kwds);
if (!result)
return NULL;
- self->misses++;
return result;
}
@@ -827,6 +829,7 @@
Py_DECREF(key);
return NULL;
}
+ self->misses++;
result = PyObject_Call(self->func, args, kwds);
if (!result) {
Py_DECREF(key);
@@ -838,7 +841,6 @@
return NULL;
}
Py_DECREF(key);
- self->misses++;
return result;
}