diff Objects/rangeobject.c @ 31635:04eb2ebb46e2 legacy-trunk

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.
author Raymond Hettinger <python@rcn.com>
date Wed, 10 Mar 2004 10:10:42 +0000
parents 41c4e143e6e6
children 0dc8ffaac036
line wrap: on
line diff
--- a/Objects/rangeobject.c	Wed Mar 10 08:32:47 2004 +0000
+++ b/Objects/rangeobject.c	Wed Mar 10 10:10:42 2004 +0000
@@ -288,6 +288,18 @@
 	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 @@
 	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 */