10000 bpo-39829: __len__() called twice in the list() constructor · python/cpython@0a5d672 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a5d672

Browse files
bpo-39829: __len__() called twice in the list() constructor
1 parent 4d95fa1 commit 0a5d672

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

Objects/listobject.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,16 @@ list_extend(PyListObject *self, PyObject *iterable)
889889
/* It should not be possible to allocate a list large enough to cause
890890
an overflow on any relevant platform */
891891
assert(m < PY_SSIZE_T_MAX - n);
892-
if (list_resize(self, m + n) < 0) {
893-
Py_DECREF(iterable);
894-
return NULL;
892+
if (self->ob_item == NULL) {
893+
if (list_preallocate_exact(self, n) < 0) {
894+
Py_DECREF(iterable);
895+
return NULL;
896+
}
897+
else {
898+
if (list_resize(self, m + n) < 0) {
899+
Py_DECREF(iterable);
900+
return NULL;
901+
}
895902
}
896903
/* note that we may still have self == iterable here for the
897904
* situation a.extend(a), but the following code works
@@ -929,10 +936,16 @@ list_extend(PyListObject *self, PyObject *iterable)
929936
*/
930937
}
931938
else {
932-
mn = m + n;
933-
/* Make room. */
934-
if (list_resize(self, mn) < 0)
935-
goto error;
939+
if (self->ob_item == NULL) {
940+
if (list_preallocate_exact(self, n) < 0)
941+
goto error;
942+
}
943+
else {
944+
mn = m + n;
945+
/* Make room. */
946+
if (list_resize(self, mn) < 0)
947+
goto error;
948+
}
936949
/* Make the list sane again. */
937950
Py_SET_SIZE(self, m);
938951
}
@@ -2814,19 +2827,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
28142827
(void)_list_clear(self);
28152828
}
28162829
if (iterable != NULL) {
2817-
if (_PyObject_HasLen(iterable)) {
2818-
Py_ssize_t iter_len = PyObject_Size(iterable);
2819-
if (iter_len == -1) {
2820-
if (!PyErr_ExceptionMatches(PyExc_TypeError)) {
2821-
return -1;
2822-
}
2823-
PyErr_Clear();
2824-
}
2825-
if (iter_len > 0 && self->ob_item == NULL
2826-
&& list_preallocate_exact(self, iter_len)) {
2827-
return -1;
2828-
}
2829-
}
28302830
PyObject *rv = list_extend(self, iterable);
28312831
if (rv == NULL)
28322832
return -1;

0 commit comments

Comments
 (0)
0