Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: py3.5
Changeset: r88036:8596145cda3f
Date: 2016-11-01 16:02 +0000
http://bitbucket.org/pypy/pypy/changeset/8596145cda3f/

Log:    Add unpack_iterator.__length_hint__()

diff --git a/pypy/module/struct/interp_struct.py 
b/pypy/module/struct/interp_struct.py
--- a/pypy/module/struct/interp_struct.py
+++ b/pypy/module/struct/interp_struct.py
@@ -128,6 +128,12 @@
         self.index += size
         return w_res
 
+    def descr_length_hint(self, space):
+        if self.w_struct is None:
+            return space.newint(0)
+        length = (self.buf.getlength() - self.index) // self.w_struct.size
+        return space.newint(length)
+
 
 class W_Struct(W_Root):
     _immutable_fields_ = ["format", "size"]
@@ -183,7 +189,7 @@
     __new__=interp2app(new_unpackiter),
     __iter__=interp2app(W_UnpackIter.descr_iter),
     __next__=interp2app(W_UnpackIter.descr_next),
-    #__length_hint__=
+    __length_hint__=interp2app(W_UnpackIter.descr_length_hint)
 )
 
 @unwrap_spec(format=str)
diff --git a/pypy/module/struct/test/test_struct.py 
b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -399,6 +399,7 @@
         b = array.array('b', b'\0' * 16)
         s = self.struct.Struct('ii')
         it = s.iter_unpack(b)
+        assert it.__length_hint__() == 2
         assert list(it) == [(0, 0), (0, 0)]
         it = self.struct.iter_unpack('ii', b)
         assert list(it) == [(0, 0), (0, 0)]
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to