Skip to content

Commit 97bc618

Browse files
committed
list_inplace_concat() is now expressed in terms of list_extend() which
avoids creating an intermediate tuple for iterable arguments other than lists or tuples. In other words, a+=b no longer requires extra memory when b is not a list or tuple. The list and tuple cases are unchanged.
1 parent 4252a7a commit 97bc618

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

Objects/listobject.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -707,20 +707,6 @@ listextend_internal(PyListObject *self, PyObject *b)
707707
return 0;
708708
}
709709

710-
static PyObject *
711-
list_inplace_concat(PyListObject *self, PyObject *other)
712-
{
713-
other = PySequence_Fast(other, "argument to += must be iterable");
714-
if (!other)
715-
return NULL;
716-
717-
if (listextend_internal(self, other) < 0)
718-
return NULL;
719-
720-
Py_INCREF(self);
721-
return (PyObject *)self;
722-
}
723-
724710
static PyObject *
725711
listextend(PyListObject *self, PyObject *b)
726712
{
@@ -790,6 +776,19 @@ listextend(PyListObject *self, PyObject *b)
790776
return NULL;
791777
}
792778

779+
static PyObject *
780+
list_inplace_concat(PyListObject *self, PyObject *other)
781+
{
782+
PyObject *result;
783+
784+
result = listextend(self, other);
785+
if (result == NULL)
786+
return result;
787+
Py_DECREF(result);
788+
Py_INCREF(self);
789+
return (PyObject *)self;
790+
}
791+
793792
static PyObject *
794793
listpop(PyListObject *self, PyObject *args)
795794
{

0 commit comments

Comments
 (0)