@@ -863,7 +863,6 @@ list_extend(PyListObject *self, PyObject *iterable)
863
863
PyObject * it ; /* iter(v) */
864
864
Py_ssize_t m ; /* size of self */
865
865
Py_ssize_t n ; /* guess for size of iterable */
866
- Py_ssize_t mn ; /* m + n */
867
866
Py_ssize_t i ;
868
867
PyObject * (* iternext )(PyObject * );
869
868
@@ -887,7 +886,13 @@ list_extend(PyListObject *self, PyObject *iterable)
887
886
/* It should not be possible to allocate a list large enough to cause
888
887
an overflow on any relevant platform */
889
888
assert (m < PY_SSIZE_T_MAX - n );
890
- if (list_resize (self , m + n ) < 0 ) {
889
+ if (self -> ob_item == NULL ) {
890
+ if (list_preallocate_exact (self , n ) < 0 ) {
891
+ return NULL ;
892
+ }
893
+ Py_SET_SIZE (self , n );
894
+ }
895
+ else if (list_resize (self , m + n ) < 0 ) {
891
896
Py_DECREF (iterable );
892
897
return NULL ;
893
898
}
@@ -926,10 +931,13 @@ list_extend(PyListObject *self, PyObject *iterable)
926
931
* eventually run out of memory during the loop.
927
932
*/
928
933
}
934
+ else if (self -> ob_item == NULL ) {
935
+ if (n && list_preallocate_exact (self , n ) < 0 )
936
+ goto error ;
937
+ }
929
938
else {
930
- mn = m + n ;
931
939
/* Make room. */
932
- if (list_resize (self , mn ) < 0 )
940
+ if (list_resize (self , m + n ) < 0 )
933
941
goto error ;
934
942
/* Make the list sane again. */
935
943
Py_SET_SIZE (self , m );
@@ -2717,19 +2725,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
2717
2725
(void )_list_clear (self );
2718
2726
}
2719
2727
if (iterable != NULL ) {
2720
- if (_PyObject_HasLen (iterable )) {
2721
- Py_ssize_t iter_len = PyObject_Size (iterable );
2722
- if (iter_len == -1 ) {
2723
- if (!PyErr_ExceptionMatches (PyExc_TypeError )) {
2724
- return -1 ;
2725
- }
2726
- PyErr_Clear ();
2727
- }
2728
- if (iter_len > 0 && self -> ob_item == NULL
2729
- && list_preallocate_exact (self , iter_len )) {
2730
- return -1 ;
2731
- }
2732
- }
2733
2728
PyObject * rv = list_extend (self , iterable );
2734
2729
if (rv == NULL )
2735
2730
return -1 ;
0 commit comments