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, amaury.forgeotdarc
Date 2011-03-25.21:21:33
SpamBayes Score 2.1288526e-13
Marked as misclassified No
Message-id <1301088094.81.0.821946400293.issue11674@psf.upfronthosting.co.za>
In-reply-to
Content
The problem is that the call to __len__ is implicit.  The Exception is simply swallowed by a list/tuple constructor.

The main issue is that the original error is masked which makes it very hard to pinpoint the actual cause of the failure.  

Here's an example of how it would fail if Test had a rather naive implementation of __getitem__():

>>> 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)
...     def __getitem__(self, m):
...         return list(self)[m]
... 
>>> t = Test()
>>> print(t[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 13, in __getitem__
IndexError: list index out of range


It's not obvious in a trivial synthetic example like that, but in a complex case like ORM abstraction this can cause a lot of pain.

Again, I'm not talking about the __len__ and TypeError protocol, it's the implicit exception swallowing by what is essentially an optional optimization hint.  Calling len(Test()) directly would bring up the correct trace just fine.
History
Date User Action Args
2011-03-25 21:21:34Elvis.Pranskevichussetrecipients: + Elvis.Pranskevichus, amaury.forgeotdarc
2011-03-25 21:21:34Elvis.Pranskevichussetmessageid: <1301088094.81.0.821946400293.issue11674@psf.upfronthosting.co.za>
2011-03-25 21:21:33Elvis.Pranskevichuslinkissue11674 messages
2011-03-25 21:21:33Elvis.Pranskevichuscreate