summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-07-03 14:42:17 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2016-07-03 14:42:17 +0300
commit6c94d10a193ffdaad9d3b7c2376d9f4de776575f (patch)
tree4910d3b8512256ee95749b0ce863e512b4d55eed
parente09132f2c764a9e7df1181b1999d517d949c99e6 (diff)
parentaf65872da2b679e4b1876412aab6a731f82a469f (diff)
downloadcpython-git-6c94d10a193ffdaad9d3b7c2376d9f4de776575f.tar.gz
Issue #27443: __length_hint__() of bytearray itearator no longer return
negative integer for resized bytearray.
-rw-r--r--Lib/test/test_bytes.py10
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/bytearrayobject.c6
3 files changed, 18 insertions, 1 deletions
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 9d878caa44..05dc26afaf 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1328,6 +1328,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 7dc5887dde..014ac547c1 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.6.0 alpha 3
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 --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 85990e0be4..50da637491 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -2303,8 +2303,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);
}