summaryrefslogtreecommitdiffstats
path: root/Lib
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 /Lib
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 'Lib')
-rw-r--r--Lib/test/test_functools.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 9503f40..b3893a1 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -14,6 +14,8 @@ import typing
import unittest
import unittest.mock
import os
+import weakref
+import gc
from weakref import proxy
import contextlib
@@ -1938,6 +1940,35 @@ class TestLRU:
return 1
self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True})
+ def test_lru_cache_weakrefable(self):
+ @self.module.lru_cache
+ def test_function(x):
+ return x
+
+ class A:
+ @self.module.lru_cache
+ def test_method(self, x):
+ return (self, x)
+
+ @staticmethod
+ @self.module.lru_cache
+ def test_staticmethod(x):
+ return (self, x)
+
+ refs = [weakref.ref(test_function),
+ weakref.ref(A.test_method),
+ weakref.ref(A.test_staticmethod)]
+
+ for ref in refs:
+ self.assertIsNotNone(ref())
+
+ del A
+ del test_function
+ gc.collect()
+
+ for ref in refs:
+ self.assertIsNone(ref())
+
@py_functools.lru_cache()
def py_cached_func(x, y):