* Add unittests for iterators that report their length

* Document the differences between them
* Fix corner cases covered by the unittests
* Use Py_RETURN_NONE where possible for dictionaries
This commit is contained in:
Raymond Hettinger 2004-04-12 18:10:01 +00:00
parent 45d0b5cc44
commit 7892b1c651
5 changed files with 276 additions and 10 deletions

View file

@ -225,6 +225,9 @@ reversed_next(reversedobject *ro)
ro->index--;
return item;
}
if (PyErr_ExceptionMatches(PyExc_IndexError) ||
PyErr_ExceptionMatches(PyExc_StopIteration))
PyErr_Clear();
}
ro->index = -1;
if (ro->seq != NULL) {
@ -242,7 +245,15 @@ PyDoc_STRVAR(reversed_doc,
static int
reversed_len(reversedobject *ro)
{
return ro->index + 1;
int position, seqsize;
if (ro->seq == NULL)
return 0;
seqsize = PySequence_Size(ro->seq);
if (seqsize == -1)
return -1;
position = ro->index + 1;
return (seqsize < position) ? 0 : position;
}
static PySequenceMethods reversed_as_sequence = {