]> granicus.if.org Git - python/commitdiff
SF bug #730296: Unexpected Changes in list Iterator
authorRaymond Hettinger <python@rcn.com>
Wed, 7 May 2003 01:28:47 +0000 (01:28 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 7 May 2003 01:28:47 +0000 (01:28 +0000)
Reverted a Py2.3b1 change to iterator in subclasses of list and tuple.
They had been changed to use __getitem__ whenever it had been overriden
in the subclass.

This caused some usabilty and performance problems.  Also, it was
inconsistent with the rest of python where many container methods
access the underlying object directly without first checking for
an overridden getter.  Users needing a change in iterator behavior
should override it directly.

Lib/test/test_types.py
Misc/NEWS
Objects/listobject.c
Objects/tupleobject.c

index 7e238df3e0ae4f7897462e2a28445475d35531e5..153229570858f2f00a80253d717ebcee8686ea66 100644 (file)
@@ -256,11 +256,11 @@ def f():
         yield i
 vereq(list(tuple(f())), range(1000))
 
-# Verify that __getitem__ overrides are recognized by __iter__
+# Verify that __getitem__ overrides are not recognized by __iter__
 class T(tuple):
   def __getitem__(self, key):
      return str(key) + '!!!'
-vereq(iter(T()).next(), '0!!!')
+vereq(iter(T((1,2))).next(), 1)
 
 print '6.5.3 Lists'
 # calling built-in types without argument must return empty
@@ -453,11 +453,11 @@ a = range(10)
 a[::2] = tuple(range(5))
 vereq(a, [0, 1, 1, 3, 2, 5, 3, 7, 4, 9])
 
-# Verify that __getitem__ overrides are recognized by __iter__
+# Verify that __getitem__ overrides are not recognized by __iter__
 class L(list):
   def __getitem__(self, key):
      return str(key) + '!!!'
-vereq(iter(L()).next(), '0!!!')
+vereq(iter(L([1,2])).next(), 1)
 
 
 print '6.6 Mappings == Dictionaries'
index 9f982dbfbbc2fa7eeb4a479f921acce82488bd6b..98feacc8a15c4a620fc109e7389561a1041531a7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -15,6 +15,11 @@ Core and builtins
 - The softspace attribute of file objects became read-only by oversight.
   It's writable again.
 
+- Reverted a 2.3 beta 1 change to iterators for subclasses of list and
+  tuple.  By default, the iterators now access data elements directly
+  instead of going through __getitem__.  If __getitem__ access is
+  preferred, then __iter__ can be overriden.
+
 Extension modules
 -----------------
 
index 36bbe22179d6eb04871624a3e601f91ff95f3f35..48f3d7d6ea01ef3fe423cdd3e26faea687911322 100644 (file)
@@ -2382,8 +2382,6 @@ list_iter(PyObject *seq)
                PyErr_BadInternalCall();
                return NULL;
        }
-       if (seq->ob_type->tp_as_sequence->sq_item != (intargfunc)list_item)
-               return PySeqIter_New(seq);
        it = PyObject_GC_New(listiterobject, &PyListIter_Type);
        if (it == NULL)
                return NULL;
index 7456533ccd4b9b48b1462577b3e58feb07b84f67..282da3e8b97b6a776fa746ec1c44ebf6f8c1a611 100644 (file)
@@ -753,8 +753,6 @@ tuple_iter(PyObject *seq)
                PyErr_BadInternalCall();
                return NULL;
        }
-       if (seq->ob_type->tp_as_sequence->sq_item != (intargfunc)tupleitem)
-               return PySeqIter_New(seq);
        it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
        if (it == NULL)
                return NULL;