Skip to content

Commit

Permalink
Use _PyDict_Pop_KnownHash() return value
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Nov 12, 2023
1 parent c8b46ed commit 8fb0bfb
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1087,19 +1087,8 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
The cache dict holds one reference to the link.
We created one other reference when the link was created.
The linked list only has borrowed references. */
(void)_PyDict_Pop_KnownHash((PyDictObject*)self->cache, link->key,
link->hash, Py_None, &popresult);
if (popresult == Py_None) {
/* Getting here means that the user function call or another
thread has already removed the old key from the dictionary.
This link is now an orphan. Since we don't want to leave the
cache in an inconsistent state, we don't restore the link. */
Py_DECREF(popresult);
Py_DECREF(link);
Py_DECREF(key);
return result;
}
if (popresult == NULL) {
if (_PyDict_Pop_KnownHash((PyDictObject*)self->cache, link->key,
link->hash, Py_None, &popresult) < 0) {
/* An error arose while trying to remove the oldest key (the one
being evicted) from the cache. We restore the link to its
original position as the oldest link. Then we allow the
Expand All @@ -1110,6 +1099,17 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
Py_DECREF(result);
return NULL;
}

if (popresult == Py_None) {
/* Getting here means that the user function call or another
thread has already removed the old key from the dictionary.
This link is now an orphan. Since we don't want to leave the
cache in an inconsistent state, we don't restore the link. */
Py_DECREF(popresult);
Py_DECREF(link);
Py_DECREF(key);
return result;
}
/* Keep a reference to the old key and old result to prevent their
ref counts from going to zero during the update. That will
prevent potentially arbitrary object clean-up code (i.e. __del__)
Expand Down

0 comments on commit 8fb0bfb

Please sign in to comment.