Bugs item #1522016, was opened at 2006-07-13 14:25
Message generated for change (Comment added) made by rhettinger
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: Closed
>Resolution: Rejected
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.


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

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-07-13 17:43

Message:
Logged In: YES 
user_id=80475

This isn't a bug.  You're making assumptions about
undocumented implementation details.  The is no shortage of
cases where the C code makes optimized direct internal calls
that bypass method overrides on subclasses of builtin types.

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1522016&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to