]> granicus.if.org Git - python/commitdiff
SF bug 665835: filter() treatment of str and tuple inconsistent
authorRaymond Hettinger <python@rcn.com>
Thu, 24 Apr 2003 16:52:47 +0000 (16:52 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 24 Apr 2003 16:52:47 +0000 (16:52 +0000)
As a side issue on this bug, it was noted that list and tuple iterators
used macros to directly access containers and would not recognize
__getitem__ overrides.  If the method is overridden, the patch returns
a generic sequence iterator which calls the __getitem__ method; otherwise,
it returns a high custom iterator with direct access to container elements.

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

index 4adbb406c68cb6d596ce7bd2ef20104d83586f01..7e238df3e0ae4f7897462e2a28445475d35531e5 100644 (file)
@@ -256,6 +256,12 @@ def f():
         yield i
 vereq(list(tuple(f())), range(1000))
 
+# Verify that __getitem__ overrides are recognized by __iter__
+class T(tuple):
+  def __getitem__(self, key):
+     return str(key) + '!!!'
+vereq(iter(T()).next(), '0!!!')
+
 print '6.5.3 Lists'
 # calling built-in types without argument must return empty
 if list() != []: raise TestFailed,'list() does not return []'
@@ -447,6 +453,12 @@ 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__
+class L(list):
+  def __getitem__(self, key):
+     return str(key) + '!!!'
+vereq(iter(L()).next(), '0!!!')
+
 
 print '6.6 Mappings == Dictionaries'
 # calling built-in types without argument must return empty
index 047c6ec98900cd2b33044268b9c284b99f2b335f..ae2b5493f8a5ae248d62f703b8a511e32989bda3 100644 (file)
@@ -2375,6 +2375,8 @@ list_iter(PyObject *seq)
                PyErr_BadInternalCall();
                return NULL;
        }
+       if (seq->ob_type->tp_as_sequence->sq_item != list_item)
+               return PySeqIter_New(seq);
        it = PyObject_GC_New(listiterobject, &PyListIter_Type);
        if (it == NULL)
                return NULL;
index 282da3e8b97b6a776fa746ec1c44ebf6f8c1a611..645480c9552e768100d03d8b1b8663f5a7bb007e 100644 (file)
@@ -753,6 +753,8 @@ tuple_iter(PyObject *seq)
                PyErr_BadInternalCall();
                return NULL;
        }
+       if (seq->ob_type->tp_as_sequence->sq_item != tupleitem)
+               return PySeqIter_New(seq);
        it = PyObject_GC_New(tupleiterobject, &PyTupleIter_Type);
        if (it == NULL)
                return NULL;