@@ -865,7 +865,6 @@ list_extend(PyListObject *self, PyObject *iterable)
865
865
PyObject * it ; /* iter(v) */
866
866
Py_ssize_t m ; /* size of self */
867
867
Py_ssize_t n ; /* guess for size of iterable */
868
- Py_ssize_t mn ; /* m + n */
869
868
Py_ssize_t i ;
870
869
PyObject * (* iternext )(PyObject * );
871
870
@@ -889,7 +888,13 @@ list_extend(PyListObject *self, PyObject *iterable)
889
888
/* It should not be possible to allocate a list large enough to cause
890
889
an overflow on any relevant platform */
891
890
assert (m < PY_SSIZE_T_MAX - n );
892
- if (list_resize (self , m + n ) < 0 ) {
891
+ if (self -> ob_item == NULL ) {
892
+ if (list_preallocate_exact (self , n ) < 0 ) {
893
+ return NULL ;
894
+ }
895
+ Py_SET_SIZE (self , n );
896
+ }
897
+ else if (list_resize (self , m + n ) < 0 ) {
893
898
Py_DECREF (iterable );
894
899
return NULL ;
895
900
}
@@ -928,10 +933,13 @@ list_extend(PyListObject *self, PyObject *iterable)
928
933
* eventually run out of memory during the loop.
929
934
*/
930
935
}
936
+ else if (self -> ob_item == NULL ) {
937
+ if (n && list_preallocate_exact (self , n ) < 0 )
938
+ goto error ;
939
+ }
931
940
else {
932
- mn = m + n ;
933
941
/* Make room. */
934
- if (list_resize (self , mn ) < 0 )
942
+ if (list_resize (self , m + n ) < 0 )
935
943
goto error ;
936
944
/* Make the list sane again. */
937
945
Py_SET_SIZE (self , m );
@@ -2814,19 +2822,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
2814
2822
(void )_list_clear (self );
2815
2823
}
2816
2824
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
- }
2830
2825
PyObject * rv = list_extend (self , iterable );
2831
2826
if (rv == NULL )
2832
2827
return -1 ;
0 commit comments