Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5b6411

Browse files
committed
Issue 3689: list_reverseiterator should support __length_hint__ instead of __len__.
1 parent 0486904 commit f5b6411

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

Lib/test/list_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def test_reversed(self):
9393
self.assertRaises(StopIteration, next, r)
9494
self.assertEqual(list(reversed(self.type2test())),
9595
self.type2test())
96+
# Bug 3689: make sure list-reversed-iterator doesn't have __len__
97+
self.assertRaises(TypeError, len, reversed([1,2,3]))
9698

9799
def test_setitem(self):
98100
a = self.type2test([0, 1])

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Core and Builtins
1616
interpreter to abort ("Fatal Python error: Could not reset the stack!")
1717
instead of throwing a MemoryError.
1818

19+
- Issue #3689: The list reversed iterator now supports __length_hint__
20+
instead of __len__. Behavior now matches other reversed iterators.
21+
1922
- Issue #4367: Python would segfault during compiling when the unicodedata
2023
module couldn't be imported and \N escapes were present.
2124

Objects/listobject.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,11 +2736,11 @@ static PyObject *list_reversed(PyListObject *, PyObject *);
27362736
static void listreviter_dealloc(listreviterobject *);
27372737
static int listreviter_traverse(listreviterobject *, visitproc, void *);
27382738
static PyObject *listreviter_next(listreviterobject *);
2739-
static Py_ssize_t listreviter_len(listreviterobject *);
2739+
static PyObject *listreviter_len(listreviterobject *);
27402740

2741-
static PySequenceMethods listreviter_as_sequence = {
2742-
(lenfunc)listreviter_len, /* sq_length */
2743-
0, /* sq_concat */
2741+
static PyMethodDef listreviter_methods[] = {
2742+
{"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
2743+
{NULL, NULL} /* sentinel */
27442744
};
27452745

27462746
PyTypeObject PyListRevIter_Type = {
@@ -2756,7 +2756,7 @@ PyTypeObject PyListRevIter_Type = {
27562756
0, /* tp_compare */
27572757
0, /* tp_repr */
27582758
0, /* tp_as_number */
2759-
&listreviter_as_sequence, /* tp_as_sequence */
2759+
0, /* tp_as_sequence */
27602760
0, /* tp_as_mapping */
27612761
0, /* tp_hash */
27622762
0, /* tp_call */
@@ -2772,6 +2772,7 @@ PyTypeObject PyListRevIter_Type = {
27722772
0, /* tp_weaklistoffset */
27732773
PyObject_SelfIter, /* tp_iter */
27742774
(iternextfunc)listreviter_next, /* tp_iternext */
2775+
listreviter_methods, /* tp_methods */
27752776
0,
27762777
};
27772778

@@ -2827,12 +2828,12 @@ listreviter_next(listreviterobject *it)
28272828
return NULL;
28282829
}
28292830

2830-
static Py_ssize_t
2831+
static PyObject *
28312832
listreviter_len(listreviterobject *it)
28322833
{
28332834
Py_ssize_t len = it->it_index + 1;
28342835
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
2835-
return 0;
2836-
return len;
2836+
len = 0;
2837+
return PyLong_FromSsize_t(len);
28372838
}
28382839

0 commit comments

Comments
 (0)