mirror of
https://github.com/python/cpython.git
synced 2024-11-27 11:55:13 +08:00
Issue #27443: __length_hint__() of bytearray itearator no longer return
negative integer for resized bytearray.
This commit is contained in:
parent
8faca61fec
commit
af65872da2
@ -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
|
||||
|
@ -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
|
||||
-------
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user