[Python-checkins] Consistently move the misses update to just before the user function call (GH-11715) (GH-11716)

Raymond Hettinger webhook-mailer at python.org
Thu Jan 31 18:35:05 EST 2019


https://github.com/python/cpython/commit/533a9b459b29e8c2254b6bf6d82fd1cfa83be8cd
commit: 533a9b459b29e8c2254b6bf6d82fd1cfa83be8cd
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2019-01-31T15:35:00-08:00
summary:

Consistently move the misses update to just before the user function call (GH-11715) (GH-11716)

files:
M Lib/functools.py
M Modules/_functoolsmodule.c

diff --git a/Lib/functools.py b/Lib/functools.py
index 592f156fe426..b734899b56de 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -500,10 +500,10 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
     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:
@@ -516,9 +516,9 @@ def wrapper(*args, **kwds):
             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 4aca63e38c8b..90c698ee41e4 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -796,10 +796,12 @@ lru_cache_make_key(PyObject *args, PyObject *kwds, int typed)
 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 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
         Py_DECREF(key);
         return NULL;
     }
+    self->misses++;
     result = PyObject_Call(self->func, args, kwds);
     if (!result) {
         Py_DECREF(key);
@@ -838,7 +841,6 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
         return NULL;
     }
     Py_DECREF(key);
-    self->misses++;
     return result;
 }
 



More information about the Python-checkins mailing list