[pypy-svn] pypy collections-module: __reduce__().

arigo commits-noreply at bitbucket.org
Tue Feb 15 17:58:46 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: collections-module
Changeset: r41983:eddc2340f69e
Date: 2011-02-15 17:57 +0100
http://bitbucket.org/pypy/pypy/changeset/eddc2340f69e/

Log:	__reduce__().

diff --git a/pypy/module/_collections/test/test_deque.py b/pypy/module/_collections/test/test_deque.py
--- a/pypy/module/_collections/test/test_deque.py
+++ b/pypy/module/_collections/test/test_deque.py
@@ -240,6 +240,31 @@
         assert d == e
         assert list(d) == list(e)
 
+    def test_reduce(self):
+        from _collections import deque
+        #
+        d = deque('hello world')
+        r = d.__reduce__()
+        assert r == (deque, (list('hello world'),))
+        #
+        d = deque('hello world', 42)
+        r = d.__reduce__()
+        assert r == (deque, (list('hello world'), 42))
+        #
+        class D(deque):
+            pass
+        d = D('hello world')
+        d.a = 5
+        r = d.__reduce__()
+        assert r == (D, (list('hello world'), None), {'a': 5})
+        #
+        class D(deque):
+            pass
+        d = D('hello world', 42)
+        d.a = 5
+        r = d.__reduce__()
+        assert r == (D, (list('hello world'), 42), {'a': 5})
+
     def test_copy(self):
         from _collections import deque
         import copy

diff --git a/pypy/module/_collections/interp_deque.py b/pypy/module/_collections/interp_deque.py
--- a/pypy/module/_collections/interp_deque.py
+++ b/pypy/module/_collections/interp_deque.py
@@ -415,6 +415,29 @@
             return space.call_function(space.type(w_self), w_self,
                                        space.wrap(self.maxint))
 
+    @unwrap_spec('self')
+    def reduce(self):
+        space = self.space
+        w_self = space.wrap(self)
+        w_type = space.type(w_self)
+        w_dict = space.findattr(w_self, space.wrap('__dict__'))
+        w_list = space.call_function(space.w_list, w_self)
+        if w_dict is None:
+            if self.maxlen == sys.maxint:
+                result = [
+                    w_type, space.newtuple([w_list])]
+            else:
+                result = [
+                    w_type, space.newtuple([w_list, space.wrap(self.maxlen)])]
+        else:
+            if self.maxlen == sys.maxint:
+                w_len = space.w_None
+            else:
+                w_len = space.wrap(self.maxlen)
+            result = [
+                w_type, space.newtuple([w_list, w_len]), w_dict]
+        return space.newtuple(result)
+
     def get_maxlen(space, self):
         if self.maxlen == sys.maxint:
             return self.space.w_None
@@ -484,6 +507,7 @@
     __setitem__ = interp2app(W_Deque.setitem),
     __delitem__ = interp2app(W_Deque.delitem),
     __copy__ = interp2app(W_Deque.copy),
+    __reduce__ = interp2app(W_Deque.reduce),
     maxlen = GetSetProperty(W_Deque.get_maxlen),
 )
 


More information about the Pypy-commit mailing list