[New-bugs-announce] [issue29897] itertools.chain behaves strangly when copied with copy.copy

Michael Seifert report at bugs.python.org
Fri Mar 24 15:14:26 EDT 2017


New submission from Michael Seifert:

When using `copy.copy` to copy an `itertools.chain` instance the results can be weird. For example

>>> from itertools import chain
>>> from copy import copy
>>> a = chain([1,2,3], [4,5,6])
>>> b = copy(a)
>>> next(a)  # looks okay
1
>>> next(b)  # jumps to the second iterable, not okay?
4
>>> tuple(a)
(2, 3)
>>> tuple(b)
(5, 6)

I don't really want to "copy.copy" such an iterator (I would either use `a, b = itertools.tee(a, 2)` or `b = a` depending on the use-case). This just came up because I investigated how pythons iterators behave when copied, deepcopied or pickled because I want to make the iterators in my extension module behave similarly.

----------
components: Library (Lib)
messages: 290106
nosy: MSeifert
priority: normal
severity: normal
status: open
title: itertools.chain behaves strangly when copied with copy.copy
type: behavior
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29897>
_______________________________________


More information about the New-bugs-announce mailing list