changeset: 105031:5b253d641826 branch: 3.6 parent: 105028:caf3ceb93307 parent: 105030:ba59f3328032 user: Yury Selivanov date: Wed Nov 09 18:56:26 2016 -0500 files: Lib/test/test_functools.py Misc/NEWS Modules/_functoolsmodule.c description: Merge 3.6 (issue #28653) diff -r caf3ceb93307 -r 5b253d641826 Lib/test/test_functools.py --- a/Lib/test/test_functools.py Wed Nov 09 23:51:54 2016 +0200 +++ b/Lib/test/test_functools.py Wed Nov 09 18:56:26 2016 -0500 @@ -1189,6 +1189,25 @@ 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): diff -r caf3ceb93307 -r 5b253d641826 Misc/NEWS --- a/Misc/NEWS Wed Nov 09 23:51:54 2016 +0200 +++ b/Misc/NEWS Wed Nov 09 18:56:26 2016 -0500 @@ -48,6 +48,8 @@ - Issue #28652: Make loop methods reject socket kinds they do not support. +- Issue #28653: Fix a refleak in functools.lru_cache. + Documentation ------------- diff -r caf3ceb93307 -r 5b253d641826 Modules/_functoolsmodule.c --- a/Modules/_functoolsmodule.c Wed Nov 09 23:51:54 2016 +0200 +++ b/Modules/_functoolsmodule.c Wed Nov 09 18:56:26 2016 -0500 @@ -793,8 +793,10 @@ 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); @@ -849,8 +851,10 @@ 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);