This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Elvis.Pranskevichus
Recipients Elvis.Pranskevichus
Date 2011-03-25.20:38:08
SpamBayes Score 6.779338e-10
Marked as misclassified No
Message-id <1301085490.36.0.28946733028.issue11674@psf.upfronthosting.co.za>
In-reply-to
Content
Consider the following:

>>> class Test:
...     def __init__(self):
...         self.items = []
...     def __len__(self):
...         if not self.items:
...             self.items = list(self.calc_items())
...         return len(self.items)
...     def __iter__(self):
...         return iter(self.items)
...     def calc_items(self, number):
...         return range(1, number)
... 
>>> l = list(Test())
>>> print(l)
[]
>>> t = tuple(Test())
>>> print(t)
()


In the above example calc_items() method is called with a missing argument, which raises TypeError.  That TypeError is being wrongly interpreted as "object of type 'Test' has no len()" and is swallowed by _PyObject_LengthHint().  

The result is entirely unpredictable as the bug is masked, which is especially annoying for objects that can have a complex call graph under __len__().  Possible solution would be to adjust _PyObject_LengthHint() to rely on some other exception rather than straight TypeError.  Swallowing a generic exception like that is bad.
History
Date User Action Args
2011-03-25 20:38:10Elvis.Pranskevichussetrecipients: + Elvis.Pranskevichus
2011-03-25 20:38:10Elvis.Pranskevichussetmessageid: <1301085490.36.0.28946733028.issue11674@psf.upfronthosting.co.za>
2011-03-25 20:38:08Elvis.Pranskevichuslinkissue11674 messages
2011-03-25 20:38:08Elvis.Pranskevichuscreate