@@ -889,9 +889,16 @@ list_extend(PyListObject *self, PyObject *iterable)
889
889
/* It should not be possible to allocate a list large enough to cause
890
890
an overflow on any relevant platform */
891
891
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
+ }
895
902
}
896
903
/* note that we may still have self == iterable here for the
897
904
* situation a.extend(a), but the following code works
@@ -929,10 +936,16 @@ list_extend(PyListObject *self, PyObject *iterable)
929
936
*/
930
937
}
931
938
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
+ }
936
949
/* Make the list sane again. */
937
950
Py_SET_SIZE (self , m );
938
951
}
@@ -2814,19 +2827,6 @@ list___init___impl(PyListObject *self, PyObject *iterable)
2814
2827
(void )_list_clear (self );
2815
2828
}
2816
2829
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
2830
PyObject * rv = list_extend (self , iterable );
2831
2831
if (rv == NULL )
2832
2832
return -1 ;
0 commit comments