On Apr 1, 2010, at 4:20 PM, Andrew Svetlov wrote: > using of copy.copy for simple iterators is forbidden > >>>> import copy >>>> copy.copy(iter([1, 2, 3])) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/home/andrew/projects/py3k/Lib/copy.py", line 96, in copy > return _reconstruct(x, rv, 0) > File "/home/andrew/projects/py3k/Lib/copy.py", line 284, in _reconstruct > y = callable(*args) > File "/home/andrew/projects/py3k/Lib/copyreg.py", line 88, in __newobj__ > return cls.__new__(cls, *args) > TypeError: object.__new__(list_iterator) is not safe, use > list_iterator.__new__() > > That behavior is safe and clean. > But it's possible to copy iterator objects returned by itertools functions: > >>>> i = itertools.chain([1, 2], [3, 4, 5]) >>>> i.__next__() > 1 >>>> j = copy.copy(i) >>>> j.__next__() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > StopIteration >>>> i.__next__() > 2 > > Looks like itertools object should be protected from usage like that. > Folks, what are you think about? >
I find it hard to get excited about this. It doesn't seem to have been a problem in the real world (no complaints, bug reports, or feature requests). The tee() itertool is the official way to split-out an iterator stream -- it is also copyable with copy.copy(). The itertools.count() function is also copyable. Running copy.copy() on other itertools is currently undefined (though I may add copy support to itertools.repeat() and the combinatoric functions). Also, I seems to me the copy.copy() is itself not very bright about what it tries to copy and in giving clear messages about whether or not it successfully made a copy. Raymond _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com