Skip to content

Commit 50c22cc

Browse files
committed
Address reviews
1 parent 218fe30 commit 50c22cc

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

Doc/c-api/list.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ List Objects
5454
.. c:function:: int PyList_Clear(PyObject *list)
5555
5656
Remove all items from *list*. Similar to:
57-
``PyList_SetSlice(L, 0, PY_SSIZE_T_MAX, NULL)``.
57+
``PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL)``.
5858
5959
Raise an exception and return ``-1`` if *list* is not a :class:`list`
6060
object. Return 0 on success.
@@ -123,10 +123,10 @@ List Objects
123123
to ``list.append(item)``.
124124
125125
126-
.. c:function:: int PyList_Extend(PyObject *self, PyObject *iterable)
126+
.. c:function:: int PyList_Extend(PyObject *list, PyObject *iterable)
127127
128128
Extend *list* with the contents of *iterable*. Similar to:
129-
``PyList_SetSlice(L, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable)``.
129+
``PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable)``.
130130
131131
Raise an exception and return ``-1`` if *list* is not a :class:`list`
132132
object. Return 0 on success.

Lib/test/test_capi/test_list.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,16 @@ def test_list_extend(self):
297297
# Test PyList_Extend()
298298
list_extend = _testcapi.list_extend
299299

300-
lst = [1]
301-
arg = [2, 3]
302-
self.assertEqual(list_extend(lst, arg), 0)
303-
self.assertEqual(lst, [1, 2, 3])
300+
for other_type in (list, tuple, str, iter):
301+
lst = list("ab")
302+
arg = other_type("def")
303+
self.assertEqual(list_extend(lst, arg), 0)
304+
self.assertEqual(lst, list("abdef"))
304305

305-
lst = []
306-
arg = (2, 3)
307-
self.assertEqual(list_extend(lst, arg), 0)
308-
self.assertEqual(lst, [2, 3])
306+
# PyList_Extend(lst, lst)
307+
lst = list("abc")
308+
self.assertEqual(list_extend(lst, lst), 0)
309+
self.assertEqual(lst, list("abcabc"))
309310

310311
self.assertRaises(TypeError, list_extend, [], object())
311312

Objects/listobject.c

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -857,19 +857,13 @@ list_append(PyListObject *self, PyObject *object)
857857
static int
858858
list_extend_fast(PyListObject *self, PyObject *iterable)
859859
{
860-
iterable = PySequence_Fast(iterable, "argument must be iterable");
861-
if (!iterable) {
862-
return -1;
863-
}
864-
865-
Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable); // size of iterable
860+
Py_ssize_t n = PySequence_Fast_GET_SIZE(iterable);
866861
if (n == 0) {
867862
/* short circuit when iterable is empty */
868-
Py_DECREF(iterable);
869863
return 0;
870864
}
871865

872-
Py_ssize_t m = Py_SIZE(self); // size of self
866+
Py_ssize_t m = Py_SIZE(self);
873867
// It should not be possible to allocate a list large enough to cause
874868
// an overflow on any relevant platform.
875869
assert(m < PY_SSIZE_T_MAX - n);
@@ -880,7 +874,6 @@ list_extend_fast(PyListObject *self, PyObject *iterable)
880874
Py_SET_SIZE(self, n);
881875
}
882876
else if (list_resize(self, m + n) < 0) {
883-
Py_DECREF(iterable);
884877
return -1;
885878
}
886879

@@ -896,28 +889,26 @@ list_extend_fast(PyListObject *self, PyObject *iterable)
896889
PyObject *o = src[i];
897890
dest[i] = Py_NewRef(o);
898891
}
899-
Py_DECREF(iterable);
900892
return 0;
901893
}
902894

