Author: Carl Friedrich Bolz-Tereick <cfb...@gmx.de>
Branch: py3.6
Changeset: r96044:e93780665de6
Date: 2019-02-18 10:09 +0100
http://bitbucket.org/pypy/pypy/changeset/e93780665de6/

Log:    fix setstate on unicode iters

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
@@ -59,7 +59,7 @@
     __next__ = interpindirect2app(W_AbstractSeqIterObject.descr_next),
     __reduce__ = interp2app(W_AbstractSeqIterObject.descr_reduce),
     __length_hint__ = interp2app(W_AbstractSeqIterObject.descr_length_hint),
-    __setstate__ = interp2app(W_AbstractSeqIterObject.descr_setstate),
+    __setstate__ = interpindirect2app(W_AbstractSeqIterObject.descr_setstate),
 )
 W_AbstractSeqIterObject.typedef.acceptable_as_base_class = False
 
@@ -127,6 +127,19 @@
         self.index += 1
         return w_res
 
+    def descr_setstate(self, space, w_state):
+        from pypy.objspace.std.unicodeobject import W_UnicodeObject
+        index = space.int_w(w_state)
+        w_seq = self.w_seq
+        if w_seq is not None:
+            assert isinstance(w_seq, W_UnicodeObject)
+            if index < 0:
+                index = 0
+            if index >= w_seq._len():
+                index = w_seq._len()
+            self.index = index
+            self.byteindex = w_seq._index_to_byte(index)
+
 
 class W_FastTupleIterObject(W_AbstractSeqIterObject):
     """Sequence iterator specialized for tuples, accessing directly
diff --git a/pypy/objspace/std/test/test_unicodeobject.py 
b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -1326,3 +1326,22 @@
         assert str(e.value) == 'decoding str is not supported'
         e = raises(TypeError, str, z, 'supposedly_the_encoding')
         assert str(e.value) == 'decoding str is not supported'
+
+    def test_reduce_iterator(self):
+        it = iter(u"abcdef")
+        assert next(it) == u"a"
+        assert next(it) == u"b"
+        assert next(it) == u"c"
+        assert next(it) == u"d"
+        args = it.__reduce__()
+        assert next(it) == u"e"
+        assert next(it) == u"f"
+        it2 = args[0](*args[1])
+        it2.__setstate__(args[2])
+        assert next(it2) == u"e"
+        assert next(it2) == u"f"
+        it3 = args[0](*args[1])
+        it3.__setstate__(args[2])
+        assert next(it3) == u"e"
+        assert next(it3) == u"f"
+
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to