]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27443: __length_hint__() of bytearray itearator no longer return
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 3 Jul 2016 11:41:36 +0000 (14:41 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 3 Jul 2016 11:41:36 +0000 (14:41 +0300)
negative integer for resized bytearray.

Lib/test/test_bytes.py
Misc/NEWS
Objects/bytearrayobject.c

index 65c00d7a5f35d048703f6e47a9c14629f743dd87..cc312b1d3e66f1c8494d5966307dea200c04355f 100644 (file)
@@ -1233,6 +1233,16 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
 
     test_exhausted_iterator = test.list_tests.CommonTest.test_exhausted_iterator
 
+    def test_iterator_length_hint(self):
+        # Issue 27443: __length_hint__ can return negative integer
+        ba = bytearray(b'ab')
+        it = iter(ba)
+        next(it)
+        ba.clear()
+        # Shouldn't raise an error
+        self.assertEqual(list(it), [])
+
+
 class AssortedBytesTest(unittest.TestCase):
     #
     # Test various combinations of bytes and bytearray
index 935f81ea2814f122429034dca0b080088c208c96..a04212cd68744ffdacf11954a3e019fdf5e1d9b1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ Release date: TBA
 Core and Builtins
 -----------------
 
+- Issue #27443: __length_hint__() of bytearray itearator no longer return
+  negative integer for resized bytearray.
+
 Library
 -------
 
index 277be59ad8c7696153d0faffdcb4c163d4dcae1c..388e9909bd9c4f78be9d126e98be670356fe99a0 100644 (file)
@@ -3192,8 +3192,12 @@ static PyObject *
 bytearrayiter_length_hint(bytesiterobject *it)
 {
     Py_ssize_t len = 0;
-    if (it->it_seq)
+    if (it->it_seq) {
         len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
+        if (len < 0) {
+            len = 0;
+        }
+    }
     return PyLong_FromSsize_t(len);
 }