Skip to content
Closed
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 @@ -285,6 +285,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 @@ -1137,6 +1137,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'')

def test_sq_item(self):
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
obj = self.type2test((42,))
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

# For tuple hashes, we normally only run a test to ensure that we get
# the same results across platforms in a handful of cases. If that's
Expand Down Expand Up @@ -434,6 +435,20 @@ 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, ())


# Notes on testing hash codes. The primary thing is that Python doesn't
# care about "random" hash codes. To the contrary, we like them to be
# very regular when possible, so that the low-order bits are as evenly
Expand Down
11 changes: 9 additions & 2 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2234,8 +2234,15 @@ bytearray_extend_impl(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
8 changes: 7 additions & 1 deletion Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3073,7 +3073,13 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)

PyBytesWriter *writer = PyBytesWriter_Create(size);
if (writer == NULL) {
return NULL;
/* gh-73126: __length_hint__() can lie and ask for too much,
so try again without preallocation. */
PyErr_Clear();
writer = PyBytesWriter_Create(0);
if (writer == NULL) {
return NULL;
}
}
char *str = PyBytesWriter_GetData(writer);
size = _PyBytesWriter_GetAllocated(writer);
Expand Down
16 changes: 10 additions & 6 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,17 +1300,21 @@ list_extend_iter_lock_held(PyListObject *self, PyObject *iterable)
*/
}
else if (self->ob_item == NULL) {
if (n && list_preallocate_exact(self, n) < 0)
goto error;
/* gh-73126: __length_hint__() can lie and ask for too much,
so fall back to appending without preallocation. */
if (n && list_preallocate_exact(self, n) < 0) {
PyErr_Clear();
}
}
else {
/* Make room. */
if (list_resize(self, m + n) < 0) {
goto error;
PyErr_Clear();
}
else {
/* Make the list sane again. */
Py_SET_SIZE(self, m);
}

/* Make the list sane again. */
Py_SET_SIZE(self, m);
}

/* Run iterator to exhaustion. */
Expand Down
Loading