mirror of https://github.com/python/cpython
Tidied up the implementations of reversed (including the custom ones
for xrange and list objects). * list.__reversed__ now checks the length of the sequence object before calling PyList_GET_ITEM() because the mutable could have changed length. * all three implementations are now tranparent with respect to length and maintain the invariant len(it) == len(list(it)) even when the underlying sequence mutates. * __builtin__.reversed() now frees the underlying sequence as soon as the iterator is exhausted. * the code paths were rearranged so that the most common paths do not require a jump.
This commit is contained in:
parent
d2c36261a2
commit
ef9bf4031a
|
@ -143,8 +143,8 @@ class TestReversed(unittest.TestCase):
|
||||||
|
|
||||||
def test_len(self):
|
def test_len(self):
|
||||||
# This is an implementation detail, not an interface requirement
|
# This is an implementation detail, not an interface requirement
|
||||||
s = 'hello'
|
for s in ('hello', tuple('hello'), list('hello'), xrange(5)):
|
||||||
self.assertEqual(len(reversed(s)), len(s))
|
self.assertEqual(len(reversed(s)), len(s))
|
||||||
|
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
|
testclasses = (EnumerateTestCase, SubclassTestCase, TestEmpty, TestBig,
|
||||||
|
|
|
@ -217,23 +217,21 @@ static PyObject *
|
||||||
reversed_next(reversedobject *ro)
|
reversed_next(reversedobject *ro)
|
||||||
{
|
{
|
||||||
PyObject *item;
|
PyObject *item;
|
||||||
|
long index = ro->index;
|
||||||
|
|
||||||
if (ro->index < 0)
|
if (index >= 0) {
|
||||||
return NULL;
|
item = PySequence_GetItem(ro->seq, index);
|
||||||
|
if (item != NULL) {
|
||||||
assert(PySequence_Check(ro->seq));
|
ro->index--;
|
||||||
item = PySequence_GetItem(ro->seq, ro->index);
|
return item;
|
||||||
if (item == NULL)
|
}
|
||||||
return NULL;
|
}
|
||||||
|
ro->index = -1;
|
||||||
ro->index--;
|
if (ro->seq != NULL) {
|
||||||
return item;
|
Py_DECREF(ro->seq);
|
||||||
}
|
ro->seq = NULL;
|
||||||
|
}
|
||||||
static int
|
return NULL;
|
||||||
reversed_len(reversedobject *ro)
|
|
||||||
{
|
|
||||||
return PyObject_Size(ro->seq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(reversed_doc,
|
PyDoc_STRVAR(reversed_doc,
|
||||||
|
@ -241,6 +239,12 @@ PyDoc_STRVAR(reversed_doc,
|
||||||
"\n"
|
"\n"
|
||||||
"Return a reverse iterator");
|
"Return a reverse iterator");
|
||||||
|
|
||||||
|
static int
|
||||||
|
reversed_len(reversedobject *ro)
|
||||||
|
{
|
||||||
|
return ro->index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
static PySequenceMethods reversed_as_sequence = {
|
static PySequenceMethods reversed_as_sequence = {
|
||||||
(inquiry)reversed_len, /* sq_length */
|
(inquiry)reversed_len, /* sq_length */
|
||||||
0, /* sq_concat */
|
0, /* sq_concat */
|
||||||
|
|
|
@ -2793,21 +2793,35 @@ listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
listreviter_next(listreviterobject *it)
|
listreviter_next(listreviterobject *it)
|
||||||
{
|
{
|
||||||
PyObject *item = NULL;
|
PyObject *item;
|
||||||
|
long index = it->it_index;
|
||||||
|
PyListObject *seq = it->it_seq;
|
||||||
|
|
||||||
assert(PyList_Check(it->it_seq));
|
if (index>=0 && index < PyList_GET_SIZE(seq)) {
|
||||||
if (it->it_index >= 0) {
|
item = PyList_GET_ITEM(seq, index);
|
||||||
assert(it->it_index < PyList_GET_SIZE(it->it_seq));
|
|
||||||
item = PyList_GET_ITEM(it->it_seq, it->it_index);
|
|
||||||
it->it_index--;
|
it->it_index--;
|
||||||
Py_INCREF(item);
|
Py_INCREF(item);
|
||||||
} else if (it->it_seq != NULL) {
|
return item;
|
||||||
Py_DECREF(it->it_seq);
|
|
||||||
it->it_seq = NULL;
|
|
||||||
}
|
}
|
||||||
return item;
|
it->it_index = -1;
|
||||||
|
if (seq != NULL) {
|
||||||
|
it->it_seq = NULL;
|
||||||
|
Py_DECREF(seq);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
listreviter_len(listreviterobject *it)
|
||||||
|
{
|
||||||
|
return it->it_index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PySequenceMethods listreviter_as_sequence = {
|
||||||
|
(inquiry)listreviter_len, /* sq_length */
|
||||||
|
0, /* sq_concat */
|
||||||
|
};
|
||||||
|
|
||||||
PyTypeObject PyListRevIter_Type = {
|
PyTypeObject PyListRevIter_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0, /* ob_size */
|
0, /* ob_size */
|
||||||
|
@ -2822,7 +2836,7 @@ PyTypeObject PyListRevIter_Type = {
|
||||||
0, /* tp_compare */
|
0, /* tp_compare */
|
||||||
0, /* tp_repr */
|
0, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
&listreviter_as_sequence, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
|
|
|
@ -288,6 +288,18 @@ rangeiter_next(rangeiterobject *r)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
rangeiter_len(rangeiterobject *r)
|
||||||
|
{
|
||||||
|
return r->len - r->index;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PySequenceMethods rangeiter_as_sequence = {
|
||||||
|
(inquiry)rangeiter_len, /* sq_length */
|
||||||
|
0, /* sq_concat */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
static PyTypeObject Pyrangeiter_Type = {
|
static PyTypeObject Pyrangeiter_Type = {
|
||||||
PyObject_HEAD_INIT(&PyType_Type)
|
PyObject_HEAD_INIT(&PyType_Type)
|
||||||
0, /* ob_size */
|
0, /* ob_size */
|
||||||
|
@ -302,7 +314,7 @@ static PyTypeObject Pyrangeiter_Type = {
|
||||||
0, /* tp_compare */
|
0, /* tp_compare */
|
||||||
0, /* tp_repr */
|
0, /* tp_repr */
|
||||||
0, /* tp_as_number */
|
0, /* tp_as_number */
|
||||||
0, /* tp_as_sequence */
|
&rangeiter_as_sequence, /* tp_as_sequence */
|
||||||
0, /* tp_as_mapping */
|
0, /* tp_as_mapping */
|
||||||
0, /* tp_hash */
|
0, /* tp_hash */
|
||||||
0, /* tp_call */
|
0, /* tp_call */
|
||||||
|
|
Loading…
Reference in New Issue