Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/list_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def __length_hint__(self):
a.extend(CustomIter())
self.assertEqual(a, [1,2,3,4])

# bpo-28940
a = self.type2test([])
a.extend(CustomIter())
self.assertEqual(a, [])

def test_insert(self):
a = self.type2test([0, 1, 2])
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,19 @@ def test_translate(self):
c = b.translate(None, delete=b'e')
self.assertEqual(c, b'hllo')

def test_from_iter(self):
# bpo-28940
class CustomIter:
def __iter__(self):
return self
def __next__(self):
raise StopIteration
def __length_hint__(self):
return sys.maxsize

a = self.type2test(CustomIter())
self.assertEqual(a, b'')


class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import gc
import pickle
import sys

class TupleTest(seq_tests.CommonTest):
type2test = tuple
Expand Down Expand Up @@ -223,5 +224,19 @@ def test_lexicographic_ordering(self):
self.assertLess(a, b)
self.assertLess(b, c)

def test_from_iter(self):
# bpo-28940
class CustomIter:
def __iter__(self):
return self
def __next__(self):
raise StopIteration
def __length_hint__(self):
return sys.maxsize

a = self.type2test(CustomIter())
self.assertEqual(a, ())


if __name__ == "__main__":
unittest.main()
12 changes: 10 additions & 2 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1859,8 +1859,16 @@ PySequence_Tuple(PyObject *v)
if (n == -1)
goto Fail;
result = PyTuple_New(n);
if (result == NULL)
goto Fail;
if (result == NULL) {
/* bpo-28940 - LengthHint could lie and asked for too much,
* try again without preallocation. */
PyErr_Clear();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will silence any possible exception raised in PyTuple_New() (same for PyByteArray_FromStringAndSize(), _PyBytesWriter_Alloc(), etc.). I don't think that's the right approach.

result = PyTuple_New(0);
if (result == NULL) {
return NULL;
}
n = 0;
}

/* Fill the tuple. */
for (j = 0; ; ++j) {
Expand Down
11 changes: 9 additions & 2 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,15 @@ bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)

bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
if (bytearray_obj == NULL) {
Py_DECREF(it);
return NULL;
/* bpo-28940 - LengthHint could lie and asked for too much,
* try again without preallocation. */
PyErr_Clear();
bytearray_obj = PyByteArray_FromStringAndSize(NULL, 1);
if (bytearray_obj == NULL) {
Py_DECREF(it);
return NULL;
}
buf_size = 1;
}
buf = PyByteArray_AS_STRING(bytearray_obj);

Expand Down
12 changes: 10 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2698,8 +2698,16 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)

_PyBytesWriter_Init(&writer);
str = _PyBytesWriter_Alloc(&writer, size);
if (str == NULL)
return NULL;
if (str == NULL) {
/* bpo-28940 - LengthHint could lie and asked for too much,
* try again without preallocation. */
PyErr_Clear();
_PyBytesWriter_Init(&writer);
str = _PyBytesWriter_Alloc(&writer, 1);
if (str == NULL) {
return NULL;
}
}
writer.overallocate = 1;
size = writer.allocated;

Expand Down
13 changes: 9 additions & 4 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,15 @@ list_extend(PyListObject *self, PyObject *iterable)
else {
mn = m + n;
/* Make room. */
if (list_resize(self, mn) < 0)
goto error;
/* Make the list sane again. */
Py_SIZE(self) = m;
if (list_resize(self, mn) < 0) {
/* bpo-28940 - LengthHint could lie and asked for too much,
* try again without preallocation. */
PyErr_Clear();
}
else {
/* Make the list sane again. */
Py_SIZE(self) = m;
}
}

/* Run iterator to exhaustion. */
Expand Down