diff options
author | Raymond Hettinger <python@rcn.com> | 2009-12-10 05:56:49 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-12-10 05:56:49 (GMT) |
commit | 287bef46b711845fbe9cdbe8011d71aa022f929f (patch) | |
tree | 2b20768c55d5bf8e920ae18f0fa35994d2f9fdd8 /Modules | |
parent | 1dbfbab1d240b4a6c2c893b552772a0c6c81f2f9 (diff) | |
download | cpython-287bef46b711845fbe9cdbe8011d71aa022f929f.zip cpython-287bef46b711845fbe9cdbe8011d71aa022f929f.tar.gz cpython-287bef46b711845fbe9cdbe8011d71aa022f929f.tar.bz2 |
Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)
Diffstat (limited to 'Modules')
-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 faf033e..42cfe6b 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -281,6 +281,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; @@ -319,6 +330,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; @@ -352,6 +374,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) { @@ -854,6 +889,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 ********************************************************/ |