[pypy-commit] pypy default: add reduce/setstate for functools.partial to match cpython, test

bdkearns noreply at buildbot.pypy.org
Wed Apr 10 21:54:48 CEST 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r63207:c7692ec6d138
Date: 2013-04-10 15:52 -0400
http://bitbucket.org/pypy/pypy/changeset/c7692ec6d138/

Log:	add reduce/setstate for functools.partial to match cpython, test

diff --git a/lib_pypy/_functools.py b/lib_pypy/_functools.py
--- a/lib_pypy/_functools.py
+++ b/lib_pypy/_functools.py
@@ -20,3 +20,16 @@
         if self.keywords is not None:
             fkeywords = dict(self.keywords, **fkeywords)
         return self.func(*(self.args + fargs), **fkeywords)
+
+    def __reduce__(self):
+        d = dict((k, v) for k, v in self.__dict__.iteritems() if k not in
+                ('func', 'args', 'keywords'))
+        if len(d) == 0:
+            d = None
+        return (type(self), (self.func,),
+                (self.func, self.args, self.keywords, d))
+
+    def __setstate__(self, state):
+        self.func, self.args, self.keywords, d = state
+        if d is not None:
+            self.__dict__.update(d)
diff --git a/pypy/module/test_lib_pypy/test_functools.py b/pypy/module/test_lib_pypy/test_functools.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/test_lib_pypy/test_functools.py
@@ -0,0 +1,19 @@
+from lib_pypy import _functools
+
+def test_partial_reduce():
+    partial = _functools.partial(test_partial_reduce)
+    state = partial.__reduce__()
+    assert state == (type(partial), (test_partial_reduce,),
+                     (test_partial_reduce, (), None, None))
+
+def test_partial_setstate():
+    partial = _functools.partial(object)
+    partial.__setstate__([test_partial_setstate, (), None, None])
+    assert partial.func == test_partial_setstate
+
+def test_partial_pickle():
+    import pickle
+    partial1 = _functools.partial(test_partial_pickle)
+    string = pickle.dumps(partial1)
+    partial2 = pickle.loads(string)
+    assert partial1.func == partial2.func


More information about the pypy-commit mailing list