Author: Amaury Forgeot d'Arc <amaur...@gmail.com>
Branch: py3.5
Changeset: r87669:55828ab39bab
Date: 2016-10-09 17:56 +0200
http://bitbucket.org/pypy/pypy/changeset/55828ab39bab/

Log:    Fix test_islice when run with -A, then change itertools.islice() to
        clear the iterator when exhausted.

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
@@ -317,7 +317,6 @@
     def __init__(self, space, w_iterable, w_startstop, args_w):
         self.iterable = space.iter(w_iterable)
         self.space = space
-        self.exhausted = False
 
         num_args = len(args_w)
 
@@ -385,24 +384,24 @@
                                 # has no effect any more
                 if stop > 0:
                     self._ignore_items(stop)
-                self.exhausted = True
+                self.iterable = None
                 raise OperationError(self.space.w_StopIteration,
                                      self.space.w_None)
             self.stop = stop - (ignore + 1)
         if ignore > 0:
             self._ignore_items(ignore)
-        if self.exhausted:
+        if self.iterable is None:
             raise OperationError(self.space.w_StopIteration, self.space.w_None)
         try:
             return self.space.next(self.iterable)
         except OperationError as e:
             if e.match(self.space, self.space.w_StopIteration):
-                self.exhausted = True
+                self.iterable = None
             raise
 
     def _ignore_items(self, num):
         w_iterator = self.iterable
-        if self.exhausted:
+        if w_iterator is None:
             raise OperationError(self.space.w_StopIteration, self.space.w_None)
 
         tp = self.space.type(w_iterator)
@@ -415,13 +414,22 @@
                 self.space.next(w_iterator)
             except OperationError as e:
                 if e.match(self.space, self.space.w_StopIteration):
-                    self.exhausted = True
+                    self.iterable = None
                 raise
             num -= 1
             if num <= 0:
                 break
 
     def descr_reduce(self, space):
+        if self.iterable is None:
+            return space.newtuple([
+                space.type(self),
+                space.newtuple([space.iter(space.newlist([])),
+                                space.wrap(0),
+                                space.wrap(0),
+                                space.wrap(1),
+                            ]),
+            ])
         start = self.start
         stop = self.stop
         if start == -1:
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
@@ -222,7 +222,7 @@
             assert wr() is not None
             list(it)  # exhaust the iterator
             import gc; gc.collect()
-            assert wr() is not None
+            assert wr() is None
             raises(StopIteration, next, it)
 
     def test_islice_dropitems_exact(self):
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to