diff -r b02d256b8827 Lib/test/test_functools.py --- a/Lib/test/test_functools.py Sun Dec 27 15:51:32 2015 +0200 +++ b/Lib/test/test_functools.py Sun Dec 27 20:18:08 2015 +0200 @@ -1262,14 +1262,24 @@ class TestLRU: def test_copy(self): cls = self.__class__ - for f in cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth: + def orig(x, y): + return 3 * x + y + part = self.module.partial(orig, 2) + funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, + self.module.lru_cache(2)(part)) + for f in funcs: with self.subTest(func=f): f_copy = copy.copy(f) self.assertIs(f_copy, f) def test_deepcopy(self): cls = self.__class__ - for f in cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth: + def orig(x, y): + return 3 * x + y + part = self.module.partial(orig, 2) + funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, + self.module.lru_cache(2)(part)) + for f in funcs: with self.subTest(func=f): f_copy = copy.deepcopy(f) self.assertIs(f_copy, f) diff -r b02d256b8827 Modules/_functoolsmodule.c --- a/Modules/_functoolsmodule.c Sun Dec 27 15:51:32 2015 +0200 +++ b/Modules/_functoolsmodule.c Sun Dec 27 20:18:08 2015 +0200 @@ -1053,6 +1053,20 @@ lru_cache_reduce(PyObject *self, PyObjec return PyObject_GetAttrString(self, "__qualname__"); } +static PyObject * +lru_cache_copy(PyObject *self, PyObject *unused) +{ + Py_INCREF(self); + return self; +} + +static PyObject * +lru_cache_deepcopy(PyObject *self, PyObject *unused) +{ + Py_INCREF(self); + return self; +} + static int lru_cache_tp_traverse(lru_cache_object *self, visitproc visit, void *arg) { @@ -1104,6 +1118,8 @@ static PyMethodDef lru_cache_methods[] = {"cache_info", (PyCFunction)lru_cache_cache_info, METH_NOARGS}, {"cache_clear", (PyCFunction)lru_cache_cache_clear, METH_NOARGS}, {"__reduce__", (PyCFunction)lru_cache_reduce, METH_NOARGS}, + {"__copy__", (PyCFunction)lru_cache_copy, METH_VARARGS}, + {"__deepcopy__", (PyCFunction)lru_cache_deepcopy, METH_VARARGS}, {NULL} };