FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Issue #14373: C implementation of functools.lru_cache() now can be us… · python/cpython@e7070f0 · GitHub

Repository navigation

Commit e7070f0

Browse files
Issue #14373: C implementation of functools.lru_cache() now can be used with
methods.
1 parent 77cb197 commit e7070f0

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎Lib/test/test_functools.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,37 @@ def test_early_detection_of_bad_call(self):
12031203
def f():
12041204
pass
12051205

1206+
def test_lru_method(self):
1207+
class X(int):
1208+
f_cnt = 0
1209+
@self.module.lru_cache(2)
1210+
def f(self, x):
1211+
self.f_cnt += 1
1212+
return x*10+self
1213+
a = X(5)
1214+
b = X(5)
1215+
c = X(7)
1216+
self.assertEqual(X.f.cache_info(), (0, 0, 2, 0))
1217+
1218+
for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3:
1219+
self.assertEqual(a.f(x), x*10 + 5)
1220+
self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0))
1221+
self.assertEqual(X.f.cache_info(), (4, 6, 2, 2))
1222+
1223+
for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2:
1224+
self.assertEqual(b.f(x), x*10 + 5)
1225+
self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0))
1226+
self.assertEqual(X.f.cache_info(), (10, 10, 2, 2))
1227+
1228+
for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1:
1229+
self.assertEqual(c.f(x), x*10 + 7)
1230+
self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5))
1231+
self.assertEqual(X.f.cache_info(), (15, 15, 2, 2))
1232+
1233+
self.assertEqual(a.f.cache_info(), X.f.cache_info())
1234+
self.assertEqual(b.f.cache_info(), X.f.cache_info())
1235+
self.assertEqual(c.f.cache_info(), X.f.cache_info())
1236+
12061237
class TestLRUC(TestLRU, unittest.TestCase):
12071238
module = c_functools
12081239

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Core and Builtins
1515
Library
1616
-------
1717

18+
- Issue #14373: C implementation of functools.lru_cache() now can be used with
19+
methods.
20+
1821
- Issue #8232: webbrowser support incomplete on Windows. Patch by Brandon
1922
Milam
2023

‎Modules/_functoolsmodule.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,16 @@ lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds)
10031003
return self->wrapper(self, args, kwds);
10041004
}
10051005

1006+
static PyObject *
1007+
lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
1008+
{
1009+
if (obj == Py_None || obj == NULL) {
1010+
Py_INCREF(self);
1011+
return self;
1012+
}
1013+
return PyMethod_New(self, obj);
1014+
}
1015+
10061016
static PyObject *
10071017
lru_cache_cache_info(lru_cache_object *self, PyObject *unused)
10081018
{
@@ -1115,7 +1125,7 @@ static PyTypeObject lru_cache_type = {
11151125
lru_cache_getsetlist, /* tp_getset */
11161126
0, /* tp_base */
11171127
0, /* tp_dict */
1118-
0, /* tp_descr_get */
1128+
lru_cache_descr_get, /* tp_descr_get */
11191129
0, /* tp_descr_set */
11201130
offsetof(lru_cache_object, dict), /* tp_dictoffset */
11211131
0, /* tp_init */

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL