diff options
author | Raymond Hettinger <python@rcn.com> | 2009-12-10 03:03:02 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-12-10 03:03:02 (GMT) |
commit | 3f9afd816de4067b8277bfe0241cf15d34b13e47 (patch) | |
tree | 4701c6f2330249aeee9a01bd061b2cee3d064e2d /Modules/_collectionsmodule.c | |
parent | 0f6cae0bffa52b16e09fce41c388b1ef70404c28 (diff) | |
download | cpython-3f9afd816de4067b8277bfe0241cf15d34b13e47.zip cpython-3f9afd816de4067b8277bfe0241cf15d34b13e47.tar.gz cpython-3f9afd816de4067b8277bfe0241cf15d34b13e47.tar.bz2 |
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Diffstat (limited to 'Modules/_collectionsmodule.c')
-rw-r--r-- | Modules/_collectionsmodule.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index cdcf4ef..ffb2a80 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -298,6 +298,17 @@ deque_extend(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; + /* Handle case where id(deque) == id(iterable) */ + if ((PyObject *)deque == iterable) { + PyObject *result; + PyObject *s = PySequence_List(iterable); + if (s == NULL) + return NULL; + result = deque_extend(deque, s); + Py_DECREF(s); + return result; + } + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -339,6 +350,17 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) { PyObject *it, *item; + /* Handle case where id(deque) == id(iterable) */ + if ((PyObject *)deque == iterable) { + PyObject *result; + PyObject *s = PySequence_List(iterable); + if (s == NULL) + return NULL; + result = deque_extendleft(deque, s); + Py_DECREF(s); + return result; + } + it = PyObject_GetIter(iterable); if (it == NULL) return NULL; @@ -375,6 +397,19 @@ deque_extendleft(dequeobject *deque, PyObject *iterable) PyDoc_STRVAR(extendleft_doc, "Extend the left side of the deque with elements from the iterable"); +static PyObject * +deque_inplace_concat(dequeobject *deque, PyObject *other) +{ + PyObject *result; + + result = deque_extend(deque, other); + if (result == NULL) + return result; + Py_DECREF(result); + Py_INCREF(deque); + return (PyObject *)deque; +} + static int _deque_rotate(dequeobject *deque, Py_ssize_t n) { @@ -875,6 +910,11 @@ static PySequenceMethods deque_as_sequence = { (ssizeargfunc)deque_item, /* sq_item */ 0, /* sq_slice */ (ssizeobjargproc)deque_ass_item, /* sq_ass_item */ + 0, /* sq_ass_slice */ + 0, /* sq_contains */ + (binaryfunc)deque_inplace_concat, /* sq_inplace_concat */ + 0, /* sq_inplace_repeat */ + }; /* deque object ********************************************************/ |