Issue532747

classification
Title: for i in iter(d)
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: bckfnn Nosy List: bckfnn, bgrus
Priority: normal Keywords:

Created on 2002-03-20.21:42:43 by bgrus, last changed 2002-05-30.19:37:05 by bckfnn.

Messages
msg627 (view) Author: Boris Gruschke (bgrus) Date: 2002-03-20.21:42:43
When using the iterator implementation from the CVS 
(which works very well btw.) I came along the 
following problem:

-- snip --
d = {1:2,3:4}
l = []
for i in iter(d): l.append(i)
-- snip --

throws "TypeError: iteration over non-sequence"

iter(d) creates a PyDictionaryIter; the for-Loop 
invokes __iter__() on that which hits 
PyObject.__iter__() and results in the TypeError.

I think the solution is pretty straigtforward:
a) __builtin__.iter(obj) checks whether obj is an 
iterator before calling obj.__iter__()
or
b) the iterator classes implement __iter__() 
returning themselves (or maybe a clone)

If it is in question whether the code above should 
work at all: It's an excerpt of test_descr.py from 
the Python 2.2 test suite.


msg628 (view) Author: Finn Bock (bckfnn) Date: 2002-05-30.19:33:37
Logged In: YES 
user_id=4201

Added as test360.py

The answer is 'b'. From the pep234: """A class that wants to
be an iterator also ought to implement __iter__() returning
itself."""




msg629 (view) Author: Finn Bock (bckfnn) Date: 2002-05-30.19:37:05
Logged In: YES 
user_id=4201

Fixed in
PyDictionary.java: 2.19;
PyStringMap.java: 2.14;
History
Date User Action Args
2002-03-20 21:42:43bgruscreate