summaryrefslogtreecommitdiffstats
path: root/Objects/listobject.c
diff options
context:
space:
mode:
authorAndrew M. Kuchling <amk@amk.ca>2000-06-18 18:43:14 (GMT)
committerAndrew M. Kuchling <amk@amk.ca>2000-06-18 18:43:14 (GMT)
commit74042d6e5d44cc9d332c28414a1e04eadd204248 (patch)
tree888efb4a5da65b5fed5d282d019af6fb29fefe0b /Objects/listobject.c
parentb75c485f0bc6394f616d4a0bc746e613fd7b1021 (diff)
downloadcpython-74042d6e5d44cc9d332c28414a1e04eadd204248.zip
cpython-74042d6e5d44cc9d332c28414a1e04eadd204248.tar.gz
cpython-74042d6e5d44cc9d332c28414a1e04eadd204248.tar.bz2
Patch from /F:
this patch introduces PySequence_Fast and PySequence_Fast_GET_ITEM, and modifies the list.extend method to accept any kind of sequence.
Diffstat (limited to 'Objects/listobject.c')
-rw-r--r--Objects/listobject.c37
1 files changed, 16 insertions, 21 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 3bb5aec..163ba2a 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -627,16 +627,14 @@ listextend(self, args)
if (!PyArg_ParseTuple(args, "O:extend", &b))
return NULL;
- if (!PyList_Check(b)) {
- PyErr_SetString(PyExc_TypeError,
- "list.extend() argument must be a list");
+ b = PySequence_Fast(b, "list.extend() argument must be a sequence");
+ if (!b)
return NULL;
- }
- if (PyList_GET_SIZE(b) == 0) {
+
+ if (PyObject_Length(b) == 0)
/* short circuit when b is empty */
- Py_INCREF(Py_None);
- return Py_None;
- }
+ goto ok;
+
if (self == (PyListObject*)b) {
/* as in list_ass_slice() we must special case the
* situation: a.extend(a)
@@ -644,6 +642,7 @@ listextend(self, args)
* XXX: I think this way ought to be faster than using
* list_slice() the way list_ass_slice() does.
*/
+ Py_DECREF(b);
b = PyList_New(selflen);
if (!b)
return NULL;
@@ -653,33 +652,29 @@ listextend(self, args)
PyList_SET_ITEM(b, i, o);
}
}
- else
- /* we want b to have the same refcount semantics for the
- * Py_XDECREF() in the finally clause regardless of which
- * branch in the above conditional we took.
- */
- Py_INCREF(b);
- blen = PyList_GET_SIZE(b);
+ blen = PyObject_Length(b);
+
/* resize a using idiom */
items = self->ob_item;
NRESIZE(items, PyObject*, selflen + blen);
- if (items == NULL ) {
+ if (items == NULL) {
PyErr_NoMemory();
- goto finally;
+ goto failed;
}
self->ob_item = items;
- /* populate the end self with b's items */
+ /* populate the end of self with b's items */
for (i = 0; i < blen; i++) {
- PyObject *o = PyList_GET_ITEM(b, i);
+ PyObject *o = PySequence_Fast_GET_ITEM(b, i);
Py_INCREF(o);
PyList_SET_ITEM(self, self->ob_size++, o);
}
+ ok:
res = Py_None;
Py_INCREF(res);
- finally:
- Py_XDECREF(b);
+ failed:
+ Py_DECREF(b);
return res;
}