Skip to content

Commit

Permalink
Consistently move the misses update to just before the user function …
Browse files Browse the repository at this point in the history
…call (pythonGH-11715)
  • Loading branch information
rhettinger committed Jan 31, 2019
1 parent dcfcd14 commit ffdf1c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,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:
Expand All @@ -557,9 +557,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:
Expand Down
8 changes: 5 additions & 3 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit ffdf1c3

Please sign in to comment.