Message29173
Consider the following code:
>>> class badstr(str):
... def __len__(self):
... return 6
... def __getitem__(self, index):
... return "a"
...
>>> filter(None, badstr("bbb"))
'aaa'
I would have expected the answer to be 'aaaaaa'.
The cause for this is that
Python/bltinmodule.c:filter{string,unicode,tuple} all
call PyString_Size (or the appropriate equivalent),
even if the sequence is a subclass of one of these
types. This bypasses any overloading of __len__ done by
the subclass, as demonstrated above.
If filter() is going to respect the subclass's
__getitem__ overload, it should also respect
overloading of __len__. The attached patch corrects this.
|
|
| Date |
User |
Action |
Args |
| 2007-08-23 14:41:20 | admin | link | issue1522016 messages |
| 2007-08-23 14:41:20 | admin | create | |
|