]> granicus.if.org Git - python/commitdiff
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
authorRaymond Hettinger <python@rcn.com>
Tue, 10 Mar 2009 12:56:32 +0000 (12:56 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 10 Mar 2009 12:56:32 +0000 (12:56 +0000)
Doc/library/collections.rst
Lib/test/test_deque.py
Misc/NEWS
Modules/_collectionsmodule.c

index ce47bd0072649585999cbddb24000d7e8a55ec9f..ef1bca2797805e51f99c5217db36c12f60f985d6 100644 (file)
@@ -361,6 +361,15 @@ counts, but the output will exclude results with counts of zero or less.
       ``d.appendleft(d.pop())``.
 
 
+   Deque objects also provide one read-only attribute:
+
+   .. attribute:: maxlen
+
+      Maximum size of a deque or *None* if unbounded.
+
+      .. versionadded:: 2.7
+
+
 In addition to the above, deques support iteration, pickling, ``len(d)``,
 ``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with
 the :keyword:`in` operator, and subscript references such as ``d[-1]``.  Indexed
index a9e010e8c58644cac180704f2b95e1d5db321546..fab5a7c6605ba9a712335a7d02c937200cadb5a5 100644 (file)
@@ -105,6 +105,16 @@ class TestBasic(unittest.TestCase):
         d.extendleft(it)
         self.assertEqual(list(it), [])
 
+    def test_maxlen_attribute(self):
+        self.assertEqual(deque().maxlen, None)
+        self.assertEqual(deque('abc').maxlen, None)
+        self.assertEqual(deque('abc', maxlen=4).maxlen, 4)
+        self.assertEqual(deque('abc', maxlen=2).maxlen, 2)
+        self.assertEqual(deque('abc', maxlen=0).maxlen, 0)
+        with self.assertRaises(AttributeError):
+            d = deque('abc')
+            d.maxlen = 10
+
     def test_comparisons(self):
         d = deque('xabc'); d.popleft()
         for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
index ad2873589b5a19e69021652012cc21b06896e236..b870bcf0522734dfabb99732bccdb4d6975c0ef0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -195,6 +195,8 @@ Core and Builtins
 Library
 -------
 
+- collections.deque() objects now have a read-only attribute called maxlen.
+
 - Issue #2638: Show a window constructed with tkSimpleDialog.Dialog only after
   it is has been populated and properly configured in order to prevent
   window flashing.
index 8d27b88ef62ec273a374ebf1a5430e8da3dcd5a6..f8d656cccbfa58328760b04349279f8f3ef9b379 100644 (file)
@@ -812,6 +812,20 @@ deque_init(dequeobject *deque, PyObject *args, PyObject *kwdargs)
        return 0;
 }
 
+static PyObject *
+deque_get_maxlen(dequeobject *deque)
+{
+       if (deque->maxlen == -1)
+               Py_RETURN_NONE;
+       return PyLong_FromSsize_t(deque->maxlen);
+}
+
+static PyGetSetDef deque_getset[] = {
+       {"maxlen", (getter)deque_get_maxlen, (setter)NULL,
+        "maximum size of a deque or None if unbounded"},
+       {0}
+};
+
 static PySequenceMethods deque_as_sequence = {
        (lenfunc)deque_len,             /* sq_length */
        0,                              /* sq_concat */
@@ -893,7 +907,7 @@ static PyTypeObject deque_type = {
        0,                              /* tp_iternext */
        deque_methods,                  /* tp_methods */
        0,                              /* tp_members */
-       0,                              /* tp_getset */
+       deque_getset,   /* tp_getset */
        0,                              /* tp_base */
        0,                              /* tp_dict */
        0,                              /* tp_descr_get */