summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2020-05-05 21:14:32 (GMT)
committerGitHub <noreply@github.com>2020-05-05 21:14:32 (GMT)
commit1253c3ef70ea5632d32ae19579a14152db0d45c1 (patch)
treed35fc3b94beab1fca2ee6b9d3c88d2c38747316f /Modules
parentc21c51235aa8061da6b0593d6f857f42fd92fd8b (diff)
downloadcpython-1253c3ef70ea5632d32ae19579a14152db0d45c1.zip
cpython-1253c3ef70ea5632d32ae19579a14152db0d45c1.tar.gz
cpython-1253c3ef70ea5632d32ae19579a14152db0d45c1.tar.bz2
bpo-40504: Allow weakrefs to lru_cache objects (GH-19938)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_functoolsmodule.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index fd4b4c2..d158d3b 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -783,6 +783,7 @@ typedef struct lru_cache_object {
Py_ssize_t misses;
PyObject *cache_info_type;
PyObject *dict;
+ PyObject *weakreflist;
} lru_cache_object;
static PyTypeObject lru_cache_type;
@@ -1196,6 +1197,7 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
Py_INCREF(cache_info_type);
obj->cache_info_type = cache_info_type;
obj->dict = NULL;
+ obj->weakreflist = NULL;
return (PyObject *)obj;
}
@@ -1227,6 +1229,8 @@ lru_cache_dealloc(lru_cache_object *obj)
lru_list_elem *list;
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(obj);
+ if (obj->weakreflist != NULL)
+ PyObject_ClearWeakRefs((PyObject*)obj);
list = lru_cache_unlink_list(obj);
Py_XDECREF(obj->cache);
@@ -1384,7 +1388,8 @@ static PyTypeObject lru_cache_type = {
(traverseproc)lru_cache_tp_traverse,/* tp_traverse */
(inquiry)lru_cache_tp_clear, /* tp_clear */
0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
+ offsetof(lru_cache_object, weakreflist),
+ /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
lru_cache_methods, /* tp_methods */