changeset: 102249:03192909160d parent: 102247:6c4c0a23fabb parent: 102248:6b084bb6c38b user: Serhiy Storchaka date: Sun Jul 03 14:42:17 2016 +0300 files: Lib/test/test_bytes.py Misc/NEWS Objects/bytearrayobject.c description: Issue #27443: __length_hint__() of bytearray itearator no longer return negative integer for resized bytearray. diff -r 6c4c0a23fabb -r 03192909160d Lib/test/test_bytes.py --- a/Lib/test/test_bytes.py Sun Jul 03 13:57:48 2016 +0300 +++ b/Lib/test/test_bytes.py Sun Jul 03 14:42:17 2016 +0300 @@ -1328,6 +1328,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 -r 6c4c0a23fabb -r 03192909160d Misc/NEWS --- a/Misc/NEWS Sun Jul 03 13:57:48 2016 +0300 +++ b/Misc/NEWS Sun Jul 03 14:42:17 2016 +0300 @@ -10,6 +10,9 @@ Core and Builtins ----------------- +- Issue #27443: __length_hint__() of bytearray itearator no longer return + negative integer for resized bytearray. + - Issue #27007: The fromhex() class methods of bytes and bytearray subclasses now return an instance of corresponding subclass. diff -r 6c4c0a23fabb -r 03192909160d Objects/bytearrayobject.c --- a/Objects/bytearrayobject.c Sun Jul 03 13:57:48 2016 +0300 +++ b/Objects/bytearrayobject.c Sun Jul 03 14:42:17 2016 +0300 @@ -2303,8 +2303,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); }