[pypy-commit] pypy stdlib-2.7.12: fix mutation: length_hint wasn't exhausting correctly (and shouldn't

pjenvey pypy.commits at gmail.com
Sun Oct 2 18:34:22 EDT 2016


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: stdlib-2.7.12
Changeset: r87529:f8f420b37c8e
Date: 2016-10-02 15:33 -0700
http://bitbucket.org/pypy/pypy/changeset/f8f420b37c8e/

Log:	fix mutation: length_hint wasn't exhausting correctly (and shouldn't
	anyway)

diff --git a/pypy/objspace/std/iterobject.py b/pypy/objspace/std/iterobject.py
--- a/pypy/objspace/std/iterobject.py
+++ b/pypy/objspace/std/iterobject.py
@@ -129,19 +129,10 @@
         return space.newtuple([new_inst, space.newtuple(tup)])
 
     def descr_length_hint(self, space):
-        if self.w_seq is None:
-            return space.wrap(0)
-        index = self.index + 1
-        w_length = space.len(self.w_seq)
-        # if length of sequence is less than index :exhaust iterator
-        if space.is_true(space.gt(space.wrap(self.index), w_length)):
-            w_len = space.wrap(0)
-            self.w_seq = None
-        else:
-            w_len = space.wrap(index)
-        if space.is_true(space.lt(w_len, space.wrap(0))):
-            w_len = space.wrap(0)
-        return w_len
+        length = self.index + 1
+        if self.w_seq is None or space.len_w(self.w_seq) < length:
+            length = 0
+        return space.wrap(length)
 
     def descr_iter(self, space):
         return self
diff --git a/pypy/objspace/std/test/test_iterobject.py b/pypy/objspace/std/test/test_iterobject.py
--- a/pypy/objspace/std/test/test_iterobject.py
+++ b/pypy/objspace/std/test/test_iterobject.py
@@ -102,6 +102,21 @@
             gc.collect(); gc.collect(); gc.collect()
             assert free[0]
 
+    def test_reversed_mutation(self):
+        n = 10
+        d = range(n)
+        it = reversed(d)
+        it.next()
+        it.next()
+        assert it.__length_hint__() == n-2
+        d.append(n)
+        assert it.__length_hint__() == n-2
+        d[1:] = []
+        assert it.__length_hint__() == 0
+        assert list(it) == []
+        d.extend(xrange(20))
+        assert it.__length_hint__() == 0
+
     def test_no_len_on_set_iter(self):
         iterable = set([1,2,3,4])
         raises(TypeError, len, iter(iterable))


More information about the pypy-commit mailing list