mirror of https://github.com/python/cpython.git
Issue 3689: list_reverseiterator should support __length_hint__ instead of __len__.
This commit is contained in:
parent
048690410f
commit
f5b64116cc
|
@ -93,6 +93,8 @@ def test_reversed(self):
|
||||||
self.assertRaises(StopIteration, next, r)
|
self.assertRaises(StopIteration, next, r)
|
||||||
self.assertEqual(list(reversed(self.type2test())),
|
self.assertEqual(list(reversed(self.type2test())),
|
||||||
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):
|
def test_setitem(self):
|
||||||
a = self.type2test([0, 1])
|
a = self.type2test([0, 1])
|
||||||
|
|
|
@ -16,6 +16,9 @@ Core and Builtins
|
||||||
interpreter to abort ("Fatal Python error: Could not reset the stack!")
|
interpreter to abort ("Fatal Python error: Could not reset the stack!")
|
||||||
instead of throwing a MemoryError.
|
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
|
- Issue #4367: Python would segfault during compiling when the unicodedata
|
||||||
module couldn't be imported and \N escapes were present.
|
module couldn't be imported and \N escapes were present.
|
||||||
|
|
||||||
|
|
|
@ -2736,11 +2736,11 @@ static PyObject *list_reversed(PyListObject *, PyObject *);
|
||||||
static void listreviter_dealloc(listreviterobject *);
|
static void listreviter_dealloc(listreviterobject *);
|
||||||
static int listreviter_traverse(listreviterobject *, visitproc, void *);
|
static int listreviter_traverse(listreviterobject *, visitproc, void *);
|
||||||
static PyObject *listreviter_next(listreviterobject *);
|
static PyObject *listreviter_next(listreviterobject *);
|
||||||
static Py_ssize_t listreviter_len(listreviterobject *);
|
static PyObject *listreviter_len(listreviterobject *);
|
||||||
|
|
||||||
static PySequenceMethods listreviter_as_sequence = {
|
static PyMethodDef listreviter_methods[] = {
|
||||||
(lenfunc)listreviter_len, /* sq_length */
|
{"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
|
||||||
0, /* sq_concat */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
PyTypeObject PyListRevIter_Type = {
|
PyTypeObject PyListRevIter_Type = {
|
||||||
|
@ -2756,7 +2756,7 @@ PyTypeObject PyListRevIter_Type = {
|
||||||
0, /* tp_compare */
|
0, /* tp_compare */
|
||||||
0, /* tp_repr */
|
0, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
&listreviter_as_sequence, /* tp_as_sequence */
|
0, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
|
@ -2772,6 +2772,7 @@ PyTypeObject PyListRevIter_Type = {
|
||||||
0, /* tp_weaklistoffset */
|
0, /* tp_weaklistoffset */
|
||||||
PyObject_SelfIter, /* tp_iter */
|
PyObject_SelfIter, /* tp_iter */
|
||||||
(iternextfunc)listreviter_next, /* tp_iternext */
|
(iternextfunc)listreviter_next, /* tp_iternext */
|
||||||
|
listreviter_methods, /* tp_methods */
|
||||||
0,
|
0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2827,12 +2828,12 @@ listreviter_next(listreviterobject *it)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Py_ssize_t
|
static PyObject *
|
||||||
listreviter_len(listreviterobject *it)
|
listreviter_len(listreviterobject *it)
|
||||||
{
|
{
|
||||||
Py_ssize_t len = it->it_index + 1;
|
Py_ssize_t len = it->it_index + 1;
|
||||||
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
|
if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
|
||||||
return 0;
|
len = 0;
|
||||||
return len;
|
return PyLong_FromSsize_t(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue