@@ -627,23 +627,22 @@ listextend(self, args)
627627 if (!PyArg_ParseTuple (args , "O:extend" , & b ))
628628 return NULL ;
629629
630- if (!PyList_Check (b )) {
631- PyErr_SetString (PyExc_TypeError ,
632- "list.extend() argument must be a list" );
630+ b = PySequence_Fast (b , "list.extend() argument must be a sequence" );
631+ if (!b )
633632 return NULL ;
634- }
635- if (PyList_GET_SIZE (b ) == 0 ) {
633+
634+ if (PyObject_Length (b ) == 0 )
636635 /* short circuit when b is empty */
637- Py_INCREF (Py_None );
638- return Py_None ;
639- }
636+ goto ok ;
637+
640638 if (self == (PyListObject * )b ) {
641639 /* as in list_ass_slice() we must special case the
642640 * situation: a.extend(a)
643641 *
644642 * XXX: I think this way ought to be faster than using
645643 * list_slice() the way list_ass_slice() does.
646644 */
645+ Py_DECREF (b );
647646 b = PyList_New (selflen );
648647 if (!b )
649648 return NULL ;
@@ -653,33 +652,29 @@ listextend(self, args)
653652 PyList_SET_ITEM (b , i , o );
654653 }
655654 }
656- else
657- /* we want b to have the same refcount semantics for the
658- * Py_XDECREF() in the finally clause regardless of which
659- * branch in the above conditional we took.
660- */
661- Py_INCREF (b );
662655
663- blen = PyList_GET_SIZE (b );
656+ blen = PyObject_Length (b );
657+
664658 /* resize a using idiom */
665659 items = self -> ob_item ;
666660 NRESIZE (items , PyObject * , selflen + blen );
667- if (items == NULL ) {
661+ if (items == NULL ) {
668662 PyErr_NoMemory ();
669- goto finally ;
663+ goto failed ;
670664 }
671665 self -> ob_item = items ;
672666
673- /* populate the end self with b's items */
667+ /* populate the end of self with b's items */
674668 for (i = 0 ; i < blen ; i ++ ) {
675- PyObject * o = PyList_GET_ITEM (b , i );
669+ PyObject * o = PySequence_Fast_GET_ITEM (b , i );
676670 Py_INCREF (o );
677671 PyList_SET_ITEM (self , self -> ob_size ++ , o );
678672 }
673+ ok :
679674 res = Py_None ;
680675 Py_INCREF (res );
681- finally :
682- Py_XDECREF (b );
676+ failed :
677+ Py_DECREF (b );
683678 return res ;
684679}
685680
0 commit comments