Skip to content

Commit 2910429

Browse files
committed
requested changes
1 parent 6c74b26 commit 2910429

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Make ``functools.lru_cache`` call the cached function unlocked to allow concurrency.
1+
Make :func:``functools.lru_cache`` call the cached function unlocked to allow concurrency.

Modules/_functoolsmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,14 +1291,15 @@ static int
12911291
bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject *kwds,
12921292
PyObject **result, PyObject **key, Py_hash_t *hash)
12931293
{
1294+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
12941295
lru_list_elem *link;
12951296

12961297
PyObject *key_ = *key = lru_cache_make_key(self->kwd_mark, args, kwds, self->typed);
12971298
if (!key_)
12981299
return -1;
12991300
Py_hash_t hash_ = *hash = PyObject_Hash(key_);
13001301
if (hash_ == -1) {
1301-
Py_DECREF(key_);
1302+
Py_DECREF(key_); /* dead reference left in *key, is not used */
13021303
return -1;
13031304
}
13041305
int res = _PyDict_GetItemRef_KnownHash_LockHeld((PyDictObject *)self->cache, key_, hash_,
@@ -1323,8 +1324,9 @@ bounded_lru_cache_get_lock_held(lru_cache_object *self, PyObject *args, PyObject
13231324

13241325
static PyObject *
13251326
bounded_lru_cache_update_lock_held(lru_cache_object *self,
1326-
PyObject *result, PyObject *key, Py_hash_t hash)
1327+
PyObject *result, PyObject *key, Py_hash_t hash)
13271328
{
1329+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
13281330
lru_list_elem *link;
13291331
PyObject *testresult;
13301332
int res;
@@ -1487,6 +1489,9 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
14871489
result = PyObject_Call(self->func, args, kwds);
14881490

14891491
Py_BEGIN_CRITICAL_SECTION(self);
1492+
/* Note: key will be released in the below function and
1493+
result may be relased on error, or returned as a passthrough
1494+
or have its reference count increased if is added to cache. */
14901495
result = bounded_lru_cache_update_lock_held(self, result, key, hash);
14911496
Py_END_CRITICAL_SECTION();
14921497

0 commit comments

Comments
 (0)