diff options
author | Raymond Hettinger <python@rcn.com> | 2004-03-10 10:10:42 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-03-10 10:10:42 (GMT) |
commit | ef9bf4031a2f9ec674817274c93a90e0f21db114 (patch) | |
tree | 030ac1457bbd9b48d731a48b582504a0b646ae5e /Objects/rangeobject.c | |
parent | d2c36261a202ec61bed023f22815c41d8179ace2 (diff) | |
download | cpython-ef9bf4031a2f9ec674817274c93a90e0f21db114.zip cpython-ef9bf4031a2f9ec674817274c93a90e0f21db114.tar.gz cpython-ef9bf4031a2f9ec674817274c93a90e0f21db114.tar.bz2 |
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.
Diffstat (limited to 'Objects/rangeobject.c')
-rw-r--r-- | Objects/rangeobject.c | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 1f56728..a6d1611 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -288,6 +288,18 @@ rangeiter_next(rangeiterobject *r) 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 = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ @@ -302,7 +314,7 @@ static PyTypeObject Pyrangeiter_Type = { 0, /* tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ - 0, /* tp_as_sequence */ + &rangeiter_as_sequence, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ |