summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-12-03 15:42:10 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-12-03 15:42:10 (GMT)
commit7989a4dccb39aa954057cbc4205473f09daae84b (patch)
tree1e3d69be16c9c149eabab7d4fe3c07f13c25f236
parentfd8a1ec4863b8a53436dd45fd190b2fe3ac4b2a8 (diff)
downloadcpython-7989a4dccb39aa954057cbc4205473f09daae84b.zip
cpython-7989a4dccb39aa954057cbc4205473f09daae84b.tar.gz
cpython-7989a4dccb39aa954057cbc4205473f09daae84b.tar.bz2
Backport r67478
-rw-r--r--Lib/test/list_tests.py2
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/listobject.c17
3 files changed, 14 insertions, 8 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index b3f24d3..c9aa316 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -84,6 +84,8 @@ class CommonTest(seq_tests.CommonTest):
self.assertRaises(StopIteration, r.next)
self.assertEqual(list(reversed(self.type2test())),
self.type2test())
+ # Bug 3689: make sure list-reversed-iterator doesn't have __len__
+ self.assertRaises(TypeError, len, reversed([1,2,3]))
def test_setitem(self):
a = self.type2test([0, 1])
diff --git a/Misc/NEWS b/Misc/NEWS
index 57c6fff..025ca4c 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@ Core and Builtins
interpreter to abort ("Fatal Python error: Could not reset the stack!")
instead of throwing a MemoryError.
+- Issue #3689: The list reversed iterator now supports __length_hint__
+ instead of __len__. Behavior now matches other reversed iterators.
+
- Issue #4367: Python would segfault during compiling when the unicodedata
module couldn't be imported and \N escapes were present.
diff --git a/Objects/listobject.c b/Objects/listobject.c
index e8379e1..7b4bf35 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2911,11 +2911,11 @@ static PyObject *list_reversed(PyListObject *, PyObject *);
static void listreviter_dealloc(listreviterobject *);
static int listreviter_traverse(listreviterobject *, visitproc, void *);
static PyObject *listreviter_next(listreviterobject *);
-static Py_ssize_t listreviter_len(listreviterobject *);
+static PyObject *listreviter_len(listreviterobject *);
-static PySequenceMethods listreviter_as_sequence = {
- (lenfunc)listreviter_len, /* sq_length */
- 0, /* sq_concat */
+static PyMethodDef listreviter_methods[] = {
+ {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
+ {NULL, NULL} /* sentinel */
};
PyTypeObject PyListRevIter_Type = {
@@ -2931,7 +2931,7 @@ PyTypeObject PyListRevIter_Type = {
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
- &listreviter_as_sequence, /* tp_as_sequence */
+ 0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
@@ -2947,6 +2947,7 @@ PyTypeObject PyListRevIter_Type = {
0, /* tp_weaklistoffset */
PyObject_SelfIter, /* tp_iter */
(iternextfunc)listreviter_next, /* tp_iternext */
+ listreviter_methods, /* tp_methods */
0,
};
@@ -3002,11 +3003,11 @@ listreviter_next(listreviterobject *it)
return NULL;
}
-static Py_ssize_t
+static PyObject *
listreviter_len(listreviterobject *it)
{
Py_ssize_t len = it->it_index + 1;
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
- return 0;
- return len;
+ len = 0;
+ return PyLong_FromSsize_t(len);
}