| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1203,6 +1203,37 @@ def test_early_detection_of_bad_call(self): | |||
| 1203 | 1203 | def f(): | |
| 1204 | 1204 | pass | |
| 1205 | 1205 | ||
| 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 | + | ||
| 1206 | 1237 | class TestLRUC(TestLRU, unittest.TestCase): | |
| 1207 | 1238 | module = c_functools | |
| 1208 | 1239 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,9 @@ Core and Builtins | |||
| 15 | 15 | Library | |
| 16 | 16 | ------- | |
| 17 | 17 | ||
| 18 | + - Issue #14373: C implementation of functools.lru_cache() now can be used with | ||
| 19 | + methods. | ||
| 20 | + | ||
| 18 | 21 | - Issue #8232: webbrowser support incomplete on Windows. Patch by Brandon | |
| 19 | 22 | Milam | |
| 20 | 23 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1003,6 +1003,16 @@ lru_cache_call(lru_cache_object *self, PyObject *args, PyObject *kwds) | |||
| 1003 | 1003 | return self->wrapper(self, args, kwds); | |
| 1004 | 1004 | } | |
| 1005 | 1005 | ||
| 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 | + | ||
| 1006 | 1016 | static PyObject * | |
| 1007 | 1017 | lru_cache_cache_info(lru_cache_object *self, PyObject *unused) | |
| 1008 | 1018 | { | |
@@ -1115,7 +1125,7 @@ static PyTypeObject lru_cache_type = { | |||
| 1115 | 1125 | lru_cache_getsetlist, /* tp_getset */ | |
| 1116 | 1126 | 0, /* tp_base */ | |
| 1117 | 1127 | 0, /* tp_dict */ | |
| 1118 | - 0, /* tp_descr_get */ | ||
| 1128 | + lru_cache_descr_get, /* tp_descr_get */ | ||
| 1119 | 1129 | 0, /* tp_descr_set */ | |
| 1120 | 1130 | offsetof(lru_cache_object, dict), /* tp_dictoffset */ | |
| 1121 | 1131 | 0, /* tp_init */ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments