From: Serhiy Storchaka Date: Sun, 3 Jul 2016 11:41:36 +0000 (+0300) Subject: Issue #27443: __length_hint__() of bytearray itearator no longer return X-Git-Tag: v3.6.0a3~41^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=af65872da2b679e4b1876412aab6a731f82a469f;p=thirdparty%2FPython%2Fcpython.git Issue #27443: __length_hint__() of bytearray itearator no longer return negative integer for resized bytearray. --- diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py index 65c00d7a5f35..cc312b1d3e66 100644 --- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -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 diff --git a/Misc/NEWS b/Misc/NEWS index 935f81ea2814..a04212cd6874 100644 --- 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 ------- diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 277be59ad8c7..388e9909bd9c 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -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); }