[New-bugs-announce] [issue25718] itertools.accumulate __reduce__/__setstate__ bug

Armin Rigo report at bugs.python.org
Tue Nov 24 03:07:23 EST 2015


New submission from Armin Rigo:

itertools.accumulate() has got methods __reduce__() and __setstate__() which fail at saving/restoring the state in all cases.  Example:

   >>> a = itertools.accumulate([0.0, 42])
   >>> next(a)
   0.0
   >>> b = copy.deepcopy(a)
   >>> next(a)
   42.0
   >>> next(b)
   42           # should have been the same as the previous result

More precisely, the problem occurs when the C-level "total" field has some value whose truth-value is false.  Then __reduce__/__setstate__ will not restore the value of this field, but keep it NULL.  That's why in the example above the intermediate total of 0.0 is forgotten, and the next(b) call will again just pick the next value without doing any addition.  (The problem can hurt more than in this example if we use a custom function instead of addition.)

A fix is easy but, as far as I can tell, it requires breaking the pickled format of accumulate() iterators that have already started.

----------
components: Extension Modules
messages: 255252
nosy: arigo
priority: normal
severity: normal
status: open
title: itertools.accumulate __reduce__/__setstate__ bug
type: behavior
versions: Python 3.6

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


More information about the New-bugs-announce mailing list