[Python-checkins] r70298 - in python/trunk: Doc/library/collections.rst Lib/test/test_deque.py Misc/NEWS Modules/_collectionsmodule.c
raymond.hettinger
python-checkins at python.org
Tue Mar 10 13:51:00 CET 2009
Author: raymond.hettinger
Date: Tue Mar 10 13:50:59 2009
New Revision: 70298
Log:
For collections.deque() objects, expose the maxlen parameter as a read-only attribute.
Modified:
python/trunk/Doc/library/collections.rst
python/trunk/Lib/test/test_deque.py
python/trunk/Misc/NEWS
python/trunk/Modules/_collectionsmodule.c
Modified: python/trunk/Doc/library/collections.rst
==============================================================================
--- python/trunk/Doc/library/collections.rst (original)
+++ python/trunk/Doc/library/collections.rst Tue Mar 10 13:50:59 2009
@@ -382,6 +382,15 @@
``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
Modified: python/trunk/Lib/test/test_deque.py
==============================================================================
--- python/trunk/Lib/test/test_deque.py (original)
+++ python/trunk/Lib/test/test_deque.py Tue Mar 10 13:50:59 2009
@@ -104,6 +104,16 @@
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)]:
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS (original)
+++ python/trunk/Misc/NEWS Tue Mar 10 13:50:59 2009
@@ -168,6 +168,8 @@
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.
Modified: python/trunk/Modules/_collectionsmodule.c
==============================================================================
--- python/trunk/Modules/_collectionsmodule.c (original)
+++ python/trunk/Modules/_collectionsmodule.c Tue Mar 10 13:50:59 2009
@@ -870,6 +870,20 @@
return 0;
}
+static PyObject *
+deque_get_maxlen(dequeobject *deque)
+{
+ if (deque->maxlen == -1)
+ Py_RETURN_NONE;
+ return PyInt_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 */
@@ -951,7 +965,7 @@
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 */
More information about the Python-checkins
mailing list