[ python-Bugs-1522016 ] filter() doesn't use __len__ of str/unicode/tuple subclasses

SourceForge.net noreply at sourceforge.net
Thu Jul 13 21:25:20 CEST 2006


Bugs item #1522016, was opened at 2006-07-13 15:25
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1522016&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Collin Winter (collinwinter)
Assigned to: Nobody/Anonymous (nobody)
Summary: filter() doesn't use __len__ of str/unicode/tuple subclasses

Initial Comment:
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.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1522016&group_id=5470


More information about the Python-bugs-list mailing list