]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #25447: Copying the lru_cache() wrapper object now always works,
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 28 Dec 2015 21:58:07 +0000 (23:58 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 28 Dec 2015 21:58:07 +0000 (23:58 +0200)
independedly from the type of the wrapped object (by returning the original
object unchanged).

Lib/test/test_functools.py
Misc/NEWS
Modules/_functoolsmodule.c

index d822b2de4578926d6c5ab48aa7c217e96dab6e46..cf0b95d73c755ba969c43fec1a438b3b3b6afce9 100644 (file)
@@ -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)
index 7a0bcef00954cc28532b26c1780703820827d38c..bb0e0568f5d02b46721759af2b65797ca7df55a3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #25447: Copying the lru_cache() wrapper object now always works,
+  independedly from the type of the wrapped object (by returning the original
+  object unchanged).
+
 - Issue #24103: Fixed possible use after free in ElementTree.XMLPullParser.
 
 - Issue #25860: os.fwalk() no longer skips remaining directories when error
index fadc0a9c2057681aff2c799ba4a7efa2c276db15..035d3d9c591a182fd26e21942ab425c814ffb315 100644 (file)
@@ -1053,6 +1053,20 @@ lru_cache_reduce(PyObject *self, PyObject *unused)
     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}
 };