Skip to content

Commit 6c74b26

Browse files
committed
_PyDict_GetItemRef_KnownHash_LockHeld()
1 parent 028f18b commit 6c74b26

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

Modules/_functoolsmodule.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,17 +1301,19 @@ bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject
13011301
Py_DECREF(key_);
13021302
return -1;
13031303
}
1304-
link = (lru_list_elem *)_PyDict_GetItem_KnownHash(self->cache, key_, hash_);
1305-
if (link != NULL) {
1304+
int res = _PyDict_GetItemRef_KnownHash_LockHeld((PyDictObject *)self->cache, key_, hash_,
1305+
(PyObject **)&link);
1306+
if (res > 0) {
13061307
lru_cache_extract_link(link);
13071308
lru_cache_append_link(self, link);
13081309
*result = link->result;
13091310
self->hits++;
13101311
Py_INCREF(link->result);
1312+
Py_DECREF(link);
13111313
Py_DECREF(key_);
13121314
return 1;
13131315
}
1314-
if (PyErr_Occurred()) {
1316+
if (res < 0) {
13151317
Py_DECREF(key_);
13161318
return -1;
13171319
}
@@ -1325,20 +1327,23 @@ bounded_lru_cache_update_lock_held(lru_cache_object *self,
13251327
{
13261328
lru_list_elem *link;
13271329
PyObject *testresult;
1330+
int res;
13281331

13291332
if (!result) {
13301333
Py_DECREF(key);
13311334
return NULL;
13321335
}
1333-
testresult = _PyDict_GetItem_KnownHash(self->cache, key, hash);
1334-
if (testresult != NULL) {
1336+
res = _PyDict_GetItemRef_KnownHash_LockHeld((PyDictObject *)self->cache, key, hash,
1337+
&testresult);
1338+
if (res > 0) {
13351339
/* Getting here means that this same key was added to the cache
13361340
during the PyObject_Call(). Since the link update is already
13371341
done, we need only return the computed result. */
1342+
Py_DECREF(testresult);
13381343
Py_DECREF(key);
13391344
return result;
13401345
}
1341-
if (PyErr_Occurred()) {
1346+
if (res < 0) {
13421347
/* This is an unusual case since this same lookup
13431348
did not previously trigger an error during lookup.
13441349
Treat it the same as an error in user function
@@ -1402,7 +1407,7 @@ bounded_lru_cache_update_lock_held(lru_cache_object *self,
14021407
The cache dict holds one reference to the link.
14031408
We created one other reference when the link was created.
14041409
The linked list only has borrowed references. */
1405-
int res = _PyDict_Pop_KnownHash((PyDictObject*)self->cache, link->key,
1410+
res = _PyDict_Pop_KnownHash((PyDictObject*)self->cache, link->key,
14061411
link->hash, &popresult);
14071412
if (res < 0) {
14081413
/* An error arose while trying to remove the oldest key (the one

0 commit comments

Comments
 (0)