Fix variants of deque.extend: d.extend(d) d+=d d.extendleft(d)

This commit is contained in:
Raymond Hettinger 2009-12-10 05:56:49 +00:00
parent 1dbfbab1d2
commit 287bef46b7
3 changed files with 53 additions and 0 deletions

View File

@ -109,12 +109,23 @@ class TestBasic(unittest.TestCase):
self.assertRaises(TypeError, d.extend, 1)
d.extend('bcd')
self.assertEqual(list(d), list('abcd'))
d.extend(d)
self.assertEqual(list(d), list('abcdabcd'))
def test_iadd(self):
d = deque('a')
d += 'bcd'
self.assertEqual(list(d), list('abcd'))
d += d
self.assertEqual(list(d), list('abcdabcd'))
def test_extendleft(self):
d = deque('a')
self.assertRaises(TypeError, d.extendleft, 1)
d.extendleft('bcd')
self.assertEqual(list(d), list(reversed('abcd')))
d.extendleft(d)
self.assertEqual(list(d), list('abcddcba'))
d = deque()
d.extendleft(range(1000))
self.assertEqual(list(d), list(reversed(range(1000))))

View File

@ -33,6 +33,8 @@ Core and Builtins
Library
-------
- Fix variations of extending deques: d.extend(d) d.extendleft(d) d+=d
- Issue #1923: Fixed the removal of meaningful spaces when PKG-INFO is
generated in Distutils. Patch by Stephen Emslie.

View File

@ -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 ********************************************************/