[Python-checkins] cpython (3.5): Issue #27443: __length_hint__() of bytearray itearator no longer return

serhiy.storchaka python-checkins at python.org
Sun Jul 3 07:42:40 EDT 2016


https://hg.python.org/cpython/rev/6b084bb6c38b
changeset:   102248:6b084bb6c38b
branch:      3.5
parent:      102240:b2c3837f7833
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Jul 03 14:41:36 2016 +0300
summary:
  Issue #27443: __length_hint__() of bytearray itearator no longer return
negative integer for resized bytearray.

files:
  Lib/test/test_bytes.py    |  10 ++++++++++
  Misc/NEWS                 |   3 +++
  Objects/bytearrayobject.c |   6 +++++-
  3 files changed, 18 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1233,6 +1233,16 @@
 
     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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #27443: __length_hint__() of bytearray itearator no longer return
+  negative integer for resized bytearray.
+
 Library
 -------
 
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -3192,8 +3192,12 @@
 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);
 }
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list