mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-39829: Fix __len__()
is called twice in list() constructor (GH-31816)
(cherry picked from commit 2153daf0a0
)
This patch fixes gh-87740 too.
Co-authored-by: Crowthebird <78076854+thatbirdguythatuknownot@users.noreply.github.com>
This commit is contained in:
parent
add8820df8
commit
009aeb6bb6
3 changed files with 14 additions and 17 deletions
|
@ -863,7 +863,6 @@ list_extend(PyListObject *self, PyObject *iterable)
|
|||
PyObject *it; /* iter(v) */
|
||||
Py_ssize_t m; /* size of self */
|
||||
Py_ssize_t n; /* guess for size of iterable */
|
||||
Py_ssize_t mn; /* m + n */
|
||||
Py_ssize_t i;
|
||||
PyObject *(*iternext)(PyObject *);
|
||||
|
||||
|
@ -887,7 +886,13 @@ list_extend(PyListObject *self, PyObject *iterable)
|
|||
/* It should not be possible to allocate a list large enough to cause
|
||||
an overflow on any relevant platform */
|
||||
assert(m < PY_SSIZE_T_MAX - n);
|
||||
if (list_resize(self, m + n) < 0) {
|
||||
if (self->ob_item == NULL) {
|
||||
if (list_preallocate_exact(self, n) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
Py_SET_SIZE(self, n);
|
||||
}
|
||||
else if (list_resize(self, m + n) < 0) {
|
||||
Py_DECREF(iterable);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -926,10 +931,13 @@ list_extend(PyListObject *self, PyObject *iterable)
|
|||
* eventually run out of memory during the loop.
|
||||
*/
|
||||
}
|
||||
else if (self->ob_item == NULL) {
|
||||
if (n && list_preallocate_exact(self, n) < 0)
|
||||
goto error;
|
||||
}
|
||||
else {
|
||||
mn = m + n;
|
||||
/* Make room. */
|
||||
if (list_resize(self, mn) < 0)
|
||||
if (list_resize(self, m + n) < 0)
|
||||
goto error;
|
||||
/* Make the list sane again. */
|
||||
Py_SET_SIZE(self, m);
|
||||
|
@ -2717,19 +2725,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
|
|||
(void)_list_clear(self);
|
||||
}
|
||||
if (iterable != NULL) {
|
||||
if (_PyObject_HasLen(iterable)) {
|
||||
Py_ssize_t iter_len = PyObject_Size(iterable);
|
||||
if (iter_len == -1) {
|
||||
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
|
||||
return -1;
|
||||
}
|
||||
PyErr_Clear();
|
||||
}
|
||||
if (iter_len > 0 && self->ob_item == NULL
|
||||
&& list_preallocate_exact(self, iter_len)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
PyObject *rv = list_extend(self, iterable);
|
||||
if (rv == NULL)
|
||||
return -1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue