self.assertEqual(misses, 4)
self.assertEqual(currsize, 2)
+ def test_lru_type_error(self):
+ # Regression test for issue #28653.
+ # lru_cache was leaking when one of the arguments
+ # wasn't cacheable.
+
+ @functools.lru_cache(maxsize=None)
+ def infinite_cache(o):
+ pass
+
+ @functools.lru_cache(maxsize=10)
+ def limited_cache(o):
+ pass
+
+ with self.assertRaises(TypeError):
+ infinite_cache([])
+
+ with self.assertRaises(TypeError):
+ limited_cache([])
+
def test_lru_with_maxsize_none(self):
@self.module.lru_cache(maxsize=None)
def fib(n):
if (!key)
return NULL;
hash = PyObject_Hash(key);
- if (hash == -1)
+ if (hash == -1) {
+ Py_DECREF(key);
return NULL;
+ }
result = _PyDict_GetItem_KnownHash(self->cache, key, hash);
if (result) {
Py_INCREF(result);
if (!key)
return NULL;
hash = PyObject_Hash(key);
- if (hash == -1)
+ if (hash == -1) {
+ Py_DECREF(key);
return NULL;
+ }
link = (lru_list_elem *)_PyDict_GetItem_KnownHash(self->cache, key, hash);
if (link) {
lru_cache_extricate_link(link);