[Python-checkins] cpython (merge 3.5 -> default): Issue #14373: C implementation of functools.lru_cache() now can be used with

serhiy.storchaka python-checkins at python.org
Mon Jun 8 10:21:09 CEST 2015


https://hg.python.org/cpython/rev/f4b785d14792
changeset: 96538:f4b785d14792
parent: 96536:da331f50aad4
parent: 96537:eff50d543c79
user: Serhiy Storchaka <storchaka at gmail.com>
date: Mon Jun 08 11:20:22 2015 +0300
summary:
 Issue #14373: C implementation of functools.lru_cache() now can be used with
methods.
files:
 Lib/test/test_functools.py | 31 ++++++++++++++++++++++++++
 Misc/NEWS | 3 ++
 Modules/_functoolsmodule.c | 12 +++++++++-
 3 files changed, 45 insertions(+), 1 deletions(-)
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1203,6 +1203,37 @@
 def f():
 pass
 
+ def test_lru_method(self):
+ class X(int):
+ f_cnt = 0
+ @self.module.lru_cache(2)
+ def f(self, x):
+ self.f_cnt += 1
+ return x*10+self
+ a = X(5)
+ b = X(5)
+ c = X(7)
+ self.assertEqual(X.f.cache_info(), (0, 0, 2, 0))
+
+ for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3:
+ self.assertEqual(a.f(x), x*10 + 5)
+ self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0))
+ self.assertEqual(X.f.cache_info(), (4, 6, 2, 2))
+
+ for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2:
+ self.assertEqual(b.f(x), x*10 + 5)
+ self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0))
+ self.assertEqual(X.f.cache_info(), (10, 10, 2, 2))
+
+ for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1:
+ self.assertEqual(c.f(x), x*10 + 7)
+ self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5))
+ self.assertEqual(X.f.cache_info(), (15, 15, 2, 2))
+
+ self.assertEqual(a.f.cache_info(), X.f.cache_info())
+ self.assertEqual(b.f.cache_info(), X.f.cache_info())
+ self.assertEqual(c.f.cache_info(), X.f.cache_info())
+
 class TestLRUC(TestLRU, unittest.TestCase):
 module = c_functools
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
 Library
 -------
 
+- Issue #14373: C implementation of functools.lru_cache() now can be used with
+ methods.
+
 - Issue #8232: webbrowser support incomplete on Windows. Patch by Brandon
 Milam
 
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -1004,6 +1004,16 @@
 }
 
 static PyObject *
+lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
+{
+ if (obj == Py_None || obj == NULL) {
+ Py_INCREF(self);
+ return self;
+ }
+ return PyMethod_New(self, obj);
+}
+
+static PyObject *
 lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
 {
 return PyObject_CallFunction(self->cache_info_type, "nnOn",
@@ -1115,7 +1125,7 @@
 lru_cache_getsetlist, /* tp_getset */
 0, /* tp_base */
 0, /* tp_dict */
- 0, /* tp_descr_get */
+ lru_cache_descr_get, /* tp_descr_get */
 0, /* tp_descr_set */
 offsetof(lru_cache_object, dict), /* tp_dictoffset */
 0, /* tp_init */
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /