Accept Jim Jewett's api suggestion to use None instead of -1 to indicate unbounded deques.

This commit is contained in:
Raymond Hettinger 2007-10-10 00:26:46 +00:00
parent 77ae87c11e
commit 68995867d5
3 changed files with 45 additions and 12 deletions

View File

@ -51,7 +51,7 @@ ordered dictionaries.
.. versionadded:: 2.4
If *maxlen* is not specified or is *-1*, deques may grow to an
If *maxlen* is not specified or is *None*, deques may grow to an
arbitrary length. Otherwise, the deque is bounded to the specified maximum
length. Once a bounded length deque is full, when new items are added, a
corresponding number of items are discarded from the opposite end. Bounded

View File

@ -48,6 +48,7 @@ class TestBasic(unittest.TestCase):
self.assertEqual(list(d), range(50, 150))
def test_maxlen(self):
self.assertRaises(ValueError, deque, 'abc', -1)
self.assertRaises(ValueError, deque, 'abc', -2)
d = deque(range(10), maxlen=3)
self.assertEqual(repr(d), 'deque([7, 8, 9], maxlen=3)')
@ -73,7 +74,7 @@ class TestBasic(unittest.TestCase):
fo.close()
os.remove(test_support.TESTFN)
d = deque(range(10), maxlen=-1)
d = deque(range(10), maxlen=None)
self.assertEqual(repr(d), 'deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])')
try:
fo = open(test_support.TESTFN, "wb")
@ -489,6 +490,22 @@ class TestSubclass(unittest.TestCase):
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
d = Deque('abcde', maxlen=4)
e = d.__copy__()
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
e = Deque(d)
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
s = pickle.dumps(d)
e = pickle.loads(s)
self.assertNotEqual(id(d), id(e))
self.assertEqual(type(d), type(e))
self.assertEqual(list(d), list(e))
## def test_pickle(self):
## d = Deque('abc')
## d.append(d)

View File

@ -598,8 +598,11 @@ deque_nohash(PyObject *self)
static PyObject *
deque_copy(PyObject *deque)
{
return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "Oi",
deque, ((dequeobject *)deque)->maxlen, NULL);
if (((dequeobject *)deque)->maxlen == -1)
return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "O", deque, NULL);
else
return PyObject_CallFunction((PyObject *)(Py_Type(deque)), "Oi",
deque, ((dequeobject *)deque)->maxlen, NULL);
}
PyDoc_STRVAR(copy_doc, "Return a shallow copy of a deque.");
@ -617,10 +620,17 @@ deque_reduce(dequeobject *deque)
Py_XDECREF(dict);
return NULL;
}
if (dict == NULL)
result = Py_BuildValue("O(Oi)", Py_Type(deque), aslist, deque->maxlen);
else
result = Py_BuildValue("O(Oi)O", Py_Type(deque), aslist, deque->maxlen, dict);
if (dict == NULL) {
if (deque->maxlen == -1)
result = Py_BuildValue("O(O)", Py_Type(deque), aslist);
else
result = Py_BuildValue("O(Oi)", Py_Type(deque), aslist, deque->maxlen);
} else {
if (deque->maxlen == -1)
result = Py_BuildValue("O(OO)O", Py_Type(deque), aslist, Py_None, dict);
else
result = Py_BuildValue("O(Oi)O", Py_Type(deque), aslist, deque->maxlen, dict);
}
Py_XDECREF(dict);
Py_DECREF(aslist);
return result;
@ -797,14 +807,20 @@ static int
deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
{
PyObject *iterable = NULL;
PyObject *maxlenobj = NULL;
int maxlen = -1;
char *kwlist[] = {"iterable", "maxlen", 0};
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|Oi:deque", kwlist, &iterable, &maxlen))
return -1;
if (maxlen < -1) {
PyErr_SetString(PyExc_ValueError, "maxlen must be -1 or greater");
if (!PyArg_ParseTupleAndKeywords(args, kwdargs, "|OO:deque", kwlist, &iterable, &maxlenobj))
return -1;
if (maxlenobj != NULL && maxlenobj != Py_None) {
maxlen = PyInt_AsLong(maxlenobj);
if (maxlen == -1 && PyErr_Occurred())
return -1;
if (maxlen < 0) {
PyErr_SetString(PyExc_ValueError, "maxlen must be non-negative");
return -1;
}
}
deque->maxlen = maxlen;
if (iterable != NULL) {