homepage

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.

classification
Title: listextend (and therefore list.extend and list.__init__) peek at len before iter
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Julian, benjamin.peterson
Priority: normal Keywords:

Created on 2012年06月07日 05:23 by Julian, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (3)
msg162457 - (view) Author: Julian Berman (Julian) * Date: 2012年06月07日 05:23
The following code raises an unexpected exception:
 class Foo(object):
 def __len__(self):
 raise Exception()
 def __iter__(self):
 return iter([])
 list(Foo())
In the optimizations being done in listextend, it appears len is getting called first for some reason (sorry, haven't gotten a chance to step through it carefully yet).
Tangentially, PyPy (correctly I guess) throws no such exception.
msg162458 - (view) Author: Julian Berman (Julian) * Date: 2012年06月07日 05:26
Oh, and, with apologies for the double post, tuple does the same, while set, dict, collections.deque do not.
msg162477 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012年06月07日 16:59
It's an optimization. No one says we can't call __len__().
History
Date User Action Args
2022年04月11日 14:57:31adminsetgithub: 59228
2012年06月07日 19:23:32r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2012年06月07日 16:59:35benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg162477
2012年06月07日 05:26:01Juliansetmessages: + msg162458
2012年06月07日 05:23:12Juliancreate

AltStyle によって変換されたページ (->オリジナル) /