[pypy-commit] pypy py3.3: merge heads

arigo noreply at buildbot.pypy.org
Sat Jul 26 15:36:14 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.3
Changeset: r72513:b14679e8321d
Date: 2014-07-26 15:35 +0200
http://bitbucket.org/pypy/pypy/changeset/b14679e8321d/

Log:	merge heads

diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -711,6 +711,13 @@
             if pred ^ self.reverse:
                 return w_obj
 
+    def descr_reduce(self, space):
+        w_filter = space.getattr(space.getbuiltinmodule('builtins'),
+                space.wrap('filter'))
+        args = [space.w_None if self.no_predicate else self.w_predicate,
+                self.iterable]
+        return space.newtuple([w_filter, space.newtuple(args)])
+
 
 def W_Filter___new__(space, w_subtype, w_predicate, w_iterable):
     r = space.allocate_instance(W_Filter, w_subtype)
@@ -722,6 +729,7 @@
         __new__  = interp2app(W_Filter___new__),
         __iter__ = interp2app(W_Filter.iter_w),
         __next__ = interp2app(W_Filter.next_w),
+        __reduce__ = interp2app(W_Filter.descr_reduce),
         __doc__  = """\
 Return an iterator yielding those items of iterable for which function(item)
 is true. If function is None, return the items that are true.""")
diff --git a/pypy/module/__builtin__/test/test_filter_pickle.py b/pypy/module/__builtin__/test/test_filter_pickle.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/__builtin__/test/test_filter_pickle.py
@@ -0,0 +1,72 @@
+class AppTestFilterPickle:
+
+    def test_filter_unpickle(self):
+        """Test just the unpickling."""
+        import pickle
+
+        # This is filter(None, 'abc') pickled with cpython
+        dump = b'\x80\x03cbuiltins\nfilter\nq\x00Ncbuiltins\niter\nq\x01X\x03'\
+               b'\x00\x00\x00abcq\x02\x85q\x03Rq\x04K\x00b\x86q\x05Rq\x06.'
+        t = pickle.loads(dump)
+        assert list(t) == ['a', 'b', 'c']
+
+    def test_iterator_pickle(self):
+        """Pickle and unpickle just a simple iterator."""
+        import pickle
+
+        i0 = iter("abc")
+        i1 = iter("abc")
+
+        d = pickle.dumps(i1)
+        i1 = pickle.loads(d)
+
+        assert list(i0) == list(i1)
+
+    def test_reduce_ex(self):
+        """"""
+        f0 = filter(None, "abc")
+        f1 = filter(None, "abc")
+
+        print(f0)
+        r = f1.__reduce_ex__(3)
+        # __reduce_ex__ doesn't return any arguments to the filter, so the next
+        # line will fail with TypeError.
+        f1 = r[0](*r[1])
+
+        assert list(f0) == list(f1)
+
+    def test_nonfilter_pickle(self):
+        """Pickle and unpickle a filter with no filtering."""
+        import pickle
+
+        f0 = filter(None, "abc")
+        d = pickle.dumps(f0)
+        f1 = pickle.loads(d)
+
+    def test_filter_pickle(self):
+        """Clone of the original test."""
+        import pickle
+
+        def check_iter_pickle(it, seq):
+            itorg = it
+            d = pickle.dumps(it)
+            it = pickle.loads(d)
+            assert type(itorg) == type(it)
+            assert list(it) == seq
+
+            #test the iterator after dropping one from it
+            it = pickle.loads(d)
+            try:
+                next(it)
+            except StopIteration:
+                return
+            d = pickle.dumps(it)
+            it = pickle.loads(d)
+            assert list(it) == seq[1:]
+
+        # We use ord instead of filter_char because the filter function has to
+        # be defined in the global scope for the picking to work and we can't
+        # do it from this test.
+        f1 = filter(ord, "abcdeabcde")
+        f2 = filter(ord, "abcdeabcde")
+        check_iter_pickle(f1, list(f2))


More information about the pypy-commit mailing list