Author: Amaury Forgeot d'Arc <amaur...@gmail.com>
Branch: py3.3
Changeset: r82013:a5416d54ea4e
Date: 2016-01-31 21:11 +0100
http://bitbucket.org/pypy/pypy/changeset/a5416d54ea4e/

Log:    Pickle support for itertools.islice

diff --git a/pypy/module/itertools/interp_itertools.py 
b/pypy/module/itertools/interp_itertools.py
--- a/pypy/module/itertools/interp_itertools.py
+++ b/pypy/module/itertools/interp_itertools.py
@@ -404,6 +404,15 @@
             if num <= 0:
                 break
 
+    def descr_reduce(self, space):
+        return space.newtuple([
+            space.type(self),
+            space.newtuple([self.iterable,
+                            space.wrap(self.start),
+                            space.wrap(self.stop),
+                            space.wrap(self.ignore + 1)]),
+        ])
+
 def W_ISlice___new__(space, w_subtype, w_iterable, w_startstop, args_w):
     r = space.allocate_instance(W_ISlice, w_subtype)
     r.__init__(space, w_iterable, w_startstop, args_w)
@@ -414,6 +423,7 @@
         __new__  = interp2app(W_ISlice___new__),
         __iter__ = interp2app(W_ISlice.iter_w),
         __next__ = interp2app(W_ISlice.next_w),
+        __reduce__ = interp2app(W_ISlice.descr_reduce),
         __doc__  = """Make an iterator that returns selected elements from the
     iterable.  If start is non-zero, then elements from the iterable
     are skipped until start is reached. Afterward, elements are
diff --git a/pypy/module/itertools/test/test_itertools.py 
b/pypy/module/itertools/test/test_itertools.py
--- a/pypy/module/itertools/test/test_itertools.py
+++ b/pypy/module/itertools/test/test_itertools.py
@@ -988,6 +988,11 @@
             assert list(op(testIntermediate)) == [
                 (0,1,3), (0,2,3), (1,2,3)]
 
+    def test_islice_pickle(self):
+        import itertools, pickle
+        it = itertools.islice(range(100), 10, 20, 3)
+        assert list(pickle.loads(pickle.dumps(it))) == 
list(range(100)[10:20:3])
+
     def test_cycle_pickle(self):
         import itertools, pickle
         c = itertools.cycle('abc')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to