903895
static int
904896
list_extend_iter(PyListObject *self, PyObject *iterable)
905897
{
906-
907-
PyObject *it = PyObject_GetIter(iterable); // iter(v)
898+
PyObject *it = PyObject_GetIter(iterable);
908899
if (it == NULL) {
909900
return -1;
910901
}
911902
PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
912903

913904
/* Guess a result list size. */
914-
Py_ssize_t n = PyObject_LengthHint(iterable, 8); // guess iterable size
905+
Py_ssize_t n = PyObject_LengthHint(iterable, 8);
915906
if (n < 0) {
916907
Py_DECREF(it);
917908
return -1;
918909
}
919910

920-
Py_ssize_t m = Py_SIZE(self); /* size of self */
911+
Py_ssize_t m = Py_SIZE(self);
921912
if (m > PY_SSIZE_T_MAX - n) {
922913
/* m + n overflowed; on the chance that n lied, and there really
923914
* is enough room, ignore it. If n was telling the truth, we'll
@@ -930,8 +921,10 @@ list_extend_iter(PyListObject *self, PyObject *iterable)
930921
}
931922
else {
932923
/* Make room. */
933-
if (list_resize(self, m + n) < 0)
924+
if (list_resize(self, m + n) < 0) {
934925
goto error;
926+
}
927+
935928
/* Make the list sane again. */
936929
Py_SET_SIZE(self, m);
937930
}
@@ -948,10 +941,10 @@ list_extend_iter(PyListObject *self, PyObject *iterable)
948941
}
949942
break;
950943
}
944+
951945
if (Py_SIZE(self) < self->allocated) {
952-
/* steals ref */
953946
Py_ssize_t len = Py_SIZE(self);
954-
PyList_SET_ITEM(self, len, item);
947+
PyList_SET_ITEM(self, len, item); // steals item ref
955948
Py_SET_SIZE(self, len + 1);
956949
}
957950
else {
@@ -974,6 +967,7 @@ list_extend_iter(PyListObject *self, PyObject *iterable)
974967
return -1;
975968
}
976969

970+
977971
static int
978972
list_extend(PyListObject *self, PyObject *iterable)
979973
{
@@ -984,22 +978,31 @@ list_extend(PyListObject *self, PyObject *iterable)
984978
|| PyTuple_CheckExact(iterable)
985979
|| (PyObject *)self == iterable)
986980
{
987-
return list_extend_fast(self, iterable);
981+
iterable = PySequence_Fast(iterable, "argument must be iterable");
982+
if (!iterable) {
983+
return -1;
984+
}
985+
986+
int res = list_extend_fast(self, iterable);
987+
Py_DECREF(iterable);
988+
return res;
988989
}
989990
else {
990991
return list_extend_iter(self, iterable);
991992
}
992993
}
993994

994-
static PyObject *
995-
list_extend_method(PyListObject *self, PyObject *iterable)
995+
996+
PyObject *
997+
_PyList_Extend(PyListObject *self, PyObject *iterable)
996998
{
997999
if (list_extend(self, iterable) < 0) {
9981000
return NULL;
9991001
}
10001002
Py_RETURN_NONE;
10011003
}
10021004

1005+
10031006
/*[clinic input]
10041007
list.extend as py_list_extend
10051008
@@ -1013,14 +1016,9 @@ static PyObject *
10131016
py_list_extend(PyListObject *self, PyObject *iterable)
10141017
/*[clinic end generated code: output=b8e0bff0ceae2abd input=9a8376a8633ed3ba]*/
10151018
{
1016-
return list_extend_method(self, iterable);
1019+
return _PyList_Extend(self, iterable);
10171020
}
10181021

1019-
PyObject *
1020-
_PyList_Extend(PyListObject *self, PyObject *iterable)
1021-
{
1022-
return list_extend_method(self, iterable);
1023-
}
10241022

10251023
int
10261024
PyList_Extend(PyObject *self, PyObject *iterable)
@@ -1033,6 +1031,7 @@ PyList_Extend(PyObject *self, PyObject *iterable)
10331031
return list_extend((PyListObject*)self, iterable);
10341032
}
10351033

1034+
10361035
int
10371036
PyList_Clear(PyObject *self)
10381037
{
@@ -1045,6 +1044,7 @@ PyList_Clear(PyObject *self)
10451044
return 0;
10461045
}
10471046

1047+
10481048
static PyObject *
10491049
list_inplace_concat(PyListObject *self, PyObject *other)
10501050
{

0 commit comments

Comments
 (0)