Merge 3.6 (issue #28653)

This commit is contained in:
Yury Selivanov 2016-11-09 18:57:00 -05:00
commit 3e05a9c2b0
2 changed files with 25 additions and 2 deletions

View File

@ -1189,6 +1189,25 @@ class TestLRU:
self.assertEqual(misses, 4) self.assertEqual(misses, 4)
self.assertEqual(currsize, 2) 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): def test_lru_with_maxsize_none(self):
@self.module.lru_cache(maxsize=None) @self.module.lru_cache(maxsize=None)
def fib(n): def fib(n):

View File

@ -793,8 +793,10 @@ infinite_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwd
if (!key) if (!key)
return NULL; return NULL;
hash = PyObject_Hash(key); hash = PyObject_Hash(key);
if (hash == -1) if (hash == -1) {
Py_DECREF(key);
return NULL; return NULL;
}
result = _PyDict_GetItem_KnownHash(self->cache, key, hash); result = _PyDict_GetItem_KnownHash(self->cache, key, hash);
if (result) { if (result) {
Py_INCREF(result); Py_INCREF(result);
@ -849,8 +851,10 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
if (!key) if (!key)
return NULL; return NULL;
hash = PyObject_Hash(key); hash = PyObject_Hash(key);
if (hash == -1) if (hash == -1) {
Py_DECREF(key);
return NULL; return NULL;
}
link = (lru_list_elem *)_PyDict_GetItem_KnownHash(self->cache, key, hash); link = (lru_list_elem *)_PyDict_GetItem_KnownHash(self->cache, key, hash);
if (link) { if (link) {
lru_cache_extricate_link(link); lru_cache_extricate_link(link);